Spark2.x学习笔记:15、Spark SQL的SQL

15、 Spark SQL的SQL

15.1 Spark SQL所支持的SQL语法

select [distinct] [column names]|[wildcard]
from tableName
[join clause tableName on join condition]
[where condition]
[group by column name]
[having conditions]
[order by column names [asc|desc]]

如果只用join进行查询,则支持的语法为:

select statement
from statement
[join | inner join | left join | left semi join | left outer join | right join |right outer join | full join | full outer join]
on join condition

15.2 Spark SQL的SQL的框架

15.3 与Hive Metastore结合

(1)Spark要能找到HDFS和Hive的配置文件

  • 第1种方法:可以直接将core-site.xml、hdfs-site.xml和hive-site.xml复制到Spark安装目录下的conf目录中。该方法存在一个缺陷,如果HDFS或Hive的配置修改了,则需要手动修改Spark对应的配置文件。
  • 第2种方法:在Spark配置文件中指定Hadoop配置文件目录

(2)Spark SQL与Hive Metastore结合,直接使用spark.sql(“select … from table where …”)

15.4 实例演示

(1)spark-shell

[root@node1 ~]# spark-shell
17/10/24 10:15:21 WARN NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
Spark context Web UI available at http://192.168.80.131:4040
Spark context available as 'sc' (master = local[*], app id = local-1508854525067).
Spark session available as 'spark'.
Welcome to
      ____              __
     / __/__  ___ _____/ /__
    _\ \/ _ \/ _ `/ __/  '_/
   /___/ .__/\_,_/_/ /_/\_\   version 2.2.0
      /_/

Using Scala version 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_112)
Type in expressions to have them evaluated.
Type :help for more information.

scala> spark.sql("show databases").show
+------------+
|databaseName|
+------------+
|     default|
|        test|
+------------+


scala> spark.sql("show tables").show
+--------+---------+-----------+
|database|tableName|isTemporary|
+--------+---------+-----------+
| default|  copyemp|      false|
| default|     demo|      false|
| default|     dept|      false|
| default|     dual|      false|
| default|      emp|      false|
| default|   empbak|      false|
| default|employees|      false|
| default|     mytb|      false|
| default|    users|      false|
+--------+---------+-----------+


scala> spark.sql("select * from emp").show
+----+------+---------+----+----------+------+------+----+
| eid| ename|      job| mgr|  hiredate|   sal|  comm| did|
+----+------+---------+----+----------+------+------+----+
|7782| CLARK|  MANAGER|7839|1981-06-09|2450.0|   0.0|  10|
|7839|  KING|PRESIDENT|   0|1981-11-17|5000.0|   0.0|  10|
|7934|MILLER|    CLERK|7782|1982-01-23|1300.0|   0.0|  10|
|7369| SMITH|    CLERK|7902|1980-12-17| 800.0|   0.0|  20|
|7566| JONES|  MANAGER|7839|1981-04-02|2975.0|   0.0|  20|
|7902|  FORD|  ANALYST|7566|1981-12-03|3000.0|   0.0|  20|
|7499| ALLEN| SALESMAN|7698|1981-02-20|1600.0| 300.0|  30|
|7521|  WARD| SALESMAN|7698|1981-02-22|1250.0| 500.0|  30|
|7654|MARTIN| SALESMAN|7698|1981-09-28|1250.0|1400.0|  30|
|7698| BLAKE|  MANAGER|7839|1981-05-01|2850.0|   0.0|  30|
|7844|TURNER| SALESMAN|7698|1981-09-08|1500.0|   0.0|  30|
|7900| JAMES|    CLERK|7698|1981-12-03| 950.0|   0.0|  30|
|8888|HADRON|     null|null|2016-08-31|6666.0|  null|null|
+----+------+---------+----+----------+------+------+----+


scala> 

(2)spark-sql

[root@node1 ~]# spark-sql
17/10/24 10:17:21 WARN NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
17/10/24 10:17:32 WARN ObjectStore: Version information not found in metastore. hive.metastore.schema.verification is not enabled so recording the schema version 1.2.0
17/10/24 10:17:32 WARN ObjectStore: Failed to get database default, returning NoSuchObjectException
spark-sql> show databases;
default
test
Time taken: 3.93 seconds, Fetched 2 row(s)
spark-sql> show tables;
default copyemp false
default demo    false
default dept    false
default dual    false
default emp false
default empbak  false
default employees   false
default mytb    false
default users   false
Time taken: 0.145 seconds, Fetched 9 row(s)
spark-sql> select * from emp;
7782    CLARK   MANAGER 7839    1981-06-09  2450.0  0.0 10
7839    KING    PRESIDENT   0   1981-11-17  5000.0  0.0 10
7934    MILLER  CLERK   7782    1982-01-23  1300.0  0.0 10
7369    SMITH   CLERK   7902    1980-12-17  800.0   0.0 20
7566    JONES   MANAGER 7839    1981-04-02  2975.0  0.0 20
7902    FORD    ANALYST 7566    1981-12-03  3000.0  0.0 20
7499    ALLEN   SALESMAN    7698    1981-02-20  1600.0  300.0   30
7521    WARD    SALESMAN    7698    1981-02-22  1250.0  500.0   30
7654    MARTIN  SALESMAN    7698    1981-09-28  1250.0  1400.0  30
7698    BLAKE   MANAGER 7839    1981-05-01  2850.0  0.0 30
7844    TURNER  SALESMAN    7698    1981-09-08  1500.0  0.0 30
7900    JAMES   CLERK   7698    1981-12-03  950.0   0.0 30
8888    HADRON  NULL    NULL    2016-08-31  6666.0  NULL    NULL
Time taken: 3.266 seconds, Fetched 13 row(s)
spark-sql> 

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏闵开慧

Failed to set permissions of path

12/09/04 16:46:34 INFO support.ClassPathXmlApplicationContext: Refreshing org...

2507
来自专栏乐沙弥的世界

SQL 基础--> 集合运算(UNION 与UNION ALL)

--=============================================

431
来自专栏Netkiller

数据库安全·开发加密插件

以下节选择《Netkiller Architect 手札》 作者:netkiller 地址 http://www.netkiller.cn/archit...

3007
来自专栏SAP最佳业务实践

SAP S/4 HANA新变化-MM-IM物料帐:物料评估

Material Ledger Obligatory for Material Valuation 物料帐强制启用 Description This simpl...

3786
来自专栏猿人谷

Reverse Linked List II

Reverse a linked list from position m to n. Do it in-place and in one-pass. For...

1667
来自专栏Data Analysis & Viz

手把手教你完成一个数据科学小项目(6):城市提取与可视化

请先阅读《“中国年轻人正带领国家走向危机”,这锅背是不背? 》 一文,以对“手把手教你完成一个数据科学小项目”系列有个全局性的了解。

792
来自专栏乐沙弥的世界

SQL*Plus break与compute的简单用法

   在SQL*Plus提示符下输出求和报表,我们可以借助break与compute两个命令来实现。这个两个命令简单易用,可满足日常需求,其实质也相当于在编写S...

976
来自专栏闵开慧

URI was not reported to parser for entity

 org.xml.sax.SAXParseException: URI was not reported to parser for entity [do...

2678
来自专栏琦小虾的Binary

Matlab R2012b 重复激活,License 失效问题解决

前段时间好多同学的 Matlab 突然同时不能用了,相当诡异。后来查了一下资料,现在已经解决该问题。 解决方案: 之前的破解方法按照下面链接进行操作即可: ...

3989
来自专栏乐沙弥的世界

SQL基础-->过滤和排序

--=======================================

612

扫码关注云+社区