首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用Cloud连接两个或多个键上的烧烤表?

如何使用Cloud连接两个或多个键上的烧烤表?
EN

Stack Overflow用户
提问于 2018-05-04 13:19:42
回答 2查看 760关注 0票数 1

我有两个表A和B。它们都有字段session_idcookie_id。如何在session_id上创建一个连接A与B连接的表输出,如何在Dataflow管道的帮助下创建cookie_idCoGroupByKey方法允许您在一个键上连接。在文档中也找不到任何有用的东西。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-05-07 22:14:01

扩展用户9720010的答案。您可以通过将字段映射到session_idcookie_id的组合来创建复合键。此模式在通用数据流用例模式博客中进行了解释。假设您使用的是BigQuery,则可以执行以下类似操作:

代码语言:javascript
运行
复制
Pipeline pipeline = Pipeline.create(options);

// Create tuple tags for the value types in each collection.
final TupleTag<TableRow> table1Tag = new TupleTag<>();
final TupleTag<TableRow> table2Tag = new TupleTag<>();

// Transform for keying table rows by session_id and cookie_id
WithKeys<String, TableRow> sessionAndCookieKeys = WithKeys.of(
    (TableRow row) ->
        String.format("%s#%s",
            row.get("session_id"),
            row.get("cookie_id")))
    .withKeyType(TypeDescriptors.strings());

/*
 * Steps:
 *  1) Read table 1's rows
 *  2) Read table 2's rows
 *  3) Map each row to a composite key
 *  4) Join on the composite key
 *  5) Process the results
 */
PCollection<KV<String, TableRow>> table1Rows = pipeline
    .apply(
        "ReadTable1",
        BigQueryIO
            .readTableRows()
            .from(options.getTable1()))
    .apply("WithKeys", sessionAndCookieKeys);

PCollection<KV<String, TableRow>> table2Rows = pipeline
    .apply(
        "ReadTable2",
        BigQueryIO
            .readTableRows()
            .from(options.getTable2()))
    .apply("WithKeys", sessionAndCookieKeys);

//Merge collection values into a CoGbkResult collection
PCollection<KV<String, CoGbkResult>> coGbkResult = KeyedPCollectionTuple
    .of(table1Tag, table1Rows)
    .and(table2Tag, table2Rows)
    .apply("JoinOnSessionAndCookie", CoGroupByKey.create());

// Process the results
coGbkResult.apply(
    "ProcessResults", 
    ParDo.of(new DoFn<KV<String, CoGbkResult>, Object>() {
      @ProcessElement
      public void processElement(ProcessContext context) {
        // Do something here
      }
    }));
票数 5
EN

Stack Overflow用户

发布于 2018-05-07 02:47:39

在这种情况下,我遵循的一种方法是创建一个特殊的键,它是两个键的组合。在将读取数据转换为键值对时,我会将session_id$cookie_id输出为一个连接字符串。这里,$可以是任何不构成两个键的字符集的分隔符。分隔符也可以忽略。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50175971

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档