访问BigQuery Apache Beam中的TableRow列

时间:2018-04-17 16:49:25

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

我正在尝试

1.从Cloud Pub / Sub

中读取JSON事件

2.使用文件加载每15分钟将事件从Cloud Pub / Sub加载到BigQuery,以节省流式插入的成本。

3.目的地将根据" user_id"而不同和" campaign_id" JSON事件中的字段," user_id"将是数据集名称和" campaign_id"将是表名。分区名称来自事件时间戳。

4.所有表的架构保持不变。

我是Java和Beam的新手。我认为我的代码主要完成了我想做的事情,而我在这里只需要一些帮助。

但我无法访问" campaign_id"和" user_id" JSON消息中的字段。 所以,我的事件没有路由到正确的表格。

package ...;

import com.google.api.services.bigquery.model.TableSchema;
import javafx.scene.control.TableRow;
import org.apache.beam.sdk.Pipeline;
import org.apache.beam.sdk.coders.Coder;
import org.apache.beam.sdk.io.gcp.bigquery.BigQueryIO;
import org.apache.beam.sdk.io.gcp.bigquery.DynamicDestinations;
import org.apache.beam.sdk.io.gcp.bigquery.TableDestination;
import org.apache.beam.sdk.io.gcp.bigquery.TableRowJsonCoder;
import org.apache.beam.sdk.io.gcp.pubsub.PubsubIO;
import org.apache.beam.sdk.transforms.MapElements;
import org.apache.beam.sdk.transforms.PTransform;
import org.apache.beam.sdk.transforms.SimpleFunction;
import org.apache.beam.sdk.values.PCollection;
import org.apache.beam.sdk.values.ValueInSingleWindow;
import org.joda.time.Duration;
import org.joda.time.Instant;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;

import static org.apache.beam.sdk.io.gcp.bigquery.BigQueryIO.Write.CreateDisposition.CREATE_IF_NEEDED;
import static org.apache.beam.sdk.io.gcp.bigquery.BigQueryIO.Write.Method.FILE_LOADS;
import static org.apache.beam.sdk.io.gcp.bigquery.BigQueryIO.Write.WriteDisposition.WRITE_APPEND;

public class ClickLogConsumer {
    private static final int BATCH_INTERVAL_SECS = 15 * 60;
    private static final String PROJECT = "pure-app";

    public static PTransform<PCollection<String>, PCollection<com.google.api.services.bigquery.model.TableRow>> jsonToTableRow() {
        return new JsonToTableRow();
    }

    private static class JsonToTableRow
            extends PTransform<PCollection<String>, PCollection<com.google.api.services.bigquery.model.TableRow>> {

        @Override
        public PCollection<com.google.api.services.bigquery.model.TableRow> expand(PCollection<String> stringPCollection) {
            return stringPCollection.apply("JsonToTableRow", MapElements.<String, com.google.api.services.bigquery.model.TableRow>via(
                    new SimpleFunction<String, com.google.api.services.bigquery.model.TableRow>() {
                        @Override
                        public com.google.api.services.bigquery.model.TableRow apply(String json) {
                            try {

                                InputStream inputStream = new ByteArrayInputStream(
                                        json.getBytes(StandardCharsets.UTF_8.name()));

                                //OUTER is used here to prevent EOF exception
                                return TableRowJsonCoder.of().decode(inputStream, Coder.Context.OUTER);
                            } catch (IOException e) {
                                throw new RuntimeException("Unable to parse input", e);
                            }
                        }
                    }));
        }
    }


    public static void main(String[] args) throws Exception {
        Pipeline pipeline = Pipeline.create(options);
        pipeline
                .apply(PubsubIO.readStrings().withTimestampAttribute("timestamp").fromTopic("projects/pureapp-199410/topics/clicks"))
                .apply(jsonToTableRow())
                .apply("WriteToBQ",
                        BigQueryIO.writeTableRows()
                                .withMethod(FILE_LOADS)
                                .withWriteDisposition(WRITE_APPEND)
                                .withCreateDisposition(CREATE_IF_NEEDED)
                                .withTriggeringFrequency(Duration.standardSeconds(BATCH_INTERVAL_SECS))
                                .withoutValidation()
                                .to(new DynamicDestinations<TableRow, String>() {
                                    @Override
                                    public String getDestination(ValueInSingleWindow<TableRow> element) {
                                        String tableName = "campaign_id"; // JSON message in Pub/Sub has "campaign_id" field, how do I access it here?
                                        String datasetName = "user_id"; // JSON message in Pub/Sub has "user_id" field, how do I access it here?
                                        Instant eventTimestamp = element.getTimestamp();
                                        String partition = new SimpleDateFormat("yyyyMMdd").format(eventTimestamp);
                                        return String.format("%s:%s.%s$%s", PROJECT, datasetName, tableName, partition);
                                    }

                                    @Override
                                    public TableDestination getTable(String table) {
                                        return new TableDestination(table, null);
                                    }

                                    @Override
                                    public TableSchema getSchema(String destination) {
                                        return getTableSchema();
                                    }
                                }));
        pipeline.run();
    }
}

我根据以下内容得出了上述代码:

1。https://medium.com/myheritage-engineering/kafka-to-bigquery-load-a-guide-for-streaming-billions-of-daily-events-cbbf31f4b737

2。https://shinesolutions.com/2017/12/05/fun-with-serializable-functions-and-dynamic-destinations-in-cloud-dataflow/

3。https://beam.apache.org/documentation/sdks/javadoc/2.0.0/org/apache/beam/sdk/io/gcp/bigquery/DynamicDestinations.html

4。BigQueryIO - Write performance with streaming and FILE_LOADS

5。Inserting into BigQuery via load jobs (not streaming)

更新

import com.google.api.services.bigquery.model.TableFieldSchema;
import com.google.api.services.bigquery.model.TableRow;
import com.google.api.services.bigquery.model.TableSchema;
import com.google.api.services.bigquery.model.TimePartitioning;
import com.google.common.collect.ImmutableList;
import org.apache.beam.sdk.Pipeline;
import org.apache.beam.sdk.coders.Coder;
import org.apache.beam.sdk.io.gcp.bigquery.BigQueryIO;
import org.apache.beam.sdk.io.gcp.bigquery.TableDestination;
import org.apache.beam.sdk.io.gcp.bigquery.TableRowJsonCoder;
import org.apache.beam.sdk.io.gcp.pubsub.PubsubIO;
import org.apache.beam.sdk.transforms.MapElements;
import org.apache.beam.sdk.transforms.PTransform;
import org.apache.beam.sdk.transforms.SimpleFunction;
import org.apache.beam.sdk.values.PCollection;
import org.joda.time.Duration;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

import static org.apache.beam.sdk.io.gcp.bigquery.BigQueryIO.Write.CreateDisposition.CREATE_IF_NEEDED;
import static org.apache.beam.sdk.io.gcp.bigquery.BigQueryIO.Write.Method.FILE_LOADS;
import static org.apache.beam.sdk.io.gcp.bigquery.BigQueryIO.Write.WriteDisposition.WRITE_APPEND;

public class ClickLogConsumer {
    private static final int BATCH_INTERVAL_SECS = 15 * 60;
    private static final String PROJECT = "pure-app";

    public static PTransform<PCollection<String>, PCollection<TableRow>> jsonToTableRow() {
        return new JsonToTableRow();
    }

    private static class JsonToTableRow
            extends PTransform<PCollection<String>, PCollection<TableRow>> {

        @Override
        public PCollection<TableRow> expand(PCollection<String> stringPCollection) {
            return stringPCollection.apply("JsonToTableRow", MapElements.<String, com.google.api.services.bigquery.model.TableRow>via(
                    new SimpleFunction<String, TableRow>() {
                        @Override
                        public TableRow apply(String json) {
                            try {

                                InputStream inputStream = new ByteArrayInputStream(
                                        json.getBytes(StandardCharsets.UTF_8.name()));

                                //OUTER is used here to prevent EOF exception
                                return TableRowJsonCoder.of().decode(inputStream, Coder.Context.OUTER);
                            } catch (IOException e) {
                                throw new RuntimeException("Unable to parse input", e);
                            }
                        }
                    }));
        }
    }


    public static void main(String[] args) throws Exception {
        Pipeline pipeline = Pipeline.create(options);
        pipeline
                .apply(PubsubIO.readStrings().withTimestampAttribute("timestamp").fromTopic("projects/pureapp-199410/topics/clicks"))
                .apply(jsonToTableRow())
                .apply(BigQueryIO.write()
                        .withTriggeringFrequency(Duration.standardSeconds(BATCH_INTERVAL_SECS))
                        .withMethod(FILE_LOADS)
                        .withWriteDisposition(WRITE_APPEND)
                        .withCreateDisposition(CREATE_IF_NEEDED)
                        .withSchema(new TableSchema().setFields(
                                ImmutableList.of(
                                        new TableFieldSchema().setName("timestamp").setType("TIMESTAMP"),
                                        new TableFieldSchema().setName("exchange").setType("STRING"))))
                        .to((row) -> {
                            String datasetName = row.getValue().get("user_id").toString();
                            String tableName = row.getValue().get("campaign_id").toString();
                            return new TableDestination(String.format("%s:%s.%s", PROJECT, datasetName, tableName), "Some destination");
                        })
                        .withTimePartitioning(new TimePartitioning().setField("timestamp")));
        pipeline.run();
    }
}

1 个答案:

答案 0 :(得分:1)

如何:String tableName = element.getValue().get("campaign_id").toString()和数据集一样。

此外,为了插入时间分区表,我强烈建议使用BigQuery的基于列的分区,而不是在表名中使用分区装饰器。请参阅the javadoc中的“将历史数据加载到时间分区的BigQuery表中” - 您需要一个时间戳列。 (请注意,javadoc有一个拼写错误:“time”vs“timestamp”)