Java Code Examples for org.apache.hadoop.hbase.client.HTable#getScanner()

The following examples show how to use org.apache.hadoop.hbase.client.HTable#getScanner() . 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: HBaseTestCase.java    From aliyun-maxcompute-data-collectors with Apache License 2.0 6 votes vote down vote up
protected int countHBaseTable(String tableName, String colFamily)
    throws IOException {
  int count = 0;
  HTable table = new HTable(new Configuration(
      hbaseTestUtil.getConfiguration()), Bytes.toBytes(tableName));
  try {
    ResultScanner scanner = table.getScanner(Bytes.toBytes(colFamily));
    for(Result result = scanner.next();
        result != null;
        result = scanner.next()) {
      count++;
    }
  } finally {
    table.close();
  }
  return count;
}
 
Example 2
Source File: ImmutableIndexIT.java    From phoenix with Apache License 2.0 6 votes vote down vote up
public static boolean verifyRowsForEmptyColValue(Connection conn, String tableName, byte[] valueBytes)
        throws IOException, SQLException {
    PTable table = PhoenixRuntime.getTable(conn, tableName);
    byte[] emptyCF = SchemaUtil.getEmptyColumnFamily(table);
    byte[] emptyCQ = EncodedColumnsUtil.getEmptyKeyValueInfo(table).getFirst();
    HTable htable = (HTable) conn.unwrap(PhoenixConnection.class).getQueryServices().getTable(table.getPhysicalName().getBytes());
    Scan scan = new Scan();
    scan.addColumn(emptyCF, emptyCQ);
    ResultScanner resultScanner = htable.getScanner(scan);

    for (Result result = resultScanner.next(); result != null; result = resultScanner.next()) {
        if (Bytes.compareTo(result.getValue(emptyCF, emptyCQ), 0, valueBytes.length,
                valueBytes, 0, valueBytes.length) != 0) {
            return false;
        }
    }
    return true;
}
 
Example 3
Source File: HbaseAdapter.java    From canal with Apache License 2.0 6 votes vote down vote up
@Override
public Map<String, Object> count(String task) {
    MappingConfig config = hbaseMapping.get(task);
    String hbaseTable = config.getHbaseMapping().getHbaseTable();
    long rowCount = 0L;
    try {
        HTable table = (HTable) hbaseTemplate.getConnection().getTable(TableName.valueOf(hbaseTable));
        Scan scan = new Scan();
        scan.setFilter(new FirstKeyOnlyFilter());
        ResultScanner resultScanner = table.getScanner(scan);
        for (Result result : resultScanner) {
            rowCount += result.size();
        }
    } catch (IOException e) {
        logger.error(e.getMessage(), e);
    }
    Map<String, Object> res = new LinkedHashMap<>();
    res.put("hbaseTable", hbaseTable);
    res.put("count", rowCount);
    return res;
}
 
Example 4
Source File: IndexTestingUtils.java    From phoenix with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Verify the state of the index table between the given key and time ranges against the list of
 * expected keyvalues.
 * @throws IOException
 */
@SuppressWarnings("javadoc")
public static void verifyIndexTableAtTimestamp(HTable index1, List<KeyValue> expected,
    long start, long end, byte[] startKey, byte[] endKey) throws IOException {
  LOG.debug("Scanning " + Bytes.toString(index1.getTableName()) + " between times (" + start
      + ", " + end + "] and keys: [" + Bytes.toString(startKey) + ", " + Bytes.toString(endKey)
      + "].");
  Scan s = new Scan(startKey, endKey);
  // s.setRaw(true);
  s.setMaxVersions();
  s.setTimeRange(start, end);
  List<KeyValue> received = new ArrayList<KeyValue>();
  ResultScanner scanner = index1.getScanner(s);
  for (Result r : scanner) {
    received.addAll(r.list());
    LOG.debug("Received: " + r.list());
  }
  scanner.close();
  assertEquals("Didn't get the expected kvs from the index table!", expected, received);
}
 
Example 5
Source File: ImmutableIndexExtendedIT.java    From phoenix with Apache License 2.0 6 votes vote down vote up
private static int getRowCountForEmptyColValue(Connection conn, String tableName,
        byte[] valueBytes)  throws IOException, SQLException {

    PTable table = PhoenixRuntime.getTable(conn, tableName);
    byte[] emptyCF = SchemaUtil.getEmptyColumnFamily(table);
    byte[] emptyCQ = EncodedColumnsUtil.getEmptyKeyValueInfo(table).getFirst();
    ConnectionQueryServices queryServices =
            conn.unwrap(PhoenixConnection.class).getQueryServices();
    HTable htable = (HTable) queryServices.getTable(table.getPhysicalName().getBytes());
    Scan scan = new Scan();
    scan.addColumn(emptyCF, emptyCQ);
    ResultScanner resultScanner = htable.getScanner(scan);
    int count = 0;

    for (Result result = resultScanner.next(); result != null; result = resultScanner.next()) {
        if (Bytes.compareTo(result.getValue(emptyCF, emptyCQ), 0, valueBytes.length,
                valueBytes, 0, valueBytes.length) == 0) {
            ++count;
        }
    }
    return count;
}
 
Example 6
Source File: HBaseTable.java    From wifi with Apache License 2.0 5 votes vote down vote up
public static void scan() throws Exception {
	HTable table = new HTable(cfg, tableName);
	Scan s = new Scan();
	ResultScanner rs = table.getScanner(s);
	for (Result r : rs) {
		System.out.println("Scan: " + r);
	}
}
 
Example 7
Source File: HbaseTestCase.java    From wifi with Apache License 2.0 5 votes vote down vote up
public static void scan(String tablename) throws Exception{
     HTable table = new HTable(cfg, tablename);
     Scan s = new Scan();
     ResultScanner rs = table.getScanner(s);
     for(Result r:rs){
         System.out.println("Scan: "+r);
     }
}
 
Example 8
Source File: TestHBaseStorage.java    From spork with Apache License 2.0 5 votes vote down vote up
/**
 * load from hbase 'TESTTABLE_1' using HBaseBinary format, and store it into
 * 'TESTTABLE_2' using HBaseBinaryFormat
 *
 * @throws IOException
 */
@Test
public void testStoreToHBase_1() throws IOException {
    prepareTable(TESTTABLE_1, true, DataFormat.HBaseBinary);
    prepareTable(TESTTABLE_2, false, DataFormat.HBaseBinary);

    pig.getPigContext().getProperties()
            .setProperty(MRConfiguration.FILEOUTPUTCOMMITTER_MARKSUCCESSFULJOBS, "true");

    scanTable1(pig, DataFormat.HBaseBinary);
    pig.store("a", "hbase://" +  TESTTABLE_2,
            "org.apache.pig.backend.hadoop.hbase.HBaseStorage('"
            + TESTCOLUMN_A + " " + TESTCOLUMN_B + " "
            + TESTCOLUMN_C + "','-caster HBaseBinaryConverter')");
    HTable table = new HTable(conf, TESTTABLE_2);
    ResultScanner scanner = table.getScanner(new Scan());
    Iterator<Result> iter = scanner.iterator();
    int i = 0;
    for (i = 0; iter.hasNext(); ++i) {
        Result result = iter.next();
        String v = i + "";
        String rowKey = Bytes.toString(result.getRow());
        int col_a = Bytes.toInt(getColValue(result, TESTCOLUMN_A));
        double col_b = Bytes.toDouble(getColValue(result, TESTCOLUMN_B));
        String col_c = Bytes.toString(getColValue(result, TESTCOLUMN_C));

        Assert.assertEquals("00".substring(v.length()) + v, rowKey);
        Assert.assertEquals(i, col_a);
        Assert.assertEquals(i + 0.0, col_b, 1e-6);
        Assert.assertEquals("Text_" + i, col_c);
    }
    Assert.assertEquals(100, i);

    pig.getPigContext().getProperties()
            .setProperty(MRConfiguration.FILEOUTPUTCOMMITTER_MARKSUCCESSFULJOBS, "false");
    table.close();
}
 
Example 9
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 10
Source File: LocalIndexIT.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Test
public void testPutsToLocalIndexTable() throws Exception {
    createBaseTable(TestUtil.DEFAULT_DATA_TABLE_NAME, null, "('e','i','o')");
    Connection conn1 = DriverManager.getConnection(getUrl());
    conn1.createStatement().execute("CREATE LOCAL INDEX " + TestUtil.DEFAULT_INDEX_TABLE_NAME + " ON " + TestUtil.DEFAULT_DATA_TABLE_NAME + "(v1)");
    conn1.createStatement().execute("UPSERT INTO "+TestUtil.DEFAULT_DATA_TABLE_NAME+" values('b',1,2,4,'z')");
    conn1.createStatement().execute("UPSERT INTO "+TestUtil.DEFAULT_DATA_TABLE_NAME+" values('f',1,2,3,'z')");
    conn1.createStatement().execute("UPSERT INTO "+TestUtil.DEFAULT_DATA_TABLE_NAME+" values('j',2,4,2,'a')");
    conn1.createStatement().execute("UPSERT INTO "+TestUtil.DEFAULT_DATA_TABLE_NAME+" values('q',3,1,1,'c')");
    conn1.commit();
    ResultSet rs = conn1.createStatement().executeQuery("SELECT COUNT(*) FROM " + TestUtil.DEFAULT_INDEX_TABLE_NAME);
    assertTrue(rs.next());
    assertEquals(4, rs.getInt(1));
    HBaseAdmin admin = driver.getConnectionQueryServices(getUrl(), TestUtil.TEST_PROPERTIES).getAdmin();
    HTable indexTable = new HTable(admin.getConfiguration() ,TableName.valueOf(MetaDataUtil.getLocalIndexTableName(TestUtil.DEFAULT_DATA_TABLE_NAME)));
    Pair<byte[][], byte[][]> startEndKeys = indexTable.getStartEndKeys();
    byte[][] startKeys = startEndKeys.getFirst();
    byte[][] endKeys = startEndKeys.getSecond();
    for (int i = 0; i < startKeys.length; i++) {
        Scan s = new Scan();
        s.setStartRow(startKeys[i]);
        s.setStopRow(endKeys[i]);
        ResultScanner scanner = indexTable.getScanner(s);
        int count = 0;
        for(Result r:scanner){
            count++;
        }
        scanner.close();
        assertEquals(1, count);
    }
    indexTable.close();
}
 
Example 11
Source File: LocalIndexIT.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Test
public void testBuildIndexWhenUserTableAlreadyHasData() throws Exception {
    createBaseTable(TestUtil.DEFAULT_DATA_TABLE_NAME, null, "('e','i','o')");
    Connection conn1 = DriverManager.getConnection(getUrl());
    conn1.createStatement().execute("UPSERT INTO "+TestUtil.DEFAULT_DATA_TABLE_NAME+" values('b',1,2,4,'z')");
    conn1.createStatement().execute("UPSERT INTO "+TestUtil.DEFAULT_DATA_TABLE_NAME+" values('f',1,2,3,'z')");
    conn1.createStatement().execute("UPSERT INTO "+TestUtil.DEFAULT_DATA_TABLE_NAME+" values('j',2,4,2,'a')");
    conn1.createStatement().execute("UPSERT INTO "+TestUtil.DEFAULT_DATA_TABLE_NAME+" values('q',3,1,1,'c')");
    conn1.commit();
    conn1.createStatement().execute("CREATE LOCAL INDEX " + TestUtil.DEFAULT_INDEX_TABLE_NAME + " ON " + TestUtil.DEFAULT_DATA_TABLE_NAME + "(v1)");
    ResultSet rs = conn1.createStatement().executeQuery("SELECT COUNT(*) FROM " + TestUtil.DEFAULT_INDEX_TABLE_NAME);
    assertTrue(rs.next());
    assertEquals(4, rs.getInt(1));
    HBaseAdmin admin = driver.getConnectionQueryServices(getUrl(), TestUtil.TEST_PROPERTIES).getAdmin();
    HTable indexTable = new HTable(admin.getConfiguration() ,TableName.valueOf(MetaDataUtil.getLocalIndexTableName(TestUtil.DEFAULT_DATA_TABLE_NAME)));
    Pair<byte[][], byte[][]> startEndKeys = indexTable.getStartEndKeys();
    byte[][] startKeys = startEndKeys.getFirst();
    byte[][] endKeys = startEndKeys.getSecond();
    for (int i = 0; i < startKeys.length; i++) {
        Scan s = new Scan();
        s.setStartRow(startKeys[i]);
        s.setStopRow(endKeys[i]);
        ResultScanner scanner = indexTable.getScanner(s);
        int count = 0;
        for(Result r:scanner){
            count++;
        }
        scanner.close();
        assertEquals(1, count);
    }
    indexTable.close();
}
 
Example 12
Source File: LocalIndexIT.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Test
public void testDropLocalIndexShouldDeleteDataFromLocalIndexTable() throws Exception {
    createBaseTable(TestUtil.DEFAULT_DATA_TABLE_NAME, null, "('e','i','o')");
    Connection conn1 = DriverManager.getConnection(getUrl());
    try {
        conn1.createStatement().execute("UPSERT INTO " + TestUtil.DEFAULT_DATA_TABLE_NAME + " values('b',1,2,4,'z')");
        conn1.createStatement().execute("UPSERT INTO " + TestUtil.DEFAULT_DATA_TABLE_NAME + " values('f',1,2,3,'a')");
        conn1.createStatement().execute("UPSERT INTO " + TestUtil.DEFAULT_DATA_TABLE_NAME + " values('j',2,4,2,'a')");
        conn1.createStatement().execute("UPSERT INTO " + TestUtil.DEFAULT_DATA_TABLE_NAME + " values('q',3,1,1,'c')");
        conn1.commit();
        conn1.createStatement().execute("CREATE LOCAL INDEX " + TestUtil.DEFAULT_INDEX_TABLE_NAME + " ON " + TestUtil.DEFAULT_DATA_TABLE_NAME + "(v1)");
        conn1.createStatement().execute("DROP INDEX " + TestUtil.DEFAULT_INDEX_TABLE_NAME + " ON " + TestUtil.DEFAULT_DATA_TABLE_NAME);
        HBaseAdmin admin = driver.getConnectionQueryServices(getUrl(), TestUtil.TEST_PROPERTIES).getAdmin();
        HTable indexTable = new HTable(admin.getConfiguration() ,TableName.valueOf(MetaDataUtil.getLocalIndexTableName(TestUtil.DEFAULT_DATA_TABLE_NAME)));
        Pair<byte[][], byte[][]> startEndKeys = indexTable.getStartEndKeys();
        byte[][] startKeys = startEndKeys.getFirst();
        byte[][] endKeys = startEndKeys.getSecond();
        // No entry should be present in local index table after drop index.
        for (int i = 0; i < startKeys.length; i++) {
            Scan s = new Scan();
            s.setStartRow(startKeys[i]);
            s.setStopRow(endKeys[i]);
            ResultScanner scanner = indexTable.getScanner(s);
            int count = 0;
            for(Result r:scanner){
                count++;
            }
            scanner.close();
            assertEquals(0, count);
        }
        indexTable.close();
    } finally {
        conn1.close();
    }
}
 
Example 13
Source File: TestHBaseStorage.java    From spork with Apache License 2.0 5 votes vote down vote up
/**
 * load from hbase 'TESTTABLE_1' using HBaseBinary format, and store it into
 * 'TESTTABLE_2' using UTF-8 Plain Text format
 *
 * @throws IOException
 */
@Test
public void testStoreToHBase_2() throws IOException {
    prepareTable(TESTTABLE_1, true, DataFormat.HBaseBinary);
    prepareTable(TESTTABLE_2, false, DataFormat.HBaseBinary);
    scanTable1(pig, DataFormat.HBaseBinary);
    pig.store("a", TESTTABLE_2,
            "org.apache.pig.backend.hadoop.hbase.HBaseStorage('"
            + TESTCOLUMN_A + " " + TESTCOLUMN_B + " "
            + TESTCOLUMN_C + "')");

    HTable table = new HTable(conf, TESTTABLE_2);
    ResultScanner scanner = table.getScanner(new Scan());
    Iterator<Result> iter = scanner.iterator();
    int i = 0;
    for (i = 0; iter.hasNext(); ++i) {
        Result result = iter.next();
        String v = i + "";
        String rowKey = new String(result.getRow());
        int col_a = Integer.parseInt(new String(getColValue(result, TESTCOLUMN_A)));
        double col_b = Double.parseDouble(new String(getColValue(result, TESTCOLUMN_B)));
        String col_c = new String(getColValue(result, TESTCOLUMN_C));

        Assert.assertEquals("00".substring(v.length()) + v, rowKey);
        Assert.assertEquals(i, col_a);
        Assert.assertEquals(i + 0.0, col_b, 1e-6);
        Assert.assertEquals("Text_" + i, col_c);
    }
    Assert.assertEquals(100, i);
    table.close();
}
 
Example 14
Source File: HBaseTestClusterUtil.java    From tajo with Apache License 2.0 5 votes vote down vote up
public void startHBaseCluster() throws Exception {
  if (zkCluster == null) {
    startMiniZKCluster();
  }
  if (hbaseCluster != null) {
    return;
  }

  System.setProperty("HBASE_ZNODE_FILE", testBaseDir + "/hbase_znode_file");
  if (conf.getInt(ServerManager.WAIT_ON_REGIONSERVERS_MINTOSTART, -1) == -1) {
    conf.setInt(ServerManager.WAIT_ON_REGIONSERVERS_MINTOSTART, 1);
  }
  if (conf.getInt(ServerManager.WAIT_ON_REGIONSERVERS_MAXTOSTART, -1) == -1) {
    conf.setInt(ServerManager.WAIT_ON_REGIONSERVERS_MAXTOSTART, 1);
  }
  conf.setBoolean(HConstants.REPLICATION_ENABLE_KEY, false);
  createRootDir();

  Configuration c = HBaseConfiguration.create(this.conf);
  // randomize hbase info port
  c.setInt(HConstants.MASTER_INFO_PORT, 0);

  hbaseCluster = new MiniHBaseCluster(c, 1);

  // Don't leave here till we've done a successful scan of the hbase:meta
  HTable t = new HTable(c, TableName.META_TABLE_NAME);
  ResultScanner s = t.getScanner(new Scan());
  while (s.next() != null) {
    continue;
  }
  s.close();
  t.close();
  LOG.info("MiniHBaseCluster started");

}
 
Example 15
Source File: GenerateTestingHTables.java    From SpyGlass with Apache License 2.0 5 votes vote down vote up
/**
 * Method to print-out an HTable
 */
private static void printHTable(TestingTable testingTable)
		throws IOException {

	HTable table = new HTable(config, testingTable.name());

	Scan s = new Scan();
	// Let scanner know which columns we are interested in
	ResultScanner scanner = table.getScanner(s);

	LOG.info("Printing HTable: " + Bytes.toString(table.getTableName()));

	try {
		// Iterate results
		for (Result rr = scanner.next(); rr != null; rr = scanner.next()) {
			String key = Bytes.toString(rr.getRow());
			Iterator<KeyValue> iter = rr.list().iterator();

			String header = "Key:\t";
			String data = key + "\t";

			while (iter.hasNext()) {
				KeyValue kv = iter.next();
				header += Bytes.toString(kv.getFamily()) + ":"
						+ Bytes.toString(kv.getQualifier()) + "\t";
				data += Bytes.toString(kv.getValue()) + "\t";
			}

			LOG.info(header);
			LOG.info(data);
		}
		System.out.println();
	} finally {
		// Make sure you close your scanners when you are done!
		// Thats why we have it inside a try/finally clause
		scanner.close();
		table.close();
	}
}
 
Example 16
Source File: TestHBaseStorage.java    From spork with Apache License 2.0 5 votes vote down vote up
/**
 * load from hbase 'TESTTABLE_1' using HBaseBinary format, and store it into
 * 'TESTTABLE_2' using HBaseBinaryFormat projecting out column c
 *
 * @throws IOException
 */
@Test
public void testStoreToHBase_1_with_projection() throws IOException {
    System.getProperties().setProperty("pig.usenewlogicalplan", "false");
    prepareTable(TESTTABLE_1, true, DataFormat.HBaseBinary);
    prepareTable(TESTTABLE_2, false, DataFormat.HBaseBinary);
    scanTable1(pig, DataFormat.HBaseBinary);
    pig.registerQuery("b = FOREACH a GENERATE rowKey, col_a, col_b;");
    pig.store("b",  TESTTABLE_2,
            "org.apache.pig.backend.hadoop.hbase.HBaseStorage('"
            + TESTCOLUMN_A + " " + TESTCOLUMN_B +
            "','-caster HBaseBinaryConverter')");

    HTable table = new HTable(conf, TESTTABLE_2);
    ResultScanner scanner = table.getScanner(new Scan());
    Iterator<Result> iter = scanner.iterator();
    int i = 0;
    for (i = 0; iter.hasNext(); ++i) {
        Result result = iter.next();
        String v = String.valueOf(i);
        String rowKey = Bytes.toString(result.getRow());
        int col_a = Bytes.toInt(getColValue(result, TESTCOLUMN_A));
        double col_b = Bytes.toDouble(getColValue(result, TESTCOLUMN_B));

        Assert.assertEquals("00".substring(v.length()) + v, rowKey);
        Assert.assertEquals(i, col_a);
        Assert.assertEquals(i + 0.0, col_b, 1e-6);
    }
    Assert.assertEquals(100, i);
    table.close();
}
 
Example 17
Source File: TestHBaseStorage.java    From spork with Apache License 2.0 5 votes vote down vote up
/**
 * load from hbase 'TESTTABLE_1' using HBaseBinary format, and store it into
 * 'TESTTABLE_2' using UTF-8 Plain Text format projecting column c
 *
 * @throws IOException
 */
@Test
public void testStoreToHBase_2_with_projection() throws IOException {
    prepareTable(TESTTABLE_1, true, DataFormat.HBaseBinary);
    prepareTable(TESTTABLE_2, false, DataFormat.UTF8PlainText);
    scanTable1(pig, DataFormat.HBaseBinary);
    pig.registerQuery("b = FOREACH a GENERATE rowKey, col_a, col_b;");
    pig.store("b", TESTTABLE_2,
            "org.apache.pig.backend.hadoop.hbase.HBaseStorage('"
            + TESTCOLUMN_A + " " + TESTCOLUMN_B + "')");

    HTable table = new HTable(conf, TESTTABLE_2);
    ResultScanner scanner = table.getScanner(new Scan());
    Iterator<Result> iter = scanner.iterator();
    int i = 0;
    for (i = 0; iter.hasNext(); ++i) {
        Result result = iter.next();
        String v = i + "";
        String rowKey = new String(result.getRow());
        int col_a = Integer.parseInt(new String(getColValue(result, TESTCOLUMN_A)));
        double col_b = Double.parseDouble(new String(getColValue(result, TESTCOLUMN_B)));

        Assert.assertEquals("00".substring(v.length()) + v, rowKey);
        Assert.assertEquals(i, col_a);
        Assert.assertEquals(i + 0.0, col_b, 1e-6);
    }
    Assert.assertEquals(100, i);
    table.close();
}
 
Example 18
Source File: TestHBaseStorage.java    From spork with 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 19
Source File: BaseIndexIT.java    From phoenix with Apache License 2.0 4 votes vote down vote up
@Test
public void testUpsertAfterIndexDrop() throws Exception {
    Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
    String tableName = "TBL_" + generateUniqueName();
    String indexName = "IND_" + generateUniqueName();
    String fullTableName = SchemaUtil.getTableName(TestUtil.DEFAULT_SCHEMA_NAME, tableName);
    String fullIndexName = SchemaUtil.getTableName(TestUtil.DEFAULT_SCHEMA_NAME, indexName);
    try (Connection conn = DriverManager.getConnection(getUrl(), props)) {
        conn.setAutoCommit(false);
        String query;
        ResultSet rs;
        // make sure that the tables are empty, but reachable
        conn.createStatement().execute(
                "CREATE TABLE " + fullTableName
                + " (k VARCHAR NOT NULL PRIMARY KEY, v1 VARCHAR, v2 VARCHAR)" + tableDDLOptions);
        query = "SELECT * FROM " + fullTableName;
        rs = conn.createStatement().executeQuery(query);
        assertFalse(rs.next());

        conn.createStatement().execute(
                "CREATE " + (localIndex ? "LOCAL " : "") + "INDEX " + indexName + " ON " + fullTableName + " (v1, v2)");
        query = "SELECT * FROM " + fullIndexName;
        rs = conn.createStatement().executeQuery(query);
        assertFalse(rs.next());

        // load some data into the table
        PreparedStatement stmt =
                conn.prepareStatement("UPSERT INTO " + fullTableName + " VALUES(?,?,?)");
        stmt.setString(1, "a");
        stmt.setString(2, "x");
        stmt.setString(3, "1");
        stmt.execute();
        conn.commit();

        // make sure the index is working as expected
        query = "SELECT * FROM " + fullIndexName;
        rs = conn.createStatement().executeQuery(query);
        assertTrue(rs.next());
        assertEquals("x", rs.getString(1));
        assertEquals("1", rs.getString(2));
        assertEquals("a", rs.getString(3));
        assertFalse(rs.next());

        String ddl = "DROP INDEX " + indexName + " ON " + fullTableName;
        conn.createStatement().execute(ddl);

        stmt = conn.prepareStatement("UPSERT INTO " + fullTableName + "(k, v1) VALUES(?,?)");
        if (mutable) {
         stmt.setString(1, "a");
         stmt.setString(2, "y");
         stmt.execute();
         conn.commit();
        }
        stmt.setString(1, "b");
        stmt.setString(2, "x");
        stmt.execute();
        conn.commit();
         
        // the index table is one row
        HTable table = (HTable) conn.unwrap(PhoenixConnection.class).getQueryServices().getTable(fullTableName.getBytes());
        ResultScanner resultScanner = table.getScanner(new Scan());
        for (Result result : resultScanner) {
        	System.out.println(result);
        }
        resultScanner.close();
        table.close();

        query = "SELECT * FROM " + fullTableName;

        // check that the data table matches as expected
        rs = conn.createStatement().executeQuery(query);
        assertTrue(rs.next());
        assertEquals("a", rs.getString(1));
        assertEquals(mutable ? "y" : "x", rs.getString(2));
        assertEquals("1", rs.getString(3));
        assertTrue(rs.next());
        assertEquals("b", rs.getString(1));
        assertEquals("x", rs.getString(2));
        assertNull(rs.getString(3));
        assertFalse(rs.next());
        }
    }
 
Example 20
Source File: TestClientKeyValue.java    From phoenix with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private Result getSingleScanResult(HTable ht, Scan scan) throws IOException {
  ResultScanner scanner = ht.getScanner(scan);
  Result result = scanner.next();
  scanner.close();
  return result;
}