2016-06-30 9 views
0

異なるデータフレームから列を追加/追加する方法は? placeNameのパーセンタイルが3以上であることを確認しようとしています。異なるデータフレームから列を追加する方法:スカラフレーム

// sc : An existing SparkContext. 
val sqlContext = new org.apache.spark.sql.SQLContext(sc) 
val df = sqlContext.jsonFile("temp.txt") 
//df.show() 


val res = df.withColumn("visited", explode($"visited")) 

val result1 =res.groupBy($"customerId", $"visited.placeName").agg(count("*").alias("total")) 

val result2 = res 
.filter($"visited.rating" < 4) 
    .groupBy($"requestId", $"visited.placeName") 
    .agg(count("*").alias("top")) 

result1.show() 

result2.show() 

val finalResult = result1.join(result2, result1("placeName") <=> result2("placeName") && result1("customerId") <=> result2("customerId"), "outer").show() 

結果1には合計があり、結果2にはフィルタリング後の合計があります。今、私は見つけるしようとしています:

sqlContext.sql("select top/total as percentile from temp groupBy placeName") 

しかしfinalResultは、重複した列地名とはcustomerIdがあります。誰かが私がここで間違っていることを教えてもらえますか?また、参加をせずにこれを行う方法はありますか?

マイスキーマ: percnetile

{ 
     "country": "France", 
     "customerId": "France001", 
     "visited": [ 
      { 
       "placeName": "US", 
       "rating": "2", 
       "famousRest": "N/A", 
       "placeId": "AVBS34" 

      }, 
       { 
       "placeName": "US", 
       "rating": "3", 
       "famousRest": "SeriousPie", 
       "placeId": "VBSs34" 

      }, 
       { 
       "placeName": "Canada", 
       "rating": "3", 
       "famousRest": "TimHortons", 
       "placeId": "AVBv4d" 

      }   
    ] 
} 

US top = 1 count = 3 
Canada top = 1 count = 3 


{ 
     "country": "Canada", 
     "customerId": "Canada012", 
     "visited": [ 
      { 
       "placeName": "UK", 
       "rating": "3", 
       "famousRest": "N/A", 
       "placeId": "XSdce2" 

      }, 


    ] 
} 
UK top = 1 count = 1 


{ 
     "country": "France", 
     "customerId": "France001", 
     "visited": [ 
      { 
       "placeName": "US", 
       "rating": "4.3", 
       "famousRest": "N/A", 
       "placeId": "AVBS34" 

      }, 
       { 
       "placeName": "US", 
       "rating": "3.3", 
       "famousRest": "SeriousPie", 
       "placeId": "VBSs34" 

      }, 
       { 
       "placeName": "Canada", 
       "rating": "4.3", 
       "famousRest": "TimHortons", 
       "placeId": "AVBv4d" 

      }   
    ] 
} 

米国のトップ= 2カウント= 3 カナダのトップ= 1つのカウント= 3

地名米国(1 + 1 + 2)/(3 + 1 + 3)* 100 カナダ(1 + 1)/(3 + 3)* 100 UK 1 * 100

スキーマ:

root 
|-- country: string(nullable=true) 
|-- customerId:string(nullable=true) 
|-- visited: array (nullable = true) 
| |-- element: struct (containsNull = true) 
| | |-- placeId: string (nullable = true) 
| | |-- placeName: string (nullable = true) 
| | |-- famousRest: string (nullable = true) 
| | |-- rating: string (nullable = true) 

答えて

1

Also is there a way to do this without doing join ?

Can someone tell me what I am doing wrong here ?

何もありません。私は取得しています

result1.join(result2, List("placeName","customerId"), "outer") 
+0

:43:あなたが必要としない場合の両方がこれを使用エラー:型の不一致。 found:List [String] required:org.apache.spark.sql.Column –

+0

私はスパーク1.5を使用しています –

関連する問題