分散式 SQL 引擎
Spark SQL 也可以使用其 JDBC/ODBC 或命令列介面作為分散式查詢引擎。在此模式中,最終使用者或應用程式可以與 Spark SQL 直接互動以執行 SQL 查詢,而無需撰寫任何程式碼。
執行 Thrift JDBC/ODBC 伺服器
在此實作的 Thrift JDBC/ODBC 伺服器對應於內建 Hive 中的 HiveServer2
。您可以使用 Spark 或相容 Hive 附帶的 beeline 腳本測試 JDBC 伺服器。
若要啟動 JDBC/ODBC 伺服器,請在 Spark 目錄中執行下列指令
./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://127.0.0.1:10000
Beeline 會要求您輸入使用者名稱和密碼。在非安全模式下,只需輸入您電腦上的使用者名稱和空白密碼即可。對於安全模式,請遵循 beeline 文件 中提供的說明。
Hive 的組態是透過將 hive-site.xml
、core-site.xml
和 hdfs-site.xml
檔案放置在 conf/
中來完成的。
您也可以使用 Hive 附帶的 beeline 腳本。
Thrift JDBC 伺服器也支援透過 HTTP 傳輸傳送 Thrift RPC 訊息。使用下列設定以系統屬性或 conf/
中的 hive-site.xml
檔案啟用 HTTP 模式
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 以 http 模式連線到 JDBC/ODBC 伺服器,指令如下
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
若要從 shell 使用 Spark SQL 命令列介面 (CLI)
./bin/spark-sql
有關詳細資訊,請參閱 Spark SQL CLI