sparkSQL1.1入门之七:ThriftServer和CLI,


      spark1.1相较于spark1.0,最大的差别就在于spark1.1增加了万人期待的CLI和ThriftServer。使得hive用户还有用惯了命令行的RDBMS数据库管理员很容易地上手sparkSQL,在真正意义上进入了SQL时代。下面先简单介绍其使用,限于时间关系,以后再附上源码分析。
1:ThriftServer和CLI的命令参数 A:令人惊讶的CLI       刚部署好spark1.1就迫不及待地先测试CLI,对于习惯了sql命令行的本人,失去了shark后,对于sparkSQL1.0一度很是抵触(其实对于开发调试人员来说,spark-shell才是利器,可以很方便得使用各个spark生态中的组件)。急切中,没有关闭hive metastore服务,然后一个bin/spark-sql就进入了命令行,然后通过hive metastore就可以直接对hive进行查询了:
spark-sql> use saledata;
//所有订单中每年的销售单数、销售总额
spark-sql> select c.theyear,count(distinct a.ordernumber),sum(b.amount) from tblStock a join tblStockDetail b on a.ordernumber=b.ordernumber join tbldate c on a.dateid=c.dateid group by c.theyear order by c.theyear;
运行结果:
      顺便地测试了一下hive0.13的语法(测试系统中使用的是hive0.13,spark1.1编译的时候是hive0.12,毫无意外地,在CLI里是不能使用hive0.12,必须使用和spark匹配的hive版本的hive语法)。       这种方式之所以能连接到hive metastore,是由于将hive的配置文件hive-site放置在了conf下,CLI能直接读取hive的配置信息。       下面进入正题,先看看ThriftServer和CLI的命令参数。ThriftServer和CLI的命令参数都有两部分组成,前一部分是spark的运行参数,可以参照Spark1.0.0 应用程序部署工具spark-submit 的参数;后一部分才是ThriftServer和CLI的命令参数。
B:ThriftServer命令参数
[hadoop@hadoop3 spark110]$ sbin/start-thriftserver.sh --help
Usage: ./sbin/start-thriftserver [options] [thrift server options]
Thrift server options:
      Use value for given property
其中[options] 是ThriftServer启动一个SparkSQL应用程序的参数,如果不设置--master的话,将在启动ThriftServer的机器以local方式运行,只能通过http://机器名:4040进行监控。在集群中提供ThriftServer的话,一定要配置master、executor-memory等参数;[thrift server options]是ThriftServer的参数,可以使用[thrift server options]进行配置,在实际应用上,因为参数比较多,通常使用conf/hive-site.xml配置。
C:CLI命令参数
[hadoop@hadoop3 spark110]$ bin/spark-sql --help
Usage: ./bin/spark-sql [options] [cli option]
CLI options:
 -d,--define <key=value>          Variable subsitution to apply to hive
                                  commands. e.g. -d A=B or --define A=B
    --database <databasename>     Specify the database to use
 -e <quoted-query-string>         SQL from command line
 -f <filename>                    SQL from files
 -h <hostname>                    connecting to Hive Server on remote host
    --hiveconf <property=value>   Use value for given property
    --hivevar <key=value>         Variable subsitution to apply to hive
                                  commands. e.g. --hivevar A=B
 -i <filename>                    Initialization SQL file
 -p <port>                        connecting to Hive Server on port number
 -S,--silent                      Silent mode in interactive shell
 -v,--verbose                     Verbose mode (echo executed SQL to the
                                  console)
其中[options] 是CLI启动一个SparkSQL应用程序的参数,如果不设置--master的话,将在启动ThriftServer的机器以local方式运行,只能通过http://机器名:4040进行监控。[cli option]是CLI的参数,通过这些参数,CLI可以直接运行SQL文件、进入命令行运行SQL命令等等,类似以前的shark的使用。需要注意的是CLI不是使用JDBC连接,所以不能连接到ThriftServer;但可以配置conf/hive-site.xml连接到hive的metastore(如令人惊讶的CLI中所述),然后对hive数据进行查询。
2:ThriftServer       ThriftServer启动的时候,启动了一个sparkSQL的应用程序,同时开启一个侦听器,等待JDBC客户端的连接和提交查询。所以,在配置ThriftServer的时候,至少要配置ThriftServer的主机名和端口,如果要使用hive数据的话,还要提供hive metastore的uris。通常,可以在conf/hive-site.xml中定义以下几项配置,也可以使用环境变量的方式进行配置(环境变量的优先级高于hive-site.xml)。       下面是在实验集群中hadoop2上启动ThriftServer的hive-site.xml配置:
//配置thriftserver的hive-site.xml
<configuration>
<property>
<name>hive.metastore.uris</name>
<value>thrift://hadoop3:9083</value>
<description>Thrift URI for the remote metastore. Used by metastore client to connect to remote metastore.</description>
</property>

<property>
<name>hive.server2.thrift.min.worker.threads</name>
<value>5</value>
<description>Minimum number of Thrift worker threads</description>
</property>

<property>
<name>hive.server2.thrift.max.worker.threads</name>
<value>500</value>
<description>Maximum number of Thrift worker threads</description>
</property>

<property>
<name>hive.server2.thrift.port</name>
<value>10000</value>
<description>Port number of HiveServer2 Thrift interface. Can be overridden by setting $HIVE_SERVER2_THRIFT_PORT</description>
</property>

<property>
<name>hive.server2.thrift.bind.host</name>
<value>hadoop2</value>
<description>Bind host on which to run the HiveServer2 Thrift interface.Can be overridden by setting$HIVE_SERVER2_THRIFT_BIND_HOST</description>
</property>
</configuration>
然后,在hadoop2上直接sbin/start-thriftserver.sh启动ThriftServer,注意不要将hive.server2.thrift.bind.host配置能localhost,不然远程客户端不能连接。 切换到客户端wyy,使用bin/beeline连接ThriftServer:
然后,可以直接使用sql命令进行操作:
当然,也可以在用户的程序使用JDBC连接ThriftServer,具体的请参照:HiveServer2 Clients。 这时,查看hadoop2:4040,可以看到用户的操作:
最后提一句,ThriftServer启动后处于监听状态,用户可以使用ctrl+c退出ThriftServer;而beeline的退出使用!q命令。   3:CLI       CLI的配置非常简单,在conf/hive-site.xml中之需要指定hive metastore的uris就可以使用了。现在要在客户端wyy上使用spark-sql,配置conf/hive-site.xml如下:
//配置CLI的hive-site.xml
<configuration>
<property>
<name>hive.metastore.uris</name>
<value>thrift://hadoop3:9083</value>
<description>Thrift URI for the remote metastore. Used by metastore client to connect to remote metastore.</description>
</property>
</configuration>
然后启动spark-sql:
bin/spark-sql --master spark://hadoop1:7077 --executor-memory 3g
这是集群监控页面可以看到启动了SparkSQL应用程序:
这时就可以使用HQL语句对hive数据进行查询,另外,可以使用COMMAND,如使用set进行设置参数:默认情况下,sparkSQL shuffle的时候是200个partition,可以使用如下命令修改这个参数:
SET spark.sql.shuffle.partitions=20;
运行同一个查询语句,参数改变后,Task(partition)的数量就由200变成了20。
基本上,在CLI可以使用绝大多数的hive特性。      
4:小结       总的来说,ThriftServer和CLI的引入,使得sparkSQL可以更方便的使用hive数据,使得sparkSQL可以更接近使用者,而非开发者。



相关内容