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中文网其他相关文章!

你应该关心Go语言中的"strings"包,因为它提供了处理文本数据的工具,从基本的字符串拼接到高级的正则表达式匹配。1)"strings"包提供了高效的字符串操作,如Join函数用于拼接字符串,避免性能问题。2)它包含高级功能,如ContainsAny函数,用于检查字符串是否包含特定字符集。3)Replace函数用于替换字符串中的子串,需注意替换顺序和大小写敏感性。4)Split函数可以根据分隔符拆分字符串,常用于正则表达式处理。5)使用时需考虑性能,如

“编码/二进制”软件包interingoisentialForHandlingBinaryData,oferingToolSforreDingingAndWritingBinaryDataEfficely.1)Itsupportsbothlittle-endianandBig-endianBig-endianbyteorders,CompialforOss-System-System-System-compatibility.2)

掌握Go语言中的bytes包有助于提高代码的效率和优雅性。1)bytes包对于解析二进制数据、处理网络协议和内存管理至关重要。2)使用bytes.Buffer可以逐步构建字节切片。3)bytes包提供了搜索、替换和分割字节切片的功能。4)bytes.Reader类型适用于从字节切片读取数据,特别是在I/O操作中。5)bytes包与Go的垃圾回收器协同工作,提高了大数据处理的效率。

你可以使用Go语言中的"strings"包来操纵字符串。1)使用strings.TrimSpace去除字符串两端的空白字符。2)用strings.Split将字符串按指定分隔符拆分成切片。3)通过strings.Join将字符串切片合并成一个字符串。4)用strings.Contains检查字符串是否包含特定子串。5)利用strings.ReplaceAll进行全局替换。注意使用时要考虑性能和潜在的陷阱。

ThebytespackageinGoishighlyeffectiveforbyteslicemanipulation,offeringfunctionsforsearching,splitting,joining,andbuffering.1)Usebytes.Containstosearchforbytesequences.2)bytes.Splithelpsbreakdownbyteslicesusingdelimiters.3)bytes.Joinreconstructsbytesli

thealternativestogo'sbytespackageincageincludethestringspackage,bufiopackage和customstructs.1)thestringspackagecanbeusedforbytemanipulationforbytemanipulationbybyconvertingbytestostostostostostrings.2))

“字节”包装封装forefforeflyManipulatingByteslices,CocialforbinaryData,网络交易和andfilei/o.itoffersfunctionslikeIndexForsearching,BufferForhandLinglaRgedLargedLargedAtaTasets,ReaderForsimulatingStreamReadReadImreAmreadReamReadinging,以及Joineffiter和Joineffiter和Joineffore

go'sstringspackageIscialforficientficientsTringManipulation,uperingToolSlikestrings.split(),strings.join(),strings.replaceall(),andStrings.contains.contains.contains.contains.contains.contains.split.split(split()strings.split()dividesStringoSubSubStrings; 2)strings.joins.joins.joinsillise.joinsinelline joinsiline joinsinelline; 3);


热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

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

热门文章

热工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

WebStorm Mac版
好用的JavaScript开发工具

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具