0

は私が火花を使用してCSVファイルから寄木細工構造を作成しましたデータセット:データ型の不一致ながら

Dataset<org.apache.spark.sql.Row> df = spark.read().parquet("sample.parquet"); 
df.createOrReplaceTempView("tmpview"); 
Dataset<Row> namesDF = spark.sql("SELECT *, md5(station_id) as hashkey FROM tmpview"); 

残念ながら、データ型の不一致エラーが発生します。データ型を明示的に割り当てる必要がありますか?

17/04/12九時21分52秒INFO SparkSqlParser:スレッド "メイン" org.apache.spark.sql.AnalysisExceptionでtmpview例外からハッシュキーとしてSELECT *、 MD5(station_id):コマンドの構文解析:データ型の不一致による 'md5(tmpview。station_id)'を解決できません:引数1 には 'tmpview'が必要です。 station_id 'はint型です。 line 1 pos 10; 'subqueryAlias tmpview、tmpview + - Relation [station_id#0、bikes_available#1、#0、bikes_available#1、035、あなたがmd5を適用する前にstringstation_idをキャストする必要があるので、時間の#3] 寄木細工

答えて

1

はい、スパークdocumentationあたりとして、md5関数はbinary(テキスト/文字列)列でのみ動作しますdocks_available#2、。スパークSQLでは、することができますチェーンの両方一緒に、例えば:

Dataset<Row> namesDF = spark.sql("SELECT *, md5(cast(station_id as string)) as hashkey FROM tmpview"); 

それとも、データフレームに新しい列を作成し、それにmd5を適用することができ、例えば:md5cast

val newDf = df.withColumn("station_id_str", df.col("station_id").cast(StringType)) 
newDf.createOrReplaceTempView("tmpview"); 
Dataset<Row> namesDF = spark.sql("SELECT *, md5(station_id_str) as hashkey FROM tmpview");