如何从不同的数据框添加列:Scala框架

时间:2016-06-30 22:11:23

标签: scala join apache-spark apache-spark-sql

如何添加/追加不同数据框的列?我试图找到被评为3级及以上的placeName的百分位数。

// 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()

result1具有total的行,而result2在过滤后具有total。现在我想找到:

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

但是finalResult有重复的列placeName和customerId。谁能告诉我这里做错了什么?还有没有办法在没有加入的情况下做到这一点?

我的架构:

 {
        "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

PlaceName percnetile 美国(1 + 1 + 2)/(3 + 1 + 3)* 100 加拿大(1 + 1)/(3 + 3)* 100 英国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 个答案:

答案 0 :(得分:1)

  

还有一种方法可以在不加入的情况下执行此操作吗?

没有

  

有人可以告诉我这里做错了吗?

无。如果您不需要两者,请使用:

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