Java Code Examples for org.apache.hadoop.hbase.client.ResultScanner#next()

The following examples show how to use org.apache.hadoop.hbase.client.ResultScanner#next() . 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 File: TestVisibilityLabels.java    From hbase with Apache License 2.0 6 votes vote down vote up
@Test
public void testVisibilityLabelsOnRSRestart() throws Exception {
  final TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
  List<RegionServerThread> regionServerThreads = TEST_UTIL.getHBaseCluster()
      .getRegionServerThreads();
  for (RegionServerThread rsThread : regionServerThreads) {
    rsThread.getRegionServer().abort("Aborting ");
  }
  // Start one new RS
  RegionServerThread rs = TEST_UTIL.getHBaseCluster().startRegionServer();
  waitForLabelsRegionAvailability(rs.getRegionServer());
  try (Table table = createTableAndWriteDataWithLabels(tableName, "(" + SECRET + "|" + CONFIDENTIAL
      + ")", PRIVATE)) {
    Scan s = new Scan();
    s.setAuthorizations(new Authorizations(SECRET));
    ResultScanner scanner = table.getScanner(s);
    Result[] next = scanner.next(3);
    assertTrue(next.length == 1);
  }
}
 
Example 2
Source File: TestVisibilityLabels.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Test
public void testAuthorizationsWithSpecialUnicodeCharacters() throws Exception {
  TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
  try (Table table = createTableAndWriteDataWithLabels(tableName,
      CellVisibility.quote(UC1) + "|" + CellVisibility.quote(UC2), CellVisibility.quote(UC1),
      CellVisibility.quote(UNICODE_VIS_TAG))) {
    Scan s = new Scan();
    s.setAuthorizations(new Authorizations(UC1, UC2, ACCENT,
        UNICODE_VIS_TAG));
    ResultScanner scanner = table.getScanner(s);
    Result[] next = scanner.next(3);
    assertTrue(next.length == 3);
    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));
    cellScanner = next[2].cellScanner();
    cellScanner.advance();
    current = cellScanner.current();
    assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(),
        current.getRowLength(), row3, 0, row3.length));
  }
}
 
Example 3
Source File: TestPartialResultsFromClientSide.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Ensure that we only see Results marked as partial when the allowPartial flag is set
 * @throws Exception
 */
@Test
public void testAllowPartialResults() throws Exception {
  Scan scan = new Scan();
  scan.setAllowPartialResults(true);
  scan.setMaxResultSize(1);
  ResultScanner scanner = TABLE.getScanner(scan);
  Result result = scanner.next();

  assertTrue(result != null);
  assertTrue(result.mayHaveMoreCellsInRow());
  assertTrue(result.rawCells() != null);
  assertTrue(result.rawCells().length == 1);

  scanner.close();

  scan.setAllowPartialResults(false);
  scanner = TABLE.getScanner(scan);
  result = scanner.next();

  assertTrue(result != null);
  assertTrue(!result.mayHaveMoreCellsInRow());
  assertTrue(result.rawCells() != null);
  assertTrue(result.rawCells().length == NUM_COLS);

  scanner.close();
}
 
Example 4
Source File: CustomerEnrichedInfoHbaseRepo.java    From examples with Apache License 2.0 5 votes vote down vote up
protected void load() throws ClassNotFoundException, SQLException
{
  List<SingleRecord> customerInfoList = Lists.newArrayList();
  try {
    HTable table = store.getTable();
    Scan scan = new Scan();
    ResultScanner scanner = table.getScanner(scan);

    Map<String, String> nameValueMap = new HashMap<String, String>();
    while (true) {
      Result result = scanner.next();
      if (result == null) {
        break;
      }

      nameValueMap.clear();

      String imsi = Bytes.toString(result.getRow());
      nameValueMap.put("imsi", imsi);

      List<Cell> cells = result.listCells();
      for (Cell cell : cells) {
        String columnName = Bytes.toString(CellUtil.cloneQualifier(cell));
        String value = Bytes.toString(CellUtil.cloneValue(cell));
        nameValueMap.put(columnName, value);
      }
      SingleRecord record = new SingleRecord(nameValueMap);

      customerInfoList.add(record);
    }

  } catch (Exception e) {
    e.printStackTrace();
  }

  customerInfoArray = customerInfoList.toArray(new SingleRecord[0]);
}
 
Example 5
Source File: HBaseService.java    From kite with Apache License 2.0 5 votes vote down vote up
/**
 * Wait for the hbase cluster to start up and come online, and then return.
 * 
 * @param hbaseCluster
 *          The hbase cluster to wait for.
 * @throws IOException
 */
private static void waitForHBaseToComeOnline(MiniHBaseCluster hbaseCluster)
    throws IOException, InterruptedException {
  // Wait for the master to be initialized. This is required because even
  // before it's initialized, the regionserver can come online and the meta
  // table can be scannable. If the cluster is quickly shut down after all of
  // this before the master is initialized, it can cause the shutdown to hang
  // indefinitely as initialization tasks will block forever.
  //
  // Unfortunately, no method available to wait for master to come online like
  // regionservers, so we use a while loop with a sleep so we don't hammer the
  // isInitialized method.
  while (!hbaseCluster.getMaster().isInitialized()) {
    Thread.sleep(1000);
  }
  // Now wait for the regionserver to come online.
  hbaseCluster.getRegionServer(0).waitForServerOnline();
  // Don't leave here till we've done a successful scan of the hbase:meta
  // This validates that not only is the regionserver up, but that the
  // meta region is online so there are no race conditions where operations
  // requiring the meta region might run before it's available. Otherwise,
  // operations are susceptible to region not online errors.
  HTable t = new HTable(hbaseCluster.getConf(), HBASE_META_TABLE);
  ResultScanner s = t.getScanner(new Scan());
  while (s.next() != null) {
    continue;
  }
  s.close();
  t.close();
}
 
Example 6
Source File: UpsertValuesIT.java    From phoenix with Apache License 2.0 5 votes vote down vote up
public void testColumnQualifierForUpsertedValues() throws Exception {
    String schemaName = "A";
    String tableName = "TEST";
    String fullTableName = SchemaUtil.getTableName(schemaName, tableName);
    String ddl = "create table " + fullTableName 
            + " (" 
            + " K varchar primary key,"
            + " CF1.V1 varchar, CF2.V2 VARCHAR, CF2.V3 VARCHAR)";
    try (Connection conn = DriverManager.getConnection(getUrl())) {
        conn.createStatement().execute(ddl);
    }
    String dml = "UPSERT INTO " + fullTableName + " VALUES (?, ?, ?, ?)";
    try (Connection conn = DriverManager.getConnection(getUrl())) {
        PreparedStatement stmt = conn.prepareStatement(dml);
        stmt.setString(1, "KEY1");
        stmt.setString(2, "VALUE1");
        stmt.setString(3, "VALUE2");
        stmt.setString(4, "VALUE3");
        stmt.executeUpdate();
        conn.commit();
    }
    // Issue a raw hbase scan and assert that key values have the expected column qualifiers.
    try (Connection conn = DriverManager.getConnection(getUrl())) {
        Table table = conn.unwrap(PhoenixConnection.class).getQueryServices().getTable(Bytes.toBytes(fullTableName));
        ResultScanner scanner = table.getScanner(new Scan());
        Result next = scanner.next();
        assertTrue(next.containsColumn(Bytes.toBytes("CF1"), PInteger.INSTANCE.toBytes(1)));
        assertTrue(next.containsColumn(Bytes.toBytes("CF2"), PInteger.INSTANCE.toBytes(2)));
        assertTrue(next.containsColumn(Bytes.toBytes("CF2"), PInteger.INSTANCE.toBytes(3)));
    }
}
 
Example 7
Source File: TestPartialResultsFromClientSide.java    From hbase with Apache License 2.0 5 votes vote down vote up
public void testPartialResultsReassembly(Scan scanBase) throws Exception {
  Scan partialScan = new Scan(scanBase);
  partialScan.setMaxResultSize(1);
  partialScan.setAllowPartialResults(true);
  ResultScanner partialScanner = TABLE.getScanner(partialScan);

  Scan oneShotScan = new Scan(scanBase);
  oneShotScan.setMaxResultSize(Long.MAX_VALUE);
  ResultScanner oneShotScanner = TABLE.getScanner(oneShotScan);

  ArrayList<Result> partials = new ArrayList<>();
  for (int i = 0; i < NUM_ROWS; i++) {
    Result partialResult = null;
    Result completeResult = null;
    Result oneShotResult = null;
    partials.clear();

    do {
      partialResult = partialScanner.next();
      partials.add(partialResult);
    } while (partialResult != null && partialResult.mayHaveMoreCellsInRow());

    completeResult = Result.createCompleteResult(partials);
    oneShotResult = oneShotScanner.next();

    compareResults(completeResult, oneShotResult, null);
  }

  assertTrue(oneShotScanner.next() == null);
  assertTrue(partialScanner.next() == null);

  oneShotScanner.close();
  partialScanner.close();
}
 
Example 8
Source File: TestVisibilityLabelsWithACL.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Test
public void testScanForUserWithFewerLabelAuthsThanLabelsInScanAuthorizations() throws Throwable {
  String[] auths = { SECRET };
  String user = "user2";
  VisibilityClient.setAuths(TEST_UTIL.getConnection(), auths, user);
  TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
  final Table table = createTableAndWriteDataWithLabels(tableName, SECRET + "&" + CONFIDENTIAL
      + "&!" + PRIVATE, SECRET + "&!" + PRIVATE);
  SecureTestUtil.grantOnTable(TEST_UTIL, NORMAL_USER2.getShortName(), tableName,
    null, null, Permission.Action.READ);
  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();
        assertTrue(!result.isEmpty());
        assertTrue(Bytes.equals(Bytes.toBytes("row2"), result.getRow()));
        result = scanner.next();
        assertNull(result);
      }
      return null;
    }
  };
  NORMAL_USER2.runAs(scanAction);
}
 
Example 9
Source File: VisibilityLabelsWithDeletesTestBase.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Test
public void testVisibilityLabelsWithDeleteFamily() throws Exception {
  setAuths();
  final TableName tableName = TableName.valueOf(testName.getMethodName());
  try (Table table = createTableAndWriteDataWithLabels(SECRET, CONFIDENTIAL + "|" + TOPSECRET)) {
    PrivilegedExceptionAction<Void> actiona = new PrivilegedExceptionAction<Void>() {
      @Override
      public Void run() throws Exception {
        try (Connection connection = ConnectionFactory.createConnection(conf);
          Table table = connection.getTable(tableName)) {
          Delete d = new Delete(row2);
          d.setCellVisibility(new CellVisibility(TOPSECRET + "|" + CONFIDENTIAL));
          d.addFamily(fam);
          table.delete(d);
        } catch (Throwable t) {
          throw new IOException(t);
        }
        return null;
      }
    };
    SUPERUSER.runAs(actiona);

    TEST_UTIL.getAdmin().flush(tableName);
    Scan s = new Scan();
    s.setAuthorizations(new Authorizations(SECRET, PRIVATE, CONFIDENTIAL));
    ResultScanner scanner = table.getScanner(s);
    Result[] next = scanner.next(3);
    assertTrue(next.length == 1);
    CellScanner cellScanner = next[0].cellScanner();
    cellScanner.advance();
    Cell current = cellScanner.current();
    assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(), current.getRowLength(),
      row1, 0, row1.length));
  }
}
 
Example 10
Source File: AclService.java    From Kylin with Apache License 2.0 5 votes vote down vote up
@Override
public List<ObjectIdentity> findChildren(ObjectIdentity parentIdentity) {
    List<ObjectIdentity> oids = new ArrayList<ObjectIdentity>();
    HTableInterface htable = null;
    try {
        htable = HBaseConnection.get(hbaseUrl).getTable(aclTableName);

        Scan scan = new Scan();
        SingleColumnValueFilter parentFilter = new SingleColumnValueFilter(Bytes.toBytes(ACL_INFO_FAMILY), Bytes.toBytes(ACL_INFO_FAMILY_PARENT_COLUMN), CompareOp.EQUAL, domainObjSerializer.serialize(new DomainObjectInfo(parentIdentity)));
        parentFilter.setFilterIfMissing(true);
        scan.setFilter(parentFilter);

        ResultScanner scanner = htable.getScanner(scan);
        for (Result result = scanner.next(); result != null; result = scanner.next()) {
            String id = Bytes.toString(result.getRow());
            String type = Bytes.toString(result.getValue(Bytes.toBytes(ACL_INFO_FAMILY), Bytes.toBytes(ACL_INFO_FAMILY_TYPE_COLUMN)));

            oids.add(new ObjectIdentityImpl(type, id));
        }
    } catch (IOException e) {
        throw new RuntimeException(e.getMessage(), e);
    } finally {
        IOUtils.closeQuietly(htable);
    }

    return oids;
}
 
Example 11
Source File: TestTags.java    From hbase with Apache License 2.0 5 votes vote down vote up
private void result(byte[] fam, byte[] row, byte[] qual, byte[] row2, Table table, byte[] value,
    byte[] value2, byte[] row1, byte[] value1) throws IOException {
  Scan s = new Scan().withStartRow(row);
  // If filters are used this attribute can be specifically check for in
  // filterKV method and
  // kvs can be filtered out if the tags of interest is not found in that kv
  s.setAttribute("visibility", Bytes.toBytes("myTag"));
  ResultScanner scanner = null;
  try {
    scanner = table.getScanner(s);
    Result next = scanner.next();

    assertTrue(Bytes.equals(next.getRow(), row));
    assertTrue(Bytes.equals(next.getValue(fam, qual), value));

    Result next2 = scanner.next();
    assertTrue(next2 != null);
    assertTrue(Bytes.equals(next2.getRow(), row1));
    assertTrue(Bytes.equals(next2.getValue(fam, qual), value1));

    next2 = scanner.next();
    assertTrue(next2 != null);
    assertTrue(Bytes.equals(next2.getRow(), row2));
    assertTrue(Bytes.equals(next2.getValue(fam, qual), value2));

  } finally {
    if (scanner != null)
      scanner.close();
  }
}
 
Example 12
Source File: TestPartialResultsFromClientSide.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Test
public void testMayHaveMoreCellsInRowReturnsTrueAndSetBatch() throws IOException {
  Table table = createTestTable(TableName.valueOf(name.getMethodName()), ROWS, FAMILIES,
      QUALIFIERS, VALUE);
  Scan scan = new Scan();
  scan.setBatch(1);
  scan.setFilter(new FirstKeyOnlyFilter());
  ResultScanner scanner = table.getScanner(scan);
  Result result;
  while ((result = scanner.next()) != null) {
    assertTrue(result.rawCells() != null);
    assertEquals(1, result.rawCells().length);
  }
}
 
Example 13
Source File: TestLoadAndSwitchEncodeOnDisk.java    From hbase with Apache License 2.0 5 votes vote down vote up
private void assertAllOnLine(final Table t) throws IOException {
  List<HRegionLocation> regions;
  try(RegionLocator rl = TEST_UTIL.getConnection().getRegionLocator(t.getName())) {
    regions = rl.getAllRegionLocations();
  }
  for (HRegionLocation e: regions) {
    byte [] startkey = e.getRegion().getStartKey();
    Scan s = new Scan().withStartRow(startkey);
    ResultScanner scanner = t.getScanner(s);
    Result r = scanner.next();
    org.junit.Assert.assertTrue(r != null && r.size() > 0);
    scanner.close();
  }
}
 
Example 14
Source File: StatsEnabledSplitSystemCatalogIT.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private void testUpdatableViewWithIndex(Integer saltBuckets, boolean localIndex) throws Exception {
    String schemaName = TestUtil.DEFAULT_SCHEMA_NAME + "_" + generateUniqueName();
    String tableName = "T_" + generateUniqueName();
    String fullTableName = SchemaUtil.getTableName(schemaName, tableName);
    String viewName = "V_" + generateUniqueName();
    ViewIT.testUpdatableView(fullTableName, viewName, null, null, saltBuckets, tableDDLOptions);
    Pair<String, Scan> pair = ViewIT.testUpdatableViewIndex(fullTableName, saltBuckets, localIndex, viewName);
    Scan scan = pair.getSecond();
    String physicalTableName = pair.getFirst();
    // Confirm that dropping the view also deletes the rows in the index
    if (saltBuckets == null) {
        try (Connection conn = DriverManager.getConnection(getUrl())) {
            Table htable = conn.unwrap(PhoenixConnection.class).getQueryServices()
                    .getTable(Bytes.toBytes(physicalTableName));
            if (ScanUtil.isLocalIndex(scan)) {
                ScanUtil.setLocalIndexAttributes(scan, 0, HConstants.EMPTY_BYTE_ARRAY, HConstants.EMPTY_BYTE_ARRAY,
                        scan.getStartRow(), scan.getStopRow());
            }
            ResultScanner scanner = htable.getScanner(scan);
            Result result = scanner.next();
            // Confirm index has rows
            assertTrue(result != null && !result.isEmpty());

            conn.createStatement().execute("DROP VIEW " + viewName);

            // Confirm index has no rows after view is dropped
            scanner = htable.getScanner(scan);
            result = scanner.next();
            assertTrue(result == null || result.isEmpty());
        }
    }
}
 
Example 15
Source File: TestBaillisAnomaliesWithTXs.java    From phoenix-omid with Apache License 2.0 4 votes vote down vote up
@Test(timeOut = 10_000)
public void testSIPreventsReadSkewUsingWritePredicate(ITestContext context) throws Exception {
    // TX History for G-single:
    // begin; set transaction isolation level repeatable read; -- T1
    // begin; set transaction isolation level repeatable read; -- T2
    // select * from test where id = 1; -- T1. Shows 1 => 10
    // select * from test; -- T2
    // update test set value = 12 where id = 1; -- T2
    // update test set value = 18 where id = 2; -- T2
    // commit; -- T2
    // delete from test where value = 20; -- T1. Prints "ERROR: could not serialize access due to concurrent update"
    // abort; -- T1. There's nothing else we can do, this transaction has failed

    // 0) Start transactions
    TransactionManager tm = newTransactionManager(context);
    TTable txTable = new TTable(connection, TEST_TABLE);
    Transaction tx1 = tm.begin();
    Transaction tx2 = tm.begin();

    // 1) select * from test; -- T1
    assertNumberOfRows(txTable, tx1, 2, new Scan());

    // 2) select * from test; -- T2
    assertNumberOfRows(txTable, tx2, 2, new Scan());

    // 3) update test set value = 12 where id = 1; -- T2
    // 4) update test set value = 18 where id = 2; -- T2
    Put updateRow1Tx2 = new Put(rowId1);
    updateRow1Tx2.addColumn(famName, colName, Bytes.toBytes(12));
    Put updateRow2Tx2 = new Put(rowId2);
    updateRow2Tx2.addColumn(famName, colName, Bytes.toBytes(18));
    txTable.put(tx2, Arrays.asList(updateRow1Tx2, updateRow2Tx2));

    // 5) commit; -- T2
    tm.commit(tx2);

    // 6) delete from test where value = 20; -- T1. Prints
    // "ERROR: could not serialize access due to concurrent update"
    Filter f = new SingleColumnValueFilter(famName, colName, CompareFilter.CompareOp.EQUAL, Bytes.toBytes(20));
    Scan checkFor20 = new Scan();
    checkFor20.setFilter(f);
    ResultScanner checkFor20Scanner = txTable.getScanner(tx1, checkFor20);
    Result res = checkFor20Scanner.next();
    while (res != null) {
        LOG.info("RESSS {}", res);
        LOG.info("Deleting row id {} with value {}", Bytes.toString(res.getRow()), Bytes.toInt(res.getValue(famName, colName)));
        Delete delete20 = new Delete(res.getRow());
        txTable.delete(tx1, delete20);
        res = checkFor20Scanner.next();
    }

    // 7) abort; -- T1
    try {
        tm.commit(tx1);
        fail("Should be aborted");
    } catch (RollbackException e) {
        // Expected
    }

}
 
Example 16
Source File: ScanPerformanceEvaluation.java    From hbase with Apache License 2.0 4 votes vote down vote up
public void testScan() throws IOException {
  Stopwatch tableOpenTimer = Stopwatch.createUnstarted();
  Stopwatch scanOpenTimer = Stopwatch.createUnstarted();
  Stopwatch scanTimer = Stopwatch.createUnstarted();

  tableOpenTimer.start();
  Connection connection = ConnectionFactory.createConnection(getConf());
  Table table = connection.getTable(TableName.valueOf(tablename));
  tableOpenTimer.stop();

  Scan scan = getScan();
  scanOpenTimer.start();
  ResultScanner scanner = table.getScanner(scan);
  scanOpenTimer.stop();

  long numRows = 0;
  long numCells = 0;
  scanTimer.start();
  while (true) {
    Result result = scanner.next();
    if (result == null) {
      break;
    }
    numRows++;

    numCells += result.rawCells().length;
  }
  scanTimer.stop();
  scanner.close();
  table.close();
  connection.close();

  ScanMetrics metrics = scanner.getScanMetrics();
  long totalBytes = metrics.countOfBytesInResults.get();
  double throughput = (double)totalBytes / scanTimer.elapsed(TimeUnit.SECONDS);
  double throughputRows = (double)numRows / scanTimer.elapsed(TimeUnit.SECONDS);
  double throughputCells = (double)numCells / scanTimer.elapsed(TimeUnit.SECONDS);

  System.out.println("HBase scan: ");
  System.out.println("total time to open table: " +
    tableOpenTimer.elapsed(TimeUnit.MILLISECONDS) + " ms");
  System.out.println("total time to open scanner: " +
    scanOpenTimer.elapsed(TimeUnit.MILLISECONDS) + " ms");
  System.out.println("total time to scan: " +
    scanTimer.elapsed(TimeUnit.MILLISECONDS) + " ms");

  System.out.println("Scan metrics:\n" + metrics.getMetricsMap());

  System.out.println("total bytes: " + totalBytes + " bytes ("
      + StringUtils.humanReadableInt(totalBytes) + ")");
  System.out.println("throughput  : " + StringUtils.humanReadableInt((long)throughput) + "B/s");
  System.out.println("total rows  : " + numRows);
  System.out.println("throughput  : " + StringUtils.humanReadableInt((long)throughputRows) + " rows/s");
  System.out.println("total cells : " + numCells);
  System.out.println("throughput  : " + StringUtils.humanReadableInt((long)throughputCells) + " cells/s");
}
 
Example 17
Source File: StoreNullsIT.java    From phoenix with Apache License 2.0 4 votes vote down vote up
private void ensureNullsStoredCorrectly(Connection conn) throws Exception {
    ResultSet rs1 = conn.createStatement().executeQuery("SELECT NAME FROM "+dataTableName);
    rs1.next();
    assertEquals("v1", rs1.getString(1));
    rs1.next();
    assertNull(rs1.getString(1));
    rs1.next();
    Table htable =
            ConnectionFactory.createConnection(getUtility().getConfiguration()).getTable(
                TableName.valueOf(dataTableName));
    Scan s = new Scan();
    s.setRaw(true);
    ResultScanner scanner = htable.getScanner(s);
    // first row has a value for name
    Result rs = scanner.next();
    PTable table = conn.unwrap(PhoenixConnection.class).getTable(new PTableKey(null, dataTableName));
    PColumn nameColumn = table.getColumnForColumnName("NAME");
    byte[] qualifier = table.getImmutableStorageScheme()== ImmutableStorageScheme.SINGLE_CELL_ARRAY_WITH_OFFSETS ? QueryConstants.SINGLE_KEYVALUE_COLUMN_QUALIFIER_BYTES : nameColumn.getColumnQualifierBytes();
    assertTrue(rs.containsColumn(QueryConstants.DEFAULT_COLUMN_FAMILY_BYTES, qualifier));
    assertTrue(rs.size() == 2); // 2 because it also includes the empty key value column
    KeyValueColumnExpression colExpression =
            table.getImmutableStorageScheme() == ImmutableStorageScheme.SINGLE_CELL_ARRAY_WITH_OFFSETS
                    ? new SingleCellColumnExpression(nameColumn, "NAME",
                            table.getEncodingScheme(), table.getImmutableStorageScheme())
                    : new KeyValueColumnExpression(nameColumn);
    ImmutableBytesPtr ptr = new ImmutableBytesPtr();
    colExpression.evaluate(new ResultTuple(rs), ptr);
    assertEquals(new ImmutableBytesPtr(PVarchar.INSTANCE.toBytes("v1")), ptr);
    rs = scanner.next();
    
    if ( !mutable && !columnEncoded // we don't issue a put with empty value for immutable tables with cols stored per key value
            || (mutable && !storeNulls)) { // for this case we use a delete to represent the null
        assertFalse(rs.containsColumn(QueryConstants.DEFAULT_COLUMN_FAMILY_BYTES, qualifier));
        assertEquals(1, rs.size());
    }
    else { 
        assertTrue(rs.containsColumn(QueryConstants.DEFAULT_COLUMN_FAMILY_BYTES, qualifier));
        assertEquals(2, rs.size()); 
    }
    // assert null stored correctly 
    ptr = new ImmutableBytesPtr();
    if (colExpression.evaluate(new ResultTuple(rs), ptr)) {
        assertEquals(new ImmutableBytesPtr(ByteUtil.EMPTY_BYTE_ARRAY), ptr);
    }
    assertNull(scanner.next());
    scanner.close();
    htable.close();
}
 
Example 18
Source File: TestVisibilityLabelsWithDeletes.java    From hbase with Apache License 2.0 4 votes vote down vote up
@Test
public void testDeleteFamilyLatestTimeStampWithMulipleVersions() throws Exception {
  setAuths();
  final TableName tableName = TableName.valueOf(testName.getMethodName());
  try (Table table = doPuts(tableName)) {
    TEST_UTIL.getAdmin().flush(tableName);
    PrivilegedExceptionAction<Void> actiona = new PrivilegedExceptionAction<Void>() {
      @Override
      public Void run() throws Exception {
        try (Connection connection = ConnectionFactory.createConnection(conf);
          Table table = connection.getTable(tableName)) {
          Delete d = new Delete(row1);
          d.setCellVisibility(new CellVisibility(SECRET + "&" + TOPSECRET));
          d.addFamily(fam);
          table.delete(d);
        } catch (Throwable t) {
          throw new IOException(t);
        }
        return null;
      }
    };
    SUPERUSER.runAs(actiona);

    TEST_UTIL.getAdmin().flush(tableName);
    Scan s = new Scan();
    s.readVersions(5);
    s.setAuthorizations(new Authorizations(SECRET, PRIVATE, CONFIDENTIAL, TOPSECRET));
    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));
    assertEquals(127L, current.getTimestamp());
    cellScanner.advance();
    current = cellScanner.current();
    assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(), current.getRowLength(),
      row1, 0, row1.length));
    assertEquals(126L, current.getTimestamp());
    cellScanner = next[1].cellScanner();
    cellScanner.advance();
    current = cellScanner.current();
    assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(), current.getRowLength(),
      row2, 0, row2.length));
  }
}
 
Example 19
Source File: TestUpdateScan.java    From phoenix-omid with Apache License 2.0 4 votes vote down vote up
@Test(timeOut = 10_000)
public void testGet(ITestContext context) throws Exception {
    try {
        TransactionManager tm = newTransactionManager(context);
        TTable table = new TTable(connection, TEST_TABLE);
        Transaction t = tm.begin();
        int[] lInts = new int[]{100, 243, 2342, 22, 1, 5, 43, 56};
        for (int i = 0; i < lInts.length; i++) {
            byte[] data = Bytes.toBytes(lInts[i]);
            Put put = new Put(data);
            put.addColumn(Bytes.toBytes(TEST_FAMILY), Bytes.toBytes(TEST_COL), data);
            table.put(t, put);
        }
        int startKeyValue = lInts[3];
        int stopKeyValue = lInts[3];
        byte[] startKey = Bytes.toBytes(startKeyValue);
        byte[] stopKey = Bytes.toBytes(stopKeyValue);
        Get g = new Get(startKey);
        Result r = table.get(t, g);
        if (!r.isEmpty()) {
            int tmp = Bytes.toInt(r.getValue(Bytes.toBytes(TEST_FAMILY), Bytes.toBytes(TEST_COL)));
            LOG.info("Result:" + tmp);
            assertTrue(tmp == startKeyValue, "Bad value, should be " + startKeyValue + " but is " + tmp);
        } else {
            Assert.fail("Bad result");
        }
        tm.commit(t);

        Scan s = new Scan(startKey);
        CompareFilter.CompareOp op = CompareFilter.CompareOp.LESS_OR_EQUAL;
        RowFilter toFilter = new RowFilter(op, new BinaryPrefixComparator(stopKey));
        boolean startInclusive = true;
        if (!startInclusive) {
            FilterList filters = new FilterList(FilterList.Operator.MUST_PASS_ALL);
            filters.addFilter(new RowFilter(CompareFilter.CompareOp.GREATER, new BinaryPrefixComparator(startKey)));
            filters.addFilter(new WhileMatchFilter(toFilter));
            s.setFilter(filters);
        } else {
            s.setFilter(new WhileMatchFilter(toFilter));
        }
        t = tm.begin();
        ResultScanner res = table.getScanner(t, s);
        Result rr;
        int count = 0;
        while ((rr = res.next()) != null) {
            int iTmp = Bytes.toInt(rr.getValue(Bytes.toBytes(TEST_FAMILY), Bytes.toBytes(TEST_COL)));
            LOG.info("Result: " + iTmp);
            count++;
        }
        assertEquals(count, 1, "Count is wrong");
        LOG.info("Rows found " + count);
        tm.commit(t);
        table.close();
    } catch (Exception e) {
        LOG.error("Exception in test", e);
    }
}
 
Example 20
Source File: TestVisibilityLabelsWithDeletes.java    From hbase with Apache License 2.0 4 votes vote down vote up
@Test
public void testDeleteFamilySpecificTimeStampWithMulipleVersions() throws Exception {
  setAuths();
  final TableName tableName = TableName.valueOf(testName.getMethodName());
  try (Table table = doPuts(tableName)) {
    TEST_UTIL.getAdmin().flush(tableName);
    PrivilegedExceptionAction<Void> actiona = new PrivilegedExceptionAction<Void>() {
      @Override
      public Void run() throws Exception {
        try (Connection connection = ConnectionFactory.createConnection(conf);
          Table table = connection.getTable(tableName)) {
          Delete d = new Delete(row1);
          d.setCellVisibility(new CellVisibility(
              "(" + PRIVATE + "&" + CONFIDENTIAL + ")|(" + SECRET + "&" + TOPSECRET + ")"));
          d.addFamily(fam, 126L);
          table.delete(d);
        } catch (Throwable t) {
          throw new IOException(t);
        }
        return null;
      }
    };
    SUPERUSER.runAs(actiona);

    TEST_UTIL.getAdmin().flush(tableName);
    Scan s = new Scan();
    s.readVersions(5);
    s.setAuthorizations(new Authorizations(SECRET, PRIVATE, CONFIDENTIAL, TOPSECRET));
    ResultScanner scanner = table.getScanner(s);
    Result[] next = scanner.next(6);
    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));
    assertEquals(127L, current.getTimestamp());
    cellScanner.advance();
    current = cellScanner.current();
    assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(), current.getRowLength(),
      row1, 0, row1.length));
    assertEquals(125L, current.getTimestamp());
    cellScanner.advance();
    current = cellScanner.current();
    assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(), current.getRowLength(),
      row1, 0, row1.length));
    assertEquals(123L, current.getTimestamp());
    cellScanner = next[1].cellScanner();
    cellScanner.advance();
    current = cellScanner.current();
    assertTrue(Bytes.equals(current.getRowArray(), current.getRowOffset(), current.getRowLength(),
      row2, 0, row2.length));
  }
}