如何使用Spark SQL 的JDBC server

简介

        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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Spark SQL是一个用于处理结构化数据的模块,它提供了一种基于SQL的编程接口,可以让用户使用SQL语句来查询数据。ThriftServerSpark SQL的一个组件,它提供了一个基于Thrift协议的服务,可以让用户通过网络连接到Spark SQL,并使用SQL语句来查询数据。Beeline是一个用于连接到ThriftServer的命令行工具,它可以让用户通过命令行界面来执行SQL语句。 使用ThriftServer和Beeline可以让用户通过网络连接到Spark SQL,并使用SQL语句来查询数据。首先需要启动ThriftServer,可以使用以下命令: ``` ./sbin/start-thriftserver.sh ``` 启动成功后,可以使用Beeline连接到ThriftServer,可以使用以下命令: ``` ./bin/beeline -u jdbc:hive2://localhost:10000 ``` 连接成功后,就可以使用SQL语句来查询数据了。例如,可以使用以下命令查询表格: ``` SELECT * FROM table_name; ``` 使用ThriftServer和Beeline可以方便地查询Spark SQL中的数据,特别是在需要远程访问数据时非常有用。 ### 回答2: ThriftServer(又称HiveServer2)和Beeline都是Spark SQL中常用的工具,用于连接和操作Spark SQL。 ThriftServer是一个支持Hive/Spark SQL的服务,它允许用户通过多种编程语言(如Java、Python等)来访问和查询数据。ThriftServer通过Thrift协议提供了基于网络的服务,允许远程客户端连接到Spark集群并执行Spark SQL查询。ThriftServer可以通过配置来启用或禁用Kerberos身份验证,以实现安全连接。 Beeline是一个基于命令行的工具,它是Hive和Spark SQL的原生客户端。可以使用Beeline连接到ThriftServer,并通过命令行界面执行Spark SQL查询。Beeline支持多种连接方式,例如通过JDBC连接到ThriftServer、通过Kerberos进行身份验证等。用户可以通过Beeline执行SQL语句、管理数据库、查看查询结果等。 使用ThriftServer和Beeline的步骤如下: 1. 首先,确保Spark集群已经启动,并且ThriftServer已经启动。可以通过spark-sqlspark-sql2启动ThriftServer,默认情况下会监听端口10000。 2. 使用Beeline连接到ThriftServer。可以通过命令beeline -u jdbc:hive2://hostname:port进行连接,其中hostname是ThriftServer所在的主机名或IP地址,port是ThriftServer监听的端口号。此外,还需要提供用户名和密码进行身份验证。 3. 连接成功后,可以在Beeline中执行SQL语句。输入SQL语句后,按下回车键即可执行。查询结果会显示在命令行界面上。还可以使用Beeline提供的命令来管理数据库、查看表、导入导出数据等操作。 ThriftServer和Beeline的使用简单而方便,能够有效地连接和操作Spark SQL。它们为用户提供了一种灵活的方式来查询和管理数据,便于开发人员和数据分析师使用Spark SQL进行数据处理和分析。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值