I need to insert records into table1 , based on number of records in another table, say table2 , using pyspark's spark.sql(). Currently am able to get one record by doing join, but i need to get as many records inserted into table1 based on 2nd table.
Am providing sample dataframes here:
df1= sqlContext.createDataFrame([("xxx1","81A01","TERR NAME 01"),("xxx1","81A01","TERR NAME 02"), ("xxx1","81A01","TERR NAME 03")], ["zip_code","zone_code","territory_name"])
df2= sqlContext.createDataFrame([("xxx1","81A01","","NY")], ["zip_code","zone_code","territory_name","state"])
df1.show()
+--------+--------------+--------------+
|zip_code|zone_code |territory_name|
+--------+--------------+--------------+
| xxx1| 81A01| TERR NAME 01|
| xxx1| 81A01| TERR NAME 02|
| xxx1| 81A01| TERR NAME 03|
+---------------------------------------
# Print out information about this data
df2.show()
+--------+--------------+--------------+-----+
|zip_code|zone_code |territory_name|state|
+--------+--------------+--------------+-----+
| xxx1| 81A01| null | NY|
+---------------------------------------------
In the above sample i need to join df2 with df1, based on the zip_code, and get as many records as that of territory_names in df1.
Expected result in df2 is:
+--------+--------------+--------------+-----+
|zip_code|zone_code |territory_name|state|
+--------+--------------+--------------+-----+
| xxx1| 81A01| TERR NAME 01| NY|
| xxx1| 81A01| TERR NAME 02| NY|
| xxx1| 81A01| TERR NAME 03| NY|
+---------------------------------------------
Need help please, currently am able to get one record by doing join
Spark.sql query sample for getting one record:
df1.createOrReplaceTempView('df1')
df2.createOrReplaceTempView('df2')
spark.sql("select a.zip_code,a.zone_code,b.territory_name,a.state from df1 a
left join df2 b on a.zip_code = b.zip_code where a.territory_name is null").createOrReplaceTempView('df2')
Thanks
Would like to provide the code snippet, so maybe it would be useful to some.
df1= sqlContext.createDataFrame([("xxx1","81A01","TERR NAME 01"),("xxx1","81A01","TERR NAME 02"), ("xxx1","81A01","TERR NAME 03")], ["zip_code","zone_code","territory_name"])
df2= sqlContext.createDataFrame([("xxx1","","","NY"), ("xxx1","","TERR NAME 99","NY")], ["zip_code","zone_code","territory_name","state"])
df1.createOrReplaceTempView('df1')
df2.createOrReplaceTempView('df2')
spark.sql(“select * from df1”)
+--------+---------+--------------+
|zip_code|zone_code|territory_name|
+--------+---------+--------------+
| xxx1 | 81A01 | TERR NAME 01 |
| xxx1 | 81A01 | TERR NAME 02 |
| xxx1 | 81A01 | TERR NAME 03 |
+--------+---------+--------------+
spark.sql(“select * from df2”)
+--------+---------+--------------+-----+
|zip_code|zone_code|territory_name|state|
+--------+---------+--------------+-----+
| xxx1 | | | NY |
| xxx1 | | TERR NAME 99 | NY |
+--------+---------+--------------+-----+
spark.sql("""select a.zip_code, b.zone_code, b.territory_name, a.state from df2 a
left join df1 b
on a.zip_code = b.zip_code
where a.territory_name = ''
UNION
select a.zip_code, b.zone_code, a.territory_name, a.state from df2 a
left join df1 b
on a.zip_code = b.zip_code
where a.territory_name != ''
""").createOrReplaceTempView('df3')
spark.sql(“select * from df3”)
+--------+---------+--------------+-----+
|zip_code|zone_code|territory_name|state|
+--------+---------+--------------+-----+
| xxx1 | 81A01 | TERR NAME 03 | NY |
| xxx1 | 81A01 | TERR NAME 99 | NY |
| xxx1 | 81A01 | TERR NAME 01 | NY |
| xxx1 | 81A01 | TERR NAME 02 | NY |
+--------+---------+--------------+-----+
Thanks to those who helped.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.