Ale*_*ina 6 java dataframe apache-spark spark-dataframe
首先,感谢您抽出时间阅读我的问题.
我的问题如下:在Spark with Java中,我在两个数据帧中加载了两个csv文件的数据.
这些数据框将具有以下信息.
Dataframe机场
Id | Name | City
-----------------------
1 | Barajas | Madrid
Run Code Online (Sandbox Code Playgroud)
Dataframe airport_city_state
City | state
----------------
Madrid | España
Run Code Online (Sandbox Code Playgroud)
我想加入这两个数据帧,使它看起来像这样:
数据帧结果
Id | Name | City | state
--------------------------
1 | Barajas | Madrid | España
Run Code Online (Sandbox Code Playgroud)
哪里 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");
Run Code Online (Sandbox Code Playgroud)
您可以使用join带有列名的方法来连接两个数据帧,例如:
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"));
Run Code Online (Sandbox Code Playgroud)
还有一个重载版本,允许您将join类型指定为第三个参数,例如:
Dataset <Row> joined = dfairport.join(dfairport_city_state, dfairport_city_state("City"), "left_outer");
这里的更多的连接.
首先,非常感谢您的答复。
我已经尝试了两种解决方案,但是都无法使用,但出现以下错误:未为类型ETL_Airport定义方法dfairport_city_state(String)
我无法访问数据框的特定列以进行连接。
编辑:已经要做加入,我把解决方案放在这里,以防有人帮助;)
感谢您所做的一切和最诚挚的问候
//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")));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14423 次 |
| 最近记录: |