我正在本地运行spark并希望访问位于远程Hadoop集群中的Hive表.
我可以通过SPARK_HOME下的直线访问蜂巢表
[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 …Run Code Online (Sandbox Code Playgroud) 我正在 Scala 2.12 中使用 Spark 3.x SQL 查询 Spark 的数据库表。我遵循了互联网上给出的示例。
我正在使用的数据库:Spark SQL的数据库并使用Centos 7。我正在查询的表(示例)具有以下列:
create table example( tutorial_title VARCHAR(22) NOT NULL) ;
Run Code Online (Sandbox Code Playgroud)
var example= spark.read.format("jdbc")
.option("url", "jdbc:hive2://localhost:10000/test2")
.option("dbtable", "example")
.option("user", "username")
.option("password", "123456")
.option("fetchsize", "10")
.option("driver", "org.apache.hive.jdbc.HiveDriver")
.load()
Run Code Online (Sandbox Code Playgroud)
这给了我以下输出:
+-------+-------
|tutorial_title|
+-------+-------
|tutorial_title|
|tutorial_title|
|tutorial_title|
+-------+-------
Run Code Online (Sandbox Code Playgroud)
即重复每行的列名称而不提供数据。我的桌子有 3 行。我尝试更改数据库中的行数,我的输出也会相应更改。
如果我使用./bin/spark-sql并选择该表,它会显示实际记录。但./bin/Spark-shell将列名称指定为结果/记录。
Spark-sql 和 beeline 客户端具有正确的记录,但 Sparkread.format("jdbc")和 Spark-shell 具有上述错误记录。
我试图在独立模式和hive 1.2.0 jdbc版本中使用spark 1.5.1执行hive查询.
这是我的一段代码:
private static final String HIVE_DRIVER = "org.apache.hive.jdbc.HiveDriver";
private static final String HIVE_CONNECTION_URL = "jdbc:hive2://localhost:10000/idw";
private static final SparkConf sparkconf = new SparkConf().set("spark.master", "spark://impetus-i0248u:7077").set("spark.app.name", "sparkhivesqltest")
.set("spark.cores.max", "1").set("spark.executor.memory", "512m");
private static final JavaSparkContext sc = new JavaSparkContext(sparkconf);
private static final SQLContext sqlContext = new SQLContext(sc);
public static void main(String[] args) {
//Data source options
Map<String, String> options = new HashMap<String, String>();
options.put("driver", HIVE_DRIVER);
options.put("url", HIVE_CONNECTION_URL);
options.put("dbtable", "(select * from idw.emp) as employees_name");
DataFrame jdbcDF = sqlContext.read().format("jdbc").options(options).load();
} …Run Code Online (Sandbox Code Playgroud)