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(); } }
@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); } }
@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); } }
private void doRawScan() throws IOException { FilterList filterList = new FilterList(); CompareFilter.CompareOp startOp = CompareFilter.CompareOp.GREATER_OR_EQUAL; CompareFilter.CompareOp stopOp = CompareFilter.CompareOp.LESS_OR_EQUAL; for (int i = 0; i < indexColumnNames.length && i < scanValues.length; i++) { filterList.addFilter( new SingleColumnValueFilter(familyName, Bytes.toBytes(indexColumnNames[i]), startOp, Bytes.toBytes(scanValues[i][0]))); filterList.addFilter( new SingleColumnValueFilter(familyName, Bytes.toBytes(indexColumnNames[i]), stopOp, Bytes.toBytes(scanValues[i][1]))); } Scan scan = new Scan(); scan.setFilter(filterList); scan.setId("raw-scan"); Table table = conn.getTable(tableName); ResultScanner scanner = table.getScanner(scan); Result result; int count = 0; while ((result = scanner.next()) != null) { ++count; if (PRINT_RESULT) printResult(result); } scanner.close(); System.out.println("raw scan has " + count + " records"); }
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; } }
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; }
private ResultScanner buildScanner(String keyPrefix, String value, Table ht) throws IOException { // OurFilterList allFilters = new OurFilterList(); FilterList allFilters = new FilterList(/* FilterList.Operator.MUST_PASS_ALL */); allFilters.addFilter(new PrefixFilter(Bytes.toBytes(keyPrefix))); SingleColumnValueFilter filter = new SingleColumnValueFilter(Bytes .toBytes("trans-tags"), Bytes.toBytes("qual2"), CompareOp.EQUAL, Bytes .toBytes(value)); filter.setFilterIfMissing(true); allFilters.addFilter(filter); // allFilters.addFilter(new // RowExcludingSingleColumnValueFilter(Bytes.toBytes("trans-tags"), // Bytes.toBytes("qual2"), CompareOp.EQUAL, Bytes.toBytes(value))); Scan scan = new Scan(); scan.addFamily(Bytes.toBytes("trans-blob")); scan.addFamily(Bytes.toBytes("trans-type")); scan.addFamily(Bytes.toBytes("trans-date")); scan.addFamily(Bytes.toBytes("trans-tags")); scan.addFamily(Bytes.toBytes("trans-group")); scan.setFilter(allFilters); return ht.getScanner(scan); }
private InternalScanner buildScanner(String keyPrefix, String value, HRegion r) throws IOException { // Defaults FilterList.Operator.MUST_PASS_ALL. FilterList allFilters = new FilterList(); allFilters.addFilter(new PrefixFilter(Bytes.toBytes(keyPrefix))); // Only return rows where this column value exists in the row. SingleColumnValueFilter filter = new SingleColumnValueFilter(Bytes.toBytes("trans-tags"), Bytes.toBytes("qual2"), CompareOp.EQUAL, Bytes.toBytes(value)); filter.setFilterIfMissing(true); allFilters.addFilter(filter); Scan scan = new Scan(); scan.addFamily(Bytes.toBytes("trans-blob")); scan.addFamily(Bytes.toBytes("trans-type")); scan.addFamily(Bytes.toBytes("trans-date")); scan.addFamily(Bytes.toBytes("trans-tags")); scan.addFamily(Bytes.toBytes("trans-group")); scan.setFilter(allFilters); return r.getScanner(scan); }
private void runScanner(Table table, boolean slow) throws Exception { long time = System.nanoTime(); Scan scan = new Scan(); scan.addColumn(cf_essential, col_name); scan.addColumn(cf_joined, col_name); SingleColumnValueFilter filter = new SingleColumnValueFilter( cf_essential, col_name, CompareFilter.CompareOp.EQUAL, flag_yes); filter.setFilterIfMissing(true); scan.setFilter(filter); scan.setLoadColumnFamiliesOnDemand(!slow); ResultScanner result_scanner = table.getScanner(scan); Result res; long rows_count = 0; while ((res = result_scanner.next()) != null) { rows_count++; } double timeSec = (System.nanoTime() - time) / 1000000000.0; result_scanner.close(); LOG.info((slow ? "Slow" : "Joined") + " scanner finished in " + Double.toString(timeSec) + " seconds, got " + Long.toString(rows_count/2) + " rows"); }
public static Range[] fromFilter(SingleColumnValueFilter filter) { if (!(filter.getComparator() instanceof BinaryComparator)) { return new Range[0]; } byte[] column = KeyValue.makeColumn(filter.getFamily(), filter.getQualifier()); CompareOp compareOp = filter.getOperator(); byte[] value = filter.getComparator().getValue(); if (compareOp == CompareOp.NOT_EQUAL) { return new Range[] { new Range(column, null, CompareOp.NO_OP, value, CompareOp.LESS), new Range(column, value, CompareOp.GREATER, null, CompareOp.NO_OP) }; } else { switch (compareOp) { case EQUAL: case GREATER_OR_EQUAL: case GREATER: return new Range[] { new Range(column, value, compareOp, null, CompareOp.NO_OP) }; case LESS: case LESS_OR_EQUAL: return new Range[] { new Range(column, null, CompareOp.NO_OP, value, compareOp) }; default: return new Range[0]; } } }
private FilterList getColumnValueFilters(Row row) { FilterList filterList = new FilterList(Operator.MUST_PASS_ALL); Set<String> filterColumnNames = Sets.newHashSet(row.schema().fieldNames()); for (Map.Entry<String, ColumnDef> column : columns.entrySet()) { if (!column.getValue().cf.equals("rowkey")) { if (filterColumnNames.contains(column.getKey())) { byte[] value = getColumnValueAsBytes(column.getValue().name, column.getValue().type, row); if (value != null) { SingleColumnValueFilter columnValueFilter = new SingleColumnValueFilter( Bytes.toBytes(column.getValue().cf), Bytes.toBytes(column.getValue().name), CompareFilter.CompareOp.EQUAL, value ); filterList.addFilter(columnValueFilter); } } } } return filterList; }
/** * Applies the message to {@link org.apache.hadoop.hbase.filter.Filter} to context. */ @Override public void apply(CamelContext context, HBaseRow rowModel) { fl.getFilters().clear(); if (rowModel != null) { for (HBaseCell cell : rowModel.getCells()) { if (cell.getValue() != null) { byte[] family = HBaseHelper.getHBaseFieldAsBytes(cell.getFamily()); byte[] qualifier = HBaseHelper.getHBaseFieldAsBytes(cell.getQualifier()); byte[] value = context.getTypeConverter().convertTo(byte[].class, cell.getValue()); SingleColumnValueFilter columnValueFilter = new SingleColumnValueFilter(family, qualifier, CompareFilter.CompareOp.EQUAL, value); fl.addFilter(columnValueFilter); } } } }
public static ConditionTree preprocess(HRegion region, Filter filter, float maxScale) { if (filter == null) return null; ConditionTree tree = null; if (isIndexFilter(region, filter)) { tree = new ConditionTreeNoneLeafNode(region, (SingleColumnValueFilter) filter, maxScale); } else if (filter instanceof FilterList) { tree = new ConditionTreeNoneLeafNode(region, (FilterList) filter, maxScale); } if (tree.isPrune()) { System.out.println("winter tree is prune"); return null; } else { return tree; } }
private ResultScanner buildScanner(String keyPrefix, String value, HTable ht) throws IOException { // OurFilterList allFilters = new OurFilterList(); FilterList allFilters = new FilterList(/* FilterList.Operator.MUST_PASS_ALL */); allFilters.addFilter(new PrefixFilter(Bytes.toBytes(keyPrefix))); SingleColumnValueFilter filter = new SingleColumnValueFilter(Bytes .toBytes("trans-tags"), Bytes.toBytes("qual2"), CompareOp.EQUAL, Bytes .toBytes(value)); filter.setFilterIfMissing(true); allFilters.addFilter(filter); // allFilters.addFilter(new // RowExcludingSingleColumnValueFilter(Bytes.toBytes("trans-tags"), // Bytes.toBytes("qual2"), CompareOp.EQUAL, Bytes.toBytes(value))); Scan scan = new Scan(); scan.addFamily(Bytes.toBytes("trans-blob")); scan.addFamily(Bytes.toBytes("trans-type")); scan.addFamily(Bytes.toBytes("trans-date")); scan.addFamily(Bytes.toBytes("trans-tags")); scan.addFamily(Bytes.toBytes("trans-group")); scan.setFilter(allFilters); return ht.getScanner(scan); }
@Override public ResultScanner getScanner() throws IOException { Scan scan = new Scan(); FilterList filters = new FilterList(); for (Range range : ranges) { if (range.getStartValue() != null) { filters.addFilter(new SingleColumnValueFilter(range.getFamily(), range.getQualifier(), range.getStartType(), range.getStartValue())); } if (range.getStopValue() != null) { filters.addFilter(new SingleColumnValueFilter(range.getFamily(), range.getQualifier(), range.getStopType(), range.getStopValue())); } System.out.println("coffey hbase main index range: " + Bytes.toString(range.getColumn()) + " [" + LCCIndexConstant.getStringOfValueAndType(range.getDataType(), range.getStartValue()) + "," + LCCIndexConstant.getStringOfValueAndType(range.getDataType(), range.getStopValue()) + "]"); scan.setCacheBlocks(false); } scan.setCacheBlocks(false); scan.setFilter(filters); return table.getScanner(scan); }
@Override public ResultScanner getScanner() throws IOException { Scan scan = new Scan(); RangeList list = new RangeList(); FilterList filters = new FilterList(); for (Range r : ranges) { list.addRange(r); if (r.getStartValue() != null) { filters.addFilter(new SingleColumnValueFilter(Bytes.toBytes(TPCHConstants.FAMILY_NAME), r .getQualifier(), r.getStartType(), r.getStartValue())); } if (r.getStopValue() != null) { filters.addFilter(new SingleColumnValueFilter(Bytes.toBytes(TPCHConstants.FAMILY_NAME), r .getQualifier(), r.getStopType(), r.getStopValue())); } } scan.setFilter(filters); scan.setAttribute(IndexConstants.SCAN_WITH_INDEX, Writables.getBytes(list)); scan.setAttribute(IndexConstants.MAX_SCAN_SCALE, Bytes.toBytes(0.3)); scan.setCacheBlocks(false); return table.getScanner(scan); }
@Override public ResultScanner getScanner() throws IOException { Scan scan = new Scan(); FilterList filters = new FilterList(); List<CF_INFO> cfs = PutTestConstants.getCFInfo(); for (CF_INFO ci : cfs) { if (ci.isIndex) { filters.addFilter(new SingleColumnValueFilter(Bytes.toBytes(FAMILY_NAME), Bytes .toBytes(ci.qualifier), CompareOp.GREATER_OR_EQUAL, LCCIndexConstant .parsingStringToBytesWithType(ci.type, String.valueOf(startValue)))); filters.addFilter(new SingleColumnValueFilter(Bytes.toBytes(FAMILY_NAME), Bytes .toBytes(ci.qualifier), CompareOp.LESS_OR_EQUAL, LCCIndexConstant .parsingStringToBytesWithType(ci.type, String.valueOf(stopValue)))); System.out.println("coffey hbase add filter for type: " + ci.type + " [" + startValue + "," + stopValue + "]"); } } scan.setCacheBlocks(false); scan.setFilter(filters); return table.getScanner(scan); }
private static Filter constructFilter(HBaseColumnSchema hbaseColumnSchema, CompareOp compareOp, byte[] value, boolean filterIfMissing) { Util.checkNull(hbaseColumnSchema); Util.checkNull(compareOp); Util.checkNull(value); byte[] familyBytes = hbaseColumnSchema.getFamilyBytes(); byte[] qualifierBytes = hbaseColumnSchema.getQualifierBytes(); SingleColumnValueFilter singleColumnValueFilter = new SingleColumnValueFilter( familyBytes, qualifierBytes, compareOp, value); singleColumnValueFilter.setFilterIfMissing(filterIfMissing); return singleColumnValueFilter; }
protected void addTimeConstraints(Scan scan) { List<Filter> filters = new ArrayList<>(); if(context.getQuery().getAfter() != null) { filters.add(new SingleColumnValueFilter(Schema.F_INFO, Schema.Q_EPOCH, CompareFilter.CompareOp.GREATER_OR_EQUAL, Bytes.toBytes(context.getQuery().getAfter()))); } if(context.getQuery().getBefore() != null) { filters.add(new SingleColumnValueFilter(Schema.F_INFO, Schema.Q_EPOCH, CompareFilter.CompareOp.LESS_OR_EQUAL, Bytes.toBytes(context.getQuery().getBefore()))); } if(!filters.isEmpty()) { FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL, filters); scan.setFilter(filterList); } }
/** * Gets filter based on the condition to be performed * @param condition To be applied server sided * @param passAll boolean if all elements have to pass the test * @param famNames to be checked * @param qualNames to be checked * @param params that could be needed * @return FilterList containing all filters needed */ public FilterList getFilter(XPath condition, boolean passAll, String []famNames, String []qualNames, String []params) { FilterList list = new FilterList((passAll)?FilterList.Operator.MUST_PASS_ALL: FilterList.Operator.MUST_PASS_ONE); for (int iCont = 0; iCont < famNames.length; iCont ++) { SingleColumnValueFilter filterTmp = new SingleColumnValueFilter( Bytes.toBytes(famNames[iCont]), Bytes.toBytes(qualNames[iCont]), CompareOp.EQUAL, Bytes.toBytes(params[iCont]) ); list.addFilter(filterTmp); } return list; }
/** * Create a filter that will emit all cells in a row if a given qualifier * has a given value. */ private RowFilter createEmitRowsWithValueFilter( FilterAdapterContext context, SingleColumnValueFilter filter) throws IOException { return RowFilter.newBuilder() .setCondition( Condition.newBuilder() .setPredicateFilter( RowFilter.newBuilder() .setChain( Chain.newBuilder() .addFilters(createColumnSpecFilter(filter)) .addFilters(createValueMatchFilter(context, filter)))) .setTrueFilter(ALL_VALUES_FILTER)) .build(); }
@Test public void latestVersionOnlyComparisonsAreDone() throws IOException { byte[] filterValue = Bytes.toBytes("foobar"); byte[] qualifier = Bytes.toBytes("someColumn"); byte[] family = Bytes.toBytes("f"); SingleColumnValueFilter filter = new SingleColumnValueFilter( family, qualifier, CompareFilter.CompareOp.EQUAL, new BinaryComparator(filterValue)); filter.setFilterIfMissing(false); filter.setLatestVersionOnly(true); RowFilter adaptedFilter = adapter.adapt( new FilterAdapterContext(new Scan()), filter); assertFilterIfNotMIssingMatches( family, qualifier, filterValue, 1 /* latest version only = true */, adaptedFilter); }
@Test public void allVersionComparisonAreDone() throws IOException { byte[] filterValue = Bytes.toBytes("foobar"); byte[] qualifier = Bytes.toBytes("someColumn"); byte[] family = Bytes.toBytes("f"); SingleColumnValueFilter filter = new SingleColumnValueFilter( family, qualifier, CompareFilter.CompareOp.EQUAL, new BinaryComparator(filterValue)); filter.setFilterIfMissing(false); filter.setLatestVersionOnly(false); RowFilter adaptedFilter = adapter.adapt( new FilterAdapterContext(new Scan()), filter); assertFilterIfNotMIssingMatches( family, qualifier, filterValue, Integer.MAX_VALUE /* latest version only = false */, adaptedFilter); }
private Filter handleFilterWithinAND(Filter filter) { if (filter instanceof FilterList) { FilterList fList = (FilterList) filter; if (fList.getOperator() == Operator.MUST_PASS_ONE) { return new FilterGroupingWorker().group(fList); } else { List<Filter> filters = fList.getFilters(); for (Filter subFilter : filters) { handleFilterWithinAND(subFilter); } } } else if (filter instanceof SingleColumnValueFilter) { handleScvf((SingleColumnValueFilter) filter); } // TODO when we expose SingleColumnRangeFilter to handle that also here. return null; }
Filter doFiltersRestruct(Filter filter) { if (filter instanceof SingleColumnValueFilter) { ValuePartition vp = null; if (filter instanceof SingleColumnValuePartitionFilter) { vp = ((SingleColumnValuePartitionFilter) filter).getValuePartition(); } SingleColumnValueFilter scvf = (SingleColumnValueFilter) filter; if (scvf.getOperator().equals(CompareOp.LESS) || scvf.getOperator().equals(CompareOp.LESS_OR_EQUAL) || scvf.getOperator().equals(CompareOp.GREATER) || scvf.getOperator().equals(CompareOp.GREATER_OR_EQUAL)) { return new SingleColumnRangeFilter(scvf.getFamily(), scvf.getQualifier(), vp, scvf .getComparator().getValue(), scvf.getOperator(), null, null); } } FilterGroupingWorker groupWorker = new FilterGroupingWorker(); return groupWorker.group(filter); }
FilterNode evalFilterForIndexSelection(Filter filter, List<IndexSpecification> indices) { if (filter instanceof FilterList) { FilterList fList = (FilterList) filter; GroupingCondition condition = (fList.getOperator() == Operator.MUST_PASS_ALL) ? GroupingCondition.AND : GroupingCondition.OR; NonLeafFilterNode nonLeafFilterNode = new NonLeafFilterNode(condition); List<Filter> filters = fList.getFilters(); for (Filter fltr : filters) { FilterNode node = evalFilterForIndexSelection(fltr, indices); nonLeafFilterNode.addFilterNode(node); } return handleNonLeafFilterNode(nonLeafFilterNode); } else if (filter instanceof SingleColumnValueFilter) { // Check for the availability of index return selectBestFitAndPossibleIndicesForSCVF(indices, (SingleColumnValueFilter) filter); } else if (filter instanceof SingleColumnRangeFilter) { return selectBestFitAndPossibleIndicesForSCRF(indices, (SingleColumnRangeFilter) filter); } return new NoIndexFilterNode(); }
private FilterNode selectBestFitAndPossibleIndicesForSCVF(List<IndexSpecification> indices, SingleColumnValueFilter filter) { if (CompareOp.NOT_EQUAL == filter.getOperator() || CompareOp.NO_OP == filter.getOperator()) { return new NoIndexFilterNode(); } FilterColumnValueDetail detail = null; if (filter instanceof SingleColumnValuePartitionFilter) { SingleColumnValuePartitionFilter escvf = (SingleColumnValuePartitionFilter) filter; detail = new FilterColumnValueDetail(escvf.getFamily(), escvf.getQualifier(), escvf .getComparator().getValue(), escvf.getValuePartition(), escvf.getOperator()); } else { detail = new FilterColumnValueDetail(filter.getFamily(), filter.getQualifier(), filter .getComparator().getValue(), filter.getOperator()); } return selectBestFitIndexForColumn(indices, detail); }
@Test public void testSingleIndexExpressionWithOneEqualsExpression() throws Exception { String indexName = "idx1"; SingleIndexExpression singleIndexExpression = new SingleIndexExpression(indexName); byte[] value = "1".getBytes(); Column column = new Column(FAMILY1, QUALIFIER1); EqualsExpression equalsExpression = new EqualsExpression(column, value); singleIndexExpression.addEqualsExpression(equalsExpression); Scan scan = new Scan(); scan.setAttribute(Constants.INDEX_EXPRESSION, IndexUtils.toBytes(singleIndexExpression)); Filter filter = new SingleColumnValueFilter(FAMILY1, QUALIFIER1, CompareOp.EQUAL, value); scan.setFilter(filter); ScanFilterEvaluator evaluator = new ScanFilterEvaluator(); List<IndexSpecification> indices = new ArrayList<IndexSpecification>(); IndexSpecification index = new IndexSpecification(indexName); HColumnDescriptor colDesc = new HColumnDescriptor(FAMILY1); index.addIndexColumn(colDesc, COL1, ValueType.String, 10); indices.add(index); HRegion region = initHRegion(tableName.getBytes(), null, null, "testSingleIndexExpressionWithOneEqualsExpression", TEST_UTIL.getConfiguration(), FAMILY1); IndexRegionScanner scanner = evaluator.evaluate(scan, indices, new byte[0], region, tableName); // TODO add assertions }
@Test(timeout = 180000) public void testScanWith4IdxAnd2ColumnsInFiltersShouldBeSuccessful() throws Exception { HTable table = put4ColumnIndex(); int i = 0; Scan s = new Scan(); FilterList filterList = new FilterList(); // check for combination of cat in q1 and dog in q1 Filter filter1 = new SingleColumnValueFilter("col1".getBytes(), "ql".getBytes(), CompareOp.EQUAL, "cat".getBytes()); Filter filter2 = new SingleColumnValueFilter("col2".getBytes(), "ql".getBytes(), CompareOp.EQUAL, "dog".getBytes()); filterList.addFilter(filter1); filterList.addFilter(filter2); s.setFilter(filterList); ResultScanner scanner = table.getScanner(s); for (Result result : scanner) { i++; } Assert.assertEquals( "Should match for 5 rows in multiple index with diff column family successfully ", 5, i); Assert.assertTrue("Seek points should be added ", IndexRegionObserver.getSeekpointAdded()); Assert.assertTrue("Indexed table should be used ", IndexRegionObserver.getIndexedFlowUsed()); }
protected void applyDefaultFilterList(Scan scan) { List<Filter> filters = new ArrayList<Filter>(); SingleColumnValueFilter filter1 = new SingleColumnValueFilter( ColumnFamilyNameBytes, QName1, CompareOp.GREATER, new BinaryComparator(Bytes.toBytes(1L))); filters.add(filter1); SingleColumnValueFilter filter2 = new SingleColumnValueFilter( ColumnFamilyNameBytes, QName2, CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes(32L))); filters.add(filter2); FilterList filterList = new FilterList(filters); scan.setFilter(filterList); }
/** * 初始化scan集合 * * @param job * @return */ private List<Scan> initScans(Job job) { // 时间戳+.... Configuration conf = job.getConfiguration(); // 获取运行时间: yyyy-MM-dd String date = conf.get(GlobalConstants.RUNNING_DATE_PARAMES); long startDate = TimeUtil.parseString2Long(date); long endDate = startDate + GlobalConstants.DAY_OF_MILLISECONDS; Scan scan = new Scan(); // 定义hbase扫描的开始rowkey和结束rowkey scan.setStartRow(Bytes.toBytes(Long.toString(startDate))); scan.setStopRow(Bytes.toBytes(Long.toString(endDate))); FilterList filterList = new FilterList(); // 过滤数据,只分析launch事件 filterList.addFilter(new SingleColumnValueFilter(Bytes.toBytes(EventLogConstants.EVENT_LOGS_FAMILY_NAME), Bytes.toBytes(EventLogConstants.LOG_COLUMN_NAME_EVENT_NAME), CompareOp.EQUAL, Bytes.toBytes(EventEnum.LAUNCH.alias))); // 定义mapper中需要获取的列名 String[] columns = new String[] { EventLogConstants.LOG_COLUMN_NAME_EVENT_NAME, EventLogConstants.LOG_COLUMN_NAME_UUID, EventLogConstants.LOG_COLUMN_NAME_SERVER_TIME, EventLogConstants.LOG_COLUMN_NAME_PLATFORM, EventLogConstants.LOG_COLUMN_NAME_BROWSER_NAME, EventLogConstants.LOG_COLUMN_NAME_BROWSER_VERSION }; // scan.addColumn(family, qualifier) filterList.addFilter(this.getColumnFilter(columns)); scan.setAttribute(Scan.SCAN_ATTRIBUTES_TABLE_NAME, Bytes.toBytes(EventLogConstants.HBASE_NAME_EVENT_LOGS)); scan.setFilter(filterList); return Lists.newArrayList(scan); }
/** * 多条件查询 * @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(); } }
@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); } }