Google cloud dataflow 如何使用带有多个键的apache beam api聚合数据

Google cloud dataflow 如何使用带有多个键的apache beam api聚合数据,google-cloud-dataflow,Google Cloud Dataflow,我对谷歌云数据平台和ApacheBeamAPI都是新手。我希望基于多个键聚合数据。在我的需求中,我将获得一个交易提要,其中包含客户id、客户名称、交易金额和交易类型等字段。我想根据客户id和交易类型汇总数据。这里有一个例子 customer id,customer name,transction amount,transaction type cust123,ravi,100,D cust123,ravi,200,D cust234,Srini,200,C cust444,shaker,500,

我对谷歌云数据平台和ApacheBeamAPI都是新手。我希望基于多个键聚合数据。在我的需求中,我将获得一个交易提要,其中包含客户id、客户名称、交易金额和交易类型等字段。我想根据客户id和交易类型汇总数据。这里有一个例子

customer id,customer name,transction amount,transaction type
cust123,ravi,100,D
cust123,ravi,200,D
cust234,Srini,200,C
cust444,shaker,500,D
cust123,ravi,100,C
cust123,ravi,300,C

O/p should be

cust123,ravi,300,D
cust123,ravi,400,C
cust234,Srini,200,C
cust444,shaker,500,D
在谷歌,大多数的例子都是基于单键的,比如按单键分组。请任何人帮助我了解我的PTTransform在我的需求中是什么样子的,以及如何生成聚合数据以及其他字段

问候,,
拉维。

这里有一个简单的方法。我将所有的键连接在一起形成一个键,然后在子键和子键之后分割键,以您想要的方式组织输出。如果有任何问题,请告诉我

该代码不希望CSV文件中包含标头。我只是把它写得简短一些,以说明你所问的要点

import apache_beam as beam
import sys

class Split(beam.DoFn):

    def process(self, element):
        """
        Splits each row on commas and returns a tuple representing the row to process
        """
        customer_id, customer_name, transction_amount, transaction_type = element.split(",")
        return [
            (customer_id +","+customer_name+","+transaction_type, float(transction_amount))
        ]

if __name__ == '__main__':
   p = beam.Pipeline(argv=sys.argv)
   input = 'aggregate.csv'
   output_prefix = 'C:\\pythonVirtual\\Mycodes\\output'

   (p
      | 'ReadFile' >> beam.io.ReadFromText(input)
      | 'parse' >> beam.ParDo(Split())
      | 'sum' >> beam.CombinePerKey(sum)
      | 'convertToString' >>beam.Map(lambda (combined_key, total_balance): '%s,%s,%s,%s' % (combined_key.split(",")[0], combined_key.split(",")[1],total_balance,combined_key.split(",")[2]))
      | 'write' >> beam.io.WriteToText(output_prefix)
   )

   p.run().wait_until_finish()
它将产生如下输出:

cust234,Srini,200.0,C
cust444,shaker,500.0,D
cust123,ravi,300.0,D
cust123,ravi,400.0,C

别担心,伙计。这个建议对你有用吗?如果答案对你有帮助,请接受。