如何使用Spark SQL 的JDBC server

本文详细介绍了如何在CDH5.3.0环境下配置并使用Spark SQL的JDBC服务器,包括启动JDBC服务器、连接及操作过程,并通过Beeline进行数据表创建、加载、查询等基本操作。

简介

        Spark SQL  provides JDBC connectivity, which is useful for connecting business intelligence (BI) tools to a Spark cluster and for sharing a cluster across multipleusers. The JDBC server runs as a standalone Spark driver program that can be shared by multiple clients. Any client can cache tables in memory, query them, and so on and the cluster resources and cached data will be shared among all of them.

    Spark SQL’s JDBC server corresponds to the HiveServer2 in Hive.  It is also known as the “Thrift server” since it uses the Thrift communication protocol. Note that the JDBC server requires Spark be built with Hive support

运行环境

集群环境:CDH5.3.0

具体JAR版本如下:

spark版本:1.2.0-cdh5.3.0

hive版本:0.13.1-cdh5.3.0

hadoop版本:2.5.0-cdh5.3.0

启动 JDBC server

cd /etc/spark/conf
ln -s /etc/hive/conf/hive-site.xml hive-site.xml
cd /opt/cloudera/parcels/CDH/lib/spark/
chmod- -R 777 logs/
cd /opt/cloudera/parcels/CDH/lib/spark/sbin
./start-thriftserver.sh  --master yarn --hiveconf hive.server2.thrift.port=10008

 Connecting to the JDBC server with Beeline

cd /opt/cloudera/parcels/CDH/lib/spark/bin
beeline -u jdbc:hive2://hadoop04:10000

[root@hadoop04 bin]# beeline -u jdbc:hive2://hadoop04:10000
scan complete in 2ms
Connecting to jdbc:hive2://hadoop04:10000
Connected to: Spark SQL (version 1.2.0)
Driver: Hive JDBC (version 0.13.1-cdh5.3.0)
Transaction isolation: TRANSACTION_REPEATABLE_READ
Beeline version 0.13.1-cdh5.3.0 by Apache Hive
0: jdbc:hive2://hadoop04:10000>

Working with Beeline

Within the Beeline client, you can use standard HiveQL commands to create, list, and query tables. You can find the full details of HiveQL in the  Hive Language Manual,but here, we show a few common operations.

CREATE TABLE IF NOT EXISTS mytable (key INT, value STRING)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ',';

create table mytable(name string,addr string,status string) row format delimited fields terminated by '#'

#加载本地文件
load data local inpath '/external/tmp/data.txt' into table mytable

#加载hdfs文件
load data inpath 'hdfs://ju51nn/external/tmp/data.txt' into table mytable;

describe mytable;

explain select * from mytable where name = '张三'

select * from mytable where name = '张三'   

cache table mytable

 select count(*) total,count(distinct addr) num1,count(distinct status) num2 from mytable where addr='gz';
 
 uncache table mytable

使用数据示例

张三#广州#学生
李四#贵州#教师
王五#武汉#讲师
赵六#成都#学生
lisa#广州#学生
lily#gz#studene

Standalone Spark SQL Shell

Spark SQL also supports a simple shell you can use as a single process: spark-sql

它主要用于本地的开发环境,在共享集群环境中,请使用JDBC SERVER

cd /opt/cloudera/parcels/CDH/lib/spark/bin
./spark-sql


转载于:https://my.oschina.net/cloudcoder/blog/467713

<think>嗯,用户的问题是关于如何在Java中使用Spark SQLJDBC连接Trino,并且需要启用SSL=true的配置。首先,我需要回忆一下Trino的JDBC连接方式以及Spark SQL的配置方法。Trino的JDBC URL通常以jdbc:trino://开头,后面跟着主机名、端口、目录和模式。启用SSL的话,应该在URL中添加SSL=true参数。 接下来,Spark SQL连接JDBC通常是通过DataFrame API或者使用Properties对象来设置连接属性。记得在SparkJDBC连接中,需要指定driver选项,Trino的JDBC驱动类应该是io.trino.jdbc.TrinoDriver。然后,SSL配置可能需要额外的参数,比如信任存储文件的位置和密码,特别是如果Trino服务器使用的是自签名证书,可能需要配置信任存储。 用户可能需要一个具体的代码示例,所以需要整理出一个使用Java的Spark程序,配置JDBC连接Trino并启用SSL。同时,要考虑到可能的常见问题,比如驱动未找到,或者SSL握手失败,这时候需要检查驱动包的版本和依赖,以及信任存储的配置是否正确。 另外,用户可能需要知道如何获取Trino的JDBC驱动,可能需要从Maven仓库添加依赖,比如io.trino:trino-jdbc对应的版本。还要注意Spark的版本兼容性,不同版本的Spark可能对JDBC的支持略有不同。 可能还需要提到在Trino服务器端正确配置SSL,确保服务端已经启用了SSL,并且客户端(即Spark应用)信任服务器的证书。如果使用的是自签名证书,需要将证书导入到客户端的信任存储中,通常是Java的cacerts文件,或者指定自定义的信任存储路径。 最后,需要验证连接是否成功,可以通过执行简单的查询来测试,比如SELECT 1。如果出现连接错误,需要检查URL的格式、驱动类名是否正确,SSL参数是否遗漏,以及网络连通性等问题。</think>### Java Spark SQL JDBC 连接 Trino 并启用 SSL 的配置方法 #### 1. 添加依赖 首先需引入 Trino JDBC 驱动(以 Maven 为例): ```xml <dependency> <groupId>io.trino</groupId> <artifactId>trino-jdbc</artifactId> <version>428</version> <!-- 根据 Trino 版本调整 --> </dependency> ``` #### 2. 配置 JDBC 连接参数 在 Spark 中通过 `Properties` 对象设置连接属性: ```java import java.util.Properties; import org.apache.spark.sql.Dataset; import org.apache.spark.sql.Row; import org.apache.spark.sql.SparkSession; public class TrinoSSLExample { public static void main(String[] args) { SparkSession spark = SparkSession.builder() .appName("TrinoSSLExample") .master("local[*]") .getOrCreate(); Properties connectionProperties = new Properties(); connectionProperties.put("user", "your_username"); connectionProperties.put("password", "your_password"); connectionProperties.put("SSL", "true"); connectionProperties.put("SSLTrustStorePath", "/path/to/truststore.jks"); connectionProperties.put("SSLTrustStorePassword", "truststore_password"); String jdbcUrl = "jdbc:trino://trino-server-host:443/catalog/schema"; Dataset<Row> df = spark.read() .jdbc(jdbcUrl, "table_name", connectionProperties); df.show(); } } ``` #### 3. 关键参数说明 - **SSL=true**:强制启用 SSL 加密通信 - **SSLTrustStorePath**:信任存储文件路径(若服务器证书未被公共 CA 签发) - **SSLTrustStorePassword**:信任存储密码 - **端口**:Trino 默认 SSL 端口为 443,需与服务器配置一致[^1] #### 4. 服务器端要求 - Trino 需已配置 `etc/config.properties` 包含: ```properties http-server.https.enabled=true http-server.https.port=443 http-server.https.keystore.path=/path/to/keystore.jks http-server.https.keystore.key=keystore_password ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值