方法:
hive配置hiveserver2和metastore url
<!-- 指定hiveserver2连接的host -->
<property><name>hive.server2.thrift.bind.host</name><value>hadoop111</value>
</property><!-- 指定hiveserver2连接的端口号 -->
<property><name>hive.server2.thrift.port</name><value>10000</value>
</property><!-- 指定存储元数据要连接的地址 -->
<property><name>hive.metastore.uris</name><value>thrift://hadoop111:9083</value>
</property>
启动metastore
hive --service metastore
启动hiveserver2
hive --service hiveserver2
python代码
给个demo示例:
from pyspark.sql import SparkSession# 创建 SparkSession
spark = SparkSession \.builder \.appName("HiveConnectionExample") \.config("spark.sql.warehouse.dir", "hdfs://192.168.10.111:8020/user/hive/warehouse") \.config("hive.metastore.uris", "thrift://192.168.10.111:9083") \.enableHiveSupport() \.getOrCreate()# 读取 Hive 表
# df = spark.sql("SELECT * FROM a")
df = spark.sql("show tables")
df.show()# 关闭 SparkSession
spark.stop()