web-dev-qa-db-ja.com

データフレームに参加するspark java

まず、私の質問を読んでいただきありがとうございます。

私の質問は次のとおりです:Spark Javaの場合、2つのデータフレームに2つのcsvファイルのデータをロードします。

これらのデータフレームには、次の情報が含まれます。

データフレーム空港

Id | Name    | City
-----------------------
1  | Barajas | Madrid

データフレームairport_city_state

City | state
----------------
Madrid | España

この2つのデータフレームを結合して、次のようにします。

データフレーム結果

Id | Name    | City   | state
--------------------------
1  | Barajas | Madrid | España

どこ dfairport.city = dfaiport_city_state.city

しかし、構文を明確にできないため、結合を正しく行うことができます。変数の作成方法の小さなコード:

 // Load the csv, you have to specify that you have header and what delimiter you have
Dataset <Row> dfairport = Load.Csv (sqlContext, data_airport);
Dataset <Row> dfairport_city_state = Load.Csv (sqlContext,   data_airport_city_state);


// Change the name of the columns in the csv dataframe to match the columns in the database
// Once they match the name we can insert them
Dfairport
.withColumnRenamed ("leg_key", "id")
.withColumnRenamed ("leg_name", "name")
.withColumnRenamed ("leg_city", "city")

dfairport_city_state
.withColumnRenamed("city", "ciudad")
.withColumnRenamed("state", "estado");
6
Alejandro Reina

まず、ご回答ありがとうございます。

私は両方の解決策を試しましたが、どれも機能しません。次のエラーが発生します。メソッドdfairport_city_state(String)がタイプETL_Airportに対して未定義です

結合するデータフレームの特定の列にアクセスできません。

編集:すでに参加する必要があります、誰かが助けた場合のためにここに解決策を置きます;)

何卒よろしくお願いいたします

//Join de tablas en las que comparten ciudad
Dataset <Row> joined = dfairport.join(dfairport_city_state, dfairport.col("leg_city").equalTo(dfairport_city_state.col("city")));
7
Alejandro Reina

列名を指定したjoinメソッドを使用して、2つのデータフレームを結合できます。例:

_Dataset <Row> dfairport = Load.Csv (sqlContext, data_airport);
Dataset <Row> dfairport_city_state = Load.Csv (sqlContext,   data_airport_city_state);

Dataset <Row> joined = dfairport.join(dfairport_city_state, dfairport_city_state("City"));
_

joinタイプを3番目の引数として指定できるようにするオーバーロードバージョンもあります。例:

Dataset <Row> joined = dfairport.join(dfairport_city_state, dfairport_city_state("City"), "left_outer");

ここ の結合についての詳細。

11
Darshan Mehta