Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Apache spark 使用spark结构化流在postgresql中升级数据_Apache Spark_Spark Structured Streaming - Fatal编程技术网

Apache spark 使用spark结构化流在postgresql中升级数据

Apache spark 使用spark结构化流在postgresql中升级数据,apache-spark,spark-structured-streaming,Apache Spark,Spark Structured Streaming,我正在尝试使用(py)spark运行结构化流媒体应用程序。我的数据是从卡夫卡主题中读取的,然后在事件时间运行窗口聚合 # I have been able to create data frame pn_data_df after reading data from Kafka Schema of pn_data_df | - id StringType - source StringType - source_id StringType - delivered_t

我正在尝试使用(py)spark运行结构化流媒体应用程序。我的数据是从卡夫卡主题中读取的,然后在事件时间运行窗口聚合

# I have been able to create data frame pn_data_df after reading data from Kafka

Schema of pn_data_df
  |
   - id StringType
   - source StringType
   - source_id StringType
   - delivered_time TimeStamp

windowed_report_df = pn_data_df.filter(pn_data_df.source == 'campaign') \
    .withWatermark("delivered_time", "24 hours") \
    .groupBy('source_id', window('delivered_time', '15 minute')) \
    .count()
windowed_report_df = windowed_report_df \
    .withColumn('start_ts', unix_timestamp(windowed_report_df.window.start)) \
    .withColumn('end_ts', unix_timestamp(windowed_report_df.window.end)) \
    .selectExpr('CAST(source_id as LONG)', 'start_ts', 'end_ts', 'count')
我正在将这个窗口聚合写入我已经创建的postgresql数据库

CREATE TABLE pn_delivery_report(
   source_id bigint not null,
   start_ts bigint not null,
   end_ts bigint not null,
   count integer not null,
   unique(source_id, start_ts)
);
使用spark jdbc写入postgresql允许我
追加
覆盖
。如果数据库中存在现有的复合键,则追加模式将失败,而Overwrite只会用当前批处理输出覆盖整个表

def write_pn_report_to_postgres(df, epoch_id):
    df.write \
    .mode('append') \
    .format('jdbc') \
    .option("url", "jdbc:postgresql://db_endpoint/db") \
    .option("driver", "org.postgresql.Driver") \
    .option("dbtable", "pn_delivery_report") \
    .option("user", "postgres") \
    .option("password", "PASSWORD") \
    .save()

windowed_report_df.writeStream \
   .foreachBatch(write_pn_report_to_postgres) \
   .option("checkpointLocation", '/home/hadoop/campaign_report_df_windowed_checkpoint') \
   .outputMode('update') \
   .start()
如何执行这样的查询

INSERT INTO pn_delivery_report (source_id, start_ts, end_ts, COUNT)
VALUES (1001, 125000000001, 125000050000, 128),
       (1002, 125000000001, 125000050000, 127) ON conflict (source_id, start_ts) DO
UPDATE
SET COUNT = excluded.count;
foreachBatch

Spark有一张jira的特写票,但似乎到目前为止它还没有被优先考虑


您是否设法解决了您的问题?我想知道如何做同样的事情@Abhishek@Mahnaz-没有。我放弃了这个,而是使用了ksql。你可以在我为Confluent撰写的博客上阅读我的实现谢谢,我读了你的博客,这是一个很好的解决方案,虽然它看起来很贵。维护和监控所有这些功能(spark结构化流媒体+至少3个卡夫卡主题+Ksql+卡夫卡连接作业)应该很困难(我的意思是如何确保所有功能都已启动并运行?)。你知道这样的流媒体作业有什么编排工具吗?你用过吗@Abhishek@Mahnaz-我没有使用Spark结构化流媒体。我使用Python workers将事件流式传输到Kafka,并使用Ksql聚合事件。我们的用例非常简单,没有使用任何编排工具。我们使用Kubernetes来托管infra,因此维护和监控变得非常容易。但是,您始终可以使用托管Ksql并在融合云上连接,而融合云不需要任何维护。您是否设法解决了您的问题?我想知道如何做同样的事情@Abhishek@Mahnaz-没有。我放弃了这个,而是使用了ksql。你可以在我为Confluent撰写的博客上阅读我的实现谢谢,我读了你的博客,这是一个很好的解决方案,虽然它看起来很贵。维护和监控所有这些功能(spark结构化流媒体+至少3个卡夫卡主题+Ksql+卡夫卡连接作业)应该很困难(我的意思是如何确保所有功能都已启动并运行?)。你知道这样的流媒体作业有什么编排工具吗?你用过吗@Abhishek@Mahnaz-我没有使用Spark结构化流媒体。我使用Python workers将事件流式传输到Kafka,并使用Ksql聚合事件。我们的用例非常简单,没有使用任何编排工具。我们使用Kubernetes来托管infra,因此维护和监控变得非常容易。但是,您可以始终使用托管Ksql并在融合云上进行连接,这不需要任何维护。