【问题标题】:Large dataframe generation in pysparkpyspark 中的大型数据框生成
【发布时间】:2021-04-28 02:29:51
【问题描述】:

在互联网上找不到解决方案。

尝试创建具有 10 个 int 列和 n 行随机值的数据框。需要生成大量行(例如 n = 1000 万)。如果我自己有 10 行生成的数据(pyspark 数据框),我该如何填充它们直到 n 行通过。
生成的数据框应由 pyspark 用 parquet 编写。

什么是最好的解决方案?

【问题讨论】:

    标签: python dataframe pyspark


    【解决方案1】:

    您可以在循环中使用flatMap 来创建指数增长的行数:

    rdd = spark.sparkContext.parallelize([(1,2,3,4,5,6,7,8,9,10)])
    
    def f(t):    
        for c in range(0,10):        
            yield tuple((i+c) * 1664525 for i in t)
    
    #Increase the size of this loop to create more data.
    #The number of rows will be 10 ^ n
    for _ in range(0, 2):
        rdd = rdd.flatMap(f)
        rdd = rdd.repartition(int(spark.conf.get('spark.sql.shuffle.partitions')))
        print(rdd.count())
    
    #write result to parquet file
    df = spark.createDataFrame(rdd)
    df.write.parquet("mytestdata")
    

    【讨论】:

      猜你喜欢
      • 2020-12-16
      • 2020-04-11
      • 1970-01-01
      • 1970-01-01
      • 2020-03-16
      • 2017-10-10
      • 2018-02-19
      • 2018-01-09
      • 2020-04-22
      相关资源
      最近更新 更多