【问题标题】:PySpark/HIVE: append to an existing tablePySpark/HIVE:附加到现有表
【发布时间】:2017-11-29 16:09:25
【问题描述】:

真正的基本问题 pyspark/hive 问题:

如何追加到现有表?我的尝试如下

from pyspark import SparkContext, SparkConf
from pyspark.sql import HiveContext
conf_init = SparkConf().setAppName('pyspark2')
sc = SparkContext(conf = conf_init)
hive_cxt = HiveContext(sc)

import pandas as pd
df = pd.DataFrame({'a':[0,0], 'b':[0,0]})
sdf = hive_cxt.createDataFrame(df)
sdf.write.mode('overwrite').saveAsTable('database.table') #this line works

df = pd.DataFrame({'a':[1,1,1], 'b':[2,2,2]})
sdf = hive_cxt.createDataFrame(df)
sdf.write.mode('append').saveAsTable('database.table') #this line does not work
#sdf.write.insertInto('database.table',overwrite = False) #this line does not work

谢谢! 山姆

【问题讨论】:

  • sdf.write.mode('append') 是附加到 Hive 表的正确方法。在 PySpark 2.2.0 中,上面的示例按预期工作。您收到任何错误消息吗?
  • 嗯。我正在使用相同的版本。我收到一长串错误消息,但我认为这是主要的:IllegalArgumentException: 'Expected exactly one path to be specified, but got: '
  • 这听起来像是 Hive 配置问题。如果您将.option("path", "hdfs://....") 添加到写入中,它是否有效?
  • 感谢您的帮助!愚蠢的问题,如何获得hdfs的路径? (我在展示我是多么的新手!我用谷歌搜索了它,但结果并不是很有帮助。)
  • HDFS 路径,如果你配置好了,可以在 Spark 的 Master WebUI(8080 端口)中找到。您的错误消息表明路径为空。

标签: python apache-spark hive pyspark


【解决方案1】:

似乎使用 option('overwrite') 导致了问题;它删除表,然后重新创建一个新表。如果我执行以下操作,一切正常:

from pyspark import SparkContext, SparkConf
from pyspark.sql import HiveContext

conf_init = SparkConf().setAppName('pyspark2')
sc = SparkContext(conf = conf_init)
print(sc.version)
hive_cxt = HiveContext(sc)
hive_cxt.sql('USE database')

query = """
        CREATE TABLE IF NOT EXISTS table (a int, b int)
        STORED AS parquet
        """
hive_cxt.sql(query)

import pandas as pd
df = pd.DataFrame({'a':[0,0], 'b':[0,0]})
sdf = hive_cxt.createDataFrame(df)
sdf.write.mode('append').format('hive').saveAsTable('table')

query = """
        SELECT *
        FROM   table
        """
df = hive_cxt.sql(query)
df = df.toPandas()
print(df) # successfully pull the data in table

df = pd.DataFrame({'a':[1,1,1], 'b':[2,2,2]})
sdf = hive_cxt.createDataFrame(df)
sdf.write.mode('append').format('hive').saveAsTable('table')

【讨论】:

    【解决方案2】:

    我认为您之前忘记使用格式选项,当您尝试追加而不是像上面提到的那样覆盖时会导致问题。

    【讨论】:

      猜你喜欢
      • 2022-01-26
      • 2018-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-20
      • 2020-05-23
      • 2018-07-13
      • 1970-01-01
      相关资源
      最近更新 更多