【问题标题】:Reading Data from Hive through Pyspark通过 Pyspark 从 Hive 读取数据
【发布时间】:2019-04-21 10:21:56
【问题描述】:

我正在尝试通过 Pyspark 从 Hive 表中读取数据。我已经成功地在 Hive 和 spark 之间建立了连接。我还可以看到数据库中存在的表,但是当我尝试查询表时出现此错误:

代码: spark.sql("select count(*) from my_table").show(truncate = False)

错误:

Py4JJavaError: 调用 o90.showString 时出错

【问题讨论】:

    标签: python hive pyspark


    【解决方案1】:

    您可以使用SQLContext() 尝试此解决方案:

    from pyspark import SparkContext
    from pyspark.sql import SQLContext
    
    sc = SparkContext()
    sqlC = SQLContext(sc)
    
    sqlC.sql("select count(*) from my_table").show(truncate = False)
    

    或者使用HiveContext()试试这个:

    from pyspark import SparkContext
    from pyspark.sql import HiveContext
    
    sc = SparkContext()
    hivctx = HiveContext(sc)
    
    hivctx.sql("select count(*) from my_table").show(truncate = False)
    

    【讨论】:

    • 您的表是否保存在数据库中?你能告诉我my_table的类型吗?
    • 是的表在 Hive 数据库中。 my_table 是 hive 数据库中的表名
    • 如果我这样执行我的查询: df= sparkSession.sql('select * from my_table') .. 它没有给出错误.. 我什至可以通过执行 df 查看表模式显示 DataFrame[version: bigint, app: string, bytes: smallint, city: string, client_ip: string, code: tinyint, country_code: string, country_code3: string].. 但我不能访问该表的数据
    • 您的表格中是否有任何非UTF-8null 字符?
    • 我看过类似的错误解决方案,他们说也许这就是原因。尝试df.registerTempTable('testTable') 然后spark.sql('select count(*) from testTable').show()不要从 hive 数据库中读取。检查你是否得到同样的错误!
    猜你喜欢
    • 2019-07-09
    • 2019-07-27
    • 1970-01-01
    • 2013-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多