1
votes

In our project we have a requirement, we will have to make multiple PreFix scan and query BigTable.

For example, Reference the below rowKey of a BigTable

1st 4 rows have prefix 9JNzZAGX, 
2nd 3 rows have prefix sRbfH5fW,
3rd 1 rows have prefix PnQvPYtA,
4th 2 rows have prefix C7M5fjUg,
9JNzZAGX-hkncRBPb
9JNzZAGX-gFfXvVxx
9JNzZAGX-saQaP62S
9JNzZAGX-S5prLFns

sRbfH5fW-PLez7PF5
sRbfH5fW-Pg5PJjuq
sRbfH5fW-7HfgXgJe

PnQvPYtA-UUNC4mhw

C7M5fjUg-6nvM2ReV
C7M5fjUg-hSpQungj

If I have to fetch the rows with starting with prefix sRbfH5fW and C7M5fjUg, below 5 rows need to be returned.

sRbfH5fW-PLez7PF5
sRbfH5fW-Pg5PJjuq
sRbfH5fW-7HfgXgJe
C7M5fjUg-6nvM2ReV
C7M5fjUg-hSpQungj

If there a way using Java APIs I can fetch them in single call to DB. This prefix LKist can be in 100s thus parallel or sequential search in code for each does not sound good option.

2

2 Answers

0
votes

do something like this:

    // Creates the settings to configure a bigtable data client.

      BigtableDataSettings settings=BigtableDataSettings.newBuilder().setProjectId(projectId).setInstanceId(instanceId).build();

    // Creates a bigtable data client.

      dataClient = BigtableDataClient.create(settings);
// read row using readRow() method
      Row row = dataClient.readRow(tableId, ROW_KEY_PREFIX);
      System.out.println("Row: " + row.getKey().toStringUtf8());
      for (RowCell cell : row.getCells()) {
        System.out.printf(
            "Family: %s    Qualifier: %s    Value: %s%n",
            cell.getFamily(), cell.getQualifier().toStringUtf8(), cell.getValue().toStringUtf8());
      }

check this page for more details

here you can find a full sample code.

Note: I have noticed that your row prefixes are hashed values which is not recommended anymore becasue It results in row keys that are basically meaningless, which makes it challenging to use the Key Visualizer tool to troubleshoot issues with Cloud Bigtable.

0
votes

For one prefix scan you would use the built in prefix scan which you seem to know:

  Query query = Query.create(tableId).prefix("phone");
  ServerStream<Row> rows = dataClient.readRows(query);

You can actually just call prefix again on the query and it will add more prefixes, which should be super easy for your use case:

try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
  Query query =
      Query.create(tableId)
          .prefix("9JNzZAGX")
          .prefix("sRbfH5fW")
          .prefix("PnQvPYtA")
          .prefix("C7M5fjUg");
  ServerStream<Row> rows = dataClient.readRows(query);
  for (Row row : rows) {
   // DO SOMETHING
  }
} catch (IOException e) {
  System.out.println(
      "Unable to initialize service client, as a network error occurred: \n" + e.toString());
}