在Apache Beam数据流中使用Spanner

时间:2018-07-19 08:48:59

标签: google-cloud-platform google-cloud-dataflow apache-beam google-cloud-spanner

我正在尝试在Apache Beam ParDo(DoFn)中添加Spanner连接。我需要查找一些行作为ParDo的一部分。数据流创建了许多工作线程(通常最多4个),并且我使用startBundle和finishBundle方法打开和关闭工作线程的扳手连接。然后在processElement方法中,我对通过DatabaseClient并使用singleUseReadOnlyTransaction的每个项目执行查找。

我应该添加它正在GCP下作为数据流运行

一些代码来说明这一点。

private static CustomDoFn<String, TransactionImport> processRow = new CustomDoFn<String, TransactionImport>(){
    private static final long serialVersionUID = 1L;

    private Spanner spanner = null;
    private DatabaseClient dbClient = null;

    @StartBundle
    public void startBundle(StartBundleContext c){
      TransactionFileOptions options = c.getPipelineOptions().as(TransactionFileOptions.class);

      com.google.cloud.spanner.SpannerOptions spannerOptions = com.google.cloud.spanner.SpannerOptions.newBuilder().build();
      spanner = spannerOptions.getService();
      String spannerProjectID = options.getSpannerProjectId();
      String spannerInstanceID = options.getSpannerInstanceId();
      String spannerDatabaseID = options.getSpannerDatabaseId();

      DatabaseId db = DatabaseId.of(spannerProjectID, spannerInstanceID, spannerDatabaseID);
      dbClient = spanner.getDatabaseClient(db);
    }

    @FinishBundle
    public void finishBundle(FinishBundleContext c){
        spanner.close();  
    }

    @ProcessElement
    public void processElement(DoFn<String, TransactionImport>.ProcessContext c) throws Exception {
    TransactionImport import = new TransactionImport();

    Statement statement = Statement.newBuilder("SELECT * FROM Table1 WHERE Name= @Name")
            .bind("Name").to( text)
            .build();

    ResultSet resultSet = dbClient.singleUseReadOnlyTransaction().executeQuery(statement);

    // set some value  on import dependant on retrieved value

    c.output(import);

}

这总是导致数据流未完成,并且当我检查日志时看到:

Processing stuck in step Process Rows for at least 05m00s without outputting or completing in state process
at sun.misc.Unsafe.park(Native Method)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
at java.util.concurrent.SynchronousQueue$TransferStack.awaitFulfill(SynchronousQueue.java:458)
at java.util.concurrent.SynchronousQueue$TransferStack.transfer(SynchronousQueue.java:362)
at java.util.concurrent.SynchronousQueue.take(SynchronousQueue.java:924)
at com.google.common.util.concurrent.Uninterruptibles.takeUninterruptibly(Uninterruptibles.java:233)
at com.google.cloud.spanner.SessionPool$Waiter.take(SessionPool.java:411)
at com.google.cloud.spanner.SessionPool$Waiter.access$3300(SessionPool.java:399)
at com.google.cloud.spanner.SessionPool.getReadSession(SessionPool.java:754)
at com.google.cloud.spanner.DatabaseClientImpl.singleUseReadOnlyTransaction(DatabaseClientImpl.java:52)
at com.mycompany.pt.SpannerDataAccess.getBinDetails(SpannerDataAccess.java:197)
at com.mycompany.pt.transactionFiles.TransactionFileDataflow$1.processLine(TransactionFileDataflow.java:411)
at com.mycompany.pt.transactionFiles.TransactionFileDataflow$1.processElement(TransactionFileDataflow.java:336)
at com.mycompany.pt.transactionFiles.TransactionFileDataflow$1$DoFnInvoker.invokeProcessElement(Unknown Source)

`

有人在ParDo中使用过Spanner吗?

1 个答案:

答案 0 :(得分:1)

我不是扳手专家,但也许我可以帮忙:

  1. 您应该使用@ Setup / @ Teardown连接和断开扳手。 @ {Start,Finish}捆绑在一个工人的生命周期内多次被调用。有关更多详细信息,请参见此处:https://beam.apache.org/documentation/execution-model/#bundling-and-persistence

  2. 您的processElement方法是否使用以下方法发出元素? c.output(...)?如果没有,Beam会认为您的管道阻塞了