ama*_*uni 21 hadoop hive apache-spark apache-spark-sql
我正在使用HiveContext和SparkSQL,我正在尝试连接到远程Hive Metastore,设置hive Metastore的唯一方法是在类路径中包含hive-site.xml(或将其复制到/ etc/spark/CONF /).
有没有办法在不包含hive-site.xml的情况下以编程方式在java代码中设置此参数?如果是这样,使用什么是Spark配置?
ama*_*uni 30
对于Spark 1.x,您可以设置:
System.setProperty("hive.metastore.uris", "thrift://METASTORE:9083");
final SparkConf conf = new SparkConf();
SparkContext sc = new SparkContext(conf);
HiveContext hiveContext = new HiveContext(sc);
Run Code Online (Sandbox Code Playgroud)
要么
final SparkConf conf = new SparkConf();
SparkContext sc = new SparkContext(conf);
HiveContext hiveContext = new HiveContext(sc);
hiveContext.setConf("hive.metastore.uris", "thrift://METASTORE:9083");
Run Code Online (Sandbox Code Playgroud)
更新如果您的Hive是Kerberized:
在创建HiveContext之前尝试设置它们:
System.setProperty("hive.metastore.sasl.enabled", "true");
System.setProperty("hive.security.authorization.enabled", "false");
System.setProperty("hive.metastore.kerberos.principal", hivePrincipal);
System.setProperty("hive.metastore.execute.setugi", "true");
Run Code Online (Sandbox Code Playgroud)
pgi*_*ard 20
在火花2.0.+它应该看起来像这样:
别忘了用你的"hive.metastore.uris"替换它.这假设您已经启动了hive Metastore服务(不是hiveserver).
val spark = SparkSession
.builder()
.appName("interfacing spark sql to hive metastore without configuration file")
.config("hive.metastore.uris", "thrift://localhost:9083") // replace with your hivemetastore service's thrift url
.enableHiveSupport() // don't forget to enable hive support
.getOrCreate()
import spark.implicits._
import spark.sql
// create an arbitrary frame
val frame = Seq(("one", 1), ("two", 2), ("three", 3)).toDF("word", "count")
// see the frame created
frame.show()
/**
* +-----+-----+
* | word|count|
* +-----+-----+
* | one| 1|
* | two| 2|
* |three| 3|
* +-----+-----+
*/
// write the frame
frame.write.mode("overwrite").saveAsTable("t4")
Run Code Online (Sandbox Code Playgroud)
我也遇到了同样的问题,但解决了。只需在 Spark 2.0 版本中执行此步骤
步骤 1:从 Hive conf 文件夹中复制 hive-site.xml 文件到 spark conf。

第 2 步:编辑 spark-env.sh 文件并配置您的 mysql 驱动程序。(如果您使用 Mysql 作为 Hive Metastore。)

或者将 MySQL 驱动程序添加到 Maven/SBT(如果使用那些)
步骤 3:在创建 spark 会话时添加 enableHiveSupport()
val spark = SparkSession.builder.master("local").appName("testing") .enableHiveSupport() .getOrCreate()
示例代码:
package sparkSQL
/**
* Created by venuk on 7/12/16.
*/
import org.apache.spark.sql.SparkSession
object hivetable {
def main(args: Array[String]): Unit = {
val spark = SparkSession.builder.master("local[*]").appName("hivetable").enableHiveSupport().getOrCreate()
spark.sql("create table hivetab (name string, age int, location string) row format delimited fields terminated by ',' stored as textfile")
spark.sql("load data local inpath '/home/hadoop/Desktop/asl' into table hivetab").show()
val x = spark.sql("select * from hivetab")
x.write.saveAsTable("hivetab")
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
一些类似的问题被标记为重复,这是为了从 Spark 连接到 Hive,而不使用hive.metastore.uris或单独的 thrift 服务器(9083),并且不将 hive-site.xml 复制到 SPARK_CONF_DIR。
import org.apache.spark.sql.SparkSession
val spark = SparkSession
.builder()
.appName("hive-check")
.config(
"spark.hadoop.javax.jdo.option.ConnectionURL",
"JDBC_CONNECT_STRING"
)
.config(
"spark.hadoop.javax.jdo.option.ConnectionDriverName",
"org.postgresql.Driver"
)
.config("spark.sql.warehouse.dir", "/user/hive/warehouse")
.config("spark.hadoop.javax.jdo.option.ConnectionUserName", "JDBC_USER")
.config("spark.hadoop.javax.jdo.option.ConnectionPassword", "JDBC_PASSWORD")
.enableHiveSupport()
.getOrCreate()
spark.catalog.listDatabases.show(false)
Run Code Online (Sandbox Code Playgroud)
在尝试从 Spark 连接到 Hive Metastore 而不使用 hive-site.xml 时,我观察到一种奇怪的行为。
hive.metastore.uris当我们在创建 SparkSession 时使用 Spark 代码中的属性时,一切正常。但是,如果我们不在代码中指定,而是在使用spark-shell或spark-submit带有--conf标志时指定,则它将不起作用。
它将抛出如下所示的警告,并且不会连接到远程元存储。
Warning: Ignoring non-Spark config property: hive.metastore.uris
Run Code Online (Sandbox Code Playgroud)
解决此问题的一种方法是使用以下属性。
spark.hadoop.hive.metastore.uris
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
51476 次 |
| 最近记录: |