/** * Put a bunch of Writables as bytes all into the one byte array. * @param ws writable * @return The bytes of <code>w</code> gotten by running its * {@link Writable#write(java.io.DataOutput)} method. * @throws IOException e */ public static byte [] getBytes(final Writable... ws) throws IOException { List<byte []> bytes = new ArrayList<>(ws.length); int size = 0; for (Writable w: ws) { byte [] b = getBytes(w); size += b.length; bytes.add(b); } byte [] result = new byte[size]; int offset = 0; for (byte [] b: bytes) { System.arraycopy(b, 0, result, offset, b.length); offset += b.length; } return result; }
/** * Set bytes into the passed Writable by calling its * {@link Writable#readFields(java.io.DataInput)}. * @param bytes serialized bytes * @param w An empty Writable (usually made by calling the null-arg * constructor). * @return The passed Writable after its readFields has been called fed * by the passed <code>bytes</code> array or IllegalArgumentException * if passed null or an empty <code>bytes</code> array. * @throws IOException e * @throws IllegalArgumentException */ public static Writable getWritable(final byte [] bytes, final Writable w) throws IOException { return getWritable(bytes, 0, bytes.length, w); }
/** * Copy one Writable to another. Copies bytes using data streams. * @param src Source Writable * @param tgt Target Writable * @return The target Writable. * @throws IOException e */ public static Writable copyWritable(final Writable src, final Writable tgt) throws IOException { return copyWritable(getBytes(src), tgt); }
@Override public boolean next(ImmutableBytesWritable rowKey, Result value) throws IOException { boolean next = false; try { next = recordReader.nextKeyValue(); if (next) { rowKey.set(recordReader.getCurrentValue().getRow()); Writables.copyWritable(recordReader.getCurrentValue(), value); } } catch (InterruptedException e) { throw new IOException(e); } return next; } };
static void checkSplit(final Result r, final Put p, final byte [] which) throws IOException { byte [] hriSplitBytes = getBytes(r, which); if (!isMigrated(hriSplitBytes)) { // This will convert the HRI from 090 to 092 HRI. HRegionInfo hri = Writables.getHRegionInfo(hriSplitBytes); p.add(HConstants.CATALOG_FAMILY, which, Writables.getBytes(hri)); } }
return; HRegionInfo h = Writables.getHRegionInfoOrNull(value); Writables.getBytes(hri)); r.put(put); return; HRegionInfo h = Writables.getHRegionInfoOrNull(value); LOG.debug("New " + Bytes.toString(HConstants.CATALOG_FAMILY) + ":" + Bytes.toString(HConstants.REGIONINFO_QUALIFIER) + " for " +
/** * @param bytes serialized bytes * @return A HRegionInfo instance built out of passed <code>bytes</code> * or <code>null</code> if passed bytes are null or an empty array. * @throws IOException e */ public static HRegionInfo getHRegionInfoOrNull(final byte [] bytes) throws IOException { return (bytes == null || bytes.length <= 0)? null : getHRegionInfo(bytes); }
@Override public boolean processRow(Result rowResult) throws IOException { HRegionInfo info = Writables.getHRegionInfoOrNull( rowResult.getValue(HConstants.CATALOG_FAMILY, HConstants.REGIONINFO_QUALIFIER)); HRegionInfo splitA = Writables.getHRegionInfo(rowResult.getValue(HConstants.CATALOG_FAMILY, HConstants.SPLITA_QUALIFIER)); HRegionInfo splitB = Writables.getHRegionInfo(rowResult.getValue(HConstants.CATALOG_FAMILY, HConstants.SPLITB_QUALIFIER));
/** * Get daughter HRegionInfo out of parent info:splitA/info:splitB columns. * @param result * @param which Whether "info:splitA" or "info:splitB" column * @return Deserialized content of the info:splitA or info:splitB as a * HRegionInfo * @throws IOException */ private HRegionInfo getDaughterRegionInfo(final Result result, final byte [] which) throws IOException { byte [] bytes = result.getValue(HConstants.CATALOG_FAMILY, which); return Writables.getHRegionInfoOrNull(bytes); }
} else if (Bytes.compareTo(e.getKey(), Bytes.toBytes("TIMERANGE")) == 0) { TimeRangeTracker timeRangeTracker = new TimeRangeTracker(); Writables.copyWritable(e.getValue(), timeRangeTracker); System.out.println(timeRangeTracker.getMinimumTimestamp() + "...." + timeRangeTracker.getMaximumTimestamp());
throw new IOException("no information for row " + Bytes.toString(row)); HRegionInfo info = Writables.getHRegionInfo(value); Put put = new Put(row); info.setOffline(onlineOffline); put.add(HConstants.CATALOG_FAMILY, HConstants.REGIONINFO_QUALIFIER, Writables.getBytes(info)); t.put(put);
public static HRegionInfo getHRegionInfo( Result data) throws IOException { byte [] bytes = data.getValue(HConstants.CATALOG_FAMILY, HConstants.REGIONINFO_QUALIFIER); if (bytes == null) return null; HRegionInfo info = Writables.getHRegionInfo(bytes); LOG.info("Current INFO from scan results = " + info); return info; }
/** * Parse the content of the cell at {@link HConstants#CATALOG_FAMILY} and * <code>qualifier</code> as an HRegionInfo and return it, or null. * For use on catalog table {@link Result}. * @param r Result instance to pull from. * @param qualifier Column family qualifier -- either * {@link HConstants#SPLITA_QUALIFIER}, {@link HConstants#SPLITB_QUALIFIER} or * {@link HConstants#REGIONINFO_QUALIFIER}. * @return An HRegionInfo instance or null. * @throws IOException */ public static HRegionInfo parseHRegionInfoFromCatalogResult(final Result r, byte [] qualifier) throws IOException { byte [] bytes = r.getValue(HConstants.CATALOG_FAMILY, qualifier); if (bytes == null || bytes.length <= 0) return null; return Writables.getHRegionInfoOrNull(bytes); }
public void addKeyToZK(AuthenticationKey key) { String keyZNode = getKeyNode(key.getKeyId()); try { byte[] keyData = Writables.getBytes(key); // TODO: is there any point in retrying beyond what ZK client does? ZKUtil.createSetData(watcher, keyZNode, keyData); } catch (KeeperException ke) { LOG.error(HBaseMarkers.FATAL, "Unable to synchronize master key "+key.getKeyId()+ " to znode "+keyZNode, ke); watcher.abort("Unable to synchronize secret key "+ key.getKeyId()+" in zookeeper", ke); } catch (IOException ioe) { // this can only happen from an error serializing the key watcher.abort("Failed serializing key "+key.getKeyId(), ioe); } }
@Override public void nodeDataChanged(String path) { if (keysParentZNode.equals(ZKUtil.getParent(path))) { try { byte[] data = ZKUtil.getDataAndWatch(watcher, path); if (data == null || data.length == 0) { LOG.debug("Ignoring empty node "+path); return; } AuthenticationKey key = (AuthenticationKey)Writables.getWritable(data, new AuthenticationKey()); secretManager.addKey(key); } catch (KeeperException ke) { LOG.error(HBaseMarkers.FATAL, "Error reading data from zookeeper", ke); watcher.abort("Error reading updated key znode "+path, ke); } catch (IOException ioe) { LOG.error(HBaseMarkers.FATAL, "Error reading key writables", ioe); watcher.abort("Error reading key writables from znode "+path, ioe); } } }
/** * Copy one Writable to another. Copies bytes using data streams. * @param src Source Writable * @param tgt Target Writable * @return The target Writable. * @throws IOException e */ public static Writable copyWritable(final Writable src, final Writable tgt) throws IOException { return copyWritable(getBytes(src), tgt); }
Writables.copyWritable( (Writable) result, (Writable) value ); } else {
/** * Get HRegionInfo from passed Map of row values. * @param result Map to do lookup in. * @return Null if not found (and logs fact that expected COL_REGIONINFO * was missing) else deserialized {@link HRegionInfo} * @throws IOException */ static HRegionInfo getHRegionInfo(final Result result) throws IOException { byte [] bytes = result.getValue(HConstants.CATALOG_FAMILY, HConstants.REGIONINFO_QUALIFIER); if (bytes == null) { LOG.warn("REGIONINFO_QUALIFIER is empty in " + result); return null; } return Writables.getHRegionInfo(bytes); }
/** * Returns the daughter regions by reading from the corresponding columns of the .META. table * Result. If the region is not a split parent region, it returns PairOfSameType(null, null). */ public static PairOfSameType<HRegionInfo> getDaughterRegions(Result data) throws IOException { HRegionInfo splitA = Writables.getHRegionInfoOrNull( data.getValue(HConstants.CATALOG_FAMILY, HConstants.SPLITA_QUALIFIER)); HRegionInfo splitB = Writables.getHRegionInfoOrNull( data.getValue(HConstants.CATALOG_FAMILY, HConstants.SPLITB_QUALIFIER)); return new PairOfSameType<HRegionInfo>(splitA, splitB); }
public void updateKeyInZK(AuthenticationKey key) { String keyZNode = getKeyNode(key.getKeyId()); try { byte[] keyData = Writables.getBytes(key); try { ZKUtil.updateExistingNodeData(watcher, keyZNode, keyData, -1); } catch (KeeperException.NoNodeException ne) { // node was somehow removed, try adding it back ZKUtil.createSetData(watcher, keyZNode, keyData); } } catch (KeeperException ke) { LOG.error(HBaseMarkers.FATAL, "Unable to update master key "+key.getKeyId()+ " in znode "+keyZNode); watcher.abort("Unable to synchronize secret key "+ key.getKeyId()+" in zookeeper", ke); } catch (IOException ioe) { // this can only happen from an error serializing the key watcher.abort("Failed serializing key "+key.getKeyId(), ioe); } }