Java Code Examples for org.apache.hadoop.hbase.util.Bytes#toShort()

The following examples show how to use org.apache.hadoop.hbase.util.Bytes#toShort() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HBaseFieldInfo.java    From attic-apex-malhar with Apache License 2.0 6 votes vote down vote up
public Object toValue( byte[] bytes )
{
  final SupportType type = getType();
  switch (type) {
    case BOOLEAN:
      return Bytes.toBoolean( bytes );
    case SHORT:
      return Bytes.toShort( bytes );
    case INTEGER:
      return Bytes.toInt( bytes );
    case LONG:
      return Bytes.toLong( bytes );
    case FLOAT:
      return Bytes.toFloat( bytes );
    case DOUBLE:
      return Bytes.toDouble( bytes );
    case STRING:
      return Bytes.toString( bytes );
    default:
      throw new IllegalArgumentException("Unsupported type: " + type);
  }
}
 
Example 2
Source File: ValueBitSet.java    From phoenix with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void or(ImmutableBytesWritable ptr) {
    if (schema == null) {
        return;
    }
    if (isVarLength()) {
        int offset = ptr.getOffset() + ptr.getLength() - Bytes.SIZEOF_SHORT;
        short nLongs = Bytes.toShort(ptr.get(), offset);
        offset -= nLongs * Bytes.SIZEOF_LONG;
        for (int i = 0; i < nLongs; i++) {
            bits[i] |= Bytes.toLong(ptr.get(), offset);
            offset += Bytes.SIZEOF_LONG;
        }
        maxSetBit = Math.max(maxSetBit, nLongs * Bytes.SIZEOF_LONG - 1);
    } else {
        long l = Bytes.toShort(ptr.get(), ptr.getOffset() + ptr.getLength() - Bytes.SIZEOF_SHORT);
        bits[0] |= l;
        maxSetBit = Math.max(maxSetBit, BITS_PER_SHORT - 1);
    }
    
}
 
Example 3
Source File: KeyValue.java    From hbase with Apache License 2.0 6 votes vote down vote up
/**
 * Use for logging.
 * @param b Key portion of a KeyValue.
 * @param o Offset to start of key
 * @param l Length of key.
 * @return Key as a String.
 */
public static String keyToString(final byte [] b, final int o, final int l) {
  if (b == null) return "";
  int rowlength = Bytes.toShort(b, o);
  String row = Bytes.toStringBinary(b, o + Bytes.SIZEOF_SHORT, rowlength);
  int columnoffset = o + Bytes.SIZEOF_SHORT + 1 + rowlength;
  int familylength = b[columnoffset - 1];
  int columnlength = l - ((columnoffset - o) + TIMESTAMP_TYPE_SIZE);
  String family = familylength == 0? "":
    Bytes.toStringBinary(b, columnoffset, familylength);
  String qualifier = columnlength == 0? "":
    Bytes.toStringBinary(b, columnoffset + familylength,
    columnlength - familylength);
  long timestamp = Bytes.toLong(b, o + (l - TIMESTAMP_TYPE_SIZE));
  String timestampStr = humanReadableTimestamp(timestamp);
  byte type = b[o + l - 1];
  return row + "/" + family +
    (family != null && family.length() > 0? ":" :"") +
    qualifier + "/" + timestampStr + "/" + Type.codeToType(type);
}
 
Example 4
Source File: HBaseRowInputFormat.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private Object deserialize(byte[] value, int typeIdx) {
	switch (typeIdx) {
		case 0: // byte[]
			return value;
		case 1:
			return new String(value, stringCharset);
		case 2: // byte
			return value[0];
		case 3:
			return Bytes.toShort(value);
		case 4:
			return Bytes.toInt(value);
		case 5:
			return Bytes.toLong(value);
		case 6:
			return Bytes.toFloat(value);
		case 7:
			return Bytes.toDouble(value);
		case 8:
			return Bytes.toBoolean(value);
		case 9: // sql.Timestamp encoded as long
			return new Timestamp(Bytes.toLong(value));
		case 10: // sql.Date encoded as long
			return new Date(Bytes.toLong(value));
		case 11: // sql.Time encoded as long
			return new Time(Bytes.toLong(value));
		case 12:
			return Bytes.toBigDecimal(value);
		case 13:
			return new BigInteger(value);

		default:
			throw new IllegalArgumentException("Unknown type index " + typeIdx);
	}
}
 
Example 5
Source File: PTable.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Override
public int decode(byte[] bytes, int offset, int length) {
    if (length == 4) {
        return getReservedQualifier(bytes, offset, length);
    }
    if (length != 2) {
        throw new InvalidQualifierBytesException(2, length);
    }
    return Bytes.toShort(bytes, offset, length) + c;
}
 
Example 6
Source File: HBaseManager.java    From hbase-secondary-index with GNU General Public License v3.0 5 votes vote down vote up
public static String getRealRowKey(KeyValue kv) {
	int rowlength = Bytes.toShort(kv.getBuffer(), kv.getOffset()
			+ KeyValue.ROW_OFFSET);
	String rowKey = Bytes.toStringBinary(kv.getBuffer(), kv.getOffset()
			+ KeyValue.ROW_OFFSET + Bytes.SIZEOF_SHORT, rowlength);
	return rowKey;
}
 
Example 7
Source File: PArrayDataType.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private static int getOffset(byte[] bytes, int arrayIndex, boolean useShort, int indexOffset) {
    int offset;
    if (useShort) {
        offset = indexOffset + (Bytes.SIZEOF_SHORT * arrayIndex);
        return Bytes.toShort(bytes, offset, Bytes.SIZEOF_SHORT) + Short.MAX_VALUE;
    } else {
        offset = indexOffset + (Bytes.SIZEOF_INT * arrayIndex);
        return Bytes.toInt(bytes, offset, Bytes.SIZEOF_INT);
    }
}
 
Example 8
Source File: PUnsignedSmallint.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Override
public short decodeShort(byte[] b, int o, SortOrder sortOrder) {
  Preconditions.checkNotNull(sortOrder);
  checkForSufficientLength(b, o, Bytes.SIZEOF_SHORT);
  if (sortOrder == SortOrder.DESC) {
    b = SortOrder.invert(b, o, new byte[Bytes.SIZEOF_SHORT], 0, Bytes.SIZEOF_SHORT);
  }
  short v = Bytes.toShort(b, o);
  if (v < 0) {
    throw newIllegalDataException();
  }
  return v;
}
 
Example 9
Source File: HBaseTypeUtils.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Deserialize byte array to Java Object with the given type.
 */
public static Object deserializeToObject(byte[] value, int typeIdx, Charset stringCharset) {
	switch (typeIdx) {
		case 0: // byte[]
			return value;
		case 1: // String
			return Arrays.equals(EMPTY_BYTES, value) ? null : new String(value, stringCharset);
		case 2: // byte
			return value[0];
		case 3:
			return Bytes.toShort(value);
		case 4:
			return Bytes.toInt(value);
		case 5:
			return Bytes.toLong(value);
		case 6:
			return Bytes.toFloat(value);
		case 7:
			return Bytes.toDouble(value);
		case 8:
			return Bytes.toBoolean(value);
		case 9: // sql.Timestamp encoded as long
			return new Timestamp(Bytes.toLong(value));
		case 10: // sql.Date encoded as long
			return new Date(Bytes.toLong(value));
		case 11: // sql.Time encoded as long
			return new Time(Bytes.toLong(value));
		case 12:
			return Bytes.toBigDecimal(value);
		case 13:
			return new BigInteger(value);

		default:
			throw new IllegalArgumentException("unsupported type index:" + typeIdx);
	}
}
 
Example 10
Source File: TestTableScan.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Read protobuf stream.
 * @param inputStream the input stream
 * @return The number of rows in the cell set model.
 * @throws IOException Signals that an I/O exception has occurred.
 */
public int readProtobufStream(InputStream inputStream) throws IOException{
  DataInputStream stream = new DataInputStream(inputStream);
  CellSetModel model = null;
  int rowCount = 0;
  try {
    while (true) {
      byte[] lengthBytes = new byte[2];
      int readBytes = stream.read(lengthBytes);
      if (readBytes == -1) {
        break;
      }
      assertEquals(2, readBytes);
      int length = Bytes.toShort(lengthBytes);
      byte[] cellset = new byte[length];
      stream.read(cellset);
      model = new CellSetModel();
      model.getObjectFromMessage(cellset);
      checkRowsNotNull(model);
      rowCount = rowCount + TestScannerResource.countCellSet(model);
    }
  } catch (EOFException exp) {
    exp.printStackTrace();
  } finally {
    stream.close();
  }
  return rowCount;
}
 
Example 11
Source File: ByteArrayValueMappers.java    From hbase-indexer with Apache License 2.0 4 votes vote down vote up
@Override
protected Object mapInternal(byte[] input) {
    return Bytes.toShort(input);
}
 
Example 12
Source File: ResultUtil.java    From phoenix with Apache License 2.0 4 votes vote down vote up
static int getKeyLength(Result r) {
    // Key length stored right before key as a short
    return Bytes.toShort(getRawBytes(r), getKeyOffset(r) - Bytes.SIZEOF_SHORT);
}
 
Example 13
Source File: KeyValue.java    From hbase with Apache License 2.0 4 votes vote down vote up
/**
 * This is a HFile block index key optimization.
 * @param leftKey
 * @param rightKey
 * @return 0 if equal, &lt;0 if left smaller, &gt;0 if right smaller
 * @deprecated Since 0.99.2;
 */
@Deprecated
public byte[] getShortMidpointKey(final byte[] leftKey, final byte[] rightKey) {
  if (rightKey == null) {
    throw new IllegalArgumentException("rightKey can not be null");
  }
  if (leftKey == null) {
    return Arrays.copyOf(rightKey, rightKey.length);
  }
  if (compareFlatKey(leftKey, rightKey) >= 0) {
    throw new IllegalArgumentException("Unexpected input, leftKey:" + Bytes.toString(leftKey)
      + ", rightKey:" + Bytes.toString(rightKey));
  }

  short leftRowLength = Bytes.toShort(leftKey, 0);
  short rightRowLength = Bytes.toShort(rightKey, 0);
  int leftCommonLength = ROW_LENGTH_SIZE + FAMILY_LENGTH_SIZE + leftRowLength;
  int rightCommonLength = ROW_LENGTH_SIZE + FAMILY_LENGTH_SIZE + rightRowLength;
  int leftCommonLengthWithTSAndType = TIMESTAMP_TYPE_SIZE + leftCommonLength;
  int rightCommonLengthWithTSAndType = TIMESTAMP_TYPE_SIZE + rightCommonLength;
  int leftColumnLength = leftKey.length - leftCommonLengthWithTSAndType;
  int rightColumnLength = rightKey.length - rightCommonLengthWithTSAndType;
  // rows are equal
  if (leftRowLength == rightRowLength && compareRows(leftKey, ROW_LENGTH_SIZE, leftRowLength,
    rightKey, ROW_LENGTH_SIZE, rightRowLength) == 0) {
    // Compare family & qualifier together.
    int comparison = Bytes.compareTo(leftKey, leftCommonLength, leftColumnLength, rightKey,
      rightCommonLength, rightColumnLength);
    // same with "row + family + qualifier", return rightKey directly
    if (comparison == 0) {
      return Arrays.copyOf(rightKey, rightKey.length);
    }
    // "family + qualifier" are different, generate a faked key per rightKey
    byte[] newKey = Arrays.copyOf(rightKey, rightKey.length);
    Bytes.putLong(newKey, rightKey.length - TIMESTAMP_TYPE_SIZE, HConstants.LATEST_TIMESTAMP);
    Bytes.putByte(newKey, rightKey.length - TYPE_SIZE, Type.Maximum.getCode());
    return newKey;
  }
  // rows are different
  short minLength = leftRowLength < rightRowLength ? leftRowLength : rightRowLength;
  short diffIdx = 0;
  while (diffIdx < minLength
      && leftKey[ROW_LENGTH_SIZE + diffIdx] == rightKey[ROW_LENGTH_SIZE + diffIdx]) {
    diffIdx++;
  }
  byte[] newRowKey = null;
  if (diffIdx >= minLength) {
    // leftKey's row is prefix of rightKey's.
    newRowKey = new byte[diffIdx + 1];
    System.arraycopy(rightKey, ROW_LENGTH_SIZE, newRowKey, 0, diffIdx + 1);
  } else {
    int diffByte = leftKey[ROW_LENGTH_SIZE + diffIdx];
    if ((0xff & diffByte) < 0xff && (diffByte + 1) <
        (rightKey[ROW_LENGTH_SIZE + diffIdx] & 0xff)) {
      newRowKey = new byte[diffIdx + 1];
      System.arraycopy(leftKey, ROW_LENGTH_SIZE, newRowKey, 0, diffIdx);
      newRowKey[diffIdx] = (byte) (diffByte + 1);
    } else {
      newRowKey = new byte[diffIdx + 1];
      System.arraycopy(rightKey, ROW_LENGTH_SIZE, newRowKey, 0, diffIdx + 1);
    }
  }
  return new KeyValue(newRowKey, null, null, HConstants.LATEST_TIMESTAMP,
    Type.Maximum).getKey();
}
 
Example 14
Source File: KeyValue.java    From hbase with Apache License 2.0 4 votes vote down vote up
/**
 * @return Row length
 */
@Override
public short getRowLength() {
  return Bytes.toShort(this.bytes, getKeyOffset());
}
 
Example 15
Source File: DeserializedBooleanComparator.java    From pentaho-hadoop-shims with Apache License 2.0 4 votes vote down vote up
public static Boolean decodeBoolFromNumber( byte[] rawEncoded ) {
  if ( rawEncoded.length == Bytes.SIZEOF_BYTE ) {
    byte val = rawEncoded[ 0 ];
    if ( val == 0 || val == 1 ) {
      return new Boolean( val == 1 );
    }
  }

  if ( rawEncoded.length == Bytes.SIZEOF_SHORT ) {
    short tempShort = Bytes.toShort( rawEncoded );

    if ( tempShort == 0 || tempShort == 1 ) {
      return new Boolean( tempShort == 1 );
    }
  }

  if ( rawEncoded.length == Bytes.SIZEOF_INT || rawEncoded.length == Bytes.SIZEOF_FLOAT ) {
    int tempInt = Bytes.toInt( rawEncoded );
    if ( tempInt == 1 || tempInt == 0 ) {
      return new Boolean( tempInt == 1 );
    }

    float tempFloat = Bytes.toFloat( rawEncoded );
    if ( tempFloat == 0.0f || tempFloat == 1.0f ) {
      return new Boolean( tempFloat == 1.0f );
    }
  }

  if ( rawEncoded.length == Bytes.SIZEOF_LONG || rawEncoded.length == Bytes.SIZEOF_DOUBLE ) {
    long tempLong = Bytes.toLong( rawEncoded );
    if ( tempLong == 0L || tempLong == 1L ) {
      return new Boolean( tempLong == 1L );
    }

    double tempDouble = Bytes.toDouble( rawEncoded );
    if ( tempDouble == 0.0 || tempDouble == 1.0 ) {
      return new Boolean( tempDouble == 1.0 );
    }
  }

  // not identifiable from a number
  return null;
}
 
Example 16
Source File: ResultUtil.java    From phoenix with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
static int getKeyLength(Result r) {
    // Key length stored right before key as a short
    return Bytes.toShort(getRawBytes(r), getKeyOffset(r) - Bytes.SIZEOF_SHORT);
}
 
Example 17
Source File: ResultUtil.java    From phoenix with Apache License 2.0 4 votes vote down vote up
static int getKeyLength(Result r) {
    // Key length stored right before key as a short
    return Bytes.toShort(getRawBytes(r), getKeyOffset(r) - Bytes.SIZEOF_SHORT);
}
 
Example 18
Source File: DefaultColumnCoder.java    From tddl5 with Apache License 2.0 4 votes vote down vote up
protected Object decodeShortFromBytes(byte[] v) {
    return Bytes.toShort(v);
}
 
Example 19
Source File: KeyValue.java    From hbase with Apache License 2.0 3 votes vote down vote up
/**
 * A setter that helps to avoid object creation every time and whenever
 * there is a need to create new KeyOnlyKeyValue.
 * @param key
 * @param offset
 * @param length
 */
public void setKey(byte[] key, int offset, int length) {
  this.bytes = key;
  this.offset = offset;
  this.length = length;
  this.rowLen = Bytes.toShort(this.bytes, this.offset);
}
 
Example 20
Source File: IntegrationTestBigLinkedList.java    From hbase with Apache License 2.0 2 votes vote down vote up
/**
 * @param bs
 * @return Type from the Counts enum of this row. Reads prefix added by
 * {@link #addPrefixFlag(int, byte[])}
 */
public static Counts whichType(final byte [] bs) {
  int ordinal = Bytes.toShort(bs, 0, Bytes.SIZEOF_SHORT);
  return Counts.values()[ordinal];
}