如何集成测试Dataflow管道写入Bigtable?

Ada*_*air 3 google-cloud-dataflow google-cloud-bigtable apache-beam

根据Beam网站,

通常,对管道代码执行本地单元测试比调试管道的远程执行更快更简单.

我想为我的Beam/Dataflow应用程序使用测试驱动开发,因为这个原因写入Bigtable.

然而,遵循Beam测试文档我陷入了僵局--PAssert没用,因为输出PCollection包含org.apache.hadoop.hbase.client.Put对象,它们不会覆盖equals方法.

无法获得 PCollection 的内容来对它们进行验证,因为

不可能直接获取PCollection的内容 - Apache Beam或Dataflow管道更像是应该进行什么处理的查询计划,其中PCollection是计划中的逻辑中间节点,而不是包含数据.

那么除了手动运行之外,我该如何测试这个管道呢?我正在使用Maven和JUnit(在Java中,因为所有Dataflow Bigtable Connector似乎都支持).

Ada*_*air 6

Bigtable的模拟器Maven插件可以用来写这个集成测试:

  • 配置Maven Failsafe插件并将测试用例的结尾从*Test更改为*IT以作为集成测试运行.
  • 在命令行上的gcloud sdk中安装Bigtable Emulator:

    gcloud components install bigtable   
    
    Run Code Online (Sandbox Code Playgroud)

    请注意,这个必需的步骤将减少代码的可移植性(例如,它将在您的构建系统上运行吗?在其他开发人员的机器上?)所以我将在部署到构建系统之前使用Docker将其容器化.

  • 根据自述文件将模拟器插件添加到pom中

  • 使用HBase Client API并查看示例Bigtable Emulator集成测试以设置会话和表.

  • 按照Beam文档正常编写测试,除了使用PAssert实际调用CloudBigtableIO.writeToTable,然后使用HBase Client从表中读取数据进行验证.

这是一个示例集成测试:

package adair.example;

import static org.apache.hadoop.hbase.util.Bytes.toBytes;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

import org.apache.beam.sdk.Pipeline;
import org.apache.beam.sdk.transforms.Create;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.Mutation;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
import org.hamcrest.collection.IsIterableContainingInAnyOrder;
import org.junit.Assert;
import org.junit.Test;

import com.google.cloud.bigtable.beam.CloudBigtableIO;
import com.google.cloud.bigtable.beam.CloudBigtableTableConfiguration;
import com.google.cloud.bigtable.hbase.BigtableConfiguration;

/**
 *  A simple integration test example for use with the Bigtable Emulator maven plugin.
 */
public class DataflowWriteExampleIT {

  private static final String PROJECT_ID = "fake";
  private static final String INSTANCE_ID = "fakeinstance";
  private static final String TABLE_ID = "example_table";
  private static final String COLUMN_FAMILY = "cf";
  private static final String COLUMN_QUALIFIER = "cq";

  private static final CloudBigtableTableConfiguration TABLE_CONFIG =
    new CloudBigtableTableConfiguration.Builder()
      .withProjectId(PROJECT_ID)
      .withInstanceId(INSTANCE_ID)
      .withTableId(TABLE_ID)
      .build();

  public static final List<String> VALUES_TO_PUT = Arrays
    .asList("hello", "world", "introducing", "Bigtable", "plus", "Dataflow", "IT");

  @Test
  public void testPipelineWrite() throws IOException {
    try (Connection connection = BigtableConfiguration.connect(PROJECT_ID, INSTANCE_ID)) {
      Admin admin = connection.getAdmin();
      createTable(admin);

      List<Mutation> puts = createTestPuts();

      //Use Dataflow to write the data--this is where you'd call the pipeline you want to test.
      Pipeline p = Pipeline.create();
      p.apply(Create.of(puts)).apply(CloudBigtableIO.writeToTable(TABLE_CONFIG));
      p.run().waitUntilFinish();

      //Read the data from the table using the regular hbase api for validation
      ResultScanner scanner = getTableScanner(connection);
      List<String> resultValues = new ArrayList<>();
      for (Result row : scanner) {
        String cellValue = getRowValue(row);
        System.out.println("Found value in table: " + cellValue);
        resultValues.add(cellValue);
      }

      Assert.assertThat(resultValues,
        IsIterableContainingInAnyOrder.containsInAnyOrder(VALUES_TO_PUT.toArray()));
    }
  }

  private void createTable(Admin admin) throws IOException {
    HTableDescriptor tableDesc = new HTableDescriptor(TableName.valueOf(TABLE_ID));
    tableDesc.addFamily(new HColumnDescriptor(COLUMN_FAMILY));

    admin.createTable(tableDesc);
  }

  private ResultScanner getTableScanner(Connection connection) throws IOException {
    Scan scan = new Scan();
    Table table = connection.getTable(TableName.valueOf(TABLE_ID));
    return table.getScanner(scan);
  }

  private String getRowValue(Result row) {
    return Bytes.toString(row.getValue(toBytes(COLUMN_FAMILY), toBytes(COLUMN_QUALIFIER)));
  }

  private List<Mutation> createTestPuts() {
    return VALUES_TO_PUT
          .stream()
          .map(this::stringToPut)
          .collect(Collectors.toList());
  }

  private Mutation stringToPut(String cellValue){
    String key = UUID.randomUUID().toString();
    Put put = new Put(toBytes(key));
    put.addColumn(toBytes(COLUMN_FAMILY), toBytes(COLUMN_QUALIFIER), toBytes(cellValue));
    return put;
  }

}
Run Code Online (Sandbox Code Playgroud)