【问题标题】:Google Cloud Bigtable: Query multiple prefix Scan in JavaGoogle Cloud Bigtable:用Java查询多个前缀扫描
【发布时间】:2020-04-14 23:51:52
【问题描述】:

在我们的项目中,我们有一个需求,我们将要进行多次 PreFix 扫描和查询 BigTable。

例如,引用下面的 BigTable 的 rowKey

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

如果我必须获取以前缀 sRbfH5fW 和 C7M5fjUg 开头的行,则需要返回 5 行以下。

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

如果有办法使用 Java API,我可以在一次调用 DB 中获取它们。这个前缀 LKist 可以是 100 秒,因此在代码中对每个前缀进行并行或顺序搜索听起来不是很好的选择。

【问题讨论】:

    标签: hbase google-cloud-bigtable


    【解决方案1】:

    做这样的事情:

        // 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());
          }
    

    检查这个page for more details

    here你可以找到完整的示例代码。

    注意:我注意到您的行前缀是hashed values,不再推荐,因为它导致行键基本上没有意义,这使得使用 Key Visualizer 工具排查 Cloud Bigtable 问题变得具有挑战性。

    【讨论】:

    • 您能否告诉我,如何在一次调用中获取多个前缀。这是主要问题。
    • 您不能同时查询多个行键前缀。你必须为每一行前缀建立不同的调用。如果您想这样做,那么您应该考虑更改 Bigtable 表的架构,以帮助您更有效地获取所需的数据。检查此documentation 了解更多信息。这里的关键是找到您尝试查询的这些数据之间的共同点。
    【解决方案2】:

    对于一个前缀扫描,您将使用您似乎知道的内置前缀扫描:

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

    您实际上可以在查询中再次调用前缀,它会添加更多前缀,这对您的用例来说应该非常容易:

    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());
    }
    

    【讨论】:

      猜你喜欢
      • 2019-01-26
      • 2018-04-05
      • 2019-10-13
      • 2018-12-11
      • 2018-11-03
      • 2020-05-26
      • 2016-11-23
      • 1970-01-01
      • 2017-04-08
      相关资源
      最近更新 更多