Pyspark:在远程 Hive Server 中选择数据

Pyspark: selecting data in remote Hive Server

正在尝试从 Pyspark 读取和写入存储在远程 Hive 服务器中的数据。我遵循这个例子:

from os.path import expanduser, join, abspath

from pyspark.sql import SparkSession
from pyspark.sql import Row

# warehouse_location points to the default location for managed databases and tables
warehouse_location = 'hdfs://quickstart.cloudera:8020/user/hive/warehouse'

spark = SparkSession \
    .builder \
    .appName("Python Spark SQL Hive integration example") \
    .config("spark.sql.warehouse.dir", warehouse_location) \
    .enableHiveSupport() \
    .getOrCreate()

示例显示如何在仓库中创建新的 table:

# spark is an existing SparkSession
spark.sql("CREATE TABLE IF NOT EXISTS src (key INT, value STRING) USING hive")
spark.sql("LOAD DATA LOCAL INPATH 'examples/src/main/resources/kv1.txt' INTO TABLE src")

# Queries are expressed in HiveQL
spark.sql("SELECT * FROM src").show()

但是,我需要访问在 mytest.db 中创建的现有表 iris,因此 table 位置是

table_path = warehouse_location + '/mytest.db/iris`

如何从现有 table select?

更新

我有 Metastore url:

http://test.mysite.net:8888/metastore/table/mytest/iris

和table位置url:

hdfs://quickstart.cloudera:8020/user/hive/warehouse/mytest.db/iris

在上面的代码中使用 hdfs://quickstart.cloudera:8020/user/hive/warehouse 作为仓库位置并尝试时:

spark.sql("use mytest")

我遇到异常:

    raise AnalysisException(s.split(': ', 1)[1], stackTrace)
pyspark.sql.utils.AnalysisException: "Database 'mytest' not found;"

iris 到 url 到 select 什么是正确的?

您可以使用

直接调用 table
spark.sql("SELECT * FROM mytest.iris")

或者指定要使用的数据库

spark.sql("use mytest")
spark.sql("SELECT * FROM iris)