org.apache.hadoop.hbase.client.ResultScanner Java Examples

The following examples show how to use org.apache.hadoop.hbase.client.ResultScanner. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source Project: hbase   Author: apache   File: TestVisibilityLabels.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testSimpleVisibilityLabels() throws Exception {
  TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
  try (Table table = createTableAndWriteDataWithLabels(tableName, SECRET + "|" + CONFIDENTIAL,
      PRIVATE + "|" + CONFIDENTIAL)) {
    Scan s = new Scan();
    s.setAuthorizations(new Authorizations(SECRET, CONFIDENTIAL, PRIVATE));
    ResultScanner scanner = table.getScanner(s);
    Result[] next = scanner.next(3);

    assertTrue(next.length == 2);
    CellScanner cellScanner = next[0].cellScanner();
    cellScanner.advance();
    Cell current = cellScanner.current();
    assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
        current.getRowLength(), row1, 0, row1.length));
    cellScanner = next[1].cellScanner();
    cellScanner.advance();
    current = cellScanner.current();
    assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
        current.getRowLength(), row2, 0, row2.length));
  }
}
 
Example #2
Source Project: aws-athena-query-federation   Author: awslabs   File: HbaseRecordHandler.java    License: Apache License 2.0 6 votes vote down vote up
private boolean scanFilterProject(ResultScanner scanner, ReadRecordsRequest request, BlockSpiller blockSpiller, QueryStatusChecker queryStatusChecker)
{
    Schema projection = request.getSchema();
    boolean isNative = projection.getCustomMetadata().get(HBASE_NATIVE_STORAGE_FLAG) != null;

    for (Result row : scanner) {
        if (!queryStatusChecker.isQueryRunning()) {
            return true;
        }
        blockSpiller.writeRows((Block block, int rowNum) -> {
            boolean match = true;
            for (Field field : projection.getFields()) {
                if (match) {
                    match &= writeField(block, field, isNative, row, rowNum);
                }
            }
            return match ? 1 : 0;
        });
    }
    return true;
}
 
Example #3
Source Project: phoenix   Author: apache   File: TestUtil.java    License: Apache License 2.0 6 votes vote down vote up
public static void dumpIndexStatus(Connection conn, String indexName) throws IOException, SQLException {
    try (Table table = conn.unwrap(PhoenixConnection.class).getQueryServices().getTable(PhoenixDatabaseMetaData.SYSTEM_CATALOG_NAME_BYTES)) { 
        System.out.println("************ dumping index status for " + indexName + " **************");
        Scan s = new Scan();
        s.setRaw(true);
        s.setMaxVersions();
        byte[] startRow = SchemaUtil.getTableKeyFromFullName(indexName);
        s.setStartRow(startRow);
        s.setStopRow(ByteUtil.nextKey(ByteUtil.concat(startRow, QueryConstants.SEPARATOR_BYTE_ARRAY)));
        try (ResultScanner scanner = table.getScanner(s)) {
            Result result = null;
            while ((result = scanner.next()) != null) {
                CellScanner cellScanner = result.cellScanner();
                Cell current = null;
                while (cellScanner.advance()) {
                    current = cellScanner.current();
                    if (Bytes.compareTo(current.getQualifierArray(), current.getQualifierOffset(), current.getQualifierLength(), PhoenixDatabaseMetaData.INDEX_STATE_BYTES, 0, PhoenixDatabaseMetaData.INDEX_STATE_BYTES.length) == 0) {
                        System.out.println(current.getTimestamp() + "/INDEX_STATE=" + PIndexState.fromSerializedValue(current.getValueArray()[current.getValueOffset()]));
                    }
                }
            }
        }
        System.out.println("-----------------------------------------------");
}
}
 
Example #4
Source Project: hbase   Author: apache   File: TestRegionObserverInterface.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testHBASE14489() throws IOException {
  final TableName tableName = TableName.valueOf(name.getMethodName());
  Table table = util.createTable(tableName, new byte[][] { A });
  Put put = new Put(ROW);
  put.addColumn(A, A, A);
  table.put(put);

  Scan s = new Scan();
  s.setFilter(new FilterAllFilter());
  ResultScanner scanner = table.getScanner(s);
  try {
    for (Result rr = scanner.next(); rr != null; rr = scanner.next()) {
    }
  } finally {
    scanner.close();
  }
  verifyMethodResult(SimpleRegionObserver.class, new String[] { "wasScannerFilterRowCalled" },
    tableName, new Boolean[] { true });
  util.deleteTable(tableName);
  table.close();

}
 
Example #5
Source Project: aws-athena-query-federation   Author: awslabs   File: HBaseConnectionTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void scanTable()
        throws IOException
{
    logger.info("scanTable: enter");
    when(mockConnection.getTable(any(org.apache.hadoop.hbase.TableName.class))).thenReturn(mockTable);
    when(mockTable.getScanner(any(Scan.class))).thenReturn(mockScanner);

    TableName tableName = org.apache.hadoop.hbase.TableName.valueOf("schema1", "table1");
    boolean result = connection.scanTable(tableName, mock(Scan.class), (ResultScanner scanner) -> scanner != null);

    assertTrue(result);
    assertTrue(connection.isHealthy());
    assertEquals(0, connection.getRetries());
    verify(mockConnection, atLeastOnce()).getTable(anyObject());
    verify(mockTable, atLeastOnce()).getScanner(any(Scan.class));
    logger.info("scanTable: exit");
}
 
Example #6
Source Project: hbase   Author: apache   File: IntegrationTestMobCompaction.java    License: Apache License 2.0 6 votes vote down vote up
private void scanTable() {
  try {

    Result result;
    ResultScanner scanner = table.getScanner(fam);
    int counter = 0;
    while ((result = scanner.next()) != null) {
      assertTrue(Arrays.equals(result.getValue(fam, qualifier), mobVal));
      if (counter % 10000 == 0) {
        LOG.info("GET=" + counter);
      }
      counter++;
    }
    assertEquals(rowsToLoad, counter);
  } catch (Exception e) {
    e.printStackTrace();
    LOG.error("MOB Stress Test FAILED");
    if (util != null) {
      assertTrue(false);
    } else {
      System.exit(-1);
    }
  }
}
 
Example #7
Source Project: java-docs-samples   Author: GoogleCloudPlatform   File: Reads.java    License: Apache License 2.0 6 votes vote down vote up
public static void readRowRange(String projectId, String instanceId, String tableId) {
  // Initialize client that will be used to send requests. This client only needs to be created
  // once, and can be reused for multiple requests. After completing all of your requests, call
  // the "close" method on the client to safely clean up any remaining background resources.
  try (Connection connection = BigtableConfiguration.connect(projectId, instanceId)) {
    Table table = connection.getTable(TableName.valueOf(tableId));

    Scan rangeQuery =
        new Scan()
            .withStartRow(Bytes.toBytes("phone#4c410523#20190501"))
            .withStopRow(Bytes.toBytes("phone#4c410523#201906201"));

    ResultScanner rows = table.getScanner(rangeQuery);

    for (Result row : rows) {
      printRow(row);
    }

  } catch (IOException e) {
    System.out.println(
        "Unable to initialize service client, as a network error occurred: \n" + e.toString());
  }
}
 
Example #8
Source Project: hbase   Author: apache   File: TestDeleteMobTable.java    License: Apache License 2.0 6 votes vote down vote up
private String assertHasOneMobRow(Table table, TableName tn, String familyName)
    throws IOException {
  Scan scan = new Scan();
  scan.setAttribute(MobConstants.MOB_SCAN_RAW, Bytes.toBytes(Boolean.TRUE));
  ResultScanner rs = table.getScanner(scan);
  Result r = rs.next();
  Assert.assertNotNull(r);
  String fileName = MobUtils.getMobFileName(r.getColumnLatestCell(FAMILY, QF));
  Path filePath = new Path(
      MobUtils.getMobFamilyPath(TEST_UTIL.getConfiguration(), tn, familyName), fileName);
  FileSystem fs = TEST_UTIL.getTestFileSystem();
  Assert.assertTrue(fs.exists(filePath));
  r = rs.next();
  Assert.assertNull(r);
  return fileName;
}
 
Example #9
Source Project: kylin-on-parquet-v2   Author: Kyligence   File: GridTableHBaseBenchmark.java    License: Apache License 2.0 6 votes vote down vote up
private static void fullScan(Connection conn, boolean[] hits, Stats stats) throws IOException {
    Table table = conn.getTable(TableName.valueOf(TEST_TABLE));
    try {
        stats.markStart();

        Scan scan = new Scan();
        scan.addFamily(CF);
        ResultScanner scanner = table.getScanner(scan);
        int i = 0;
        for (Result r : scanner) {
            if (hits[i])
                stats.consume(r);
            dot(i, N_ROWS);
            i++;
        }

        stats.markEnd();
    } finally {
        IOUtils.closeQuietly(table);
    }
}
 
Example #10
Source Project: phoenix-tephra   Author: apache   File: DataJanitorState.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Return regions that were recorded as empty after the given time.
 *
 * @param time time in milliseconds
 * @param includeRegions If not null, the returned set will be an intersection of the includeRegions set
 *                       and the empty regions after the given time
 */
public SortedSet<byte[]> getEmptyRegionsAfterTime(long time, @Nullable SortedSet<byte[]> includeRegions)
  throws IOException {
  SortedSet<byte[]> emptyRegions = new TreeSet<>(Bytes.BYTES_COMPARATOR);
  try (HTableInterface stateTable = stateTableSupplier.get()) {
    Scan scan = new Scan(makeEmptyRegionTimeKey(Bytes.toBytes(time + 1), EMPTY_BYTE_ARRAY),
                         EMPTY_REGION_TIME_KEY_PREFIX_STOP);
    scan.addColumn(FAMILY, EMPTY_REGION_TIME_COL);

    try (ResultScanner scanner = stateTable.getScanner(scan)) {
      Result next;
      while ((next = scanner.next()) != null) {
        byte[] emptyRegion = getEmptyRegionFromKey(next.getRow());
        if (includeRegions == null || includeRegions.contains(emptyRegion)) {
          emptyRegions.add(emptyRegion);
        }
      }
    }
  }
  return Collections.unmodifiableSortedSet(emptyRegions);
}
 
Example #11
Source Project: kylin-on-parquet-v2   Author: Kyligence   File: AclTableMigrationTool.java    License: Apache License 2.0 6 votes vote down vote up
private void convertToResourceStore(KylinConfig kylinConfig, String tableName, ResourceStore store,
        ResultConverter converter) throws IOException {

    Table table = null;
    ResultScanner rs = null;
    Scan scan = new Scan();
    try {
        table = HBaseConnection.get(kylinConfig.getStorageUrl()).getTable(TableName.valueOf(tableName));
        rs = table.getScanner(scan);
        converter.convertResult(rs, store);
        store.checkAndPutResource(MIGRATE_OK_PREFIX + tableName, new StringEntity(tableName + " migrated"),
                StringEntity.serializer);
    } finally {
        IOUtils.closeQuietly(rs);
        IOUtils.closeQuietly(table);
    }

}
 
Example #12
Source Project: hbase   Author: apache   File: TestVisibilityLabelsWithACL.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testScanForSuperUserWithFewerLabelAuths() throws Throwable {
  String[] auths = { SECRET };
  String user = "admin";
  try (Connection conn = ConnectionFactory.createConnection(conf)) {
    VisibilityClient.setAuths(conn, auths, user);
  }
  TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
  final Table table = createTableAndWriteDataWithLabels(tableName, SECRET + "&" + CONFIDENTIAL
      + "&!" + PRIVATE, SECRET + "&!" + PRIVATE);
  PrivilegedExceptionAction<Void> scanAction = new PrivilegedExceptionAction<Void>() {
    @Override
    public Void run() throws Exception {
      Scan s = new Scan();
      s.setAuthorizations(new Authorizations(SECRET, CONFIDENTIAL));
      try (Connection connection = ConnectionFactory.createConnection(conf);
           Table t = connection.getTable(table.getName())) {
        ResultScanner scanner = t.getScanner(s);
        Result[] result = scanner.next(5);
        assertTrue(result.length == 2);
      }
      return null;
    }
  };
  SUPERUSER.runAs(scanAction);
}
 
Example #13
Source Project: phoenix-tephra   Author: apache   File: DataJanitorState.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Return regions that were recorded as empty after the given time.
 *
 * @param time time in milliseconds
 * @param includeRegions If not null, the returned set will be an intersection of the includeRegions set
 *                       and the empty regions after the given time
 */
public SortedSet<byte[]> getEmptyRegionsAfterTime(long time, @Nullable SortedSet<byte[]> includeRegions)
  throws IOException {
  SortedSet<byte[]> emptyRegions = new TreeSet<>(Bytes.BYTES_COMPARATOR);
  try (HTableInterface stateTable = stateTableSupplier.get()) {
    Scan scan = new Scan(makeEmptyRegionTimeKey(Bytes.toBytes(time + 1), EMPTY_BYTE_ARRAY),
                         EMPTY_REGION_TIME_KEY_PREFIX_STOP);
    scan.addColumn(FAMILY, EMPTY_REGION_TIME_COL);

    try (ResultScanner scanner = stateTable.getScanner(scan)) {
      Result next;
      while ((next = scanner.next()) != null) {
        byte[] emptyRegion = getEmptyRegionFromKey(next.getRow());
        if (includeRegions == null || includeRegions.contains(emptyRegion)) {
          emptyRegions.add(emptyRegion);
        }
      }
    }
  }
  return Collections.unmodifiableSortedSet(emptyRegions);
}
 
Example #14
Source Project: BigData-In-Practice   Author: whirlys   File: HBaseFilterTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void columnPrefixFilterTest() {
    Filter filter = new ColumnPrefixFilter(Bytes.toBytes("nam"));
    FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL, Arrays.asList(filter));
    ResultScanner scanner = HBaseUtil
            .getScanner("FileTable", "rowkey1", "rowkey3", filterList);

    if (scanner != null) {
        scanner.forEach(result -> {
            System.out.println("rowkey=" + Bytes.toString(result.getRow()));
            System.out.println("fileName=" + Bytes
                    .toString(result.getValue(Bytes.toBytes("fileInfo"), Bytes.toBytes("name"))));
            System.out.println("fileType=" + Bytes
                    .toString(result.getValue(Bytes.toBytes("fileInfo"), Bytes.toBytes("type"))));
        });
        scanner.close();
    }
}
 
Example #15
Source Project: hbase   Author: apache   File: TestScanRowPrefix.java    License: Apache License 2.0 6 votes vote down vote up
private void verifyScanResult(Table table, Scan scan, List<byte[]> expectedKeys, String message) {
  List<byte[]> actualKeys = new ArrayList<>();
  try {
    ResultScanner scanner = table.getScanner(scan);
    for (Result result : scanner) {
      actualKeys.add(result.getRow());
    }

    String fullMessage = message;
    if (LOG.isDebugEnabled()) {
      fullMessage = message + "\n" + tableOfTwoListsOfByteArrays(
              "Expected", expectedKeys,
              "Actual  ", actualKeys);
    }

    Assert.assertArrayEquals(
            fullMessage,
            expectedKeys.toArray(),
            actualKeys.toArray());
  } catch (IOException e) {
    e.printStackTrace();
    Assert.fail();
  }
}
 
Example #16
Source Project: hbase   Author: apache   File: TestSCVFWithMiniCluster.java    License: Apache License 2.0 6 votes vote down vote up
private void verify(Scan scan) throws IOException {
  ResultScanner scanner = htable.getScanner(scan);
  Iterator<Result> it = scanner.iterator();

  /* Then */
  int count = 0;
  try {
    while (it.hasNext()) {
      it.next();
      count++;
    }
  } finally {
    scanner.close();
  }
  assertEquals(expected, count);
}
 
Example #17
Source Project: hbase   Author: apache   File: TestJoinedScanners.java    License: Apache License 2.0 6 votes vote down vote up
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, CompareOperator.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");
}
 
Example #18
Source Project: hbase   Author: apache   File: TestMultiRowRangeFilter.java    License: Apache License 2.0 6 votes vote down vote up
private List<Cell> getScanResult(byte[] startRow, byte[] stopRow, Table ht) throws IOException {
  Scan scan = new Scan();
  scan.readAllVersions();
  if(!Bytes.toString(startRow).isEmpty()) {
    scan.withStartRow(startRow);
  }
  if(!Bytes.toString(stopRow).isEmpty()) {
    scan.withStopRow(stopRow);
  }
  ResultScanner scanner = ht.getScanner(scan);
  List<Cell> kvList = new ArrayList<>();
  Result r;
  while ((r = scanner.next()) != null) {
    for (Cell kv : r.listCells()) {
      kvList.add(kv);
    }
  }
  scanner.close();
  return kvList;
}
 
Example #19
Source Project: hbase   Author: apache   File: TestSpaceQuotaViolationPolicyRefresherChore.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testParsingError() throws IOException {
  when(chore.fetchSnapshotsFromQuotaTable()).thenCallRealMethod();
  ResultScanner scanner = mock(ResultScanner.class);
  Table quotaTable = mock(Table.class);
  when(conn.getTable(QuotaUtil.QUOTA_TABLE_NAME)).thenReturn(quotaTable);
  when(quotaTable.getScanner(any(Scan.class))).thenReturn(scanner);

  List<Result> results = new ArrayList<>();
  Cell c = new KeyValue(toBytes("t:inviolation"), toBytes("u"), toBytes("v"), new byte[0]);
  results.add(Result.create(Collections.singletonList(c)));
  when(scanner.iterator()).thenReturn(results.iterator());
  try {
    chore.fetchSnapshotsFromQuotaTable();
    fail("Expected an IOException, but did not receive one.");
  } catch (IOException e) {
    // We provided a garbage serialized protobuf message (empty byte array), this should
    // in turn throw an IOException
  }
}
 
Example #20
Source Project: hbase-operator-tools   Author: apache   File: TestFsRegionsMetaRecoverer.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testRemoveRegionInfoFromMeta() throws Exception {
  ResultScanner mockedRsTables = Mockito.mock(ResultScanner.class);
  List<Cell> cells = new ArrayList<>();
  cells.add(createCellForTableState(TableName.valueOf("test-tbl")));
  Result result = Result.create(cells);
  ResultScanner mockedRsRegions = Mockito.mock(ResultScanner.class);
  createRegionInMeta(mockedRsRegions);
  when(this.mockedTable.getScanner(any(Scan.class))).
    thenReturn(mockedRsTables).
      thenReturn(mockedRsRegions);
  when(mockedRsTables.next()).thenReturn(result,(Result)null);
  List<String> tableList = new ArrayList<>();
  tableList.add("default:test-tbl");
  fixer.removeExtraRegionsFromMetaForTables(tableList);
  Mockito.verify(this.mockedTable).delete(anyList());
}
 
Example #21
Source Project: metron   Author: apache   File: SimpleEnrichmentFlatFileLoaderIntegrationTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testMRLineByLine() throws Exception {
  String[] argv = {"-c cf", "-t enrichment"
          , "-e " + lineByLineExtractorConfigFile.getPath()
          , "-i " + multilineFile.getName()
          , "-m MR"
          , "-p 2", "-b 128", "-q"
  };
  FileSystem fs = FileSystem.get(config);
  HBaseUtil.INSTANCE.writeFile(new String(Files.readAllBytes(multilineFile.toPath()),
      StandardCharsets.UTF_8), new Path(multilineFile.getName()), fs);
  SimpleEnrichmentFlatFileLoader.main(config, argv);
  EnrichmentConverter converter = new EnrichmentConverter();
  ResultScanner scanner = testTable.getScanner(Bytes.toBytes(cf));
  List<LookupKV<EnrichmentKey, EnrichmentValue>> results = new ArrayList<>();
  for (Result r : scanner) {
    results.add(converter.fromResult(r, cf));
    testTable.delete(new Delete(r.getRow()));
  }
  assertEquals(NUM_LINES, results.size());
  assertTrue(results.get(0).getKey().indicator.startsWith("google"));
  assertEquals(results.get(0).getKey().type, "enrichment");
  assertEquals(results.get(0).getValue().getMetadata().size(), 2);
  assertTrue(results.get(0).getValue().getMetadata().get("meta").toString().startsWith("foo"));
  assertTrue(results.get(0).getValue().getMetadata().get("host").toString().startsWith("google"));
}
 
Example #22
Source Project: metron   Author: apache   File: SimpleEnrichmentFlatFileLoaderIntegrationTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testLocalLineByLine() throws Exception {
  String[] argv = {"-c cf", "-t enrichment"
          , "-e " + lineByLineExtractorConfigFile.getPath()
          , "-i " + multilineFile.getPath()
          , "-p 2", "-b 128", "-q"
  };
  SimpleEnrichmentFlatFileLoader.main(config, argv);
  EnrichmentConverter converter = new EnrichmentConverter();
  ResultScanner scanner = testTable.getScanner(Bytes.toBytes(cf));
  List<LookupKV<EnrichmentKey, EnrichmentValue>> results = new ArrayList<>();
  for (Result r : scanner) {
    results.add(converter.fromResult(r, cf));
    testTable.delete(new Delete(r.getRow()));
  }
  assertEquals(NUM_LINES, results.size());
  assertTrue(results.get(0).getKey().indicator.startsWith("google"));
  assertEquals(results.get(0).getKey().type, "enrichment");
  assertEquals(results.get(0).getValue().getMetadata().size(), 2);
  assertTrue(results.get(0).getValue().getMetadata().get("meta").toString().startsWith("foo"));
  assertTrue(results.get(0).getValue().getMetadata().get("host").toString().startsWith("google"));
}
 
Example #23
Source Project: phoenix-tephra   Author: apache   File: TransactionAwareHTableTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testCheckpointRollback() throws Exception {
  // start a transaction, using checkpoints between writes
  transactionContext.start();
  transactionAwareHTable.put(new Put(TestBytes.row).add(TestBytes.family, TestBytes.qualifier, TestBytes.value));
  transactionContext.checkpoint();
  transactionAwareHTable.put(new Put(TestBytes.row2).add(TestBytes.family, TestBytes.qualifier, TestBytes.value2));
  transactionContext.checkpoint();
  transactionAwareHTable.put(new Put(TestBytes.row3).add(TestBytes.family, TestBytes.qualifier, TestBytes.value));

  transactionContext.abort();

  transactionContext.start();
  verifyRow(transactionAwareHTable, TestBytes.row, null);
  verifyRow(transactionAwareHTable, TestBytes.row2, null);
  verifyRow(transactionAwareHTable, TestBytes.row3, null);

  Scan scan = new Scan();
  ResultScanner scanner = transactionAwareHTable.getScanner(scan);
  assertNull(scanner.next());
  scanner.close();
  transactionContext.finish();
}
 
Example #24
Source Project: phoenix-tephra   Author: apache   File: TransactionAwareHTableTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testCheckpointRollback() throws Exception {
  // start a transaction, using checkpoints between writes
  transactionContext.start();
  transactionAwareHTable.put(new Put(TestBytes.row).add(TestBytes.family, TestBytes.qualifier, TestBytes.value));
  transactionContext.checkpoint();
  transactionAwareHTable.put(new Put(TestBytes.row2).add(TestBytes.family, TestBytes.qualifier, TestBytes.value2));
  transactionContext.checkpoint();
  transactionAwareHTable.put(new Put(TestBytes.row3).add(TestBytes.family, TestBytes.qualifier, TestBytes.value));

  transactionContext.abort();

  transactionContext.start();
  verifyRow(transactionAwareHTable, TestBytes.row, null);
  verifyRow(transactionAwareHTable, TestBytes.row2, null);
  verifyRow(transactionAwareHTable, TestBytes.row3, null);

  Scan scan = new Scan();
  ResultScanner scanner = transactionAwareHTable.getScanner(scan);
  assertNull(scanner.next());
  scanner.close();
  transactionContext.finish();
}
 
Example #25
Source Project: spork   Author: sigmoidanalytics   File: TestHBaseStorage.java    License: Apache License 2.0 5 votes vote down vote up
private void deleteAllRows(String tableName) throws Exception {
    HTable table = new HTable(conf, tableName);
    ResultScanner scanner = table.getScanner(new Scan());
    List<Delete> deletes = Lists.newArrayList();
    for (Result row : scanner) {
        deletes.add(new Delete(row.getRow()));
    }
    table.delete(deletes);
    table.close();
}
 
Example #26
Source Project: phoenix-tephra   Author: apache   File: TransactionAwareHTableTest.java    License: Apache License 2.0 5 votes vote down vote up
private void verifyScan(Table table, Scan scan, List<KeyValue> expectedCells) throws Exception {
  List<Cell> actualCells = new ArrayList<>();
  try (ResultScanner scanner = table.getScanner(scan)) {
    Result[] results = scanner.next(expectedCells.size() + 1);
    for (Result result : results) {
      actualCells.addAll(Lists.newArrayList(result.rawCells()));
    }
    Assert.assertEquals(expectedCells, actualCells);
  }
}
 
Example #27
Source Project: nifi   Author: apache   File: HBase_1_1_2_ClientService.java    License: Apache License 2.0 5 votes vote down vote up
protected ResultScanner getResults(final Table table, final Collection<Column> columns, final Filter filter, final long minTime, List<String> authorizations) throws IOException {
    // Create a new scan. We will set the min timerange as the latest timestamp that
    // we have seen so far. The minimum timestamp is inclusive, so we will get duplicates.
    // We will record any cells that have the latest timestamp, so that when we scan again,
    // we know to throw away those duplicates.
    final Scan scan = new Scan();
    scan.setTimeRange(minTime, Long.MAX_VALUE);

    if (authorizations != null && authorizations.size() > 0) {
        scan.setAuthorizations(new Authorizations(authorizations));
    }

    if (filter != null) {
        scan.setFilter(filter);
    }

    if (columns != null) {
        for (Column col : columns) {
            if (col.getQualifier() == null) {
                scan.addFamily(col.getFamily());
            } else {
                scan.addColumn(col.getFamily(), col.getQualifier());
            }
        }
    }

    return table.getScanner(scan);
}
 
Example #28
Source Project: phoenix-omid   Author: apache   File: TestBaillisAnomaliesWithTXs.java    License: Apache License 2.0 5 votes vote down vote up
private void assertNumberOfRows(TTable txTable, Transaction tx2, int maxCount, Scan scan) throws IOException {
    int count = 0;
    ResultScanner tx2Scanner = txTable.getScanner(tx2, scan);
    Result res = tx2Scanner.next();
    while (res != null) {
        LOG.info("RESSS {}", res);
        LOG.info("Row id {} with value {}", Bytes.toString(res.getRow()), Bytes.toInt(res.getValue(famName, colName)));
        res = tx2Scanner.next();
        count++;
    }
    assertEquals(count, maxCount);
}
 
Example #29
Source Project: nifi   Author: apache   File: HBase_2_ClientService.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void scan(final String tableName, final String startRow, final String endRow, String filterExpression,
        final Long timerangeMin, final Long timerangeMax, final Integer limitRows, final Boolean isReversed,
        final Boolean blockCache, final Collection<Column> columns, List<String> visibilityLabels, final ResultHandler handler) throws IOException {

    try (final Table table = connection.getTable(TableName.valueOf(tableName));
            final ResultScanner scanner = getResults(table, startRow, endRow, filterExpression, timerangeMin,
                    timerangeMax, limitRows, isReversed, blockCache, columns, visibilityLabels)) {

        int cnt = 0;
        final int lim = limitRows != null ? limitRows : 0;
        for (final Result result : scanner) {

            if (lim > 0 && ++cnt > lim){
                break;
            }

            final byte[] rowKey = result.getRow();
            final Cell[] cells = result.rawCells();

            if (cells == null) {
                continue;
            }

            // convert HBase cells to NiFi cells
            final ResultCell[] resultCells = new ResultCell[cells.length];
            for (int i = 0; i < cells.length; i++) {
                final Cell cell = cells[i];
                final ResultCell resultCell = getResultCell(cell);
                resultCells[i] = resultCell;
            }

            // delegate to the handler
            handler.handle(rowKey, resultCells);
        }
    }
}
 
Example #30
Source Project: phoenix-tephra   Author: apache   File: DataJanitorState.java    License: Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
void deleteFromScan(Table stateTable, Scan scan) throws IOException {
  try (ResultScanner scanner = stateTable.getScanner(scan)) {
    Result next;
    while ((next = scanner.next()) != null) {
      stateTable.delete(new Delete(next.getRow()));
    }
  }
}