[英]Trying to write a streaming dataframe from spark in postgreSQL with Kafka and pyspark
我一直在这个网站的各个方面搜索这个问题,但我没有找到任何解决方案。 我写了一个 java class 在 Kafka 中创建一个生产者并发送一些文件并且它工作正常。 然后,我想编写一个 python 脚本来读取这些文件并将它们放入 postgreSQL 中的数据库中。
每个文件(每个文件都是一个包含很多列的数据集)成为 kafka 消费者中的一个主题,文件的每一行成为相关主题中的一条消息。
这是我根据流数据在 python 中创建的火花 dataframe:
list = df.select("fileName", "Satellite_PRN_number", "date", "time", "Crs", "Delta_n", "m0", "Cuc",
"e_Eccentricity",
"Cus",
"sqrt_A", "Toe_Time_of_Ephemeris", "Cic", "OMEGA_maiusc", "cis", "i0", "Crc", "omega",
"omega_dot",
"idot")
这是我的 python function 应该在我的 postgreSQL 表中插入每一行。 我使用 psycopg2 在 python 和 postgre 之间创建连接,并使用“self.cursor.execute”来编写查询。
def process_row(self, row):
self.cursor.execute(
'INSERT INTO satellite(fileName,Satellite_PRN_number, date, time,Crs,Delta_n, m0,
Cuc,e_Eccentricity,Cus,'
'sqrt_A, Toe_Time_of_Ephemeris, Cic, OMEGA_maiusc, cis, i0, Crc, omega, omega_dot, idot) VALUES
(%s,%s,%s,'
'%s,%s,%s, %s, %s, %s, %s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)',
(row.fileName, row.Satellite_PRN_number, row.date, row.time, row.Crs, row.Delta_n, row.m0, row.Cuc,
row.e_Eccentricity,
row.Cus, row.sqrt_A, row.Toe_Time_of_Ephemeris, row.Cic, row.OMEGA_maiusc, row.cis, row.i0,
row.Crc,
row.omega,
row.omega_dot, row.idot))
self.connection.commit()
最后,我使用上面的方法通过以下命令填充 postgreSQL 中的表:
query = list.writeStream.outputMode("append").foreachBatch(process_row)\
.option("checkpointLocation", "C:\\Users\\Admin\\AppData\\Local\\Temp").start()
我收到以下错误: AttributeError: 'DataFrame' object has no attribute 'cursor'
。
我认为问题出在 row.fileName 等...或方法“process_row”中。 我不完全明白如何管理方法“process_row”以便传递流式传输的每一行 dataframe 来填充 posteSQL 表。
谁能帮我? 谢谢。
您的 foreachBatch 签名似乎不正确。 它应该是这样的:
def foreach_batch_function(df, epoch_id):
# Transform and write batchDF
pass
streamingDF.writeStream.foreachBatch(foreach_batch_function).start()
如您所见,forEachBatch function 的第一个参数是 DataFrame,而不是您期望的 psycopg2 实例 class。ForEachBatch 将有一个 DataFrame,它本身将包含当前微批次中的所有行,而不仅仅是一行。
因此,您可以尝试在该 function 中声明 postgreSQL 连接的实例以进一步使用它,或者您可以尝试这种方法:
我会创建一个 hive jdbc 源表,你的 postgreSQL 数据库是这样的:
CREATE TABLE jdbcTable
USING org.apache.spark.sql.jdbc
OPTIONS (
url "jdbc:postgresql:dbserver",
dbtable "schema.tablename",
user 'username',
password 'password'
)
这将使您能够像这样使用 forEachBatch function:
def foreach_batch_function(df, epoch_id):
# Transform and write batchDF
df.write.insertInto("jdbcTable")
希望对您有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.