【问题标题】:count number of elements in each pyspark Dstream计算每个 pyspark Dstream 中的元素数
【发布时间】:2020-11-09 15:22:05
【问题描述】:

我正在寻找一种方法来计算我每次在 pyspark 中创建的 Dstream 中收到的元素数量(或 RDD 数量),我正在使用。如果您知道可以帮助我的方法,我会很高兴。 谢谢。

【问题讨论】:

    标签: python apache-spark pyspark rdd dstream


    【解决方案1】:

    我使用下面的代码,给每个数据一个,然后计算这些数据; 就像一个简单的字数统计,但我计算的是每个数据而不是字数。

    我使用下面的代码来实现,但如果你们有任何其他解决方案,请随时添加;谢谢。

    from pyspark.streaming import StreamingContext
    from pyspark import SparkContext
    
    # Create a local StreamingContext with two working thread and batch interval of 1 second
    sc = SparkContext('local[2]', 'Networkcount')
    ssc = StreamingContext(sc, 10)
    
    # Create a DStream that will connect to hostname:port, like localhost:7777
    data_received = ssc.socketTextStream("127.0.0.1", 7776)
    
    lines = data_received.map(lambda data: 1)
    count = lines.reduce(lambda x, y: x + y)
    count.pprint()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-14
      • 2016-05-22
      • 1970-01-01
      • 2017-02-22
      • 2020-02-04
      • 1970-01-01
      相关资源
      最近更新 更多