@Override public List<TResult> getScannerResults(ByteBuffer table, TScan scan, int numRows) throws TIOError, TException { Table htable = getTable(table); List<TResult> results = null; ResultScanner scanner = null; try { scanner = htable.getScanner(scanFromThrift(scan)); results = resultsFromHBase(scanner.next(numRows)); } catch (IOException e) { throw getTIOError(e); } finally { if (scanner != null) { scanner.close(); } closeTable(htable); } return results; }
@Override public List<TResult> getScannerResults(ByteBuffer table, TScan scan, int numRows) throws TIOError, TException { HTableInterface htable = getTable(table); List<TResult> results = null; ResultScanner scanner = null; try { scanner = htable.getScanner(scanFromThrift(scan)); results = resultsFromHBase(scanner.next(numRows)); } catch (IOException e) { throw getTIOError(e); } finally { if (scanner != null) { scanner.close(); } closeTable(htable); } return results; }
@Test public void testScanWithReadOnly() throws Exception { ThriftHBaseServiceHandler handler = createHandler(); ByteBuffer table = wrap(tableAname); TScan scan = new TScan(); boolean exceptionCaught = false; try { int scanId = handler.openScanner(table, scan); handler.getScannerRows(scanId, 10); handler.closeScanner(scanId); } catch (TIOError e) { exceptionCaught = true; } finally { assertFalse(exceptionCaught); } }
@Override public int openScanner(ByteBuffer table, TScan scan) throws TIOError, TException { Table htable = getTable(table); ResultScanner resultScanner = null; try { resultScanner = htable.getScanner(scanFromThrift(scan)); } catch (IOException e) { throw getTIOError(e); } finally { closeTable(htable); } return addScanner(resultScanner); }
@Override public int openScanner(ByteBuffer table, TScan scan) throws TIOError, TException { HTableInterface htable = getTable(table); ResultScanner resultScanner = null; try { resultScanner = htable.getScanner(scanFromThrift(scan)); } catch (IOException e) { throw getTIOError(e); } finally { closeTable(htable); } return addScanner(resultScanner); }
@Override public int openScanner(ByteBuffer table, TScan scan) throws TIOError, TException { HTableInterface htable = getTable(table.array()); ResultScanner resultScanner = null; try { resultScanner = htable.getScanner(scanFromThrift(scan)); } catch (IOException e) { throw getTIOError(e); } finally { closeTable(htable); } return addScanner(resultScanner); }
public static Scan scanFromThrift(TScan in) throws IOException { Scan out = new Scan(); if (in.isSetStartRow()) out.setStartRow(in.getStartRow()); if (in.isSetStopRow()) out.setStopRow(in.getStopRow()); if (in.isSetCaching()) out.setCaching(in.getCaching()); if (in.isSetMaxVersions()) { out.setMaxVersions(in.getMaxVersions()); } if (in.isSetColumns()) { for (TColumn column : in.getColumns()) { if (column.isSetQualifier()) { out.addColumn(column.getFamily(), column.getQualifier()); } else { out.addFamily(column.getFamily()); } } } TTimeRange timeRange = in.getTimeRange(); if (timeRange != null && timeRange.isSetMinStamp() && timeRange.isSetMaxStamp()) { out.setTimeRange(timeRange.getMinStamp(), timeRange.getMaxStamp()); } if (in.isSetBatchSize()) { out.setBatch(in.getBatchSize()); } if (in.isSetFilterString()) { ParseFilter parseFilter = new ParseFilter(); out.setFilter(parseFilter.parseFilterString(in.getFilterString())); } if (in.isSetAttributes()) { addAttributes(out,in.getAttributes()); } if (in.isSetAuthorizations()) { out.setAuthorizations(new Authorizations(in.getAuthorizations().getLabels())); } if (in.isSetReversed()) { out.setReversed(in.isReversed()); } return out; }
@Test public void testScanWithVisibilityLabels() throws Exception { ThriftHBaseServiceHandler handler = createHandler(); ByteBuffer table = wrap(tableAname); // insert data TColumnValue columnValue = new TColumnValue(wrap(familyAname), wrap(qualifierAname), wrap(valueAname)); List<TColumnValue> columnValues = new ArrayList<TColumnValue>(); columnValues.add(columnValue); for (int i = 0; i < 10; i++) { TPut put = new TPut(wrap(("testScan" + i).getBytes()), columnValues); if (i == 5) { put.setCellVisibility(new TCellVisibility().setExpression(PUBLIC)); } else { put.setCellVisibility(new TCellVisibility().setExpression("(" + SECRET + "|" + CONFIDENTIAL + ")" + "&" + "!" + TOPSECRET)); } handler.put(table, put); } // create scan instance TScan scan = new TScan(); List<TColumn> columns = new ArrayList<TColumn>(); TColumn column = new TColumn(); column.setFamily(familyAname); column.setQualifier(qualifierAname); columns.add(column); scan.setColumns(columns); scan.setStartRow("testScan".getBytes()); scan.setStopRow("testScan\uffff".getBytes()); TAuthorization tauth = new TAuthorization(); List<String> labels = new ArrayList<String>(); labels.add(SECRET); labels.add(PRIVATE); tauth.setLabels(labels); scan.setAuthorizations(tauth); // get scanner and rows int scanId = handler.openScanner(table, scan); List<TResult> results = handler.getScannerRows(scanId, 10); assertEquals(9, results.size()); Assert.assertFalse(Bytes.equals(results.get(5).getRow(), ("testScan" + 5).getBytes())); for (int i = 0; i < 9; i++) { if (i < 5) { assertArrayEquals(("testScan" + i).getBytes(), results.get(i).getRow()); } else if (i == 5) { continue; } else { assertArrayEquals(("testScan" + (i + 1)).getBytes(), results.get(i) .getRow()); } } // check that we are at the end of the scan results = handler.getScannerRows(scanId, 9); assertEquals(0, results.size()); // close scanner and check that it was indeed closed handler.closeScanner(scanId); try { handler.getScannerRows(scanId, 9); fail("Scanner id should be invalid"); } catch (TIllegalArgument e) { } }
@Test public void testGetScannerResultsWithAuthorizations() throws Exception { ThriftHBaseServiceHandler handler = createHandler(); ByteBuffer table = wrap(tableAname); // insert data TColumnValue columnValue = new TColumnValue(wrap(familyAname), wrap(qualifierAname), wrap(valueAname)); List<TColumnValue> columnValues = new ArrayList<TColumnValue>(); columnValues.add(columnValue); for (int i = 0; i < 20; i++) { TPut put = new TPut( wrap(("testGetScannerResults" + pad(i, (byte) 2)).getBytes()), columnValues); if (i == 3) { put.setCellVisibility(new TCellVisibility().setExpression(PUBLIC)); } else { put.setCellVisibility(new TCellVisibility().setExpression("(" + SECRET + "|" + CONFIDENTIAL + ")" + "&" + "!" + TOPSECRET)); } handler.put(table, put); } // create scan instance TScan scan = new TScan(); List<TColumn> columns = new ArrayList<TColumn>(); TColumn column = new TColumn(); column.setFamily(familyAname); column.setQualifier(qualifierAname); columns.add(column); scan.setColumns(columns); scan.setStartRow("testGetScannerResults".getBytes()); // get 5 rows and check the returned results scan.setStopRow("testGetScannerResults05".getBytes()); TAuthorization tauth = new TAuthorization(); List<String> labels = new ArrayList<String>(); labels.add(SECRET); labels.add(PRIVATE); tauth.setLabels(labels); scan.setAuthorizations(tauth); List<TResult> results = handler.getScannerResults(table, scan, 5); assertEquals(4, results.size()); for (int i = 0; i < 4; i++) { if (i < 3) { assertArrayEquals( ("testGetScannerResults" + pad(i, (byte) 2)).getBytes(), results.get(i).getRow()); } else if (i == 3) { continue; } else { assertArrayEquals( ("testGetScannerResults" + pad(i + 1, (byte) 2)).getBytes(), results .get(i).getRow()); } } }
@Test public void testScan() throws Exception { ThriftHBaseServiceHandler handler = createHandler(); ByteBuffer table = wrap(tableAname); // insert data TColumnValue columnValue = new TColumnValue(wrap(familyAname), wrap(qualifierAname), wrap(valueAname)); List<TColumnValue> columnValues = new ArrayList<TColumnValue>(); columnValues.add(columnValue); for (int i = 0; i < 10; i++) { TPut put = new TPut(wrap(("testScan" + i).getBytes()), columnValues); handler.put(table, put); } // create scan instance TScan scan = new TScan(); List<TColumn> columns = new ArrayList<TColumn>(); TColumn column = new TColumn(); column.setFamily(familyAname); column.setQualifier(qualifierAname); columns.add(column); scan.setColumns(columns); scan.setStartRow("testScan".getBytes()); scan.setStopRow("testScan\uffff".getBytes()); // get scanner and rows int scanId = handler.openScanner(table, scan); List<TResult> results = handler.getScannerRows(scanId, 10); assertEquals(10, results.size()); for (int i = 0; i < 10; i++) { // check if the rows are returned and in order assertArrayEquals(("testScan" + i).getBytes(), results.get(i).getRow()); } // check that we are at the end of the scan results = handler.getScannerRows(scanId, 10); assertEquals(0, results.size()); // close scanner and check that it was indeed closed handler.closeScanner(scanId); try { handler.getScannerRows(scanId, 10); fail("Scanner id should be invalid"); } catch (TIllegalArgument e) { } }
@Test public void testReverseScan() throws Exception { ThriftHBaseServiceHandler handler = createHandler(); ByteBuffer table = wrap(tableAname); // insert data TColumnValue columnValue = new TColumnValue(wrap(familyAname), wrap(qualifierAname), wrap(valueAname)); List<TColumnValue> columnValues = new ArrayList<TColumnValue>(); columnValues.add(columnValue); for (int i = 0; i < 10; i++) { TPut put = new TPut(wrap(("testReverseScan" + i).getBytes()), columnValues); handler.put(table, put); } // create reverse scan instance TScan scan = new TScan(); scan.setReversed(true); List<TColumn> columns = new ArrayList<TColumn>(); TColumn column = new TColumn(); column.setFamily(familyAname); column.setQualifier(qualifierAname); columns.add(column); scan.setColumns(columns); scan.setStartRow("testReverseScan\uffff".getBytes()); scan.setStopRow("testReverseScan".getBytes()); // get scanner and rows int scanId = handler.openScanner(table, scan); List<TResult> results = handler.getScannerRows(scanId, 10); assertEquals(10, results.size()); for (int i = 0; i < 10; i++) { // check if the rows are returned and in order assertArrayEquals(("testReverseScan" + (9 - i)).getBytes(), results.get(i).getRow()); } // check that we are at the end of the scan results = handler.getScannerRows(scanId, 10); assertEquals(0, results.size()); // close scanner and check that it was indeed closed handler.closeScanner(scanId); try { handler.getScannerRows(scanId, 10); fail("Scanner id should be invalid"); } catch (TIllegalArgument e) { } }
@Test public void testScanWithFilter() throws Exception { ThriftHBaseServiceHandler handler = createHandler(); ByteBuffer table = wrap(tableAname); // insert data TColumnValue columnValue = new TColumnValue(wrap(familyAname), wrap(qualifierAname), wrap(valueAname)); List<TColumnValue> columnValues = new ArrayList<TColumnValue>(); columnValues.add(columnValue); for (int i = 0; i < 10; i++) { TPut put = new TPut(wrap(("testScanWithFilter" + i).getBytes()), columnValues); handler.put(table, put); } // create scan instance with filter TScan scan = new TScan(); List<TColumn> columns = new ArrayList<TColumn>(); TColumn column = new TColumn(); column.setFamily(familyAname); column.setQualifier(qualifierAname); columns.add(column); scan.setColumns(columns); scan.setStartRow("testScanWithFilter".getBytes()); scan.setStopRow("testScanWithFilter\uffff".getBytes()); // only get the key part scan.setFilterString(wrap(("KeyOnlyFilter()").getBytes())); // get scanner and rows int scanId = handler.openScanner(table, scan); List<TResult> results = handler.getScannerRows(scanId, 10); assertEquals(10, results.size()); for (int i = 0; i < 10; i++) { // check if the rows are returned and in order assertArrayEquals(("testScanWithFilter" + i).getBytes(), results.get(i).getRow()); // check that the value is indeed stripped by the filter assertEquals(0, results.get(i).getColumnValues().get(0).getValue().length); } // check that we are at the end of the scan results = handler.getScannerRows(scanId, 10); assertEquals(0, results.size()); // close scanner and check that it was indeed closed handler.closeScanner(scanId); try { handler.getScannerRows(scanId, 10); fail("Scanner id should be invalid"); } catch (TIllegalArgument e) { } }
@Test public void testGetScannerResults() throws Exception { ThriftHBaseServiceHandler handler = createHandler(); ByteBuffer table = wrap(tableAname); // insert data TColumnValue columnValue = new TColumnValue(wrap(familyAname), wrap(qualifierAname), wrap(valueAname)); List<TColumnValue> columnValues = new ArrayList<TColumnValue>(); columnValues.add(columnValue); for (int i = 0; i < 20; i++) { TPut put = new TPut(wrap(("testGetScannerResults" + pad(i, (byte) 2)).getBytes()), columnValues); handler.put(table, put); } // create scan instance TScan scan = new TScan(); List<TColumn> columns = new ArrayList<TColumn>(); TColumn column = new TColumn(); column.setFamily(familyAname); column.setQualifier(qualifierAname); columns.add(column); scan.setColumns(columns); scan.setStartRow("testGetScannerResults".getBytes()); // get 5 rows and check the returned results scan.setStopRow("testGetScannerResults05".getBytes()); List<TResult> results = handler.getScannerResults(table, scan, 5); assertEquals(5, results.size()); for (int i = 0; i < 5; i++) { // check if the rows are returned and in order assertArrayEquals(("testGetScannerResults" + pad(i, (byte) 2)).getBytes(), results.get(i) .getRow()); } // get 10 rows and check the returned results scan.setStopRow("testGetScannerResults10".getBytes()); results = handler.getScannerResults(table, scan, 10); assertEquals(10, results.size()); for (int i = 0; i < 10; i++) { // check if the rows are returned and in order assertArrayEquals(("testGetScannerResults" + pad(i, (byte) 2)).getBytes(), results.get(i) .getRow()); } // get 20 rows and check the returned results scan.setStopRow("testGetScannerResults20".getBytes()); results = handler.getScannerResults(table, scan, 20); assertEquals(20, results.size()); for (int i = 0; i < 20; i++) { // check if the rows are returned and in order assertArrayEquals(("testGetScannerResults" + pad(i, (byte) 2)).getBytes(), results.get(i) .getRow()); } }
public static Scan scanFromThrift(TScan in) throws IOException { Scan out = new Scan(); if (in.isSetStartRow()) out.setStartRow(in.getStartRow()); if (in.isSetStopRow()) out.setStopRow(in.getStopRow()); if (in.isSetCaching()) out.setCaching(in.getCaching()); if (in.isSetMaxVersions()) { out.setMaxVersions(in.getMaxVersions()); } if (in.isSetColumns()) { for (TColumn column : in.getColumns()) { if (column.isSetQualifier()) { out.addColumn(column.getFamily(), column.getQualifier()); } else { out.addFamily(column.getFamily()); } } } TTimeRange timeRange = in.getTimeRange(); if (timeRange != null && timeRange.isSetMinStamp() && timeRange.isSetMaxStamp()) { out.setTimeRange(timeRange.getMinStamp(), timeRange.getMaxStamp()); } if (in.isSetBatchSize()) { out.setBatch(in.getBatchSize()); } if (in.isSetFilterString()) { ParseFilter parseFilter = new ParseFilter(); out.setFilter(parseFilter.parseFilterString(in.getFilterString())); } if (in.isSetAttributes()) { addAttributes(out,in.getAttributes()); } if (in.isSetAuthorizations()) { out.setAuthorizations(new Authorizations(in.getAuthorizations().getLabels())); } return out; }
public static Scan scanFromThrift(TScan in) throws IOException { Scan out = new Scan(); if (in.isSetStartRow()) { out.setStartRow(in.getStartRow()); } if (in.isSetStopRow()) { out.setStopRow(in.getStopRow()); } if (in.isSetCaching()) { out.setCaching(in.getCaching()); } if (in.isSetMaxVersions()) { out.setMaxVersions(in.getMaxVersions()); } if (in.isSetColumns()) { for (TColumn column : in.getColumns()) { if (column.isSetQualifier()) { out.addColumn(column.getFamily(), column.getQualifier()); } else { out.addFamily(column.getFamily()); } } } TTimeRange timeRange = in.getTimeRange(); if (timeRange != null && timeRange.isSetMinStamp() && timeRange.isSetMaxStamp()) { out.setTimeRange(timeRange.getMinStamp(), timeRange.getMaxStamp()); } if (in.isSetBatchSize()) { out.setBatch(in.getBatchSize()); } if (in.isSetFilterString()) { ParseFilter parseFilter = new ParseFilter(); out.setFilter(parseFilter.parseFilterString(in.getFilterString())); } if (in.isSetAttributes()) { addAttributes(out,in.getAttributes()); } if (in.isSetAuthorizations()) { out.setAuthorizations(new Authorizations(in.getAuthorizations().getLabels())); } if (in.isSetReversed()) { out.setReversed(in.isReversed()); } if (in.isSetCacheBlocks()) { out.setCacheBlocks(in.isCacheBlocks()); } if (in.isSetColFamTimeRangeMap()) { Map<ByteBuffer, TTimeRange> colFamTimeRangeMap = in.getColFamTimeRangeMap(); if (MapUtils.isNotEmpty(colFamTimeRangeMap)) { for (Map.Entry<ByteBuffer, TTimeRange> entry : colFamTimeRangeMap.entrySet()) { out.setColumnFamilyTimeRange(Bytes.toBytes(entry.getKey()), entry.getValue().getMinStamp(), entry.getValue().getMaxStamp()); } } } if (in.isSetReadType()) { out.setReadType(readTypeFromThrift(in.getReadType())); } if (in.isSetLimit()) { out.setLimit(in.getLimit()); } return out; }
@Test public void testScanWithVisibilityLabels() throws Exception { ThriftHBaseServiceHandler handler = createHandler(); ByteBuffer table = wrap(tableAname); // insert data TColumnValue columnValue = new TColumnValue(wrap(familyAname), wrap(qualifierAname), wrap(valueAname)); List<TColumnValue> columnValues = new ArrayList<>(1); columnValues.add(columnValue); for (int i = 0; i < 10; i++) { TPut put = new TPut(wrap(Bytes.toBytes("testScan" + i)), columnValues); if (i == 5) { put.setCellVisibility(new TCellVisibility().setExpression(PUBLIC)); } else { put.setCellVisibility(new TCellVisibility().setExpression("(" + SECRET + "|" + CONFIDENTIAL + ")" + "&" + "!" + TOPSECRET)); } handler.put(table, put); } // create scan instance TScan scan = new TScan(); List<TColumn> columns = new ArrayList<>(1); TColumn column = new TColumn(); column.setFamily(familyAname); column.setQualifier(qualifierAname); columns.add(column); scan.setColumns(columns); scan.setStartRow(Bytes.toBytes("testScan")); scan.setStopRow(Bytes.toBytes("testScan\uffff")); TAuthorization tauth = new TAuthorization(); List<String> labels = new ArrayList<>(2); labels.add(SECRET); labels.add(PRIVATE); tauth.setLabels(labels); scan.setAuthorizations(tauth); // get scanner and rows int scanId = handler.openScanner(table, scan); List<TResult> results = handler.getScannerRows(scanId, 10); assertEquals(9, results.size()); Assert.assertFalse(Bytes.equals(results.get(5).getRow(), Bytes.toBytes("testScan" + 5))); for (int i = 0; i < 9; i++) { if (i < 5) { assertArrayEquals(Bytes.toBytes("testScan" + i), results.get(i).getRow()); } else if (i == 5) { continue; } else { assertArrayEquals(Bytes.toBytes("testScan" + (i + 1)), results.get(i) .getRow()); } } // check that we are at the end of the scan results = handler.getScannerRows(scanId, 9); assertEquals(0, results.size()); // close scanner and check that it was indeed closed handler.closeScanner(scanId); try { handler.getScannerRows(scanId, 9); fail("Scanner id should be invalid"); } catch (TIllegalArgument e) { } }
@Test public void testGetScannerResultsWithAuthorizations() throws Exception { ThriftHBaseServiceHandler handler = createHandler(); ByteBuffer table = wrap(tableAname); // insert data TColumnValue columnValue = new TColumnValue(wrap(familyAname), wrap(qualifierAname), wrap(valueAname)); List<TColumnValue> columnValues = new ArrayList<>(1); columnValues.add(columnValue); for (int i = 0; i < 20; i++) { TPut put = new TPut( wrap(Bytes.toBytes("testGetScannerResults" + pad(i, (byte) 2))), columnValues); if (i == 3) { put.setCellVisibility(new TCellVisibility().setExpression(PUBLIC)); } else { put.setCellVisibility(new TCellVisibility().setExpression("(" + SECRET + "|" + CONFIDENTIAL + ")" + "&" + "!" + TOPSECRET)); } handler.put(table, put); } // create scan instance TScan scan = new TScan(); List<TColumn> columns = new ArrayList<>(1); TColumn column = new TColumn(); column.setFamily(familyAname); column.setQualifier(qualifierAname); columns.add(column); scan.setColumns(columns); scan.setStartRow(Bytes.toBytes("testGetScannerResults")); // get 5 rows and check the returned results scan.setStopRow(Bytes.toBytes("testGetScannerResults05")); TAuthorization tauth = new TAuthorization(); List<String> labels = new ArrayList<>(2); labels.add(SECRET); labels.add(PRIVATE); tauth.setLabels(labels); scan.setAuthorizations(tauth); List<TResult> results = handler.getScannerResults(table, scan, 5); assertEquals(4, results.size()); for (int i = 0; i < 4; i++) { if (i < 3) { assertArrayEquals( Bytes.toBytes("testGetScannerResults" + pad(i, (byte) 2)), results.get(i).getRow()); } else if (i == 3) { continue; } else { assertArrayEquals( Bytes.toBytes("testGetScannerResults" + pad(i + 1, (byte) 2)), results.get(i).getRow()); } } }
@Test public void testScan() throws Exception { ThriftHBaseServiceHandler handler = createHandler(); ByteBuffer table = wrap(tableAname); // insert data TColumnValue columnValue = new TColumnValue(wrap(familyAname), wrap(qualifierAname), wrap(valueAname)); List<TColumnValue> columnValues = new ArrayList<>(1); columnValues.add(columnValue); for (int i = 0; i < 10; i++) { TPut put = new TPut(wrap(Bytes.toBytes("testScan" + i)), columnValues); handler.put(table, put); } // create scan instance TScan scan = new TScan(); List<TColumn> columns = new ArrayList<>(1); TColumn column = new TColumn(); column.setFamily(familyAname); column.setQualifier(qualifierAname); columns.add(column); scan.setColumns(columns); scan.setStartRow(Bytes.toBytes("testScan")); scan.setStopRow(Bytes.toBytes("testScan\uffff")); // get scanner and rows int scanId = handler.openScanner(table, scan); List<TResult> results = handler.getScannerRows(scanId, 10); assertEquals(10, results.size()); for (int i = 0; i < 10; i++) { // check if the rows are returned and in order assertArrayEquals(Bytes.toBytes("testScan" + i), results.get(i).getRow()); } // check that we are at the end of the scan results = handler.getScannerRows(scanId, 10); assertEquals(0, results.size()); // close scanner and check that it was indeed closed handler.closeScanner(scanId); try { handler.getScannerRows(scanId, 10); fail("Scanner id should be invalid"); } catch (TIllegalArgument e) { } }
/** * Tests keeping a HBase scanner alive for long periods of time. Each call to getScannerRow() * should reset the ConnectionCache timeout for the scanner's connection. */ @Test public void testLongLivedScan() throws Exception { int numTrials = 6; int trialPause = 1000; int cleanUpInterval = 100; Configuration conf = new Configuration(UTIL.getConfiguration()); // Set the ConnectionCache timeout to trigger halfway through the trials conf.setInt(ThriftHBaseServiceHandler.MAX_IDLETIME, (numTrials / 2) * trialPause); conf.setInt(ThriftHBaseServiceHandler.CLEANUP_INTERVAL, cleanUpInterval); ThriftHBaseServiceHandler handler = new ThriftHBaseServiceHandler(conf, UserProvider.instantiate(conf)); ByteBuffer table = wrap(tableAname); // insert data TColumnValue columnValue = new TColumnValue(wrap(familyAname), wrap(qualifierAname), wrap(valueAname)); List<TColumnValue> columnValues = new ArrayList<>(1); columnValues.add(columnValue); for (int i = 0; i < numTrials; i++) { TPut put = new TPut(wrap(Bytes.toBytes("testScan" + i)), columnValues); handler.put(table, put); } // create scan instance TScan scan = new TScan(); List<TColumn> columns = new ArrayList<>(1); TColumn column = new TColumn(); column.setFamily(familyAname); column.setQualifier(qualifierAname); columns.add(column); scan.setColumns(columns); scan.setStartRow(Bytes.toBytes("testScan")); scan.setStopRow(Bytes.toBytes("testScan\uffff")); // Prevent the scanner from caching results scan.setCaching(1); // get scanner and rows int scanId = handler.openScanner(table, scan); for (int i = 0; i < numTrials; i++) { // Make sure that the Scanner doesn't throw an exception after the ConnectionCache timeout List<TResult> results = handler.getScannerRows(scanId, 1); assertArrayEquals(Bytes.toBytes("testScan" + i), results.get(0).getRow()); Thread.sleep(trialPause); } }
@Test public void testReverseScan() throws Exception { ThriftHBaseServiceHandler handler = createHandler(); ByteBuffer table = wrap(tableAname); // insert data TColumnValue columnValue = new TColumnValue(wrap(familyAname), wrap(qualifierAname), wrap(valueAname)); List<TColumnValue> columnValues = new ArrayList<>(1); columnValues.add(columnValue); for (int i = 0; i < 10; i++) { TPut put = new TPut(wrap(Bytes.toBytes("testReverseScan" + i)), columnValues); handler.put(table, put); } // create reverse scan instance TScan scan = new TScan(); scan.setReversed(true); List<TColumn> columns = new ArrayList<>(1); TColumn column = new TColumn(); column.setFamily(familyAname); column.setQualifier(qualifierAname); columns.add(column); scan.setColumns(columns); scan.setStartRow(Bytes.toBytes("testReverseScan\uffff")); scan.setStopRow(Bytes.toBytes("testReverseScan")); // get scanner and rows int scanId = handler.openScanner(table, scan); List<TResult> results = handler.getScannerRows(scanId, 10); assertEquals(10, results.size()); for (int i = 0; i < 10; i++) { // check if the rows are returned and in order assertArrayEquals(Bytes.toBytes("testReverseScan" + (9 - i)), results.get(i).getRow()); } // check that we are at the end of the scan results = handler.getScannerRows(scanId, 10); assertEquals(0, results.size()); // close scanner and check that it was indeed closed handler.closeScanner(scanId); try { handler.getScannerRows(scanId, 10); fail("Scanner id should be invalid"); } catch (TIllegalArgument e) { } }
@Test public void testScanWithFilter() throws Exception { ThriftHBaseServiceHandler handler = createHandler(); ByteBuffer table = wrap(tableAname); // insert data TColumnValue columnValue = new TColumnValue(wrap(familyAname), wrap(qualifierAname), wrap(valueAname)); List<TColumnValue> columnValues = new ArrayList<>(1); columnValues.add(columnValue); for (int i = 0; i < 10; i++) { TPut put = new TPut(wrap(Bytes.toBytes("testScanWithFilter" + i)), columnValues); handler.put(table, put); } // create scan instance with filter TScan scan = new TScan(); List<TColumn> columns = new ArrayList<>(1); TColumn column = new TColumn(); column.setFamily(familyAname); column.setQualifier(qualifierAname); columns.add(column); scan.setColumns(columns); scan.setStartRow(Bytes.toBytes("testScanWithFilter")); scan.setStopRow(Bytes.toBytes("testScanWithFilter\uffff")); // only get the key part scan.setFilterString(wrap(Bytes.toBytes("KeyOnlyFilter()"))); // get scanner and rows int scanId = handler.openScanner(table, scan); List<TResult> results = handler.getScannerRows(scanId, 10); assertEquals(10, results.size()); for (int i = 0; i < 10; i++) { // check if the rows are returned and in order assertArrayEquals(Bytes.toBytes("testScanWithFilter" + i), results.get(i).getRow()); // check that the value is indeed stripped by the filter assertEquals(0, results.get(i).getColumnValues().get(0).getValue().length); } // check that we are at the end of the scan results = handler.getScannerRows(scanId, 10); assertEquals(0, results.size()); // close scanner and check that it was indeed closed handler.closeScanner(scanId); try { handler.getScannerRows(scanId, 10); fail("Scanner id should be invalid"); } catch (TIllegalArgument e) { } }
@Test public void testSmallScan() throws Exception { ThriftHBaseServiceHandler handler = createHandler(); ByteBuffer table = wrap(tableAname); // insert data TColumnValue columnValue = new TColumnValue(wrap(familyAname), wrap(qualifierAname), wrap(valueAname)); List<TColumnValue> columnValues = new ArrayList<>(); columnValues.add(columnValue); for (int i = 0; i < 10; i++) { TPut put = new TPut(wrap(Bytes.toBytes("testSmallScan" + i)), columnValues); handler.put(table, put); } // small scan instance TScan scan = new TScan(); scan.setStartRow(Bytes.toBytes("testSmallScan")); scan.setStopRow(Bytes.toBytes("testSmallScan\uffff")); scan.setReadType(TReadType.PREAD); scan.setCaching(2); // get scanner and rows int scanId = handler.openScanner(table, scan); List<TResult> results = handler.getScannerRows(scanId, 10); assertEquals(10, results.size()); for (int i = 0; i < 10; i++) { // check if the rows are returned and in order assertArrayEquals(Bytes.toBytes("testSmallScan" + i), results.get(i).getRow()); } // check that we are at the end of the scan results = handler.getScannerRows(scanId, 10); assertEquals(0, results.size()); // close scanner and check that it was indeed closed handler.closeScanner(scanId); try { handler.getScannerRows(scanId, 10); fail("Scanner id should be invalid"); } catch (TIllegalArgument e) { } }