我有2个数据框,比如df1
和df2
。
df1
数据来自数据库,df2
是我从客户端收到的新数据。我需要处理新数据,并根据它是新记录还是要更新的现有记录来执行UPSERTs
。
示例数据输出:
df1= sqlContext.createDataFrame([("xxx1","81A01","TERR NAME 01","NJ"),("xxx2","81A01","TERR NAME 01","NJ"),("xxx3","81A01","TERR NAME 01","NJ"),("xxx4","81A01","TERR NAME 01","CA"),("xx5","81A01","TERR NAME 01","ME")], ["zip_code","territory_code","territory_name","state"])
df2= sqlContext.createDataFrame([("xxx1","81A01","TERR NAME 55","NY"),("xxx2","81A01","TERR NAME 55","NY"),("x103","81A01","TERR NAME 01","NJ")], ["zip_code","territory_code","territory_name","state"])
df1.show()
+--------+--------------+--------------+-----+
|zip_code|territory_code|territory_name|state|
+--------+--------------+--------------+-----+
| xxx1| 81A01| TERR NAME 01| NJ|
| xxx2| 81A01| TERR NAME 01| NJ|
| xxx3| 81A01| TERR NAME 01| NJ|
| xxx4| 81A01| TERR NAME 01| CA|
| xxx5| 81A01| TERR NAME 01| ME|
+---------------------------------------------
# Print out information about this data
df2.show()
+--------+--------------+--------------+-----+
|zip_code|territory_code|territory_name|state|
+--------+--------------+--------------+-----+
| xxx1| 81A01| TERR NAME 55| NY|
| xxx2| 81A01| TERR NAME 55| NY|
| x103| 81A01| TERR NAME 01| NJ|
+---------------------------------------------
预期结果: 我需要将 df2 数据框与 df1 进行比较。 根据上述比较创建2个新数据集,即要更新的记录和要附加/插入数据库的记录。
如果 zip_code & territory_code 是相同的,那么它是UPDATE,否则它是对数据库的INSERT。
例如: INSERT的新数据框输出:
+--------+--------------+--------------+-----+
|zip_code|territory_code|territory_name|state|
+--------+--------------+--------------+-----+
| x103| 81A01| TERR NAME 01| NJ|
+---------------------------------------------
UPDATE的新数据框:
+--------+--------------+--------------+-----+
|zip_code|territory_code|territory_name|state|
+--------+--------------+--------------+-----+
| xxx1| 81A01| TERR NAME 55| NY|
| xxx2| 81A01| TERR NAME 55| NY|
+---------------------------------------------
有人可以帮帮我吗?我正在使用AWS Glue。
更新:解决方案(使用连接和减法)
df3 = df1.join(df2, (df1.zip_code == df2.zip_code_new) & (df1.territory_code == df2.territory_code_new))
df5=df3.drop("zip_code", "territory_code", "territory_name", "state")
df5.show()
+------------+------------------+------------------+---------+
|zip_code_new|territory_code_new|territory_name_new|state_new|
+------------+------------------+------------------+---------+
| x103| 81A01| TERR NAME 01| NJ|
+------------+------------------+------------------+---------+
df4=df2.subtract(df5)
df4.show()
+------------+------------------+------------------+---------+
|zip_code_new|territory_code_new|territory_name_new|state_new|
+------------+------------------+------------------+---------+
| xxx1 | 81A01 | TERR NAME 55 | NY |
| xxx2 | 81A01 | TERR NAME 55 | NY |
+------------------------------------------------------------+
对于RDS数据库更新,我使用pymysql / Mysqldb:
db = MySQLdb.connect("xxxx.rds.amazonaws.com", "username", "password", "databasename")
cursor = db.cursor()
#cursor.execute("REPLACE INTO table SELECT * FROM table_stg")
insertQry = "INSERT INTO table VALUES('xxx1','81A01','TERR NAME 55','NY') ON DUPLICATE KEY UPDATE territory_name='TERR NAME 55', state='NY'"
n=cursor.execute(insertQry)
db.commit()
cursor.fetchall()
db.close()
由于
答案 0 :(得分:1)
这是一个解决方案草图:
在您的唯一键(zip_code和地区)上投射两个帧
使用spark dataframe api计算两个数据帧之间的交集和差异。请看这个链接:How to obtain the difference between two DataFrames?
对键的交集进行更新
为差异做一个插入(在新数据框内,而不是在现有数据中)
在scala中,这看起来像这样 - 在python中它应该非常相似:
import org.apache.spark.sql.SparkSession
case class ZipTerr(zip_code: String, territory_code: String,
territory_name: String, state:String)
case class Key(zip_code: String, territory_code: String)
val spark: SparkSession
val newData = spark.createDataFrame(List(
ZipTerr("xxx1", "81A01", "TERR NAME 01", "NJ"),
ZipTerr("xxx2", "81A01", "TERR NAME 01", "NJ"),
ZipTerr("xxx3", "81A01", "TERR NAME 01", "NJ"),
ZipTerr("xxx4", "81A01", "TERR NAME 01", "CA"),
ZipTerr("xx5","81A01","TERR NAME 01","ME")
))
val oldData = spark.createDataFrame(List(
ZipTerr("xxx1","81A01","TERR NAME 55","NY"),
ZipTerr("xxx2","81A01","TERR NAME 55","NY"),
ZipTerr("x103","81A01","TERR NAME 01","NJ")
))
val newKeys = newData.map(z => Key(z.getAs("zip_code"), z.getAs("territory_code")))
val oldKeys = oldData.map(z => Key(z.getAs("zip_code"), z.getAs("territory_code")))
val keysToInsert = newKeys.except(oldKeys)
val keysToUpdate = newKeys.intersect(oldKeys)
这有帮助吗?
注意:变量名称表明您正在使用胶水动态帧。然而,您正在使用sqlContext.createDataFrame
函数为它们分配纯火花数据帧。
答案 1 :(得分:1)
为了清楚起见,我在这里使用代码片段重现解决方案:
df1= sqlContext.createDataFrame([("xxx1","81A01","TERR NAME 01","NJ"),("xxx2","81A01","TERR NAME 01","NJ"),("xxx3","81A01","TERR NAME 01","NJ"),("xxx4","81A01","TERR NAME 01","CA"),("xx5","81A01","TERR NAME 01","ME")], ["zip_code","territory_code","territory_name","state"])
df2= sqlContext.createDataFrame([("xxx1","81A01","TERR NAME 55","NY"),("xxx2","81A01","TERR NAME 55","NY"),("x103","81A01","TERR NAME 01","NJ")], ["zip_code_new","territory_code_new","territory_name_new","state"])
df1.show()
+--------+--------------+--------------+-----+
|zip_code|territory_code|territory_name|state|
+--------+--------------+--------------+-----+
| xxx1| 81A01| TERR NAME 01| NJ|
| xxx2| 81A01| TERR NAME 01| NJ|
| xxx3| 81A01| TERR NAME 01| NJ|
| xxx4| 81A01| TERR NAME 01| CA|
| xxx5| 81A01| TERR NAME 01| ME|
+---------------------------------------------
# Print out information about this data
df2.show()
+------------+------------------+------------------+---------+
|zip_code_new|territory_code_new|territory_name_new|state_new|
+------------+------------------+------------------+---------+
| xxx1 | 81A01 | TERR NAME 55 | NY |
| xxx2 | 81A01 | TERR NAME 55 | NY |
| x103 | 81A01 | TERR NAME 01 | NJ |
+------------------------------------------------------------+
获取新记录,可以使用" 附加"将其插入到mysql中。操作
df3 = df1.join(df2, (df1.zip_code == df2.zip_code_new) & (df1.territory_code == df2.territory_code_new))
df5=df3.drop("zip_code", "territory_code", "territory_name", "state")
df5.show()
+------------+------------------+------------------+---------+
|zip_code_new|territory_code_new|territory_name_new|state_new|
+------------+------------------+------------------+---------+
| x103| 81A01| TERR NAME 01| NJ|
+------------+------------------+------------------+---------+
然后获取需要更新到mysql数据库的其余记录。在纯python需要的情况下,我们可以使用arr = df1.collect()
,然后是for r in arr:
,否则,我们可以使用pandas迭代器处理每个记录。
df4=df2.subtract(df5)
df4.show()
+------------+------------------+------------------+---------+
|zip_code_new|territory_code_new|territory_name_new|state_new|
+------------+------------------+------------------+---------+
| xxx1 | 81A01 | TERR NAME 55 | NY |
| xxx2 | 81A01 | TERR NAME 55 | NY |
+------------------------------------------------------------+
希望这可以帮助有需要的人。如果在上述场景中有更好的数据帧迭代方法,请告诉我。感谢