转载请注明出处,谢谢合作~
该篇中的示例暂时只有 Scala 版本~
分布式 SQL 引擎
- 启动 Thrift JDBC/ODBC 服务(Running the Thrift JDBC/ODBC server)
- 启动 Spark SQL CLI(Running the Spark SQL CLI)
Spark SQL 还可以通过 JDBC/ODBC 或者命令行接口接口作为分布式查询引擎提供服务。在这种模式下,终端用户或者应用程序可以通过执行 SQL 查询直接与 Spark SQL 进行交互,不需要写任何代码。
启动 Thrift JDBC/ODBC 服务
这里的 Thrift JDBC/ODBC 服务和 Hive 的 HiveServer2
一样,你可以通过 beeline
脚本测试 Spark 或者相应版本的 Hive 提供的 JDBC 服务。
运行 Spark 目录中的脚本来启动 Thrift JDBC/ODBC 服务:
./sbin/start-thriftserver.sh
这个脚本可以接受任何 bin/spark-submit
脚本可用的命令行参数,外加一个 --hiveconf
选项来指定 Hive 属性。可以运行 ./sbin/start-thriftserver.sh --help
获得完整的选项列表。默认情况下,服务的监听地址为 localhost:10000
,可以通过环境变量自定义监听地址,即:
export HIVE_SERVER2_THRIFT_PORT=<listening-port>
export HIVE_SERVER2_THRIFT_BIND_HOST=<listening-host>
./sbin/start-thriftserver.sh \
--master <master-uri> \
...
或者通过系统属性定义:
./sbin/start-thriftserver.sh \
--hiveconf hive.server2.thrift.port=<listening-port> \
--hiveconf hive.server2.thrift.bind.host=<listening-host> \
--master <master-uri>
...
现在可以用 beeline
脚本测试 Thrift JDBC/ODBC 服务:
./bin/beeline
在 beeline
中连接 JDBC/ODBC 服务:
beeline> !connect jdbc:hive2://localhost:10000
Beeline 会要求输入用户名和密码。在非安全模式下,输入系统的用户名和空的密码即可。在安全模式下,参见 beeline documentation。
关于 Hive 的配置,需要将 hive-site.xml
,core-site.xml
和 hdfs-site.xml
文件放到 conf/
目录中去。
还可以使用 Hive 的 beeline
脚本。
Thrift JDBC 服务还支持通过 HTTP 发送 thrift RPC 信息。请使用下面的配置来开启 HTTP 模式,可以通过环境变量或者 conf/
目录中的配置文件 hive-site.xml
进行配置。
hive.server2.transport.mode - Set this to value: http
hive.server2.thrift.http.port - HTTP port number to listen on; default is 10001
hive.server2.http.endpoint - HTTP endpoint; default is cliservice
通过 beeline
连接 JDBC/ODBC 服务的 HTTP 端口来进行测试:
beeline> !connect jdbc:hive2://<host>:<port>/<database>?hive.server2.transport.mode=http;hive.server2.thrift.http.path=<http_endpoint>
如果关闭了会话之后执行了 CTAS 语句,必须将配置文件 hive-site.xml
中的 fs.%s.impl.disable.cache
设置为 true,详情参见 [SPARK-21067]。
启动 Spark SQL CLI
Spark SQL CLI 是一个方便的工具,可以以本地模式运行 Hive metastore 服务,并通过命令行执行查询语句。注意,Spark SQL CLI 不能连接 Thrift JDBC 服务。
运行 Spark 目录中的脚本来启动 Spark SQL CLI:
./bin/spark-sql
关于 Hive 的配置,需要将 hive-site.xml
,core-site.xml
和 hdfs-site.xml
文件放到 conf/
目录中去。可以运行 ./sbin/spark-sql --help
获得完整的选项列表。