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

The following examples show how to use org.apache.hadoop.hbase.util.Bytes#toBigDecimal() . These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.
Example 1
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 2
/**
 * We store the column value in HBase like Bytes.toBytes(value) rather than Bytes.toBytes(value.toString)
 *
 * @param type  type
 * @param value value
 * @return object
 */
Object matchValue(Type type, byte[] value) {
    if (type == null) {
        return Bytes.toString(value);
    }

    Class typeClass = type.getClass();
    // return null if value is null
    // || (For varchar type if length is 0, return value is Bytes.toString(value), this will return "")
    // beside varchar type if length is 0, the value will be null
    if (value == null) {
        return null;
    }
    if (value.length == 0 && !typeClass.equals(VARCHAR_CLASS)) {
        return null;
    }

    if (typeClass.equals(VARCHAR_CLASS)) {
        return Bytes.toString(value);
    } else if (typeClass.equals(INTEGER_CLASS)) {
        return Bytes.toInt(value);
    } else if (typeClass.equals(BIGINT_CLASS)) {
        return Bytes.toLong(value);
    } else if (typeClass.equals(DOUBLE_CLASS)) {
        return Bytes.toDouble(value);
    } else if (typeClass.equals(TIMESTAMP_CLASS)) {
        return Bytes.toLong(value);
    } else if (typeClass.equals(BOOLEAN_CLASS)) {
        // 0: false, 1: true
        return Bytes.toInt(value);
    } else if (type.getClass().getSuperclass().equals(DecimalType.class)) {
        return Bytes.toBigDecimal(value);
    } else {
        return Bytes.toString(value);
    }
}
 
Example 3
Source Project: flink   File: HBaseTypeUtils.java    License: 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 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 4
Source Project: flink   File: HBaseSerde.java    License: Apache License 2.0 5 votes vote down vote up
private static FieldDecoder createDecimalDecoder(DecimalType decimalType) {
	final int precision = decimalType.getPrecision();
	final int scale = decimalType.getScale();
	return value -> {
		BigDecimal decimal = Bytes.toBigDecimal(value);
		return DecimalData.fromBigDecimal(decimal, precision, scale);
	};
}
 
Example 5
Source Project: flink   File: HBaseTypeUtils.java    License: 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 6
Source Project: hbase   File: BigDecimalComparator.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * @param pbBytes A pb serialized {@link BigDecimalComparator} instance
 * @return An instance of {@link BigDecimalComparator} made from <code>bytes</code>
 * @throws DeserializationException A deserialization exception
 * @see #toByteArray
 */
public static BigDecimalComparator parseFrom(final byte[] pbBytes)
    throws DeserializationException {
  ComparatorProtos.BigDecimalComparator proto;
  try {
    proto = ComparatorProtos.BigDecimalComparator.parseFrom(pbBytes);
  } catch (InvalidProtocolBufferException e) {
    throw new DeserializationException(e);
  }
  return new BigDecimalComparator(Bytes.toBigDecimal(proto.getComparable().getValue()
      .toByteArray()));
}
 
Example 7
Source Project: hbase   File: PrivateCellUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Converts the value bytes of the given cell into a BigDecimal
 * @param cell
 * @return value as BigDecimal
 */
public static BigDecimal getValueAsBigDecimal(Cell cell) {
  if (cell instanceof ByteBufferExtendedCell) {
    return ByteBufferUtils.toBigDecimal(((ByteBufferExtendedCell) cell).getValueByteBuffer(),
      ((ByteBufferExtendedCell) cell).getValuePosition(), cell.getValueLength());
  }
  return Bytes.toBigDecimal(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
}
 
Example 8
@Override
public BigDecimal getPromotedValueFromProto(BigDecimalMsg r) {
  return Bytes.toBigDecimal(r.getBigdecimalMsg().toByteArray());
}
 
Example 9
@Override
public BigDecimal getCellValueFromProto(BigDecimalMsg q) {
  return Bytes.toBigDecimal(q.getBigdecimalMsg().toByteArray());
}
 
Example 10
Source Project: hbase   File: BigDecimalComparator.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public int compareTo(byte[] value, int offset, int length) {
  BigDecimal that = Bytes.toBigDecimal(value, offset, length);
  return this.bigDecimal.compareTo(that);
}
 
Example 11
@Override
protected Object mapInternal(byte[] input) {
    return Bytes.toBigDecimal(input);
}