我对python spark很新,因为上面的主题我想将一个Rdd的字段映射到另一个Rdd的字段。这是示例
RDD1集:
c_id name
121210 abc
121211 pqr
RDD2:
c_id cn_id cn_value
121211 0 0
121210 0 1
因此,匹配的c_id将由名称替换为 cnid ,并聚合 cn_value 。所以输出就像这个abc 0 0 pqr 0 1
from pyspark import SparkContext
import csv
sc = SparkContext("local", "spark-App")
file1 = sc.textFile('/home/hduser/sample.csv').map(lambda line:line.split(',')).filter(lambda line:len(line)>1)
file2 = sc.textFile('hdfs://localhost:9000/sample2/part-00000').map(lambda line:line.split(','))
file1_fields = file1.map(lambda x: (x[0],x[1]))
file2_fields = file2.map(lambda x: (x[0],x[1],float(x[2])))
如何通过在此处添加一些代码来实现我的目标。
任何帮助都将受到高度赞赏 谢谢你
答案 0 :(得分:2)
您正在寻找的操作称为join
。鉴于你的结构,最好使用DataFrames
和spark-csv
(我假设第二个文件也是逗号分隔的,但是没有标题)。让我们从虚拟数据开始:
file1 = ... # path to the first file
file2 = ... # path to the second file
with open(file1, "w") as fw:
fw.write("c_id,name\n121210,abc\n121211,pqr")
with open(file2, "w") as fw:
fw.write("121211,0,0\n121210,0,1")
阅读第一个文件:
df1 = (sqlContext.read
.format('com.databricks.spark.csv')
.options(header='true', inferSchema='true')
.load(file1))
加载第二个文件:
schema = StructType(
[StructField(x, LongType(), False) for x in ("c_id", "cn_id", "cn_value")])
df2 = (sqlContext.read
.format('com.databricks.spark.csv')
.schema(schema)
.options(header='false')
.load(file2))
最后加入:
combined = df1.join(df2, df1["c_id"] == df2["c_id"])
combined.show()
## +------+----+------+-----+--------+
## | c_id|name| c_id|cn_id|cn_value|
## +------+----+------+-----+--------+
## |121210| abc|121210| 0| 1|
## |121211| pqr|121211| 0| 0|
## +------+----+------+-----+--------+
修改强>:
使用RDD,你可以做到这样的事情:
file1_fields.join(file2_fields.map(lambda x: (x[0], x[1:])))