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

The following examples show how to use org.apache.hadoop.hbase.util.Bytes#toFloat() . 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: DefaultHBaseSerde.java    From envelope with Apache License 2.0 6 votes vote down vote up
private static Object getColumnValue(byte[] source, int offset, int length, String type) {
  switch (type) {
    case ConfigurationDataTypes.INT:
      return Bytes.toInt(source, offset, length);
    case ConfigurationDataTypes.LONG:
      return Bytes.toLong(source, offset, length);
    case ConfigurationDataTypes.BOOLEAN:
      return Bytes.toBoolean(source);
    case ConfigurationDataTypes.FLOAT:
      return Bytes.toFloat(source);
    case ConfigurationDataTypes.DOUBLE:
      return Bytes.toDouble(source);
    case ConfigurationDataTypes.STRING:
      return Bytes.toString(source, offset, length);
    default:
      LOG.error("Unsupported column type: {}", type);
      throw new IllegalArgumentException("Unsupported column type: " + type);
  }
}
 
Example 2
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 3
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 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 File: PhTypeUtil.java    From canal with Apache License 2.0 5 votes vote down vote up
private static float decodeUnsignedFloat(byte[] b, int o) {
    checkForSufficientLength(b, o, Bytes.SIZEOF_FLOAT);
    float v = Bytes.toFloat(b, o);
    if (v < 0) {
        throw new RuntimeException();
    }
    return v;
}
 
Example 5
Source File: PUnsignedFloat.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Override
public float decodeFloat(byte[] b, int o, SortOrder sortOrder) {
  Preconditions.checkNotNull(sortOrder);
  checkForSufficientLength(b, o, Bytes.SIZEOF_FLOAT);
  if (sortOrder == SortOrder.DESC) {
    b = SortOrder.invert(b, o, new byte[Bytes.SIZEOF_FLOAT], 0, Bytes.SIZEOF_FLOAT);
    o = 0;
  }
  float v = Bytes.toFloat(b, o);
  if (v < 0) {
    throw newIllegalDataException();
  }
  return v;
}
 
Example 6
Source File: HBaseBinaryConverter.java    From spork with Apache License 2.0 5 votes vote down vote up
@Override
public Float bytesToFloat(byte[] b) throws IOException {
    if (Bytes.SIZEOF_FLOAT > b.length){
        return Bytes.toFloat(Bytes.padHead(b, Bytes.SIZEOF_FLOAT - b.length));
    } else {
        return Bytes.toFloat(Bytes.head(b, Bytes.SIZEOF_FLOAT));
    }
}
 
Example 7
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 8
Source File: PUnsignedFloat.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Override
public float decodeFloat(byte[] b, int o, SortOrder sortOrder) {
  Preconditions.checkNotNull(sortOrder);
  checkForSufficientLength(b, o, Bytes.SIZEOF_FLOAT);
  if (sortOrder == SortOrder.DESC) {
    b = SortOrder.invert(b, o, new byte[Bytes.SIZEOF_FLOAT], 0, Bytes.SIZEOF_FLOAT);
  }
  float v = Bytes.toFloat(b, o);
  if (v < 0) {
    throw newIllegalDataException();
  }
  return v;
}
 
Example 9
Source File: HBaseFloatComparator.java    From pxf with Apache License 2.0 5 votes vote down vote up
public static ByteArrayComparable parseFrom(final byte[] pbBytes) throws DeserializationException {
    ComparatorProtos.ByteArrayComparable proto;
    try {
        proto = ComparatorProtos.ByteArrayComparable.parseFrom(pbBytes);
    } catch (InvalidProtocolBufferException e) {
        throw new DeserializationException(e);
    }

    return new HBaseFloatComparator(Bytes.toFloat(proto.getValue().toByteArray()));
}
 
Example 10
Source File: MultiVersionTask.java    From DataLink with Apache License 2.0 5 votes vote down vote up
private Column convertBytesToAssignType(ColumnType columnType, byte[] byteArray) throws UnsupportedEncodingException {
    Column column;
    switch (columnType) {
        case BOOLEAN:
            column = new BoolColumn(byteArray == null ? null : Bytes.toBoolean(byteArray));
            break;
        case SHORT:
            column = new LongColumn(byteArray == null ? null : String.valueOf(Bytes.toShort(byteArray)));
            break;
        case INT:
            column = new LongColumn(byteArray == null ? null : Bytes.toInt(byteArray));
            break;
        case LONG:
            column = new LongColumn(byteArray == null ? null : Bytes.toLong(byteArray));
            break;
        case BYTES:
            column = new BytesColumn(byteArray);
            break;
        case FLOAT:
            column = new DoubleColumn(byteArray == null ? null : Bytes.toFloat(byteArray));
            break;
        case DOUBLE:
            column = new DoubleColumn(byteArray == null ? null : Bytes.toDouble(byteArray));
            break;
        case STRING:
            column = new StringColumn(byteArray == null ? null : new String(byteArray, super.encoding));
            break;
        case BINARY_STRING:
            column = new StringColumn(byteArray == null ? null : Bytes.toStringBinary(byteArray));
            break;

        default:
            throw DataXException.asDataXException(HbaseReaderErrorCode.ILLEGAL_VALUE, "Hbasereader 不支持您配置的列类型:" + columnType);
    }

    return column;
}
 
Example 11
Source File: PhTypeUtil.java    From canal-1.1.3 with Apache License 2.0 5 votes vote down vote up
private static float decodeUnsignedFloat(byte[] b, int o) {
    checkForSufficientLength(b, o, Bytes.SIZEOF_FLOAT);
    float v = Bytes.toFloat(b, o);
    if (v < 0) {
        throw new RuntimeException();
    }
    return v;
}
 
Example 12
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 13
Source File: DefaultColumnCoder.java    From tddl5 with Apache License 2.0 4 votes vote down vote up
protected Object decodeFloatFromBytes(byte[] v) {
    return Bytes.toFloat(v);
}
 
Example 14
Source File: DefaultHBaseSerde.java    From envelope with Apache License 2.0 4 votes vote down vote up
private static int addColumnValue(byte[] source, int offset, int endIndex,
                                  Object[] values, String type, int valueIndex, byte[] keySeparator, boolean last) {
  switch (type) {
    case ConfigurationDataTypes.INT:
      values[valueIndex] = Bytes.toInt(source, offset, 4);
      return 4;
    case ConfigurationDataTypes.LONG:
      values[valueIndex] = Bytes.toLong(source, offset, 8);
      return 8;
    case ConfigurationDataTypes.BOOLEAN:
      values[valueIndex] = Bytes.toInt(source, offset, 1);
      return 1;
    case ConfigurationDataTypes.FLOAT:
      values[valueIndex] = Bytes.toFloat(source, offset);
      return 4;
    case ConfigurationDataTypes.DOUBLE:
      values[valueIndex] = Bytes.toDouble(source, offset);
      return 8;
    case ConfigurationDataTypes.STRING:
      if (last) {
        // if the last field just grab it all
        values[valueIndex] = Bytes.toString(source, offset, endIndex - offset);
        return endIndex - offset;
      } else {
        int startIndex = offset;
        while (offset < endIndex) {
          if (source[offset] != keySeparator[0]) {
            offset++;
          } else {
            // Might be the start of a separator
            int startOfOffset = offset;
            int sepOffset = 1;
            boolean isSep = sepOffset == keySeparator.length;
            while (sepOffset < keySeparator.length && offset < endIndex &&
                source[offset] == keySeparator[sepOffset]) {
              isSep = sepOffset == keySeparator.length - 1;
              offset++;
              sepOffset++;
            }
            if (isSep) {
              // We found a separator, so return the string before that
              values[valueIndex] = Bytes.toString(source, startIndex, startOfOffset - startIndex);
              return startOfOffset - startIndex;
            }
          }
        }
        // We reached the end which is an error except for the last field
        if (offset == endIndex - 1) {
          LOG.error("Reached end of array while looking for separator");
          throw new IllegalArgumentException("Reached end of array while looking for separator");
        } else {
          values[valueIndex] = Bytes.toString(source, startIndex, offset - startIndex);
          return offset - startIndex;
        }
      }
    default:
      LOG.error("Unsupported column type: {}", type);
      throw new IllegalArgumentException("Unsupported column type: " + type);
  }
}
 
Example 15
Source File: RawFloat.java    From hbase with Apache License 2.0 4 votes vote down vote up
@Override
public Float decode(PositionedByteRange src) {
  float val = Bytes.toFloat(src.getBytes(), src.getOffset() + src.getPosition());
  skip(src);
  return val;
}
 
Example 16
Source File: RawFloat.java    From hbase with Apache License 2.0 4 votes vote down vote up
/**
 * Read a {@code float} value from the buffer {@code buff}.
 */
public float decodeFloat(byte[] buff, int offset) {
  return Bytes.toFloat(buff, offset);
}
 
Example 17
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 18
Source File: CommonHBaseBytesUtil.java    From pentaho-hadoop-shims with Apache License 2.0 4 votes vote down vote up
public float toFloat( byte[] value ) {
  return Bytes.toFloat( value );
}
 
Example 19
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.toFloat(input);
}