2017-11-19 8 views
0

Spark 2.2.0とScala 2.11を使用してDataFrameで何らかの変換を行っています。タイプの不一致:expected String、actualカラム

問題はこのコード行Math.abs($"right.product_price".asInstanceOf[Double] - $"left.product_price".asInstanceOf[Double])で発生します。私はleft.product_priceright.product_priceの絶対差を計算したいと思います。これらの列のいずれかにnullが含まれる場合、null0に変換されます。

しかし、「タイプの不一致:期待される文字列、実際の列」というエラーが表示されます。 この計算を正しい方法で実行するにはどうすればよいですか?

val result = df.as("left") 
    // self-join by gender: 
    .join(df.as("right"), ($"left.gender" === $"right.gender") 
    // limit to 10 results per record: 
    .withColumn("rn", row_number().over(Window.partitionBy($"left.product_PK").orderBy($"right.product_PK"))) 
    .filter($"rn <= 10").drop($"rn") 
    // group and collect_list to create products column: 
    .groupBy($"left.product_PK" as "product_PK") 
    .agg(collect_list(struct($"right.product_PK", Math.abs($"right.product_price".asInstanceOf[Double] - $"right.product_price".asInstanceOf[Double]))) as "products") 

答えて

0

あなたはMath.absを使用することはできませんし、asinstanceOfを使用することはできません。使用SQL functions.abscast

import org.apache.spark.sql.functions.abs 

... 
    .agg(collect_list(struct(
    $"right.product_PK", 
    abs($"right.product_price".cast("double)" - $"right.product_price".cast("double")) 
)) as "products") 

null0に​​3210を追加変換するには:

import org.apache.spark.sql.functions.{coalesce, lit} 

coalesce(column, lit(0)) 
+0

が、それはこの?: '.withColumn( "absolute_price_diff"、ABS(合体($「右と同じです(ドル "rn") .groupBy($ "rn")。 $ "left.product_PK"を "product_PK") .agg(collect_list($ "right.product_PK"、$ "absolute_price_diff"))as "製品") ' – Markus

関連する問題