问题描述
我正在尝试使用PySpark和结构化流(Spark 2.3)在两个Kafka Stream之间建立左外部连接.
I'm trying to make a left outer join between two Kafka Stream using PySpark and Structured Streaming (Spark 2.3).
import os
import time
from pyspark.sql.types import *
from pyspark.sql.functions import from_json, col, struct, explode, get_json_object
from ast import literal_eval
from pyspark.sql import SparkSession
from pyspark.sql.functions import expr
os.environ['PYSPARK_SUBMIT_ARGS'] = '--packages org.apache.spark:spark-sql-kafka-0-10_2.11:2.3.0 pyspark-shell'
spark = SparkSession \
.builder \
.appName("Spark Kafka Structured Streaming") \
.getOrCreate()
schema_impressions = StructType() \
.add("id_req", StringType()) \
.add("ts_imp_request", TimestampType()) \
.add("country", StringType()) \
.add("TS_IMPRESSION", TimestampType())
schema_requests = StructType() \
.add("id_req", StringType()) \
.add("page", StringType()) \
.add("conntype", StringType()) \
.add("TS_REQUEST", TimestampType())
impressions = spark.readStream \
.format("kafka") \
.option("kafka.bootstrap.servers", "ip-ec2.internal:9092") \
.option("subscribe", "ssp.datascience_impressions") \
.load()
requests = spark \
.readStream \
.format("kafka") \
.option("kafka.bootstrap.servers", "ip-ec2.internal:9092") \
.option("subscribe", "ssp.datascience_requests") \
.option("startingOffsets", "latest") \
.load()
query_requests = requests \
.select(col("timestamp"), col("key").cast("string"), from_json(col("value").cast("string"), schema_requests).alias("parsed")) \
.select(col("timestamp").alias("timestamp_req"), "parsed.id_req", "parsed.page", "parsed.conntype", "parsed.TS_REQUEST") \
.withWatermark("timestamp_req", "120 seconds")
query_impressions = impressions \
.select(col("timestamp"), col("key").cast("string"), from_json(col("value").cast("string"), schema_impressions).alias("parsed")) \
.select(col("timestamp").alias("timestamp_imp"), col("parsed.id_req").alias("id_imp"), "parsed.ts_imp_request", "parsed.country", "parsed.TS_IMPRESSION") \
.withWatermark("timestamp_imp", "120 seconds")
query_requests.printSchema()
query_impressions.printSchema()
> root
|-- timestamp_req: timestamp (nullable = true)
|-- id_req: string (nullable = true)
|-- page: string (nullable = true)
|-- conntype: string (nullable = true)
|-- TS_REQUEST: timestamp (nullable = true)
>
> root |-- timestamp_imp: timestamp (nullable = true)
|-- id_imp: string (nullable = true)
|-- ts_imp_request: timestamp (nullable = true)
|-- country: string (nullable = true)
|-- TS_IMPRESSION: timestamp (nullable = true)
在简历中,我将从两个Kafka流中获取数据,在接下来的几行中,我将尝试使用ID进行联接.
In resume, I will obtain data from two Kafka Streams, and in the next lines, I will try to make join using the IDs.
rawQuery = query_requests.join(query_impressions, expr("""
(id_req = id_imp AND
timestamp_imp >= timestamp_req AND
timestamp_imp <= timestamp_req + interval 5 minutes)
"""),
"leftOuter")
rawQuery = rawQuery \
.writeStream \
.format("parquet") \
.option("checkpointLocation", "/home/jovyan/streaming/applicationHistory") \
.option("path", "/home/jovyan/streaming").start()
print(rawQuery.status)
在处理上述异常期间,发生了另一个异常:
During handling of the above exception, another exception occurred:
回溯(最近通话最近):文件"/opt/conda/lib/python3.6/site-packages/py4j/java_gateway.py",行908,在send_command中响应= connection.send_command(命令)文件"/opt/conda/lib/python3.6/site-packages/py4j/java_gateway.py",行1067,在send_command中py4j.protocol.Py4JNetworkError:接收时出错",proto.ERROR_ON_RECEIVE)错误:py4j.java_gateway:尝试连接到时发生错误Java服务器(127.0.0.1:33968)追溯(最近一次调用为:):
文件"/opt/conda/lib/python3.6/site-packages/IPython/core/interactiveshell.py",第2910行,在run_code中exec(code_obj,self.user_global_ns,self.user_ns)文件",第3行,在打印(rawQuery.status)文件"/opt/conda/lib/python3.6/site-packages/pyspark/sql/streaming.py",第114行,状态为返回json.loads(self._jsq.status().json())文件"/opt/conda/lib/python3.6/site-packages/py4j/java_gateway.py",行1160,在致电中答案,self.gateway_client,self.target_id,self.name)文件"/opt/conda/lib/python3.6/site-packages/pyspark/sql/utils.py",行63,在装饰中返回f(* a,** kw)文件"/opt/conda/lib/python3.6/site-packages/py4j/protocol.py",第328行,在get_return_value中格式(target_id,.",名称))py4j.protocol.Py4JError:调用o92.status时发生错误
Traceback (most recent call last): File "/opt/conda/lib/python3.6/site-packages/py4j/java_gateway.py", line 908, in send_command response = connection.send_command(command) File "/opt/conda/lib/python3.6/site-packages/py4j/java_gateway.py", line 1067, in send_command "Error while receiving", e, proto.ERROR_ON_RECEIVE) py4j.protocol.Py4JNetworkError: Error while receiving ERROR:py4j.java_gateway:An error occurred while trying to connect to the Java server (127.0.0.1:33968) Traceback (most recent call last):
File "/opt/conda/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2910, in run_code exec(code_obj, self.user_global_ns, self.user_ns) File "", line 3, in print(rawQuery.status) File "/opt/conda/lib/python3.6/site-packages/pyspark/sql/streaming.py", line 114, in status return json.loads(self._jsq.status().json()) File "/opt/conda/lib/python3.6/site-packages/py4j/java_gateway.py", line 1160, in call answer, self.gateway_client, self.target_id, self.name) File "/opt/conda/lib/python3.6/site-packages/pyspark/sql/utils.py", line 63, in deco return f(*a, **kw) File "/opt/conda/lib/python3.6/site-packages/py4j/protocol.py", line 328, in get_return_value format(target_id, ".", name)) py4j.protocol.Py4JError: An error occurred while calling o92.status
在处理上述异常期间,发生了另一个异常:
During handling of the above exception, another exception occurred:
回溯(最近通话最近):文件"/opt/conda/lib/python3.6/site-packages/IPython/core/interactiveshell.py",在showtraceback中的第1828行stb = value._render_traceback_()AttributeError:"Py4JError"对象没有属性"_render_traceback _"
Traceback (most recent call last): File "/opt/conda/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 1828, in showtraceback stb = value._render_traceback_() AttributeError: 'Py4JError' object has no attribute '_render_traceback_'
在处理上述异常期间,发生了另一个异常:
During handling of the above exception, another exception occurred:
回溯(最近通话最近):文件"/opt/conda/lib/python3.6/site-packages/py4j/java_gateway.py",行852,在_get_connection中connection = self.deque.pop()IndexError:从一个空的双端队列弹出
Traceback (most recent call last): File "/opt/conda/lib/python3.6/site-packages/py4j/java_gateway.py", line 852, in _get_connection connection = self.deque.pop() IndexError: pop from an empty deque
我正在使用Jupyter Notebook在本地运行Spark.在spark/conf/ spark-defaults.conf 中,我有:
I'm running Spark in local using Jupyter Notebook. In the spark/conf/spark-defaults.conf I have:
# Example:
# spark.master spark://master:7077
# spark.eventLog.enabled true
# spark.eventLog.dir hdfs://namenode:8021/directory
# spark.serializer org.apache.spark.serializer.KryoSerializer
spark.driver.memory 15g
# spark.executor.extraJavaOptions -XX:+PrintGCDetails -Dkey=value -Dnumbers="one two three"
如果在上一个错误之后尝试使用Spark,我会收到该错误:
If I try to use Spark after the previous error, I received that error:
在处理上述异常期间,发生了另一个异常:
During handling of the above exception, another exception occurred:
回溯(最近通话最近):文件"/opt/conda/lib/python3.6/site-packages/py4j/java_gateway.py",行908,在send_command中响应= connection.send_command(命令)文件"/opt/conda/lib/python3.6/site-packages/py4j/java_gateway.py",行1067,在send_command中py4j.protocol.Py4JNetworkError:接收时出错",proto.ERROR_ON_RECEIVE)
Traceback (most recent call last): File "/opt/conda/lib/python3.6/site-packages/py4j/java_gateway.py", line 908, in send_command response = connection.send_command(command) File "/opt/conda/lib/python3.6/site-packages/py4j/java_gateway.py", line 1067, in send_command "Error while receiving", e, proto.ERROR_ON_RECEIVE) py4j.protocol.Py4JNetworkError: Error while receiving
推荐答案
我解决了这个问题!基本上,由于某种原因,该问题与Jupyter Notebook有关.我删除了先前代码的下一行:
I resolved the problem! Basically, the problem was related to Jupyter Notebook for some reason. I removed the next line of the previous code:
os.environ['PYSPARK_SUBMIT_ARGS'] = '--packages org.apache.spark:spark-sql-kafka-0-10_2.11:2.3.0 pyspark-shell'
然后我使用控制台运行代码:
And I ran the code using the console:
> spark-submit --packages org.apache.spark:spark-sql-kafka-0-10_2.11:2.3.0 spark_structured.py
这样,我可以毫无问题地运行所有代码.
In that way, I could run all the code without problems.
如果您遇到相同的问题,也可以更改 spark-default.conf ,并增加 spark.driver.memory 和 spark.executor.memory
In case of you have the same problem, you can also changethe spark-default.conf and increase the spark.driver.memory and spark.executor.memory
这篇关于结构化流错误py4j.protocol.Py4JNetworkError:来自Java端的答案为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!