Go 中的 Apache Beam 左连接
php小编小新在这里给大家介绍一下Go语言中的Apache Beam左连接。Apache Beam是一种分布式数据处理框架,它提供了一种通用的编程模型,用于在不同的分布式数据处理引擎上执行批处理和流处理任务。而左连接是一种常见的数据处理操作,它可以将两个数据集按照某个键进行关联,返回左侧数据集中的所有记录,以及与之匹配的右侧数据集中的记录。本文将详细介绍Go语言中如何使用Apache Beam进行左连接操作。
问题内容
有没有简单的方法可以使用 go 执行 2 个 pcollection 的左连接? 我发现 sql 连接仅在 java 中可用。
package main import ( "context" "flag" "github.com/apache/beam/sdks/v2/go/pkg/beam" "github.com/apache/beam/sdks/v2/go/pkg/beam/log" "github.com/apache/beam/sdks/v2/go/pkg/beam/x/beamx" ) type customer struct { CustID int FName string } type order struct { OrderID int Amount int Cust_ID int } func main() { flag.Parse() beam.Init() ctx := context.Background() p := beam.NewPipeline() s := p.Root() var custList = []customer{ {1, "Bob"}, {2, "Adam"}, {3, "John"}, {4, "Ben"}, {5, "Jose"}, {6, "Bryan"}, {7, "Kim"}, {8, "Tim"}, } var orderList = []order{ {123, 100, 1}, {125, 30, 3}, {128, 50, 7}, } custPCol := beam.CreateList(s, custList) orderPCol := beam.CreateList(s, orderList) // Left Join custPcol with orderPCol // Expected Result // CustID | FName |OrderID| Amount // 1 | Bob | 123 | 100 // 2 | Adam | | // 3 | John | 125 | 100 // 4 | Ben | | // 5 | Jose | | // 6 | Bryan | | // 7 | Kim | 125 | 100 // 8 | Tim | | if err := beamx.Run(ctx, p); err != nil { log.Exitf(ctx, "Failed to execute job: %v", err) } }
我想加入这 2 个 pcollection 并执行进一步的操作。我看到了有关 cogroupbykey 的文档,但无法将其转换为普通 sql join 可以执行的格式。
对此有什么建议吗?
解决方法
尝试这样
type resulttype struct { custid int fname string orderid int amount int } result := beam.pardo(s, func(c customer, iterorder func(*order) bool) resulttype { var o order for iterorder(&o) { if c.custid == o.cust_id { return resulttype{ custid: c.custid, fname: c.fname, orderid: o.orderid, amount: o.amount, } } } return resulttype{ custid: c.custid, fname: c.fname, } }, custpcol, beam.sideinput{input: orderpcol})
或者如果您想使用 cogroupbykey ...
custWithKeyPCol := beam.ParDo(s, func(c customer) (int, customer) { return c.CustID, c }, custPCol) orderWithKeyPCol := beam.ParDo(s, func(o order) (int, order) { return o.Cust_ID, o }, orderPCol) resultPCol := beam.CoGroupByKey(s, custWithKeyPCol, orderWithKeyPCol) beam.ParDo0(s, func(CustID int, custIter func(*customer) bool, orderIter func(*order) bool) { c, o := customer{}, order{} for custIter(&c) { if ok := orderIter(&o); ok { fmt.Println(CustID, c.FName, o.OrderID, o.Amount) } fmt.Println(CustID, c.FName) } }, resultPCol)
以上是Go 中的 Apache Beam 左连接的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

Go语言中使用RedisStream实现消息队列时类型转换问题在使用Go语言与Redis...

GoLand中自定义结构体标签不显示怎么办?在使用GoLand进行Go语言开发时,很多开发者会遇到自定义结构体标签在�...

Go爬虫Colly中的Queue线程问题探讨在使用Go语言的Colly爬虫库时,开发者常常会遇到关于线程和请求队列的问题。�...

Go语言中用于浮点数运算的库介绍在Go语言(也称为Golang)中,进行浮点数的加减乘除运算时,如何确保精度是�...

Go语言中字符串打印的区别:使用Println与string()函数的效果差异在Go...

Go语言中结构体定义的两种方式:var与type关键字的差异Go语言在定义结构体时,经常会看到两种不同的写法:一�...

Go语言中哪些库是大公司开发或知名开源项目?在使用Go语言进行编程时,开发者常常会遇到一些常见的需求,�...
