/** * Joins two byte arrays together using a separator. * * @param b1 The first byte array. * @param sep The separator to use. * @param b2 The second byte array. */ public static String toString(final byte[] b1, String sep, final byte[] b2) { return toString(b1, 0, b1.length) + sep + toString(b2, 0, b2.length); }
/** * @param b Presumed UTF-8 encoded byte array. * @return String made from <code>b</code> */ public static String toString(final byte[] b) { if (b == null) { return null; } return toString(b, 0, b.length); }
@Override public String convertFromBytes(byte[] b, int offset, int length) { return Bytes.toString(b, offset, length); }
/** * Reads a fixed-size field and interprets it as a string padded with zeros. */ public static String readStringFixedSize(final DataInput in, int size) throws IOException { byte[] b = new byte[size]; in.readFully(b); int n = b.length; while (n > 0 && b[n - 1] == 0) --n; return toString(b, 0, n); }
@Override public String[] call(Text text) throws Exception { String s = Bytes.toString(text.getBytes(), 0, text.getLength()); return s.split(BatchConstants.SEQUENCE_FILE_DEFAULT_DELIMITER, -1); } });
@Override public Collection<String[]> parseMapperInput(Object mapperInput) { Text text = (Text) mapperInput; String[] columns = Bytes.toString(text.getBytes(), 0, text.getLength()).split(delimiter, -1); return Collections.singletonList(columns); }
private void fillKeys(byte[] rowKey, String[] result) { int keyNum = keyIndexes.length; ByteBuffer byteBuffer = ByteBuffer.wrap(rowKey); byteBuffer.getShort(); // read shard for (int i = 0; i < keyNum; i++) { short keyLen = byteBuffer.getShort(); byte[] keyBytes = new byte[keyLen]; byteBuffer.get(keyBytes); result[keyIndexes[i]] = Bytes.toString(keyBytes); } }
@Override public String toString() { StringBuilder buf = new StringBuilder(); buf.append("["); for (int i = 0; i < bufferSize; i++) { if (i > 0) buf.append(", "); buf.append(Bytes.toString(splitBuffers[i].value, 0, splitBuffers[i].length)); } return buf.toString(); }
@Override public String nextLine() throws IOException { boolean hasNext = reader.next(key, value); if (hasNext) return Bytes.toString(value.getBytes(), 0, value.getLength()); else return null; }
private void fillValues(Map<byte[], byte[]> qualifierValMap, String[] result) { for (Entry<byte[], byte[]> qualifierValEntry : qualifierValMap.entrySet()) { byte[] qualifier = qualifierValEntry.getKey(); byte[] value = qualifierValEntry.getValue(); int valIdx = Integer.parseInt(Bytes.toString(qualifier)); result[valIdx] = fromBytes(value); } }
@Override public String decode(byte[] bytes, int offset, int len) { if (isNull(bytes, offset, len)) { return null; } while (len > 0 && bytes[offset + len - 1] == ROWKEY_PLACE_HOLDER_BYTE) len--; return Bytes.toString(bytes, offset, len); }
protected String fromBytes(byte[] bytes) { if (DimensionEncoding.isNull(bytes, 0, bytes.length)) { return null; } return Bytes.toString(bytes); } }
@Override public void reload(Object measureValue) { if (measureValue == null) { value = null; return; } ByteArray byteArray = (ByteArray) measureValue; //the array in ByteArray is guaranteed to be completed owned by the ByteArray value = Bytes.toString(byteArray.array()); }
private void saveResource(byte[] content, String path) throws IOException { System.out.println("Generated " + outputStore.getReadableResourcePath(path)); if (outprint) { System.out.println(Bytes.toString(content)); } outputStore.putResource(path, new ByteArrayInputStream(content), System.currentTimeMillis()); }
@Override public void doReduce(SelfDefineSortableKey skey, Iterable<NullWritable> values, Context context) throws IOException, InterruptedException { Text key = skey.getText(); String value = Bytes.toString(key.getBytes(), 1, key.getLength() - 1); builder.addValue(value); }
@Override public String convertFromBytes(byte[] b, int offset, int length) { NumberBytesCodec codec = getCodec(this.maxDigitsBeforeDecimalPoint); byte[] backup = codec.buf; codec.buf = b; codec.bufOffset = offset; codec.bufLen = length; int len = codec.decodeNumber(backup, 0); codec.buf = backup; return Bytes.toString(backup, 0, len); }
private String printKey(SelfDefineSortableKey key) { Text data = key.getText(); String fieldValue = Bytes.toString(data.getBytes(), 1, data.getLength() - 1); System.out.println("type flag:" + key.getTypeId() + " fieldValue:" + fieldValue); return fieldValue; }
private String decodeNumber(String code) { byte[] buf = Bytes.toBytes(code); System.arraycopy(buf, 0, codec.buf, 0, buf.length); codec.bufOffset = 0; codec.bufLen = buf.length; int len = codec.decodeNumber(buf, 0); return Bytes.toString(buf, 0, len); }
private String encodeNumber(String number) { byte[] num1 = Bytes.toBytes(number); codec.encodeNumber(num1, 0, num1.length); return Bytes.toString(codec.buf, codec.bufOffset, codec.bufLen); }
@Test public void testEnDeCode() { HBaseLookupRowEncoder lookupRowEncoder = new HBaseLookupRowEncoder(tableDesc, new String[] { "COUNTRY" }, 1); String[] row = new String[] { "AD", "42.546245", "1.601554", "Andorra" }; HBaseRow hBaseRow = lookupRowEncoder.encode(row); assertEquals(6, hBaseRow.getRowKey().length); assertEquals(3, hBaseRow.getQualifierValMap().size()); NavigableMap<byte[], byte[]> qualifierMap = hBaseRow.getQualifierValMap(); assertEquals("42.546245", Bytes.toString(qualifierMap.get(Bytes.toBytes("1")))); assertEquals("1.601554", Bytes.toString(qualifierMap.get(Bytes.toBytes("2")))); String[] decodeRow = lookupRowEncoder.decode(hBaseRow); assertArrayEquals(row, decodeRow); }