【Spark九十四】spark-sqlツールの使用

3405 ワード

Spark-sqlはSpark binディレクトリの下の実行可能なスクリプトで、このスクリプトによってHiveのコマンドを実行することを目的としています.
hive>入力された命令はspark-sql>入力された命令によって完了することができる.
Spark-sqlは、内蔵のHive metadata-storeを使用してもよいし、すでに独立してインストールされているHiveのmetadata storeを使用してもよい
 
Hive build into Sparkについて
1.
Spark SQL can be built with or without Apache Hive, the Hadoop SQL engine. Spark
SQL with Hive support allows us to access Hive tables, UDFs (user-defined functions),
SerDes (serialization and deserialization formats), and the Hive query language
(HiveQL). Hive query language (HQL) It is important to note that including
the Hive libraries does not require an existing Hive installation.

 2.HiveとSpark SQLの関係については、以下を参照してください.http://bit1129.iteye.com/blog/2192739
 
3.
Note that if you don’t have an existing Hive installation, Spark SQL will create its
own Hive metastore (metadata DB) in your program’s work directory, called meta
store_db. 
In addition, if you attempt to create tables using HiveQL’s CREATE TABLE
statement (not CREATE EXTERNAL TABLE), they will be placed in the /user/hive/warehouse
directory on your default filesystem (either your local filesystem, or HDFS if
you have a hdfs-site.xml on your classpath).

 
 
 
構成手順:
 
1.Hiveのconfディレクトリのhive-site.xmlをSparkのconfディレクトリにコピーする
2.hive-site.xmlの時間の構成に関する時間単位、例えばms,sをすべて削除する
3.mysql jdbcのドライバをSparkのClasspathに追加
export SPARK_CLASSPATH=$SPARK_CLASSPATH:/home/hadoop/software/spark-1.2.0-bin-hadoop2.4/lib/mysql-connector-java-5.1.34.jar

4.Spark SQLの起動
[hadoop@hadoop bin]$ ./spark-sql
Spark assembly has been built with Hive, including Datanucleus jars on classpath
SET spark.sql.hive.version=0.13.1

 
5.すべてのデータベースを表示
 
spark-sql> show databases;
OK
default
financials
financials2
sales
Time taken: 18.67 seconds

 
6.表示表
 
spark-sql> use default;
OK
Time taken: 0.657 seconds
spark-sql> show tables;
OK
abc
avro_table
employees
invites
my_word
mytable1
parquet_table
table1
word
word3
word4
word5
Time taken: 1.011 seconds

 
7.クエリー
spark-sql> select * from word
         > ;
1	MSN  
10	QQ  
100	Gtalk  
1000	Skype 
NULL	NULL
Time taken: 39.852 seconds

 
8.テーブルの作成とデータのロード
spark-sql> create table word6 (id int,word string) row format delimited fields terminated by ',' stored as textfile ; 
OK
Time taken: 10.852 seconds

 
spark-sql> load data local inpath '/home/hadoop/word.txt' into table word6;
Copying data from file:/home/hadoop/word.txt
Copying file: file:/home/hadoop/word.txt
Loading data to table default.word6
Table default.word6 stats: [numFiles=1, numRows=0, totalSize=31, rawDataSize=0]
OK
Time taken: 2.307 seconds

 
このようにして、実際にspark−sql動作はHive動作と同等であり、すなわちspark−sqlはHiveに等価な能力を提供する