如何从spark连接到远程hive服务器

tzxcd3kk  于 2021-06-28  发布在  Hive
关注(0)|答案(3)|浏览(496)

这个问题在这里已经有答案了

如何在没有hive-site.xml的情况下将spark sql连接到远程hive元存储(通过thrift协议)(8个答案)
10个月前关门了。
我正在本地运行spark,希望访问位于远程hadoop集群中的配置单元表。
我可以通过在sparkïu家下的直线发射进入Hive的table

[ml@master spark-2.0.0]$./bin/beeline 
Beeline version 1.2.1.spark2 by Apache Hive
beeline> !connect jdbc:hive2://remote_hive:10000
Connecting to jdbc:hive2://remote_hive:10000
Enter username for jdbc:hive2://remote_hive:10000: root
Enter password for jdbc:hive2://remote_hive:10000:******
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/home/ml/spark/spark-2.0.0/jars/slf4j-log4j12-1.7.16.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/usr/hadoop/share/hadoop/common/lib/slf4j-log4j12-1.7.10.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]
16/10/12 19:06:39 INFO jdbc.Utils: Supplied authorities: remote_hive:10000
16/10/12 19:06:39 INFO jdbc.Utils: Resolved authority: remote_hive:10000
16/10/12 19:06:39 INFO jdbc.HiveConnection: Will try to open client transport with JDBC Uri: jdbc:hive2://remote_hive:10000
Connected to: Apache Hive (version 1.2.1000.2.4.2.0-258)
Driver: Hive JDBC (version 1.2.1.spark2)
Transaction isolation: TRANSACTION_REPEATABLE_READ
0: jdbc:hive2://remote_hive:10000>

如何从spark以编程方式访问远程配置单元表?

np8igboo

np8igboo1#

不需要jdbc

spark直接连接到hive元存储,而不是通过hiveserver2。要配置它,
hive-site.xml 在你的 classpath ,并指定 hive.metastore.uri 到您的配置单元元存储所在的位置。另请参阅如何在sparksql中以编程方式连接到配置单元元存储?
导入 org.apache.spark.sql.hive.HiveContext ,因为它可以对配置单元表执行sql查询。
定义 val sqlContext = new org.apache.spark.sql.hive.HiveContext(sc) 验证 sqlContext.sql("show tables") 看看是否有效
配置单元表上的sparksql

结论:如果你一定要用jdbc的方式

看看远程连接apachespark和apachehive。
请注意,beeline还通过jdbc连接。从你的日志来看,这是不言而喻的。
[ml@master spark-2.0.0]$./bin/beeline beeline版本1.2.1.spark2 by apache hive beeline>!连接jdbc:hive2://远程_hive:10000
正在连接到jdbc:hive2://远程_hive:10000
所以请看一下这篇有趣的文章
方法1:使用jdbc将表拉入spark
方法2:将spark jdbcrdd与hiveserver2 jdbc驱动程序一起使用
方法3:在客户端获取数据集,然后手动创建rdd
目前hiveserver2驱动程序不允许我们使用“sparkling”方法1和2,我们只能依赖方法3
下面是可以实现的示例代码片段
通过hiveserver2jdbc连接将数据从一个hadoop集群(aka“remote”)加载到另一个集群(我的spark所在的集群,aka“domestic”)。

import java.sql.Timestamp
import scala.collection.mutable.MutableList

case class StatsRec (
  first_name: String,
  last_name: String,
  action_dtm: Timestamp,
  size: Long,
  size_p: Long,
  size_d: Long
)

val conn: Connection = DriverManager.getConnection(url, user, password)
val res: ResultSet = conn.createStatement
                   .executeQuery("SELECT * FROM stats_201512301914")
val fetchedRes = MutableList[StatsRec]()
while(res.next()) {
  var rec = StatsRec(res.getString("first_name"), 
     res.getString("last_name"), 
     Timestamp.valueOf(res.getString("action_dtm")), 
     res.getLong("size"), 
     res.getLong("size_p"), 
     res.getLong("size_d"))
  fetchedRes += rec
}
conn.close()
val rddStatsDelta = sc.parallelize(fetchedRes)
rddStatsDelta.cache()

 // Basically we are done. To check loaded data:

println(rddStatsDelta.count)
rddStatsDelta.collect.take(10).foreach(println)
mrwjdhj3

mrwjdhj32#

在为spark提供hive-ste.xml配置并启动hive metastore服务之后,
连接到配置单元时,需要在spark会话中配置两件事:
由于sparksql使用thrift连接到hivemetastore,因此我们需要在创建spark会话时提供thrift服务器uri。
hive metastore warehouse是spark sql持久化表的目录。使用与“hive.metastore.warehouse.dir”相对应的属性“spark.sql.warehouse.dir”(因为在spark 2.0中不推荐使用该属性)
比如:

SparkSession spark=SparkSession.builder().appName("Spark_SQL_5_Save To Hive").enableHiveSupport().getOrCreate();
    spark.sparkContext().conf().set("spark.sql.warehouse.dir", "/user/hive/warehouse");
    spark.sparkContext().conf().set("hive.metastore.uris", "thrift://localhost:9083");

希望这是有用的!!

of1yzvn4

of1yzvn43#

根据文件:
请注意,自spark 2.0.0以来,hive-site.xml中的hive.metastore.warehouse.dir属性已被弃用。相反,请使用spark.sql.warehouse.dir指定数据库在仓库中的默认位置。
所以在 SparkSession 您需要指定 spark.sql.uris 而不是 hive.metastore.uris ```
from pyspark.sql import SparkSession
spark = SparkSession
.builder
.appName("Python Spark SQL Hive integration example")
.config("spark.sql.uris", "thrift://<remote_ip>:9083")
.enableHiveSupport()
.getOrCreate()
spark.sql("show tables").show()

相关问题