【发布时间】:2017-02-16 08:33:48
【问题描述】:
我有以下简单的代码,用于将我的 Postgres 数据库中的表加载到 RDD 中。
# this setup is just for spark-submit, will be ignored in pyspark
from pyspark import SparkConf, SparkContext
from pyspark.sql import SQLContext
conf = SparkConf().setAppName("GA")#.setMaster("localhost")
sc = SparkContext(conf=conf)
sqlContext = SQLContext(sc)
# func for loading table
def get_db_rdd(table):
url = "jdbc:postgresql://localhost:5432/harvest?user=postgres"
print(url)
lower = 0
upper = 1000
ret = sqlContext \
.read \
.format("jdbc") \
.option("url", url) \
.option("dbtable", table) \
.option("partitionColumn", "id") \
.option("numPartitions", 1024) \
.option("lowerBound", lower) \
.option("upperBound", upper) \
.option("password", "password") \
.load()
ret = ret.rdd
return ret
# load table, and print results
print(get_db_rdd("mytable").collect())
我运行./bin/pyspark,然后将其粘贴到解释器中,它会按预期打印出我的表中的数据。
现在,如果我将该代码保存到名为 test.py 的文件中,然后执行 ./bin/spark-submit test.py,它会开始运行,但随后我会看到这些消息永远在我的控制台中发送:
17/02/16 02:24:21 INFO Executor: Running task 45.0 in stage 0.0 (TID 45)
17/02/16 02:24:21 INFO JDBCRDD: closed connection
17/02/16 02:24:21 INFO Executor: Finished task 45.0 in stage 0.0 (TID 45). 1673 bytes result sent to driver
编辑:这是在一台机器上。我还没有开始任何主人或奴隶; spark-submit 是我在系统启动后运行的唯一命令。我尝试使用具有相同结果的主/从设置。
我的spark-env.sh 文件如下所示:
export SPARK_WORKER_INSTANCES=2
export SPARK_WORKER_CORES=2
export SPARK_WORKER_MEMORY=800m
export SPARK_EXECUTOR_MEMORY=800m
export SPARK_EXECUTOR_CORES=2
export SPARK_CLASSPATH=/home/ubuntu/spark/pg_driver.jar # Postgres driver I need for SQLContext
export PYTHONHASHSEED=1337 # have to make workers use same seed in Python3
如果我 spark-submit 一个仅从列表或其他内容创建 RDD 的 Python 文件,它会起作用。我只有在尝试使用 JDBC RDD 时才会遇到问题。我错过了什么?
【问题讨论】:
标签: apache-spark jdbc pyspark