【问题标题】:pyspark generate key value pairs RDD from comma separated linespyspark 从逗号分隔的行生成键值对 RDD
【发布时间】:2022-01-25 10:57:26
【问题描述】:

我需要阅读下面提供的具有逗号分隔值的行,并生成一个键值对 RDD,如输出所示。我是新手,欢迎任何指导。

输入:

    R-001, A1, 10, A2, 20, A3, 30

    R-002, X1, 20, Y2, 10

    R-003, Z4, 30, Z10, 5, N12, 38

输出:

    R-001, A1
    R-001, A2
    R-001, A3
    R-002, X1
    R-002, Y2
    R-003, Z4
    R-003, Z10
    R-003, N12

代码:

    lines = spark.parallelize([
    "R-001, A1, 10, A2, 20, A3, 30",
    "R-002, X1, 20, Y2, 10",
    "R-003, Z4, 30, Z10, 5, N12, 38"])
     

【问题讨论】:

    标签: python-3.x apache-spark pyspark rdd


    【解决方案1】:

    您可以在lines RDD 上使用flatMap,并通过基于, 的拆分为每一行提取键和值。

    from typing import Tuple, List
    
    lines = spark.parallelize([
        "R-001, A1, 10, A2, 20, A3, 30",
        "R-002, X1, 20, Y2, 10",
        "R-003, Z4, 30, Z10, 5, N12, 38"])
    
    def processor(line: str) -> List[Tuple[str, str]]:
        tokens = line.split(",")
        key = tokens[0].strip()
        return [(key, v.strip()) for v in tokens[1::2]]
    
    lines.flatMap(processor).collect()
    

    输出

    [('R-001', 'A1'),
     ('R-001', 'A2'),
     ('R-001', 'A3'),
     ('R-002', 'X1'),
     ('R-002', 'Y2'),
     ('R-003', 'Z4'),
     ('R-003', 'Z10'),
     ('R-003', 'N12')]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-29
      • 1970-01-01
      • 1970-01-01
      • 2015-02-18
      • 1970-01-01
      • 2018-05-20
      • 2013-09-11
      相关资源
      最近更新 更多