Google cloud dataflow 如何有效地计算数据流中文件的行数?

Google cloud dataflow 如何有效地计算数据流中文件的行数?,google-cloud-dataflow,Google Cloud Dataflow,我要计算文件中的行总数。 如果可能,请解释你的代码 字符串fileAbsolutePath=“gs://sourav\u bucket\u dataflow/”+文件名 PCollection<String> data = p.apply("Reading Data From File", TextIO.read().from(fileAbsolutePath)); PCollection<Long> count = data.apply(Count.&

我要计算文件中的行总数。 如果可能,请解释你的代码

字符串fileAbsolutePath=“gs://sourav\u bucket\u dataflow/”+文件名

    PCollection<String> data = p.apply("Reading Data From File", TextIO.read().from(fileAbsolutePath));

    PCollection<Long> count = data.apply(Count.<String>globally());
PCollection data=p.apply(“从文件读取数据”,TextIO.read().From(fileAbsolutePath));
PCollection count=data.apply(count.globally());

现在我想获取值。

有多种接收器可用于从管道中获取数据。有一个当前内置IO转换的列表。

这在某种程度上取决于您想如何处理该数字。假设您希望在将来的转换中使用它,您可能希望将其转换为PCollectionView对象,并将其作为侧面输入传递给其他转换

    PCollection<String> data = p.apply("Reading Data From File", TextIO.read().from(fileAbsolutePath));
    PCollection<Long> count = data.apply(Count.<String>globally());    

    final PCollectionView<Long> view = count.apply(View.asSingleton());
其中:

    class FuncFn extends DoFn<String,String>
    {
      private final PCollectionView<Long> mySideInput;

      public FuncFn(PCollectionView<Long> mySideInput) {
          this.mySideInput = mySideInput;
      }

      @ProcessElement
      public void processElement(ProcessContext c) throws IOException
      {        
        Long count = c.sideInput(mySideInput);
        //other stuff you may want to do
      }
    }
class FuncFn扩展了DoFn
{
私有最终PCollectionView mySideInput;
公共函数fn(PCollectionView mySideInput){
this.mySideInput=mySideInput;
}
@过程元素
public void processElement(ProcessContext c)引发IOException
{        
长计数=c.sideInput(mySideInput);
//你可能想做的其他事情
}
}
希望有帮助

其中第1行中的“输入”为输入。这会奏效的

PCollection<Long> number = input.apply(Count.globally());
    number.apply(MapElements.via(new SimpleFunction<Long, Long>() 
    {
        public Long apply(Long total) 
        {
            System.out.println("Length is: " + total);
            return total;
        }
    }));
PCollection number=input.apply(Count.globally());
number.apply(MapElements.via)(新的SimpleFunction()
{
公共长期应用(长期总计)
{
System.out.println(“长度为:”+总计);
返回总数;
}
}));

您找到解决方案了吗?
PCollection<Long> number = input.apply(Count.globally());
    number.apply(MapElements.via(new SimpleFunction<Long, Long>() 
    {
        public Long apply(Long total) 
        {
            System.out.println("Length is: " + total);
            return total;
        }
    }));