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

时间:2018-07-03 20:54:36

标签: google-cloud-dataflow apache-beam google-cloud-bigtable

根据Beam website

  

通常,在您的计算机上执行本地单元测试会更快,更简单   管道代码,而不是调试管道的远程执行。

出于这个原因,我想对写入BigTable的Beam / Dataflow应用程序使用测试驱动的开发。

但是,在Beam测试文档之后,我陷入了僵局-PAssert没什么用,因为输出PCollection包含org.apache.hadoop.hbase.client.Put对象,这些对象不会覆盖equals方法。 / p>

can't get the contents都对PCollection进行了验证,因为

  

不可能直接获取PCollection的内容-   Apache Beam或Dataflow管道更像是一个关于什么的查询计划   应该完成处理,并以PCollection为逻辑   计划中的中间节点,而不是包含数据。

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

1 个答案:

答案 0 :(得分:5)

Bigtable Emulator Maven plugin可用于为此编写集成测试:

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

    gcloud components install bigtable   
    

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

  • 按照README

  • 将模拟器插件添加到pom
  • 使用HBase Client API并查看example Bigtable Emulator integration test来设置会话和表。

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

这是一个集成测试示例:

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

}