Java Code Examples for org.apache.hadoop.hbase.filter.Filter

The following are top voted examples for showing how to use org.apache.hadoop.hbase.filter.Filter. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: ditb   File: TestTableInputFormat.java   View source code 7 votes vote down vote up
@Override
public void configure(JobConf job) {
  try {
    Connection connection = ConnectionFactory.createConnection(HBaseConfiguration.create(job));
    TableName tableName = TableName.valueOf("exampleJobConfigurableTable");
    // mandatory
    initializeTable(connection, tableName);
    byte[][] inputColumns = new byte [][] { Bytes.toBytes("columnA"),
      Bytes.toBytes("columnB") };
    //optional
    Scan scan = new Scan();
    for (byte[] family : inputColumns) {
      scan.addFamily(family);
    }
    Filter exampleFilter = new RowFilter(CompareOp.EQUAL, new RegexStringComparator("aa.*"));
    scan.setFilter(exampleFilter);
    setScan(scan);
  } catch (IOException exception) {
    throw new RuntimeException("Failed to initialize.", exception);
  }
}
 
Example 2
Project: SparkDemo   File: MyClass.java   View source code 6 votes vote down vote up
public static void QueryByCondition2(String tableName) {

        try {
            HTablePool pool = new HTablePool(configuration, 1000);
            HTable table = (HTable) pool.getTable(tableName);
            Filter filter = new SingleColumnValueFilter(Bytes
                    .toBytes("column1"), null, CompareOp.EQUAL, Bytes
                    .toBytes("aaa")); // 当列column1的值为aaa时进行查询
            Scan s = new Scan();
            s.setFilter(filter);
            ResultScanner rs = table.getScanner(s);
            for (Result r : rs) {
                System.out.println("获得到rowkey:" + new String(r.getRow()));
                for (KeyValue keyValue : r.raw()) {
                    System.out.println("列:" + new String(keyValue.getFamily())
                            + "====值:" + new String(keyValue.getValue()));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
 
Example 3
Project: QDrill   File: HBaseFilterBuilder.java   View source code 6 votes vote down vote up
private HBaseScanSpec mergeScanSpecs(String functionName, HBaseScanSpec leftScanSpec, HBaseScanSpec rightScanSpec) {
  Filter newFilter = null;
  byte[] startRow = HConstants.EMPTY_START_ROW;
  byte[] stopRow = HConstants.EMPTY_END_ROW;

  switch (functionName) {
  case "booleanAnd":
    newFilter = HBaseUtils.andFilterAtIndex(leftScanSpec.filter, HBaseUtils.LAST_FILTER, rightScanSpec.filter);
    startRow = HBaseUtils.maxOfStartRows(leftScanSpec.startRow, rightScanSpec.startRow);
    stopRow = HBaseUtils.minOfStopRows(leftScanSpec.stopRow, rightScanSpec.stopRow);
    break;
  case "booleanOr":
    newFilter = HBaseUtils.orFilterAtIndex(leftScanSpec.filter, HBaseUtils.LAST_FILTER, rightScanSpec.filter);
    startRow = HBaseUtils.minOfStartRows(leftScanSpec.startRow, rightScanSpec.startRow);
    stopRow = HBaseUtils.maxOfStopRows(leftScanSpec.stopRow, rightScanSpec.stopRow);
  }
  return new HBaseScanSpec(groupScan.getTableName(), startRow, stopRow, newFilter);
}
 
Example 4
Project: QDrill   File: HBaseUtils.java   View source code 6 votes vote down vote up
public static Filter andFilterAtIndex(Filter currentFilter, int index, Filter newFilter) {
  if (currentFilter == null) {
    return newFilter;
  } else if (newFilter == null) {
    return currentFilter;
  }

  List<Filter> allFilters = Lists.newArrayList();
  if (currentFilter instanceof FilterList && ((FilterList)currentFilter).getOperator() == FilterList.Operator.MUST_PASS_ALL) {
    allFilters.addAll(((FilterList)currentFilter).getFilters());
  } else {
    allFilters.add(currentFilter);
  }
  allFilters.add((index == LAST_FILTER ? allFilters.size() : index), newFilter);
  return new FilterList(FilterList.Operator.MUST_PASS_ALL, allFilters);
}
 
Example 5
Project: QDrill   File: HBaseUtils.java   View source code 6 votes vote down vote up
public static Filter orFilterAtIndex(Filter currentFilter, int index, Filter newFilter) {
  if (currentFilter == null) {
    return newFilter;
  } else if (newFilter == null) {
    return currentFilter;
  }

  List<Filter> allFilters = Lists.newArrayList();
  if (currentFilter instanceof FilterList && ((FilterList)currentFilter).getOperator() == FilterList.Operator.MUST_PASS_ONE) {
    allFilters.addAll(((FilterList)currentFilter).getFilters());
  } else {
    allFilters.add(currentFilter);
  }
  allFilters.add((index == LAST_FILTER ? allFilters.size() : index), newFilter);
  return new FilterList(FilterList.Operator.MUST_PASS_ONE, allFilters);
}
 
Example 6
Project: dremio-oss   File: HBaseFilterBuilder.java   View source code 6 votes vote down vote up
public HBaseScanSpec parseTree() {
  HBaseScanSpec parsedSpec = le.accept(this, null);
  if (parsedSpec != null) {
    parsedSpec = mergeScanSpecs("booleanAnd", this.groupScan.getHBaseScanSpec(), parsedSpec);
    /*
     * If RowFilter is THE filter attached to the scan specification,
     * remove it since its effect is also achieved through startRow and stopRow.
     */
    Filter parsedFilter = HBaseUtils.deserializeFilter(parsedSpec.filter);
    if (parsedFilter instanceof RowFilter &&
        ((RowFilter)parsedFilter).getComparator() instanceof BinaryComparator) {
      parsedSpec.filter = null;
    }
  }
  return parsedSpec;
}
 
Example 7
Project: dremio-oss   File: HBaseFilterBuilder.java   View source code 6 votes vote down vote up
private HBaseScanSpec mergeScanSpecs(String functionName, HBaseScanSpec leftScanSpec, HBaseScanSpec rightScanSpec) {
  Filter newFilter = null;
  byte[] startRow = HConstants.EMPTY_START_ROW;
  byte[] stopRow = HConstants.EMPTY_END_ROW;

  switch (functionName) {
  case "booleanAnd":
    newFilter = HBaseUtils.andFilterAtIndex(
        HBaseUtils.deserializeFilter(leftScanSpec.filter),
        HBaseUtils.LAST_FILTER,
        HBaseUtils.deserializeFilter(rightScanSpec.filter));
    startRow = HBaseUtils.maxOfStartRows(leftScanSpec.startRow, rightScanSpec.startRow);
    stopRow = HBaseUtils.minOfStopRows(leftScanSpec.stopRow, rightScanSpec.stopRow);
    break;
  case "booleanOr":
    newFilter = HBaseUtils.orFilterAtIndex(
        HBaseUtils.deserializeFilter(leftScanSpec.filter),
        HBaseUtils.LAST_FILTER,
        HBaseUtils.deserializeFilter(rightScanSpec.filter));
    startRow = HBaseUtils.minOfStartRows(leftScanSpec.startRow, rightScanSpec.startRow);
    stopRow = HBaseUtils.maxOfStopRows(leftScanSpec.stopRow, rightScanSpec.stopRow);
  }
  return new HBaseScanSpec(groupScan.getTableName(), startRow, stopRow, newFilter);
}
 
Example 8
Project: aliyun-tablestore-hbase-client   File: TestFilterList.java   View source code 6 votes vote down vote up
@Test
public void testTwoFilterWithMustAllPassFailed() throws IOException {
    clean();
    {
        Put put = new Put(Bytes.toBytes(rowPrefix));
        put.addColumn(Bytes.toBytes(familyName), Bytes.toBytes("col_1"), Bytes.toBytes("col_1_var"));
        put.addColumn(Bytes.toBytes(familyName), Bytes.toBytes("col_2"), Bytes.toBytes("col_2_var"));
        table.put(put);
    }

    {
        Get get = new Get(Bytes.toBytes(rowPrefix));
        Filter filter1 = new SingleColumnValueFilter(Bytes.toBytes(familyName), Bytes.toBytes("col_1"),
                CompareFilter.CompareOp.EQUAL, Bytes.toBytes("col_1_var"));
        Filter filter2 = new SingleColumnValueFilter(Bytes.toBytes(familyName), Bytes.toBytes("col_2"),
                CompareFilter.CompareOp.NOT_EQUAL, Bytes.toBytes("col_2_var"));
        FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL);
        filterList.addFilter(filter1);
        filterList.addFilter(filter2);

        get.setFilter(filterList);
        Result result = table.get(get);
        assertTrue(result.getRow() == null);
    }
}
 
Example 9
Project: aliyun-tablestore-hbase-client   File: TestFilterList.java   View source code 6 votes vote down vote up
@Test
public void testTwoFilterWithMustOnePassFailed() throws IOException {
    clean();
    {
        Put put = new Put(Bytes.toBytes(rowPrefix));
        put.addColumn(Bytes.toBytes(familyName), Bytes.toBytes("col_1"), Bytes.toBytes("col_1_var"));
        put.addColumn(Bytes.toBytes(familyName), Bytes.toBytes("col_2"), Bytes.toBytes("col_2_var"));
        table.put(put);
    }

    {
        Get get = new Get(Bytes.toBytes(rowPrefix));
        Filter filter1 = new SingleColumnValueFilter(Bytes.toBytes(familyName), Bytes.toBytes("col_1"),
                CompareFilter.CompareOp.NOT_EQUAL, Bytes.toBytes("col_1_var"));
        Filter filter2 = new SingleColumnValueFilter(Bytes.toBytes(familyName), Bytes.toBytes("col_2"),
                CompareFilter.CompareOp.NOT_EQUAL, Bytes.toBytes("col_2_var"));
        FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL);
        filterList.addFilter(filter1);
        filterList.addFilter(filter2);

        get.setFilter(filterList);
        Result result = table.get(get);
        assertTrue(result.getRow() == null);
    }
}
 
Example 10
Project: ditb   File: CellCounter.java   View source code 6 votes vote down vote up
private static Scan getConfiguredScanForJob(Configuration conf, String[] args) throws IOException {
  Scan s = new Scan();
  // Set Scan Versions
  s.setMaxVersions(Integer.MAX_VALUE);
  s.setCacheBlocks(false);
  // Set Scan Column Family
  if (conf.get(TableInputFormat.SCAN_COLUMN_FAMILY) != null) {
    s.addFamily(Bytes.toBytes(conf.get(TableInputFormat.SCAN_COLUMN_FAMILY)));
  }
  // Set RowFilter or Prefix Filter if applicable.
  Filter rowFilter = getRowFilter(args);
  if (rowFilter!= null) {
    LOG.info("Setting Row Filter for counter.");
    s.setFilter(rowFilter);
  }
  // Set TimeRange if defined
  long timeRange[] = getTimeRange(args);
  if (timeRange != null) {
    LOG.info("Setting TimeRange for counter.");
    s.setTimeRange(timeRange[0], timeRange[1]);
  }
  return s;
}
 
Example 11
Project: ditb   File: ScanPreprocess.java   View source code 6 votes vote down vote up
public static ConditionTree preprocess(HRegion region, Filter filter, float maxScale) {
  if (filter == null) return null;
  ConditionTree tree = null;
  if (isIndexFilter(region, filter)) {
    System.out.println("preprocess A");
    tree = new ConditionTreeNoneLeafNode(region, (SingleColumnValueFilter) filter, maxScale);
  } else if (filter instanceof FilterList) {
    System.out.println("preprocess B");
    tree = new ConditionTreeNoneLeafNode(region, (FilterList) filter, maxScale);
  }
  if (tree.isPrune()) {
    System.out.println("return null for prune");
    return null;
  } else {
    return tree;
  }
}
 
Example 12
Project: ditb   File: PerformanceEvaluation.java   View source code 6 votes vote down vote up
protected Scan constructScan(byte[] valuePrefix) throws IOException {
  FilterList list = new FilterList();
  Filter filter = new SingleColumnValueFilter(
      FAMILY_NAME, COLUMN_ZERO, CompareFilter.CompareOp.EQUAL,
      new BinaryComparator(valuePrefix)
  );
  list.addFilter(filter);
  if(opts.filterAll) {
    list.addFilter(new FilterAllFilter());
  }
  Scan scan = new Scan();
  scan.setCaching(opts.caching);
  if (opts.addColumns) {
    scan.addColumn(FAMILY_NAME, QUALIFIER_NAME);
  } else {
    scan.addFamily(FAMILY_NAME);
  }
  scan.setFilter(list);
  return scan;
}
 
Example 13
Project: ditb   File: TestTableInputFormat.java   View source code 6 votes vote down vote up
@Override
public void configure(JobConf job) {
  try {
    HTable exampleTable = new HTable(HBaseConfiguration.create(job),
      Bytes.toBytes("exampleDeprecatedTable"));
    // mandatory
    setHTable(exampleTable);
    byte[][] inputColumns = new byte [][] { Bytes.toBytes("columnA"),
      Bytes.toBytes("columnB") };
    // mandatory
    setInputColumns(inputColumns);
    Filter exampleFilter = new RowFilter(CompareOp.EQUAL, new RegexStringComparator("aa.*"));
    // optional
    setRowFilter(exampleFilter);
  } catch (IOException exception) {
    throw new RuntimeException("Failed to configure for job.", exception);
  }
}
 
Example 14
Project: ditb   File: TestTableInputFormat.java   View source code 6 votes vote down vote up
@Override
public void configure(JobConf job) {
  try {
    HTable exampleTable = new HTable(HBaseConfiguration.create(job),
      Bytes.toBytes("exampleDeprecatedTable"));
    // mandatory
    setHTable(exampleTable);
    byte[][] inputColumns = new byte [][] { Bytes.toBytes("columnA"),
      Bytes.toBytes("columnB") };
    // optional
    Scan scan = new Scan();
    for (byte[] family : inputColumns) {
      scan.addFamily(family);
    }
    Filter exampleFilter = new RowFilter(CompareOp.EQUAL, new RegexStringComparator("aa.*"));
    scan.setFilter(exampleFilter);
    setScan(scan);
  } catch (IOException exception) {
    throw new RuntimeException("Failed to configure for job.", exception);
  }
}
 
Example 15
Project: ditb   File: TestTableInputFormat.java   View source code 6 votes vote down vote up
@Override
protected void initialize(JobContext job) throws IOException {
  Connection connection = ConnectionFactory.createConnection(HBaseConfiguration.create(
      job.getConfiguration()));
  TableName tableName = TableName.valueOf("exampleTable");
  // mandatory
  initializeTable(connection, tableName);
  byte[][] inputColumns = new byte [][] { Bytes.toBytes("columnA"),
    Bytes.toBytes("columnB") };
  //optional
  Scan scan = new Scan();
  for (byte[] family : inputColumns) {
    scan.addFamily(family);
  }
  Filter exampleFilter = new RowFilter(CompareOp.EQUAL, new RegexStringComparator("aa.*"));
  scan.setFilter(exampleFilter);
  setScan(scan);
}
 
Example 16
Project: ditb   File: ProtobufUtil.java   View source code 6 votes vote down vote up
/**
 * Convert a protocol buffer Filter to a client Filter
 *
 * @param proto the protocol buffer Filter to convert
 * @return the converted Filter
 */
@SuppressWarnings("unchecked")
public static Filter toFilter(FilterProtos.Filter proto) throws IOException {
  String type = proto.getName();
  final byte [] value = proto.getSerializedFilter().toByteArray();
  String funcName = "parseFrom";
  try {
    Class<? extends Filter> c =
      (Class<? extends Filter>)Class.forName(type, true, CLASS_LOADER);
    Method parseFrom = c.getMethod(funcName, byte[].class);
    if (parseFrom == null) {
      throw new IOException("Unable to locate function: " + funcName + " in type: " + type);
    }
    return (Filter)parseFrom.invoke(c, value);
  } catch (Exception e) {
    // Either we couldn't instantiate the method object, or "parseFrom" failed.
    // In either case, let's not retry.
    throw new DoNotRetryIOException(e);
  }
}
 
Example 17
Project: ditb   File: ScannerModel.java   View source code 5 votes vote down vote up
/**
 * @param filter the filter
 * @return the JSON representation of the filter
 * @throws Exception 
 */
public static String stringifyFilter(final Filter filter) throws Exception {
  JSONJAXBContext context =
    new JSONJAXBContext(JSONConfiguration.natural().build(),
      FilterModel.class);
  JSONMarshaller marshaller = context.createJSONMarshaller();
  StringWriter writer = new StringWriter();
  marshaller.marshallToJSON(new FilterModel(filter), writer);
  return writer.toString();
}
 
Example 18
Project: big_data   File: ActiveUserRunner.java   View source code 5 votes vote down vote up
/**
 * 获取这个列名过滤的column
 * 
 * @param columns
 * @return
 */
private Filter getColumnFilter(String[] columns) {
	int length = columns.length;
	byte[][] filter = new byte[length][];
	for (int i = 0; i < length; i++) {
		filter[i] = Bytes.toBytes(columns[i]);
	}
	return new MultipleColumnPrefixFilter(filter);
}
 
Example 19
Project: QDrill   File: HBaseFilterBuilder.java   View source code 5 votes vote down vote up
private HBaseScanSpec createRowKeyPrefixScanSpec(FunctionCall call,
    CompareFunctionsProcessor processor) {
    byte[] startRow = processor.getRowKeyPrefixStartRow();
    byte[] stopRow  = processor.getRowKeyPrefixStopRow();
    Filter filter   = processor.getRowKeyPrefixFilter();

    if (startRow != HConstants.EMPTY_START_ROW ||
      stopRow != HConstants.EMPTY_END_ROW ||
      filter != null) {
      return new HBaseScanSpec(groupScan.getTableName(), startRow, stopRow, filter);
    }

    // else
    return null;
}
 
Example 20
Project: QDrill   File: HBaseUtils.java   View source code 5 votes vote down vote up
static Filter parseFilterString(String filterString) {
  if (filterString == null) {
    return null;
  }
  try {
    return FILTER_PARSEER.parseFilterString(filterString);
  } catch (CharacterCodingException e) {
    throw new DrillRuntimeException("Error parsing filter string: " + filterString, e);
  }
}
 
Example 21
Project: QDrill   File: HBaseUtils.java   View source code 5 votes vote down vote up
public static byte[] serializeFilter(Filter filter) {
  if (filter == null) {
    return null;
  }
  try {
    FilterProtos.Filter pbFilter = ProtobufUtil.toFilter(filter);
    return pbFilter.toByteArray();
  } catch (IOException e) {
    throw new DrillRuntimeException("Error serializing filter: " + filter, e);
  }
}
 
Example 22
Project: QDrill   File: HBaseUtils.java   View source code 5 votes vote down vote up
public static Filter deserializeFilter(byte[] filterBytes) {
  if (filterBytes == null) {
    return null;
  }
  try {
    FilterProtos.Filter pbFilter = FilterProtos.Filter.parseFrom(filterBytes);
    return ProtobufUtil.toFilter(pbFilter);
  } catch (Exception e) {
    throw new DrillRuntimeException("Error deserializing filter: " + filterBytes, e);
  }
}
 
Example 23
Project: ignite-hbase   File: HBaseCacheStore.java   View source code 5 votes vote down vote up
private Scan createScan(Object[] args) {
  Scan scan = new Scan();
  scan.addColumn(family(), QUALIFIER);
  Optional<Filter> filter = FilterParser.createFilter(args);
  filter.ifPresent(scan::setFilter);
  return scan;
}
 
Example 24
Project: hadoop-ecosystem-examples   File: HBaseService.java   View source code 5 votes vote down vote up
/**
 * 多条件查询
 * @param tableName
 * @param familyNames
 * @param qualifiers
 * @param values
    * @throws IOException
    */
public static void queryByConditions(String tableName, String[] familyNames, String[] qualifiers,String[] values) throws IOException {
	Connection conn = ConnectionFactory.createConnection(conf);
	Table table = conn.getTable(TableName.valueOf(tableName));
	try {
		List<Filter> filters = new ArrayList<Filter>();
		if (familyNames != null && familyNames.length > 0) {
			int i = 0;
			for (String familyName : familyNames) {
				Filter filter = new SingleColumnValueFilter(Bytes.toBytes(familyName), Bytes.toBytes(qualifiers[i]), CompareFilter.CompareOp.EQUAL, Bytes.toBytes(values[i]));
				filters.add(filter);
				i++;
			}
		}
		FilterList filterList = new FilterList(filters);
		Scan scan = new Scan();
		scan.setFilter(filterList);
		ResultScanner rs = table.getScanner(scan);
		for (Result r : rs) {
			System.out.println("获得到rowkey:" + new String(r.getRow()));
			for (Cell keyValue : r.rawCells()) {
				System.out.println("列:" + new String(CellUtil.cloneFamily(keyValue))+":"+new String(CellUtil.cloneQualifier(keyValue)) + "====值:" + new String(CellUtil.cloneValue(keyValue)));
			}
		}
		rs.close();
	} catch (Exception e) {
		table.close();
		conn.close();
	}

}
 
Example 25
Project: dremio-oss   File: HBaseFilterBuilder.java   View source code 5 votes vote down vote up
private HBaseScanSpec createRowKeyPrefixScanSpec(FunctionCall call, CompareFunctionsProcessor processor) {
  byte[] startRow = processor.getRowKeyPrefixStartRow();
  byte[] stopRow  = processor.getRowKeyPrefixStopRow();
  Filter filter   = processor.getRowKeyPrefixFilter();

  if (startRow != HConstants.EMPTY_START_ROW ||
      stopRow != HConstants.EMPTY_END_ROW ||
      filter != null) {
    return new HBaseScanSpec(groupScan.getTableName(), startRow, stopRow, filter);
  }

  // else
  return null;
}
 
Example 26
Project: dremio-oss   File: HBaseScanSpec.java   View source code 5 votes vote down vote up
public HBaseScanSpec(String tableName, byte[] startRow, byte[] stopRow, Filter filter) {
  this.tableName = tableName;
  this.startRow = startRow;
  this.stopRow = stopRow;
  if (filter != null) {
    this.filter = HBaseUtils.serializeFilter(filter);
  } else {
    this.filter = null;
  }
}
 
Example 27
Project: dremio-oss   File: HBaseScanSpec.java   View source code 5 votes vote down vote up
@JsonIgnore
public Filter getFilter() {
  if (filterParsed == null) {
    synchronized(this) {
      if (filterParsed == null) {
        filterParsed = HBaseUtils.deserializeFilter(this.filter);
      }
    }
  }
  return filterParsed;
}
 
Example 28
Project: dremio-oss   File: HBaseScanSpec.java   View source code 5 votes vote down vote up
@Override
public String toString() {
  Filter filterToString = getFilter();
  return "HBaseScanSpec [tableName=" + tableName
      + ", startRow=" + (startRow == null ? null : Bytes.toStringBinary(startRow))
      + ", stopRow=" + (stopRow == null ? null : Bytes.toStringBinary(stopRow))
      + ", filter=" + (filter == null ? null : filterToString.toString())
      + "]";
}
 
Example 29
Project: dremio-oss   File: HBaseUtils.java   View source code 5 votes vote down vote up
static Filter parseFilterString(String filterString) {
  if (filterString == null) {
    return null;
  }
  try {
    return FILTER_PARSEER.parseFilterString(filterString);
  } catch (CharacterCodingException e) {
    throw new RuntimeException("Error parsing filter string: " + filterString, e);
  }
}
 
Example 30
Project: dremio-oss   File: HBaseUtils.java   View source code 5 votes vote down vote up
public static byte[] serializeFilter(Filter filter) {
  if (filter == null) {
    return null;
  }
  try {
    FilterProtos.Filter pbFilter = ProtobufUtil.toFilter(filter);
    return pbFilter.toByteArray();
  } catch (IOException e) {
    throw new RuntimeException("Error serializing filter: " + filter, e);
  }
}
 
Example 31
Project: dremio-oss   File: HBaseUtils.java   View source code 5 votes vote down vote up
public static Filter deserializeFilter(byte[] filterBytes) {
  if (filterBytes == null) {
    return null;
  }
  try {
    FilterProtos.Filter pbFilter = FilterProtos.Filter.parseFrom(filterBytes);
    return ProtobufUtil.toFilter(pbFilter);
  } catch (Exception e) {
    throw new RuntimeException("Error deserializing filter: " + filterBytes, e);
  }
}
 
Example 32
Project: aliyun-tablestore-hbase-client   File: TestColumnPaginationFilter.java   View source code 5 votes vote down vote up
@Test
public void testResultLessThanLimit() throws IOException {
    clean();
    {
        Put put = new Put(Bytes.toBytes(rowPrefix));
        put.addColumn(Bytes.toBytes(familyName), Bytes.toBytes("col_1"), Bytes.toBytes("col_1_var"));
        put.addColumn(Bytes.toBytes(familyName), Bytes.toBytes("col_2"), Bytes.toBytes("col_2_var"));
        put.addColumn(Bytes.toBytes(familyName), Bytes.toBytes("col_3"), Bytes.toBytes("col_3_var"));
        put.addColumn(Bytes.toBytes(familyName), Bytes.toBytes("col_4"), Bytes.toBytes("col_4_var"));
        table.put(put);
    }

    {
        Get get = new Get(Bytes.toBytes(rowPrefix));
        Filter filter = new ColumnPaginationFilter(5, 3);
        get.setFilter(filter);
        Result result = table.get(get);
        List<Cell> cells = result.getColumnCells(Bytes.toBytes(familyName), Bytes.toBytes("col_1"));
        assertEquals(0, cells.size());

        cells = result.getColumnCells(Bytes.toBytes(familyName), Bytes.toBytes("col_2"));
        assertEquals(0, cells.size());

        cells = result.getColumnCells(Bytes.toBytes(familyName), Bytes.toBytes("col_3"));
        assertEquals(0, cells.size());

        cells = result.getColumnCells(Bytes.toBytes(familyName), Bytes.toBytes("col_4"));
        assertEquals(1, cells.size());
        assertEquals("col_4_var", Bytes.toString(cells.get(0).getValueArray(), cells.get(0).getValueOffset(), cells.get(0).getValueLength()));
    }
}
 
Example 33
Project: aliyun-tablestore-hbase-client   File: TestColumnPaginationFilter.java   View source code 5 votes vote down vote up
@Test
public void testStartRow() throws IOException {
    clean();
    {
        Put put = new Put(Bytes.toBytes(rowPrefix));
        put.addColumn(Bytes.toBytes(familyName), Bytes.toBytes("col_1"), Bytes.toBytes("col_1_var"));
        put.addColumn(Bytes.toBytes(familyName), Bytes.toBytes("col_2"), Bytes.toBytes("col_2_var"));
        put.addColumn(Bytes.toBytes(familyName), Bytes.toBytes("col_3"), Bytes.toBytes("col_3_var"));
        put.addColumn(Bytes.toBytes(familyName), Bytes.toBytes("col_4"), Bytes.toBytes("col_4_var"));
        table.put(put);
    }

    {
        Get get = new Get(Bytes.toBytes(rowPrefix));
        Filter filter = new ColumnPaginationFilter(1, Bytes.toBytes("col_2"));
        get.setFilter(filter);
        Result result = table.get(get);
        List<Cell> cells = result.getColumnCells(Bytes.toBytes(familyName), Bytes.toBytes("col_1"));
        assertEquals(0, cells.size());

        cells = result.getColumnCells(Bytes.toBytes(familyName), Bytes.toBytes("col_2"));
        assertEquals(1, cells.size());
        assertEquals("col_2_var", Bytes.toString(cells.get(0).getValueArray(), cells.get(0).getValueOffset(), cells.get(0).getValueLength()));

        cells = result.getColumnCells(Bytes.toBytes(familyName), Bytes.toBytes("col_3"));
        assertEquals(0, cells.size());

        cells = result.getColumnCells(Bytes.toBytes(familyName), Bytes.toBytes("col_4"));
        assertEquals(0, cells.size());
    }
}
 
Example 34
Project: aliyun-tablestore-hbase-client   File: TestColumnPaginationFilter.java   View source code 5 votes vote down vote up
@Test
public void testStartRowByScan() throws IOException {
    clean();
    {
        Put put = new Put(Bytes.toBytes(rowPrefix));
        put.addColumn(Bytes.toBytes(familyName), Bytes.toBytes("col_1"), Bytes.toBytes("col_1_var"));
        put.addColumn(Bytes.toBytes(familyName), Bytes.toBytes("col_2"), Bytes.toBytes("col_2_var"));
        put.addColumn(Bytes.toBytes(familyName), Bytes.toBytes("col_3"), Bytes.toBytes("col_3_var"));
        put.addColumn(Bytes.toBytes(familyName), Bytes.toBytes("col_4"), Bytes.toBytes("col_4_var"));
        table.put(put);
    }

    {
        Scan scan = new Scan();
        Filter filter = new ColumnPaginationFilter(1, Bytes.toBytes("col_2"));
        scan.setFilter(filter);
        scan.setStartRow(Bytes.toBytes(rowPrefix));
        scan.setStopRow(Bytes.toBytes(rowPrefix + 5));
        ResultScanner scanResult = table.getScanner(scan);

        Result result = scanResult.next();

        List<Cell> cells = result.getColumnCells(Bytes.toBytes(familyName), Bytes.toBytes("col_1"));
        assertEquals(0, cells.size());

        cells = result.getColumnCells(Bytes.toBytes(familyName), Bytes.toBytes("col_2"));
        assertEquals(1, cells.size());
        assertEquals("col_2_var", Bytes.toString(cells.get(0).getValueArray(), cells.get(0).getValueOffset(), cells.get(0).getValueLength()));

        cells = result.getColumnCells(Bytes.toBytes(familyName), Bytes.toBytes("col_3"));
        assertEquals(0, cells.size());

        cells = result.getColumnCells(Bytes.toBytes(familyName), Bytes.toBytes("col_4"));
        assertEquals(0, cells.size());
    }
}
 
Example 35
Project: aliyun-tablestore-hbase-client   File: TestColumnPaginationFilter.java   View source code 5 votes vote down vote up
@Test
public void testStartRowIsNotExist() throws IOException {
    clean();
    {
        Put put = new Put(Bytes.toBytes(rowPrefix));
        put.addColumn(Bytes.toBytes(familyName), Bytes.toBytes("col_1"), Bytes.toBytes("col_1_var"));
        put.addColumn(Bytes.toBytes(familyName), Bytes.toBytes("col_2"), Bytes.toBytes("col_2_var"));
        put.addColumn(Bytes.toBytes(familyName), Bytes.toBytes("col_3"), Bytes.toBytes("col_3_var"));
        put.addColumn(Bytes.toBytes(familyName), Bytes.toBytes("col_4"), Bytes.toBytes("col_4_var"));
        table.put(put);
    }

    {
        Get get = new Get(Bytes.toBytes(rowPrefix));
        Filter filter = new ColumnPaginationFilter(1, Bytes.toBytes("col_3_not_exist"));
        get.setFilter(filter);
        Result result = table.get(get);
        List<Cell> cells = result.getColumnCells(Bytes.toBytes(familyName), Bytes.toBytes("col_1"));
        assertEquals(0, cells.size());

        cells = result.getColumnCells(Bytes.toBytes(familyName), Bytes.toBytes("col_2"));
        assertEquals(0, cells.size());

        cells = result.getColumnCells(Bytes.toBytes(familyName), Bytes.toBytes("col_3"));
        assertEquals(0, cells.size());

        cells = result.getColumnCells(Bytes.toBytes(familyName), Bytes.toBytes("col_4"));
        assertEquals(1, cells.size());
        assertEquals("col_4_var", Bytes.toString(cells.get(0).getValueArray(), cells.get(0).getValueOffset(), cells.get(0).getValueLength()));
    }
}
 
Example 36
Project: aliyun-tablestore-hbase-client   File: TestFilterList.java   View source code 5 votes vote down vote up
@Test
public void testTwoFilterWithDefaultOperator() throws IOException {
    clean();
    {
        Put put = new Put(Bytes.toBytes(rowPrefix));
        put.addColumn(Bytes.toBytes(familyName), Bytes.toBytes("col_1"), Bytes.toBytes("col_1_var"));
        put.addColumn(Bytes.toBytes(familyName), Bytes.toBytes("col_2"), Bytes.toBytes("col_2_var"));
        table.put(put);
    }

    {
        Get get = new Get(Bytes.toBytes(rowPrefix));
        Filter filter1 = new SingleColumnValueFilter(Bytes.toBytes(familyName), Bytes.toBytes("col_1"),
                CompareFilter.CompareOp.EQUAL, Bytes.toBytes("col_1_var"));
        Filter filter2 = new SingleColumnValueFilter(Bytes.toBytes(familyName), Bytes.toBytes("col_2"),
                CompareFilter.CompareOp.EQUAL, Bytes.toBytes("col_2_var"));
        FilterList filterList = new FilterList();
        filterList.addFilter(filter1);
        filterList.addFilter(filter2);

        get.setFilter(filterList);
        Result result = table.get(get);
        String value = Bytes.toString(result.getValue(Bytes.toBytes(familyName), Bytes.toBytes("col_1")));
        assertEquals("col_1_var", value);
        value = Bytes.toString(result.getValue(Bytes.toBytes(familyName), Bytes.toBytes("col_2")));
        assertEquals("col_2_var", value);
    }
}
 
Example 37
Project: aliyun-tablestore-hbase-client   File: TestFilterList.java   View source code 5 votes vote down vote up
@Test
public void testTwoFilterWithMustAllPassSucceeded() throws IOException {
    clean();
    {
        Put put = new Put(Bytes.toBytes(rowPrefix));
        put.addColumn(Bytes.toBytes(familyName), Bytes.toBytes("col_1"), Bytes.toBytes("col_1_var"));
        put.addColumn(Bytes.toBytes(familyName), Bytes.toBytes("col_2"), Bytes.toBytes("col_2_var"));
        table.put(put);
    }

    {
        Get get = new Get(Bytes.toBytes(rowPrefix));
        Filter filter1 = new SingleColumnValueFilter(Bytes.toBytes(familyName), Bytes.toBytes("col_1"),
                CompareFilter.CompareOp.EQUAL, Bytes.toBytes("col_1_var"));
        Filter filter2 = new SingleColumnValueFilter(Bytes.toBytes(familyName), Bytes.toBytes("col_2"),
                CompareFilter.CompareOp.EQUAL, Bytes.toBytes("col_2_var"));
        FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL);
        filterList.addFilter(filter1);
        filterList.addFilter(filter2);

        get.setFilter(filterList);
        Result result = table.get(get);
        String value = Bytes.toString(result.getValue(Bytes.toBytes(familyName), Bytes.toBytes("col_1")));
        assertEquals("col_1_var", value);
        value = Bytes.toString(result.getValue(Bytes.toBytes(familyName), Bytes.toBytes("col_2")));
        assertEquals("col_2_var", value);
    }
}
 
Example 38
Project: aliyun-tablestore-hbase-client   File: TestFilterList.java   View source code 5 votes vote down vote up
@Test
public void testTwoFilterWithMustOnePassSucceeded() throws IOException {
    clean();
    {
        Put put = new Put(Bytes.toBytes(rowPrefix));
        put.addColumn(Bytes.toBytes(familyName), Bytes.toBytes("col_1"), Bytes.toBytes("col_1_var"));
        put.addColumn(Bytes.toBytes(familyName), Bytes.toBytes("col_2"), Bytes.toBytes("col_2_var"));
        table.put(put);
    }

    {
        Get get = new Get(Bytes.toBytes(rowPrefix));
        Filter filter1 = new SingleColumnValueFilter(Bytes.toBytes(familyName), Bytes.toBytes("col_1"),
                CompareFilter.CompareOp.EQUAL, Bytes.toBytes("col_1_var"));
        Filter filter2 = new SingleColumnValueFilter(Bytes.toBytes(familyName), Bytes.toBytes("col_2"),
                CompareFilter.CompareOp.NOT_EQUAL, Bytes.toBytes("col_2_var"));
        FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ONE);
        filterList.addFilter(filter1);
        filterList.addFilter(filter2);

        get.setFilter(filterList);
        Result result = table.get(get);
        String value = Bytes.toString(result.getValue(Bytes.toBytes(familyName), Bytes.toBytes("col_1")));
        assertEquals("col_1_var", value);
        value = Bytes.toString(result.getValue(Bytes.toBytes(familyName), Bytes.toBytes("col_2")));
        assertEquals("col_2_var", value);
    }
}
 
Example 39
Project: ditb   File: ThriftServerRunner.java   View source code 5 votes vote down vote up
@Override
public int scannerOpenWithPrefix(ByteBuffer tableName,
                                 ByteBuffer startAndPrefix,
                                 List<ByteBuffer> columns,
    Map<ByteBuffer, ByteBuffer> attributes)
    throws IOError, TException {

  Table table = null;
  try {
    table = getTable(tableName);
    Scan scan = new Scan(getBytes(startAndPrefix));
    addAttributes(scan, attributes);
    Filter f = new WhileMatchFilter(
        new PrefixFilter(getBytes(startAndPrefix)));
    scan.setFilter(f);
    if (columns != null && columns.size() != 0) {
      for(ByteBuffer column : columns) {
        byte [][] famQf = KeyValue.parseColumn(getBytes(column));
        if(famQf.length == 1) {
          scan.addFamily(famQf[0]);
        } else {
          scan.addColumn(famQf[0], famQf[1]);
        }
      }
    }
    return addScanner(table.getScanner(scan), false);
  } catch (IOException e) {
    LOG.warn(e.getMessage(), e);
    throw new IOError(Throwables.getStackTraceAsString(e));
  } finally{
    closeTable(table);
  }
}
 
Example 40
Project: ditb   File: VisibilityController.java   View source code 5 votes vote down vote up
@Override
public RegionScanner preScannerOpen(ObserverContext<RegionCoprocessorEnvironment> e, Scan scan,
    RegionScanner s) throws IOException {
  if (!initialized) {
    throw new VisibilityControllerNotReadyException("VisibilityController not yet initialized!");
  }
  // Nothing to do if authorization is not enabled
  if (!authorizationEnabled) {
    return s;
  }
  Region region = e.getEnvironment().getRegion();
  Authorizations authorizations = null;
  try {
    authorizations = scan.getAuthorizations();
  } catch (DeserializationException de) {
    throw new IOException(de);
  }
  if (authorizations == null) {
    // No Authorizations present for this scan/Get!
    // In case of system tables other than "labels" just scan with out visibility check and
    // filtering. Checking visibility labels for META and NAMESPACE table is not needed.
    TableName table = region.getRegionInfo().getTable();
    if (table.isSystemTable() && !table.equals(LABELS_TABLE_NAME)) {
      return s;
    }
  }

  Filter visibilityLabelFilter = VisibilityUtils.createVisibilityLabelFilter(region,
      authorizations);
  if (visibilityLabelFilter != null) {
    Filter filter = scan.getFilter();
    if (filter != null) {
      scan.setFilter(new FilterList(filter, visibilityLabelFilter));
    } else {
      scan.setFilter(visibilityLabelFilter);
    }
  }
  return s;
}