2017-03-27 23 views
0

2つのrdd、1つは辞書のリスト、もう1つは次のようなタプルのリストです -pysparkを使って辞書リストにタプル値のリストを追加するには?

rdd1 = {{'id1'、['string'、 'string'、count]}、{'id2 'id3'、['string'、 'string'、count]}] rdd2 = [(id1、count)、(id2、count)、(id1、count)]、[文字列]、[文字列] id3、count)]

ここで、rdd2からのidがrdd1と一致する場合、rdd2からrdd1にカウントを追加します。 これを達成するために手伝ってもらえますか?

ありがとうございます。

答えて

2

ゲイツ氏の回答は正しいが、RDDを使用する場合はforループの使用を避けてください。 RDDS上の操作は並列化と大きなdatasets.Youでの作業は2 RDDSに参加して出力を再フォーマットすることで同じことを達成することができたときにforループに比べてはるかに高速ですされています

rdd1 = sc.parallelize([{'id1':['string','string',1]}, {'id2':['string','string',2]}, {'id3':['string','string',3]}]) 
rdd2 = sc.parallelize([('id1',2), ('id2',4), ('id3',6), ('id4',8)]) 
rdd_joined = rdd1.flatMap(lambda x:x.items()).join(rdd2) 
rdd_reformatted = rdd_joined.map(lambda (x,(y,z)):{x:y[:-1]+[y[-1]+z]}) 

rdd_reformatted.collect()は出力として得られます。

[{'id2': ['string', 'string', 6]}, 
{'id3': ['string', 'string', 9]}, 
{'id1': ['string', 'string', 3]}] 
+0

ありがとうJaco。あなたは素晴らしいです。あなたのコードから多くのことを学びました。 –

0

私はこれが役に立ちそうです。

rdd1 = [{'id1':['string','string',1]}, {'id2':['string','string',2]}, {'id3':['string','string',3]}] 
rdd2 = [('id1',2), ('id2',4), ('id3',6), ('id4',8)] 

for each in rdd2: 
    there = False 
    position = 0 
    for ele in rdd1: 
     if each[0] in ele.keys(): 
      #now increment the count 
      original = rdd1[position] 
      originalList = original[each[0]] 
      #updating the 3rd element 
      newList = originalList 
      newList[2] = originalList[2] + each[1] 
      #update the new list to key 
      updated = { each[0] : newList } 
      rdd1[position] = updated 
      there = True 
      break 
     position = position + 1 
print rdd1 
#output: [{'id1': ['string', 'string', 3]}, {'id2': ['string', 'string', 6]}, {'id3': ['string', 'string', 9]}] 
+0

ありがとうございました。それは本当に私を助けました。 –

+0

あなたは大歓迎です。 Jacoの答えから多くを学んだ。 –

関連する問題