我有两个表A和B。它们都有字段session_id
和cookie_id
。如何在session_id
上创建一个连接A与B连接的表输出,如何在Dataflow管道的帮助下创建cookie_id
?CoGroupByKey
方法允许您在一个键上连接。在文档中也找不到任何有用的东西。
发布于 2018-05-07 22:14:01
扩展用户9720010的答案。您可以通过将字段映射到session_id
和cookie_id
的组合来创建复合键。此模式在通用数据流用例模式博客中进行了解释。假设您使用的是BigQuery,则可以执行以下类似操作:
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
}
}));
发布于 2018-05-07 02:47:39
在这种情况下,我遵循的一种方法是创建一个特殊的键,它是两个键的组合。在将读取数据转换为键值对时,我会将session_id$cookie_id输出为一个连接字符串。这里,$可以是任何不构成两个键的字符集的分隔符。分隔符也可以忽略。
https://stackoverflow.com/questions/50175971
复制相似问题