Google cloud dataflow 将CSV文件从GCS导入BigQuery

Google cloud dataflow 将CSV文件从GCS导入BigQuery,google-cloud-dataflow,Google Cloud Dataflow,我想知道如何将一个CSV文件从GCS加载到BigQuery中。以下管道: // Create the pipeline Pipeline p = Pipeline.create(options); // Create the PCollection from csv PCollection<String> lines = p.apply(TextIO.read().from("gs://impression_tst_data/incoming_dat

我想知道如何将一个CSV文件从GCS加载到BigQuery中。以下管道:

    // Create the pipeline
    Pipeline p = Pipeline.create(options);

    // Create the PCollection from csv
    PCollection<String> lines = p.apply(TextIO.read().from("gs://impression_tst_data/incoming_data.csv"));


    // Transform into TableRow
    PCollection<TableRow> row = lines.apply(ParDo.of(new StringToRowConverter()));


    // Write table to BigQuery
    row.apply(BigQueryIO.<TableRow>writeTableRows()
            .to(“project_id:dataset.table”)
            .withSchema(getSchema())
            .withWriteDisposition(WriteDisposition.WRITE_APPEND)
            .withCreateDisposition(CreateDisposition.CREATE_IF_NEEDED));
模式:

static TableSchema getSchema() {
            return new TableSchema().setFields(new ArrayList<TableFieldSchema>() {
                // Compose the list of TableFieldSchema from tableSchema.
                {
                    add(new TableFieldSchema().setName("Event_Time").setType("TIMESTAMP"));
                    add(new TableFieldSchema().setName("Name").setType("STRING"));
                    add(new TableFieldSchema().setName("Address").setType("STRING"));
                    add(new TableFieldSchema().setName("Phone").setType("STRING"));
                    add(new TableFieldSchema().setName("etc").setType("STRING"));
                }
            });
        }
static TableSchema getSchema(){
返回新的TableSchema().setFields(新的ArrayList()){
//从tableSchema组成TableFieldSchema列表。
{
添加(新的TableFieldSchema().setName(“事件时间”).setType(“时间戳”);
添加(新的TableFieldSchema().setName(“名称”).setType(“字符串”);
添加(新的TableFieldSchema().setName(“地址”).setType(“字符串”);
添加(新的TableFieldSchema().setName(“电话”).setType(“字符串”);
添加(新TableFieldSchema().setName(“etc”).setType(“STRING”);
}
});
}
有没有比使用StringToRowConverter更好的方法

我需要使用ParDo创建TableRow PCollection,然后才能将其写入BQ。然而,我找不到一个坚实的例子来说明如何接收CSV PCollection,转换为TableRow并写出它


是的,我是一个在这里学习的noob。我希望有人能帮我写一个片段,或者给我指出一个正确的方向,让我找到最简单的方法来完成这个任务。提前感谢。

您的
StringToRowConverter
DoFn
中的代码应该解析字符串并生成一个带有多个字段的
表格行。由于每一行都是逗号分隔的,这可能需要在逗号上拆分字符串,然后使用您对列顺序的了解来执行以下操作:

String inputLine=c.element();
//可能需要使行解析更加健壮,具体取决于您的
//档案。看看如何使用Java解析CSV的行。
String[]split=inputLine.split(',');
//此外,您可能需要处理诸如列不够等错误。
TableRow输出=新建TableRow();
output.set(“事件时间”,拆分[0]);//可能需要分析字符串
输出.set(“名称”,拆分[1]);
...
c、 输出(输出);

还要注意,如果您的CSV文件在第一行中包含标题,您需要手动跳过标题-TextIO生成的PCollection行是无序的,因此无法再知道哪一行是“第一行”,因此您需要以某种方式将其过滤掉。另见,
"string_field": "6/26/17 21:28,Dave Smith,1 Learning Drive,867-5309,etc"}
static TableSchema getSchema() {
            return new TableSchema().setFields(new ArrayList<TableFieldSchema>() {
                // Compose the list of TableFieldSchema from tableSchema.
                {
                    add(new TableFieldSchema().setName("Event_Time").setType("TIMESTAMP"));
                    add(new TableFieldSchema().setName("Name").setType("STRING"));
                    add(new TableFieldSchema().setName("Address").setType("STRING"));
                    add(new TableFieldSchema().setName("Phone").setType("STRING"));
                    add(new TableFieldSchema().setName("etc").setType("STRING"));
                }
            });
        }