从另一个具有所需特定列的 rdd 创建 rdd
Creating rdd from another rdd with required specific columns
我在 spark 中有一个文件,其中包含以下 table 数据
Property ID|Location|Price|Bedrooms|Bathrooms|Size|Price SQ Ft|Status
我已将此文件读取为 rdd 使用:-
a = sc.textFile("/FileStore/tables/realestate.txt")
现在我需要根据上述 RDD 创建一个具有 PropertyID、位置、价格(= 大小 * 价格 SQ Ft)的新 RDD。
我可以通过将它转换为数据框来做到这一点,但无法弄清楚如何使用所需的列将它转换为另一个 RDD。
您可以使用地图获取前三列:
a = sc.textFile("/FileStore/tables/realestate.txt")
b = a.map(
lambda x:
(x.split('|')[:2] + [float(x.split('|')[5]) * float(x.split('|')[6])])
if x.split('|')[0] != 'Property ID'
else ['Property ID', 'Location', 'Price']
)
def splitfunc(x):
array=x.split('|')
return [array[0],array[1],array[5]*array[6]]
#array[0] is your properties and so on..
newrdd=rdd.map(splitfunc)
使用 map 函数。在 map 函数中,将 rdd 拆分为分隔符 (lines.split('|')),然后 select 数组中的必要列。
我在 spark 中有一个文件,其中包含以下 table 数据
Property ID|Location|Price|Bedrooms|Bathrooms|Size|Price SQ Ft|Status
我已将此文件读取为 rdd 使用:-
a = sc.textFile("/FileStore/tables/realestate.txt")
现在我需要根据上述 RDD 创建一个具有 PropertyID、位置、价格(= 大小 * 价格 SQ Ft)的新 RDD。
我可以通过将它转换为数据框来做到这一点,但无法弄清楚如何使用所需的列将它转换为另一个 RDD。
您可以使用地图获取前三列:
a = sc.textFile("/FileStore/tables/realestate.txt")
b = a.map(
lambda x:
(x.split('|')[:2] + [float(x.split('|')[5]) * float(x.split('|')[6])])
if x.split('|')[0] != 'Property ID'
else ['Property ID', 'Location', 'Price']
)
def splitfunc(x):
array=x.split('|')
return [array[0],array[1],array[5]*array[6]]
#array[0] is your properties and so on..
newrdd=rdd.map(splitfunc)
使用 map 函数。在 map 函数中,将 rdd 拆分为分隔符 (lines.split('|')),然后 select 数组中的必要列。