如何使用pyspark写信给Kafka?

时间:2018-05-07 15:02:15

标签: apache-spark pyspark apache-kafka hortonworks-data-platform

我正在尝试使用PySpark写信给Kafka 我被困在第零阶段:

[Stage 0:>                                                          (0 + 8) / 9]

然后我收到超时错误:

org.apache.kafka.common.errors.TimeoutException: Failed to update metadata after 60000 ms.

代码是:

import os
os.environ['PYSPARK_SUBMIT_ARGS'] = '--packages
 org.apache.spark:spark-sql-kafka-0-10_2.11:2.2.0 pyspark-shell'

from pyspark.sql.functions import *
from pyspark.sql import SparkSession
from pyspark.sql.types import *

def main():
    spark = SparkSession.builder.master("local").appName("Spark CSV Reader")
     .getOrCreate();

    dirpath =  os.path.abspath(sys.argv[1])
    os.chdir(dirpath)

    mySchema = StructType([
     StructField("id", IntegerType()),StructField("name", StringType()),\
     StructField("year", IntegerType()),StructField("rating", DoubleType()),\
     StructField("duration", IntegerType())   ])
    streamingDataFrame = spark.readStream.schema(mySchema)
     .csv('file://' + dirpath + "/" )

    streamingDataFrame.selectExpr("CAST(id AS STRING) AS key",
     "to_json(struct(*)) AS value").\
      writeStream.format("kafka").option("topic", "topicName")\
      .option("kafka.bootstrap.servers", "localhost:9092")\
      .option("checkpointLocation", "./chkpt").start()

我正在运行HDP 2.6。

1 个答案:

答案 0 :(得分:0)

正如我在评论中提到的,Spark在多台机器上运行,所有这些机器都不太可能是Kafka经纪人。

使用Kafka群集的外部地址

.option("kafka.bootstrap.servers", "<kafka-broker-1>:9092,<kafka-broker-2>:9092")\