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

The following examples show how to use org.apache.hadoop.hbase.util.Bytes#readAsInt() . 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: TestFiltersWithBinaryComponentComparator.java    From hbase with Apache License 2.0 6 votes vote down vote up
@Test
public void testRowFilterWithBinaryComponentComparator() throws IOException {
  //SELECT * from table where a=1 and b > 10 and b < 20 and c > 90 and c < 100 and d=1
  tableName = TableName.valueOf(name.getMethodName());
  Table ht = TEST_UTIL.createTable(tableName, family, Integer.MAX_VALUE);
  generateRows(ht, family, qf);
  FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL);
  setRowFilters(filterList);
  Scan scan = createScan(filterList);
  List<Cell> result = getResults(ht,scan);
  for(Cell cell: result){
    byte[] key = CellUtil.cloneRow(cell);
    int a = Bytes.readAsInt(key,aOffset,4);
    int b = Bytes.readAsInt(key,bOffset,4);
    int c = Bytes.readAsInt(key,cOffset,4);
    int d = Bytes.readAsInt(key,dOffset,4);
    assertTrue(a == 1 &&
               b > 10 &&
               b < 20 &&
               c > 90 &&
               c < 100 &&
               d == 1);
  }
  ht.close();
}
 
Example 2
Source File: PrivateCellUtil.java    From hbase with Apache License 2.0 6 votes vote down vote up
/**
 * Retrieve Cell's first tag, matching the passed in type
 * @param cell The Cell
 * @param type Type of the Tag to retrieve
 * @return Optional, empty if there is no tag of the passed in tag type
 */
public static Optional<Tag> getTag(Cell cell, byte type) {
  boolean bufferBacked = cell instanceof ByteBufferExtendedCell;
  int length = cell.getTagsLength();
  int offset =
    bufferBacked ? ((ByteBufferExtendedCell) cell).getTagsPosition() : cell.getTagsOffset();
  int pos = offset;
  while (pos < offset + length) {
    int tagLen;
    if (bufferBacked) {
      ByteBuffer tagsBuffer = ((ByteBufferExtendedCell) cell).getTagsByteBuffer();
      tagLen = ByteBufferUtils.readAsInt(tagsBuffer, pos, TAG_LENGTH_SIZE);
      if (ByteBufferUtils.toByte(tagsBuffer, pos + TAG_LENGTH_SIZE) == type) {
        return Optional.of(new ByteBufferTag(tagsBuffer, pos, tagLen + TAG_LENGTH_SIZE));
      }
    } else {
      tagLen = Bytes.readAsInt(cell.getTagsArray(), pos, TAG_LENGTH_SIZE);
      if (cell.getTagsArray()[pos + TAG_LENGTH_SIZE] == type) {
        return Optional
          .of(new ArrayBackedTag(cell.getTagsArray(), pos, tagLen + TAG_LENGTH_SIZE));
      }
    }
    pos += TAG_LENGTH_SIZE + tagLen;
  }
  return Optional.empty();
}
 
Example 3
Source File: TestFiltersWithBinaryComponentComparator.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Test
public void testRowAndValueFilterWithBinaryComponentComparator() throws IOException {
  //SELECT * from table where a=1 and b > 10 and b < 20 and c > 90 and c < 100 and d=1
  //and value has 'y' at position 1"
  tableName = TableName.valueOf(name.getMethodName());
  Table ht = TEST_UTIL.createTable(tableName, family, Integer.MAX_VALUE);
  generateRows(ht, family, qf);
  FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL);
  setRowFilters(filterList);
  setValueFilters(filterList);
  Scan scan = new Scan();
  scan.setFilter(filterList);
  List<Cell> result = getResults(ht,scan);
  for(Cell cell: result){
    byte[] key = CellUtil.cloneRow(cell);
    int a = Bytes.readAsInt(key,aOffset,4);
    int b = Bytes.readAsInt(key,bOffset,4);
    int c = Bytes.readAsInt(key,cOffset,4);
    int d = Bytes.readAsInt(key,dOffset,4);
    assertTrue(a == 1 &&
               b > 10 &&
               b < 20 &&
               c > 90 &&
               c < 100 &&
               d == 1);
    byte[] value = CellUtil.cloneValue(cell);
    assertTrue(Bytes.toString(value).charAt(1) == 'y');
  }
  ht.close();
}
 
Example 4
Source File: PrivateCellUtil.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Util method to iterate through the tags in the given cell.
 * @param cell The Cell over which tags iterator is needed.
 * @return iterator for the tags
 */
public static Iterator<Tag> tagsIterator(final Cell cell) {
  final int tagsLength = cell.getTagsLength();
  // Save an object allocation where we can
  if (tagsLength == 0) {
    return TagUtil.EMPTY_TAGS_ITR;
  }
  if (cell instanceof ByteBufferExtendedCell) {
    return tagsIterator(((ByteBufferExtendedCell) cell).getTagsByteBuffer(),
      ((ByteBufferExtendedCell) cell).getTagsPosition(), tagsLength);
  }

  return new Iterator<Tag>() {
    private int offset = cell.getTagsOffset();
    private int pos = offset;
    private int endOffset = offset + cell.getTagsLength() - 1;

    @Override
    public boolean hasNext() {
      return this.pos < endOffset;
    }

    @Override
    public Tag next() {
      if (hasNext()) {
        byte[] tags = cell.getTagsArray();
        int curTagLen = Bytes.readAsInt(tags, this.pos, Tag.TAG_LENGTH_SIZE);
        Tag tag = new ArrayBackedTag(tags, pos, curTagLen + TAG_LENGTH_SIZE);
        this.pos += Bytes.SIZEOF_SHORT + curTagLen;
        return tag;
      }
      return null;
    }

    @Override
    public void remove() {
      throw new UnsupportedOperationException();
    }
  };
}
 
Example 5
Source File: TagUtil.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Creates list of tags from given byte array, expected that it is in the expected tag format.
 * @param b The byte array
 * @param offset The offset in array where tag bytes begin
 * @param length Total length of all tags bytes
 * @return List of tags
 */
public static List<Tag> asList(byte[] b, int offset, int length) {
  List<Tag> tags = new ArrayList<>();
  int pos = offset;
  while (pos < offset + length) {
    int tagLen = Bytes.readAsInt(b, pos, Tag.TAG_LENGTH_SIZE);
    tags.add(new ArrayBackedTag(b, pos, tagLen + Tag.TAG_LENGTH_SIZE));
    pos += Tag.TAG_LENGTH_SIZE + tagLen;
  }
  return tags;
}
 
Example 6
Source File: PArrayDataType.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private static boolean isRowKeyOrderOptimized(boolean isFixedWidth, SortOrder sortOrder, byte[] buf, int offset, int length) {
    if (length == 0 || sortOrder == SortOrder.ASC || isFixedWidth) {
        return true;
    }
    int offsetToHeaderOffset = offset + length - Bytes.SIZEOF_BYTE - Bytes.SIZEOF_INT * 2;
    int offsetToSeparatorByte = Bytes.readAsInt(buf, offsetToHeaderOffset, Bytes.SIZEOF_INT) - 1;
    return buf[offsetToSeparatorByte] == QueryConstants.DESC_SEPARATOR_BYTE;
}
 
Example 7
Source File: ArrayBackedTag.java    From hbase with Apache License 2.0 4 votes vote down vote up
private static int getLength(byte[] bytes, int offset) {
  return TAG_LENGTH_SIZE + Bytes.readAsInt(bytes, offset, TAG_LENGTH_SIZE);
}