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(); } }
/** * @param pbBytes A pb serialized {@link SingleColumnValueExcludeFilter} instance * @return An instance of {@link SingleColumnValueExcludeFilter} made from <code>bytes</code> * @throws DeserializationException * @see #toByteArray */ public static SingleColumnValueExcludeFilter parseFrom(final byte [] pbBytes) throws DeserializationException { FilterProtos.SingleColumnValueExcludeFilter proto; try { proto = FilterProtos.SingleColumnValueExcludeFilter.parseFrom(pbBytes); } catch (InvalidProtocolBufferException e) { throw new DeserializationException(e); } FilterProtos.SingleColumnValueFilter parentProto = proto.getSingleColumnValueFilter(); final CompareOp compareOp = CompareOp.valueOf(parentProto.getCompareOp().name()); final ByteArrayComparable comparator; try { comparator = ProtobufUtil.toComparator(parentProto.getComparator()); } catch (IOException ioe) { throw new DeserializationException(ioe); } return new SingleColumnValueExcludeFilter(parentProto.hasColumnFamily() ? parentProto .getColumnFamily().toByteArray() : null, parentProto.hasColumnQualifier() ? parentProto .getColumnQualifier().toByteArray() : null, compareOp, comparator, parentProto .getFilterIfMissing(), parentProto.getLatestVersionOnly()); }
/** * @param pbBytes A pb serialized {@link SingleColumnValueFilter} instance * @return An instance of {@link SingleColumnValueFilter} made from <code>bytes</code> * @throws org.apache.hadoop.hbase.exceptions.DeserializationException * @see #toByteArray */ public static SingleColumnValueFilter parseFrom(final byte [] pbBytes) throws DeserializationException { FilterProtos.SingleColumnValueFilter proto; try { proto = FilterProtos.SingleColumnValueFilter.parseFrom(pbBytes); } catch (InvalidProtocolBufferException e) { throw new DeserializationException(e); } final CompareOp compareOp = CompareOp.valueOf(proto.getCompareOp().name()); final ByteArrayComparable comparator; try { comparator = ProtobufUtil.toComparator(proto.getComparator()); } catch (IOException ioe) { throw new DeserializationException(ioe); } return new SingleColumnValueFilter(proto.hasColumnFamily() ? proto.getColumnFamily() .toByteArray() : null, proto.hasColumnQualifier() ? proto.getColumnQualifier() .toByteArray() : null, compareOp, comparator, proto.getFilterIfMissing(), proto .getLatestVersionOnly()); }
public static String printRange(Range r) { StringBuilder sb = new StringBuilder(); sb.append("[" + Bytes.toString(r.getFamily()) + ":" + Bytes.toString(r.getQualifier()) + "], values ("); if (r.getStartValue() != null) { sb.append(LCIndexConstant.getStringOfValueAndType(r.getDataType(), r.getStartValue())); if (r.getStartType() == CompareOp.EQUAL || r.getStartType() == CompareOp.NOT_EQUAL) { sb.append(" <== ").append(r.getStartType()).append(" )"); return sb.toString(); } } else { sb.append("null"); } sb.append(", "); if (r.getStopValue() != null) { sb.append(LCIndexConstant.getStringOfValueAndType(r.getDataType(), r.getStopValue())); } else { sb.append("MAX"); } sb.append(")"); return sb.toString(); }
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; }
@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); } }
@Test public void testJira6912() throws Exception { TableName TABLE = TableName.valueOf("testJira6912"); Table foo = TEST_UTIL.createTable(TABLE, new byte[][] {FAMILY}, 10); List<Put> puts = new ArrayList<Put>(); for (int i=0;i !=100; i++){ Put put = new Put(Bytes.toBytes(i)); put.add(FAMILY, FAMILY, Bytes.toBytes(i)); puts.add(put); } foo.put(puts); // If i comment this out it works TEST_UTIL.flush(); Scan scan = new Scan(); scan.setStartRow(Bytes.toBytes(1)); scan.setStopRow(Bytes.toBytes(3)); scan.addColumn(FAMILY, FAMILY); scan.setFilter(new RowFilter(CompareFilter.CompareOp.NOT_EQUAL, new BinaryComparator(Bytes.toBytes(1)))); ResultScanner scanner = foo.getScanner(scan); Result[] bar = scanner.next(100); assertEquals(1, bar.length); }
@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); } }
@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); } }
@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); }
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); }
/** * Tests the the {@link WhileMatchFilter} works in combination with a * {@link Filter} that uses the * {@link Filter#filterKeyValue(org.apache.hadoop.hbase.KeyValue)} method. * * See HBASE-2258. * * @throws Exception */ @Test public void testWhileMatchFilterWithFilterKeyValue() throws Exception { Scan s = new Scan(); WhileMatchFilter filter = new WhileMatchFilter( new SingleColumnValueFilter(FAMILIES[0], QUALIFIERS_ONE[0], CompareOp.EQUAL, Bytes.toBytes("foo")) ); s.setFilter(filter); InternalScanner scanner = this.region.getScanner(s); while (true) { ArrayList<Cell> values = new ArrayList<Cell>(); boolean isMoreResults = scanner.next(values); assertTrue("The WhileMatchFilter should now filter all remaining", filter.filterAllRemaining()); if (!isMoreResults) { break; } } }
@Test public void testSingleColumnValueExcludeFilter() throws Exception { // null family/column SingleColumnValueExcludeFilter SingleColumnValueExcludeFilter singleColumnValueExcludeFilter = new SingleColumnValueExcludeFilter(null, null, CompareFilter.CompareOp.GREATER_OR_EQUAL, Bytes.toBytes("value")); assertTrue(singleColumnValueExcludeFilter.areSerializedFieldsEqual( ProtobufUtil.toFilter(ProtobufUtil.toFilter(singleColumnValueExcludeFilter)))); // non-null family/column SingleColumnValueFilter singleColumnValueExcludeFilter = new SingleColumnValueExcludeFilter(Bytes.toBytes("fam"), Bytes.toBytes("qual"), CompareFilter.CompareOp.LESS_OR_EQUAL, new NullComparator(), false, false); assertTrue(singleColumnValueExcludeFilter.areSerializedFieldsEqual( ProtobufUtil.toFilter(ProtobufUtil.toFilter(singleColumnValueExcludeFilter)))); }
@Override public void readFields(DataInput in) throws IOException { column = Bytes.readByteArray(in); dataType = WritableUtils.readEnum(in, DataType.class); if (in.readBoolean()) { startType = WritableUtils.readEnum(in, CompareOp.class); startValue = Bytes.readByteArray(in); } else { startType = CompareOp.NO_OP; startValue = null; } if (in.readBoolean()) { stopType = WritableUtils.readEnum(in, CompareOp.class); stopValue = Bytes.readByteArray(in); } else { stopType = CompareOp.NO_OP; stopValue = null; } }
public HbaseServiceConditonModel(String family, String col, String value, CompareOp op) { super(); this.family = family; this.col = col; this.value = value; this.op = op; }
/** * 初始化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); }
private void parseQuery() throws Exception { //TODO uncomment this in new version // if(columnInfo==null ||columnInfo.size()==0){ // throw new IllegalArgumentException("Column Info is not set! Please specify column info before query!"); // } //optimize the query condition if (queryCondition != null) { // ranges = scanOptimize.optimizeQuery(queryCondition, columnInfoMap); } else { ranges = new Range[1][1]; ranges[0][0] = new Range(IndexConstants.KEY, this.getTableName().getName()); ranges[0][0].setStartType(CompareOp.GREATER_OR_EQUAL); if (this.startKey != null) { ranges[0][0].setStartValue(this.startKey); } else { ranges[0][0].setStartValue(HConstants.EMPTY_BYTE_ARRAY); } if (this.endKey != null) { ranges[0][0].setEndType(CompareOp.LESS_OR_EQUAL); ranges[0][0].setEndValue(endKey); } } // ranges = new Range[range.length - 1][]; // // // set base columns of every query range // for (int i = 0; i < range.length - 1; i++) { // ranges[i] = new Range[range[i].length]; // for (int j = 0; j < range[i].length; j++) { // ranges[i][j] = range[i][j]; // } // } }
@Override public boolean preCheckAndPut(final ObserverContext<RegionCoprocessorEnvironment> e, final byte [] row, final byte [] family, final byte [] qualifier, final CompareOp compareOp, final ByteArrayComparable comparator, final Put put, final boolean result) throws IOException { return result; }
@Override public boolean preCheckAndPutAfterRowLock( final ObserverContext<RegionCoprocessorEnvironment> e, final byte[] row, final byte[] family, final byte[] qualifier, final CompareOp compareOp, final ByteArrayComparable comparator, final Put put, final boolean result) throws IOException { return result; }
@Override public boolean postCheckAndPut(final ObserverContext<RegionCoprocessorEnvironment> e, final byte [] row, final byte [] family, final byte [] qualifier, final CompareOp compareOp, final ByteArrayComparable comparator, final Put put, final boolean result) throws IOException { return result; }
@Override public boolean preCheckAndDeleteAfterRowLock( final ObserverContext<RegionCoprocessorEnvironment> e, final byte[] row, final byte[] family, final byte[] qualifier, final CompareOp compareOp, final ByteArrayComparable comparator, final Delete delete, final boolean result) throws IOException { return result; }
@Override public boolean postCheckAndDelete(final ObserverContext<RegionCoprocessorEnvironment> e, final byte [] row, final byte [] family, final byte [] qualifier, final CompareOp compareOp, final ByteArrayComparable comparator, final Delete delete, final boolean result) throws IOException { return result; }
/** * {@inheritDoc} */ @Override public boolean checkAndPut(final byte [] row, final byte [] family, final byte [] qualifier, final CompareOp compareOp, final byte [] value, final Put put) throws IOException { RegionServerCallable<Boolean> callable = new RegionServerCallable<Boolean>(connection, getName(), row) { @Override public Boolean call(int callTimeout) throws IOException { PayloadCarryingRpcController controller = new PayloadCarryingRpcController(); controller.setPriority(tableName); controller.setCallTimeout(callTimeout); try { CompareType compareType = CompareType.valueOf(compareOp.name()); MutateRequest request = RequestConverter.buildMutateRequest( getLocation().getRegionInfo().getRegionName(), row, family, qualifier, new BinaryComparator(value), compareType, put); MutateResponse response = getStub().mutate(controller, request); return Boolean.valueOf(response.getProcessed()); } catch (ServiceException se) { throw ProtobufUtil.getRemoteException(se); } } }; return rpcCallerFactory.<Boolean> newCaller().callWithRetries(callable, this.operationTimeout); }
private ScanRange selectTheBestRange(IndexTableRelation indexTableRelation, Map<byte[], Store> storeMap, ScanRange.ScanRangeList rangeList, String scanId) throws IOException { if (rangeList == null || rangeList.getRanges().size() == 0) return null; List<ScanRange> acceptableList = new ArrayList<>(); for (ScanRange r : rangeList.getRanges()) { if (r.getStartOp() == CompareFilter.CompareOp.NOT_EQUAL || r.getStopOp() == CompareFilter.CompareOp.NOT_EQUAL) continue; if (r.getStartOp() == CompareFilter.CompareOp.NO_OP && r.getStopOp() == CompareFilter.CompareOp.NO_OP) continue; acceptableList.add(r); } if (acceptableList.size() == 0) { System.out.println("no acceptable range for scan " + scanId); return null; } long start = System.currentTimeMillis(); Pair<ScanRange, Long> best = null; // only 1 index family is supported now, thus the real selecting method is HStore.selectTheBestRange for (Map.Entry<byte[], TreeSet<byte[]>> entry : indexTableRelation.getIndexFamilyMap() .entrySet()) { Store store = storeMap.get(entry.getKey()); Pair<ScanRange, Long> bestInStore = ((HStore) store).selectTheBestRange(acceptableList); if (best == null || best.getSecond() > bestInStore.getSecond()) best = bestInStore; } String bestDesc = best == null ? "null" : (best.toString() + " with " + best.getSecond()); System.out.println("cost " + (System.currentTimeMillis() - start) / 1000.0 + " seconds for the best column for scan " + scanId + " , it is: " + bestDesc); if (best == null) return null; else return best.getFirst(); }
/** * @param row row to check * @param family column family * @param qualifier column qualifier * @param compareOp the comparison operation * @param comparator the comparator * @param put data to put if check succeeds * @return true or false to return to client if default processing should * be bypassed, or null otherwise * @throws IOException e */ public Boolean preCheckAndPut(final byte [] row, final byte [] family, final byte [] qualifier, final CompareOp compareOp, final ByteArrayComparable comparator, final Put put) throws IOException { return execOperationWithResult(true, false, coprocessors.isEmpty() ? null : new RegionOperationWithResult<Boolean>() { @Override public void call(RegionObserver oserver, ObserverContext<RegionCoprocessorEnvironment> ctx) throws IOException { setResult(oserver.preCheckAndPut(ctx, row, family, qualifier, compareOp, comparator, put, getResult())); } }); }
/** * @param row row to check * @param family column family * @param qualifier column qualifier * @param compareOp the comparison operation * @param comparator the comparator * @param put data to put if check succeeds * @return true or false to return to client if default processing should * be bypassed, or null otherwise * @throws IOException e */ public Boolean preCheckAndPutAfterRowLock(final byte[] row, final byte[] family, final byte[] qualifier, final CompareOp compareOp, final ByteArrayComparable comparator, final Put put) throws IOException { return execOperationWithResult(true, false, coprocessors.isEmpty() ? null : new RegionOperationWithResult<Boolean>() { @Override public void call(RegionObserver oserver, ObserverContext<RegionCoprocessorEnvironment> ctx) throws IOException { setResult(oserver.preCheckAndPutAfterRowLock(ctx, row, family, qualifier, compareOp, comparator, put, getResult())); } }); }
/** * @param row row to check * @param family column family * @param qualifier column qualifier * @param compareOp the comparison operation * @param comparator the comparator * @param put data to put if check succeeds * @throws IOException e */ public boolean postCheckAndPut(final byte [] row, final byte [] family, final byte [] qualifier, final CompareOp compareOp, final ByteArrayComparable comparator, final Put put, boolean result) throws IOException { return execOperationWithResult(result, coprocessors.isEmpty() ? null : new RegionOperationWithResult<Boolean>() { @Override public void call(RegionObserver oserver, ObserverContext<RegionCoprocessorEnvironment> ctx) throws IOException { setResult(oserver.postCheckAndPut(ctx, row, family, qualifier, compareOp, comparator, put, getResult())); } }); }
/** * @param row row to check * @param family column family * @param qualifier column qualifier * @param compareOp the comparison operation * @param comparator the comparator * @param delete delete to commit if check succeeds * @return true or false to return to client if default processing should * be bypassed, or null otherwise * @throws IOException e */ public Boolean preCheckAndDelete(final byte [] row, final byte [] family, final byte [] qualifier, final CompareOp compareOp, final ByteArrayComparable comparator, final Delete delete) throws IOException { return execOperationWithResult(true, false, coprocessors.isEmpty() ? null : new RegionOperationWithResult<Boolean>() { @Override public void call(RegionObserver oserver, ObserverContext<RegionCoprocessorEnvironment> ctx) throws IOException { setResult(oserver.preCheckAndDelete(ctx, row, family, qualifier, compareOp, comparator, delete, getResult())); } }); }
/** * @param row row to check * @param family column family * @param qualifier column qualifier * @param compareOp the comparison operation * @param comparator the comparator * @param delete delete to commit if check succeeds * @return true or false to return to client if default processing should * be bypassed, or null otherwise * @throws IOException e */ public Boolean preCheckAndDeleteAfterRowLock(final byte[] row, final byte[] family, final byte[] qualifier, final CompareOp compareOp, final ByteArrayComparable comparator, final Delete delete) throws IOException { return execOperationWithResult(true, false, coprocessors.isEmpty() ? null : new RegionOperationWithResult<Boolean>() { @Override public void call(RegionObserver oserver, ObserverContext<RegionCoprocessorEnvironment> ctx) throws IOException { setResult(oserver.preCheckAndDeleteAfterRowLock(ctx, row, family, qualifier, compareOp, comparator, delete, getResult())); } }); }
/** * @param row row to check * @param family column family * @param qualifier column qualifier * @param compareOp the comparison operation * @param comparator the comparator * @param delete delete to commit if check succeeds * @throws IOException e */ public boolean postCheckAndDelete(final byte [] row, final byte [] family, final byte [] qualifier, final CompareOp compareOp, final ByteArrayComparable comparator, final Delete delete, boolean result) throws IOException { return execOperationWithResult(result, coprocessors.isEmpty() ? null : new RegionOperationWithResult<Boolean>() { @Override public void call(RegionObserver oserver, ObserverContext<RegionCoprocessorEnvironment> ctx) throws IOException { setResult(oserver.postCheckAndDelete(ctx, row, family, qualifier, compareOp, comparator, delete, getResult())); } }); }
@Override void testRow(final int i) throws IOException { byte [] bytes = format(i); // Put a known value so when we go to check it, it is there. Put put = new Put(bytes); put.addColumn(FAMILY_NAME, getQualifier(), bytes); this.table.put(put); RowMutations mutations = new RowMutations(bytes); mutations.add(put); this.table.checkAndMutate(bytes, FAMILY_NAME, getQualifier(), CompareOp.EQUAL, bytes, mutations); }
@Override void testRow(final int i) throws IOException { byte [] bytes = format(i); // Put a known value so when we go to check it, it is there. Put put = new Put(bytes); put.addColumn(FAMILY_NAME, getQualifier(), bytes); this.table.put(put); this.table.checkAndPut(bytes, FAMILY_NAME, getQualifier(), CompareOp.EQUAL, bytes, put); }
@Override void testRow(final int i) throws IOException { byte [] bytes = format(i); // Put a known value so when we go to check it, it is there. Put put = new Put(bytes); put.addColumn(FAMILY_NAME, getQualifier(), bytes); this.table.put(put); Delete delete = new Delete(put.getRow()); delete.addColumn(FAMILY_NAME, getQualifier()); this.table.checkAndDelete(bytes, FAMILY_NAME, getQualifier(), CompareOp.EQUAL, bytes, delete); }
protected void initialize(JobConf job, String table) throws IOException { Connection connection = ConnectionFactory.createConnection(HBaseConfiguration.create(job)); TableName tableName = TableName.valueOf(table); // mandatory initializeTable(connection, tableName); 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); }
/** * Set start type for start value. * * @param startType - can only be EQUAL, GREATER or GREATER_OR_EQUAL */ public void setStartType(CompareOp startType) { if (startType == CompareOp.EQUAL || startType == CompareOp.GREATER || startType == CompareOp.GREATER_OR_EQUAL) { this.startType = startType; } else { this.startType = startType; // throw new IllegalArgumentException("Illegal start type: " // + startType.toString()); } }
@Test public void testFilters() throws Exception { byte [] TABLE = Bytes.toBytes("testFilters"); Table ht = TEST_UTIL.createTable(TABLE, FAMILY); byte [][] ROWS = makeN(ROW, 10); byte [][] QUALIFIERS = { Bytes.toBytes("col0-<d2v1>-<d3v2>"), Bytes.toBytes("col1-<d2v1>-<d3v2>"), Bytes.toBytes("col2-<d2v1>-<d3v2>"), Bytes.toBytes("col3-<d2v1>-<d3v2>"), Bytes.toBytes("col4-<d2v1>-<d3v2>"), Bytes.toBytes("col5-<d2v1>-<d3v2>"), Bytes.toBytes("col6-<d2v1>-<d3v2>"), Bytes.toBytes("col7-<d2v1>-<d3v2>"), Bytes.toBytes("col8-<d2v1>-<d3v2>"), Bytes.toBytes("col9-<d2v1>-<d3v2>") }; for(int i=0;i<10;i++) { Put put = new Put(ROWS[i]); put.setDurability(Durability.SKIP_WAL); put.add(FAMILY, QUALIFIERS[i], VALUE); ht.put(put); } Scan scan = new Scan(); scan.addFamily(FAMILY); Filter filter = new QualifierFilter(CompareOp.EQUAL, new RegexStringComparator("col[1-5]")); scan.setFilter(filter); ResultScanner scanner = ht.getScanner(scan); int expectedIndex = 1; for(Result result : ht.getScanner(scan)) { assertEquals(result.size(), 1); assertTrue(Bytes.equals(CellUtil.cloneRow(result.rawCells()[0]), ROWS[expectedIndex])); assertTrue(Bytes.equals(CellUtil.cloneQualifier(result.rawCells()[0]), QUALIFIERS[expectedIndex])); expectedIndex++; } assertEquals(expectedIndex, 6); scanner.close(); }
@Test public void testFilterWithLongCompartor() throws Exception { byte [] TABLE = Bytes.toBytes("testFilterWithLongCompartor"); Table ht = TEST_UTIL.createTable(TABLE, FAMILY); byte [][] ROWS = makeN(ROW, 10); byte [][] values = new byte[10][]; for (int i = 0; i < 10; i ++) { values[i] = Bytes.toBytes(100L * i); } for(int i = 0; i < 10; i ++) { Put put = new Put(ROWS[i]); put.setDurability(Durability.SKIP_WAL); put.add(FAMILY, QUALIFIER, values[i]); ht.put(put); } Scan scan = new Scan(); scan.addFamily(FAMILY); Filter filter = new SingleColumnValueFilter(FAMILY, QUALIFIER, CompareOp.GREATER, new LongComparator(500)); scan.setFilter(filter); ResultScanner scanner = ht.getScanner(scan); int expectedIndex = 0; for(Result result : ht.getScanner(scan)) { assertEquals(result.size(), 1); assertTrue(Bytes.toLong(result.getValue(FAMILY, QUALIFIER)) > 500); expectedIndex++; } assertEquals(expectedIndex, 4); scanner.close(); }
@Override public boolean preCheckAndPut(ObserverContext<RegionCoprocessorEnvironment> e, byte[] row, byte[] family, byte[] qualifier, CompareOp compareOp, ByteArrayComparable comparator, Put put, boolean result) throws IOException { ctPreCheckAndPut.incrementAndGet(); return true; }
@Override public boolean preCheckAndPutAfterRowLock(ObserverContext<RegionCoprocessorEnvironment> e, byte[] row, byte[] family, byte[] qualifier, CompareOp compareOp, ByteArrayComparable comparator, Put put, boolean result) throws IOException { ctPreCheckAndPutAfterRowLock.incrementAndGet(); return true; }
@Override public boolean postCheckAndPut(ObserverContext<RegionCoprocessorEnvironment> e, byte[] row, byte[] family, byte[] qualifier, CompareOp compareOp, ByteArrayComparable comparator, Put put, boolean result) throws IOException { ctPostCheckAndPut.incrementAndGet(); return true; }