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

The following examples show how to use org.apache.hadoop.hbase.util.Bytes#toInt() . 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: ResultSerialization.java    From hbase with Apache License 2.0 6 votes vote down vote up
@Override
public Result deserialize(Result mutation) throws IOException {
  int totalBuffer = in.readInt();
  if (totalBuffer == 0) {
    return Result.EMPTY_RESULT;
  }
  byte[] buf = new byte[totalBuffer];
  readChunked(in, buf, 0, totalBuffer);
  List<Cell> kvs = new ArrayList<>();
  int offset = 0;
  while (offset < totalBuffer) {
    int keyLength = Bytes.toInt(buf, offset);
    offset += Bytes.SIZEOF_INT;
    kvs.add(new KeyValue(buf, offset, keyLength));
    offset += keyLength;
  }
  return Result.create(kvs);
}
 
Example 2
Source File: HBaseTest.java    From xxhadoop with Apache License 2.0 6 votes vote down vote up
@Test
public void testScan() throws IOException {
	Connection connection = admin.getConnection();
	Table table = connection.getTable(TableName.valueOf("tbl_girls"));
	
	Scan scan = new Scan(Bytes.toBytes("0001"), Bytes.toBytes("0004"));
	// RowKeyFilter
	Filter filter = new PrefixFilter(Bytes.toBytes("000"));
	scan.setFilter(filter);
	
	Filter filter2 = new RowFilter(CompareOp.EQUAL, new SubstringComparator("000"));
	scan.setFilter(filter2);
	
	//BinaryComparator binaryComparator = new BinaryComparator(Bytes.toBytes(29));		
	Filter filter3 = new SingleColumnValueFilter(Bytes.toBytes("base_info"), Bytes.toBytes("age"), CompareOp.GREATER, Bytes.toBytes(29));
	scan.setFilter(filter3);
	
	ResultScanner resultScanner = table.getScanner(scan);
	for (Result result : resultScanner) {
		LOGGER.info(result.toString());
		int value = Bytes.toInt(result.getValue(Bytes.toBytes("base_info"), Bytes.toBytes("age")));
		LOGGER.info(String.valueOf(value));
	}
}
 
Example 3
Source File: TestWALEntrySinkFilter.java    From hbase with Apache License 2.0 6 votes vote down vote up
@Override
public AsyncTable<AdvancedScanResultConsumer> getTable(TableName tableName) {
  return new DummyAsyncTable<AdvancedScanResultConsumer>() {

    @Override
    public <T> CompletableFuture<List<T>> batchAll(List<? extends Row> actions) {
      List<T> list = new ArrayList<>(actions.size());
      for (Row action : actions) {
        // Row is the index of the loop above where we make WALEntry and Cells.
        int row = Bytes.toInt(action.getRow());
        assertTrue("" + row, row > BOUNDARY);
        UNFILTERED.incrementAndGet();
        list.add(null);
      }
      return CompletableFuture.completedFuture(list);
    }
  };
}
 
Example 4
Source File: ChunkedResultIterator.java    From phoenix with Apache License 2.0 5 votes vote down vote up
/**
 * Prefix region start key to last key to form actual row key in case of local index scan.
 */
private void addRegionStartKeyToLaskKey() {
    byte[] offsetBytes = scan.getAttribute(STARTKEY_OFFSET);
    if (offsetBytes != null) {
        int startKeyOffset = Bytes.toInt(offsetBytes);
        byte[] actualLastkey =
                new byte[startKeyOffset + lastKey.getLength() - lastKey.getOffset()];
        System.arraycopy(scan.getStartRow(), 0, actualLastkey, 0, startKeyOffset);
        System.arraycopy(lastKey.get(), lastKey.getOffset(), actualLastkey,
            startKeyOffset, lastKey.getLength());
        lastKey.set(actualLastkey);
    }
}
 
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 != 3) {
        throw new InvalidQualifierBytesException(3, length);
    }
    byte[] toReturn = new byte[4];
    toReturn[1] = bytes[offset];
    toReturn[2] = bytes[offset + 1];
    toReturn[3] = bytes[offset + 2];
    return Bytes.toInt(toReturn);
}
 
Example 6
Source File: DataJanitorState.java    From phoenix-tephra with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
int getRegionCountForTime(Table stateTable, long time) throws IOException {
  Get get = new Get(makeTimeRegionCountKey(Bytes.toBytes(getInvertedTime(time))));
  get.addColumn(FAMILY, REGION_TIME_COL);
  Result result = stateTable.get(get);
  byte[] value = result.getValue(FAMILY, REGION_TIME_COL);
  return value == null ? -1 : Bytes.toInt(value);
}
 
Example 7
Source File: EncodedColumnsUtil.java    From phoenix with Apache License 2.0 5 votes vote down vote up
public static Pair<Integer, Integer> getMinMaxQualifiersFromScan(Scan scan) {
    Integer minQ = null, maxQ = null;
    byte[] minQualifier = scan.getAttribute(BaseScannerRegionObserver.MIN_QUALIFIER);
    if (minQualifier != null) {
        minQ = Bytes.toInt(minQualifier);
    }
    byte[] maxQualifier = scan.getAttribute(BaseScannerRegionObserver.MAX_QUALIFIER);
    if (maxQualifier != null) {
        maxQ = Bytes.toInt(maxQualifier);
    }
    if (minQualifier == null) {
        return null;
    }
    return new Pair<>(minQ, maxQ);
}
 
Example 8
Source File: DataJanitorState.java    From phoenix-tephra with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
int getRegionCountForTime(HTableInterface stateTable, long time) throws IOException {
  Get get = new Get(makeTimeRegionCountKey(Bytes.toBytes(getInvertedTime(time))));
  get.addColumn(FAMILY, REGION_TIME_COL);
  Result result = stateTable.get(get);
  byte[] value = result.getValue(FAMILY, REGION_TIME_COL);
  return value == null ? -1 : Bytes.toInt(value);
}
 
Example 9
Source File: DataJanitorState.java    From phoenix-tephra with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
int getRegionCountForTime(Table stateTable, long time) throws IOException {
  Get get = new Get(makeTimeRegionCountKey(Bytes.toBytes(getInvertedTime(time))));
  get.addColumn(FAMILY, REGION_TIME_COL);
  Result result = stateTable.get(get);
  byte[] value = result.getValue(FAMILY, REGION_TIME_COL);
  return value == null ? -1 : Bytes.toInt(value);
}
 
Example 10
Source File: TestSwitchToStreamRead.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Override
public ReturnCode filterCell(Cell c) throws IOException {
  if (Bytes.toInt(c.getRowArray(), c.getRowOffset()) == 999) {
    return ReturnCode.INCLUDE;
  } else {
    return ReturnCode.NEXT_COL;
  }
}
 
Example 11
Source File: PArrayDataType.java    From phoenix with Apache License 2.0 5 votes vote down vote up
public static int getArrayLength(ImmutableBytesWritable ptr, PDataType baseType, Integer maxLength) {
      byte[] bytes = ptr.get();
      if (ptr.getLength() == 0) {
          return 0;
      }
      if (baseType.isFixedWidth()) {
          int elemLength = maxLength == null ? baseType.getByteSize() : maxLength;
          return (ptr.getLength() / elemLength);
      }
      // In case where the number of elements is greater than SHORT.MAX_VALUE we do negate the number of
      // elements. So it is always better to return the absolute value
return (Bytes.toInt(bytes, (ptr.getOffset() + ptr.getLength() - (Bytes.SIZEOF_BYTE + Bytes.SIZEOF_INT))));
  }
 
Example 12
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 13
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 14
Source File: PTable.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Override
public int decode(byte[] bytes) {
    if (bytes.length != 4) {
        throw new InvalidQualifierBytesException(4, bytes.length);
    }
    return Bytes.toInt(bytes);
}
 
Example 15
Source File: ScanUtil.java    From phoenix with Apache License 2.0 5 votes vote down vote up
public static int getClientVersion(Scan scan) {
    int clientVersion = UNKNOWN_CLIENT_VERSION;
    byte[] clientVersionBytes = scan.getAttribute(BaseScannerRegionObserver.CLIENT_VERSION);
    if (clientVersionBytes != null) {
        clientVersion = Bytes.toInt(clientVersionBytes);
    }
    return clientVersion;
}
 
Example 16
Source File: DataJanitorState.java    From phoenix-tephra with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
int getRegionCountForTime(HTableInterface stateTable, long time) throws IOException {
  Get get = new Get(makeTimeRegionCountKey(Bytes.toBytes(getInvertedTime(time))));
  get.addColumn(FAMILY, REGION_TIME_COL);
  Result result = stateTable.get(get);
  byte[] value = result.getValue(FAMILY, REGION_TIME_COL);
  return value == null ? -1 : Bytes.toInt(value);
}
 
Example 17
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) {
        throw new InvalidQualifierBytesException(4, length);
    }
    return Bytes.toInt(bytes, offset, length);
}
 
Example 18
Source File: TestSwitchToStreamRead.java    From hbase with Apache License 2.0 4 votes vote down vote up
@Override
public boolean filterRowKey(Cell cell) throws IOException {
  return Bytes.toInt(cell.getRowArray(), cell.getRowOffset()) != 999;
}
 
Example 19
Source File: KeyValue.java    From hbase with Apache License 2.0 4 votes vote down vote up
/**
 * @return Value length
 */
@Override
public int getValueLength() {
  int vlength = Bytes.toInt(this.bytes, this.offset + Bytes.SIZEOF_INT);
  return vlength;
}
 
Example 20
Source File: CoveredColumnIndexCodec.java    From phoenix with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * Read an integer from the preceding {@value Bytes#SIZEOF_INT} bytes
 * @param bytes array to read from
 * @param start start point, backwards from which to read. For example, if specifying "25", we
 *          would try to read an integer from 21 -> 25
 * @return an integer from the proceeding {@value Bytes#SIZEOF_INT} bytes, if it exists.
 */
private static int getPreviousInteger(byte[] bytes, int start) {
  return Bytes.toInt(bytes, start - Bytes.SIZEOF_INT);
}