Hive、Pig、HBase的关系与区别

欢迎关注大数据和人工智能技术文章发布的微信公众号:清研学堂,在这里你可以学到夜白(作者笔名)精心整理的笔记,让我们每天进步一点点,让优秀成为一种习惯!
Pig

一种操作hadoop的轻量级脚本语言,最初又雅虎公司推出,不过现在正在走下坡路了。当初雅虎自己慢慢退出pig的维护之后将它开源贡献到开源社区由所有爱好者来维护。不过现在还是有些公司在用,不过我认为与其使用pig不如使用hive。:)

Pig是一种数据流语言,用来快速轻松的处理巨大的数据。

Pig包含两个部分:Pig Interface,Pig Latin。

Pig可以非常方便的处理HDFS和HBase的数据,和Hive一样,Pig可以非常高效的处理其需要做的,通过直接操作Pig查询可以节省大量的劳动和时间。当你想在你的数据上做一些转换,并且不想编写MapReduce jobs就可以用Pig.

Hive

不想用程序语言开发MapReduce的朋友比如DB们,熟悉SQL的朋友可以使用Hive开离线的进行数据处理与分析工作。

注意Hive现在适合在离线下进行数据的操作,就是说不适合在挂在真实的生产环境中进行实时的在线查询或操作,因为一个字“慢”。相反

起源于FaceBook,Hive在Hadoop中扮演数据仓库的角色。建立在Hadoop集群的最顶层,对存储在Hadoop群上的数据提供类SQL的接口进行操作。你可以用 HiveQL进行select,join,等等操作。

如果你有数据仓库的需求并且你擅长写SQL并且不想写MapReduce jobs就可以用Hive代替。

HBase

HBase作为面向列的数据库运行在HDFS之上,HDFS缺乏随即读写操作,HBase正是为此而出现。HBase以Google BigTable为蓝本,以键值对的形式存储。项目的目标就是快速在主机内数十亿行数据中定位所需的数据并访问它。

HBase是一个数据库,一个NoSql的数据库,像其他数据库一样提供随即读写功能,Hadoop不能满足实时需要,HBase正可以满足。如果你需要实时访问一些数据,就把它存入HBase。

你可以用Hadoop作为静态数据仓库,HBase作为数据存储,放那些进行一些操作会改变的数据。

Pig VS Hive

Hive更适合于数据仓库的任务,Hive主要用于静态的结构以及需要经常分析的工作。Hive与SQL相似促使 其成为Hadoop与其他BI工具结合的理想交集。

Pig赋予开发人员在大数据集领域更多的灵活性,并允许开发简洁的脚本用于转换数据流以便嵌入到较大的 应用程序。

Pig相比Hive相对轻量,它主要的优势是相比于直接使用Hadoop Java APIs可大幅削减代码量。正因为如此,Pig仍然是吸引大量的软件开发人员。

Hive和Pig都可以与HBase组合使用,Hive和Pig还为HBase提供了高层语言支持,使得在HBase上进行数据统计处理变的非常简单

Hive VS HBase

Hive是建立在Hadoop之上为了减少MapReduce jobs编写工作的批处理系统,HBase是为了支持弥补Hadoop对实时操作的缺陷的项目 。

想象你在操作RMDB数据库,如果是全表扫描,就用Hive+Hadoop,如果是索引访问,就用HBase+Hadoop 。

Hive query就是MapReduce jobs可以从5分钟到数小时不止,HBase是非常高效的,肯定比Hive高效的多。

介绍:

一、什么是hive???

1,hive是基于Hadoop的一个数据仓库工具、

2,可以将结构化的数据文件映射为一张数据库表,并提供类sql的查询功能、

3,可以将sql语句转换为mapreduce任务进行运行、

4,可以用来进行数据提取转换加载(ETL)

5,hive是sql解析引擎,它将sql 语句转换成M/R job然后在Hadoop中运行。

hive的表其实就是HDFS的目录/文件夹。

hive表中的数据 就是hdfs目录中的文件。按表名把文件夹分开。如果是分区表,则分区值是子文件夹,可以直接在M/R job里使用这些数据.

6,hive优点与缺点:

可以提供类SQL语句快速实现简单的mapreduce统计,不需要开发专门的mapreduce应用

不支持实时查询

7,hive数据分为真实存储的数据和元数据

真实数据存储在hdfs中,元数据存储在mysql中

metastore 元数据存储数据库

Hive将元数据存储在数据库中,如MySQL、derby。

Hive中的元数据包括表的名字,表的列和分区及其属性,表的属性(是否为外部表等),表的数据所在目录等。

二、hive的体系架构:

用户接口,包括 CLI(shell),JDBC/ODBC,WebUI(通过浏览器)

元数据存储,通常是存储在关系数据库如 mysql, derby 中

解释器、编译器、优化器、执行器完成HQL查询语句从语法分析,编译,优化以及查询计划的生成,生成的查询计划存储在HDFS中,并随后被mapreduce调用执行

Hadoop:用 HDFS 进行存储,利用 MapReduce 进行计算(带的查询select from teacher不会生成mapreduce任务,只是进行全表扫描)

在此强调:

Hadoop,zookpeer,spark,kafka,mysql已经正常启动

三、开始安装部署hive

基础依赖环境:

1,jdk 1.6+

2, hadoop 2.x

3,hive 0.13-0.19

4,mysql (mysql-connector-jar)

安装详细如下:

java

export JAVA_HOME=/soft/jdk1.7.0_79/

export CLASSPATH=.:​JAVA_HOME/lib/tools.jar

bin

export PATH=​JAVA_HOME/bin:​SCALA_HOME/bin:$SPARK_HOME/bin:/usr/local/hadoop/hive/bin

hadoop

export HADOOP_HOME=/usr/local/hadoop/hadoop

scala

export SCALA_HOME=/usr/local/hadoop/scala

spark

export SPARK_HOME=/usr/local/hadoop/spark

hive

export HIVE_HOME=/usr/local/hadoop/hive

一、开始安装:

1,下载:

https://hive.apache.org/downloads.html

解压:

tar xvf apache-hive-2.1.0-bin.tar.gz -C /usr/local/hadoop/

cd /usr/local/hadoop/

mv apache-hive-2.1.0 hive

2,修改配置

修改启动环境

cd /usr/local/hadoop/hive

vim bin/hive-config.sh

java

export JAVA_HOME=/soft/jdk1.7.0_79/

hadoop

export HADOOP_HOME=/usr/local/hadoop/hadoop

hive

export HIVE_HOME=/usr/local/hadoop/hive

修改默认配置文件

cd /usr/local/hadoop/hive

vim conf/hive-site.xml

​ javax.jdo.option.ConnectionURL

​ jdbc:mysql://master:3306/hive?createDatabaseInfoNotExist=true

​ JDBC connect string for a JDBC metastore

​ javax.jdo.option.ConnectionDriverName

​ com.mysql.jdbc.Driver

​ Driver class name for a JDBC metastore

​ javax.jdo.option.ConnectionUserName

​ hive

​ Username to use against metastore database

​ javax.jdo.option.ConnectionPassword

​ xujun

​ password to use against metastore database

3,修改tmp dir

修改将含有”system:java.io.tmpdir”的配置项的值修改为如上地址

/tmp/hive

4,安装mysql driver

去mysql官网下载驱动mysql-connector-java-5.1.40.zip

unzip mysql-connector-java-5.1.40.zip

cp mysql-connector-java-5.1.40-bin.jar /user/lcoal/hadoop/hive/lib/

二、安装好mysql,并且启动

1.创建数据库

create database hive

grant all on . to hive@’%’ identified by ‘hive’;

flush privileges;

三,初始化hive(初始化metadata)

cd /usr/local/hadoop/hive

bin/schematool -initSchema -dbType mysql

SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.

SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory]

Metastore connection URL: jdbc:mysql://hadoop3:3306/hive?createDatabaseInfoNotExist=true

Metastore Connection Driver : com.mysql.jdbc.Driver

Metastore connection User: hive

Starting metastore schema initialization to 2.1.0

Initialization script hive-schema-2.1.0.mysql.sql

Initialization script completed

schemaTool completed

四、启动

[hadoop@hadoop1 hadoop]$** hive/bin/hive**

which: no hbase in (/usr/lib64/qt-3.3/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin://soft/jdk1.7.0_79//bin:/bin:/bin:/bin:/usr/local/hadoop/hive/bin:/home/hadoop/bin)

SLF4J: Class path contains multiple SLF4J bindings.

SLF4J: Found binding in [jar:file:/usr/local/hadoop/hive/lib/log4j-slf4j-impl-2.4.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]

SLF4J: Found binding in [jar:file:/usr/local/hadoop/hadoop/share/hadoop/common/lib/slf4j-log4j12-1.7.10.jar!/org/slf4j/impl/StaticLoggerBinder.class]

SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.

SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory]

Logging initialized using configuration in jar:file:/usr/local/hadoop/hive/lib/hive-common-2.1.0.jar!/hive-log4j2.properties Async: true

Hive-on-MR is deprecated in Hive 2 and may not be available in the future versions. Consider using a different execution engine (i.e. tez, spark) or using Hive 1.X releases.

hive> show databases;

OK

default

Time taken: 1.184 seconds, Fetched: 1 row(s)

hive>

五,实践操作

使用hive创建表

以下两个操作只是针对当前session终端

1,hive> set hive.cli.print.current.db=true; 设置显示当前数据库名

hive (default)>

2,hive (default)> set hive.cli.print.header=true; 当使用select 查询数据时候,显示的结果会带有表的字段名称

3,创建表,并导入数据

hive> create table teacherq(id bigint,name string) row format delimited fields terminated by ‘ ’;

OK

hive> create table people (id int ,name string);

OK

Time taken: 3.363 seconds

hive> SHOW TABLES;

OK

people

teacherq

student

Time taken: 0.283 seconds, Fetched: 1 row(s)

导入数据:

hive>l**oad data local inpath ‘/root/stdent.txt’ into table teacherq;**

注意:如果你是普通用户启动hive,则使用相对路径来导入本地数据

mv stdent.txt /usr/local/hadoop/hive/

cd /usr/local/hadoop/hive

load data local inpath ‘stdent.txt’ into table teacherq;
Loading data to table default.teacherq

OK

Time taken: 2.631 seconds

hive> select * from teacherq;

OK

1 zhangsan

2 lisi

3 wangwu

4 libai

Time taken: 1.219 seconds, Fetched: 4 row(s)

hive>

4.建表(默认是内部表)

适用于先创建表,后load加载数据、

create table trade_detail(id bigint, account string, income double, expenses double, time string) row format delimited fields terminated by ‘ ’;

默认普通表load数据:

load data local inpath ‘/root/student.txt’ into table student;

建外部表 : 适用于,hdfs先有数据,后创建表,进行数据查询,分析管理

create external table td_ext(id bigint, account string, income double, expenses double, time string) row format delimited fields terminated by ‘ ’* location *‘/td_ext’;

外部表load数据:

load data local inpath ‘/root/student.txt’ into table student;

建分区表

方法一:先创建分区表,然后load数据

​ partition就是辅助查询,缩小查询范围,加快数据的检索速度和对数据按照一定的规格和条件进行管理。

​ create table td_part(id bigint, account string, income double, expenses double, time string) partitioned by (logdate string) row format delimited fields terminated by ‘ ’;

分区表中load数据

load data local inpath ‘/root/data.am’ into table beauty partition (nation=”USA”);

hive (itcast)> select * from beat;

OK

beat.idbeat.namebeat.sizebeat.nation

1glm22.0china

2slsl21.0china

3sdsd20.0china

NULLwww19.0china

Time taken: 0.22 seconds, Fetched: 4 row(s)

方法二:先在hdfs 创建目录,倒入数据,最后,更改hive元数据的信息

1, 创建分区目录

hive (itcast)> dfs -mkdir /beat/nation=japan

dfs -ls /beat;

Found 2 items

drwxr-xr-x - hadoop supergroup 0 2016-12-05 16:07 /beat/nation=china

drwxr-xr-x - hadoop supergroup 0 2016-12-05 16:16 /beat/nation=japan

2, 为分区目录加载数据

hive (itcast)> dfs -put d.c /beat/nation=japan

​ 此时查询数据:数据还未加载进来。

hive (itcast)> dfs -ls /beat/nation=japan;

Found 1 items

-rw-r–r– 3 hadoop supergroup 20 2016-12-05 16:16 /beat/nation=japan/d.c

hive (itcast)> select * from beat;

OK

beat.idbeat.namebeat.sizebeat.nation

1glm22.0china

2slsl21.0china

3sdsd20.0china

NULLwww19.0china

Time taken: 0.198 seconds, Fetched: 4 row(s)

3,手动修改hive表结构,添加分区表信息

hive (itcast)> alter table beat add partition (nation=’japan’) location “/beat/nation=japan”;

OK

Time taken: 0.089 seconds

hive (itcast)> select * from beat;

OK

beat.idbeat.namebeat.sizebeat.nation

1glm22.0china

2slsl21.0china

3sdsd20.0china

NULLwww19.0china

7ab111.0japan

8rb23234.0japan

Time taken: 0.228 seconds, Fetched: 6 row(s)

此时数据加载完成。

删除分区

用户可以用 ALTER TABLE DROP PARTITION 来删除分区。分区的元数据和数据将被一并删除。

例:

ALTER TABLE beat DROP PARTITION (nation=’japan’);

特殊情况案例:

1,表中的某个字段需要作为分区的分区名,默认不允许创建,解决方法:

hive (itcast)> create table sms(id bigint ,content string,area string) partitioned by (area string) row format delimited fields terminated by ‘ ’ ;

FAILED: SemanticException [Error 10035]: Column repeated in partitioning columns

解决方法:

建立冗余字段,即使用 area_pat来区分,

或者修改源码

hive (itcast)> create table sms(id bigint ,content string,area string) partitioned by (area_pat string) row format delimited fields terminated by ‘ ’ ;

基本的配置包括环境变量加入到etc/profile文件中

export HIVE_HOME=/home/wangpeng/opt/hive

在将HIVE_HOME加载到PATH变量中去。

2.hive-site.xml这个文件中基本的url,driver,username,password配置号

3.驱动加载好

4.系统会报异常,如果是权限问题,该权限 :注意tmp系统和var系统是两个独立的文件系统,Hadoop会征用tmp系统,所以不要乱删东西(可以进入hadoop fs 修改权限)

Hadoop fs -chmod 777 /tmp/hive

5.还有一个是hive-site.xml中配置warehouse,日志,临时文件等路径

​ hive.metastore.warehouse.dir

​ /hive/warehouse

​ location of default database for the warehouse

​ hive.exec.scratchdir

​ /home/hadoop/soft/hive/hive/tmp/hive-${user.name}

​ hive.querylog.location

​ /home/hadoop/soft/hive/hive/tmp/${user.name}

6.hwi找不到包,找到解决方法,在制作的过程发现没有root制作不了。可能更linux版本有关。

去网上下载个源码

wangpeng@com:/opt/apache-hive-1.0.0-src/hwi/web$ jar -cvfM0 hive-hwi-1.0.0.war ./

-c 创建war包

-v 显示过程信息

-f 指定 JAR 文件名,通常这个参数是必须的

-M 不产生所有项的清单(MANIFEST〕文件,此参数会忽略 -m 参数

-0:是阿拉伯数字,表示不压缩

7.hive-site.xml关注下起配置文件读取是靠相对路径来找的,所以自己要注意配置相对路径。

​ hive.hwi.war.file

​ lib/hive-hwi-1.0.0.war

​ This sets the path to the HWI war file, relative to ${HIVE_HOME}.

Hive简介

1.1 Hive定义

Hive是基于Hadoop的一个数据仓库工具,可以将结构化的数据文件映射为一张数据库表,并提供类SQL查询功能。

本质是将SQL转换为MapReduce程序。

1.2 为什么使用Hive

1、面临的问题

​ 人员学习成本太高

​ 项目周期要求太短

​ 我只是需要一个简单的环境

MapReduce 如何搞定

复杂查询好难

​ Join如何实现

2、为什么要使用Hive

操作接口采用类SQL语法,提供快速开发的能力

避免了去写MapReduce,减少开发人员的学习成本

扩展功能很方便

1.3 Hive特点

1、可扩展

Hive可以自由的扩展集群的规模,一般情况下不需要重启服务

2、延展性

Hive支持用户自定义函数,用户可以根据自己的需求来实现自己的函数

3、容错

良好的容错性,节点出现问题SQL仍可完成执行

1.4 Hive与Hadoop的关系

1.5 Hive与传统数据库的关系

1.6 Hive的历史

由FaceBook 实现并开源

2011年3月,0.7.0版本 发布,此版本为重大升级版本,增加了简单索引,HAING等众多高级特性

2011年06月,0.7.1 版本发布,修复了一些BUG,如在Windows上使用JDBC的的问题

2011年12月,0.8.0版本发布,此版本为重大升级版本,增加了insert into 、HA等众多高级特性

2012年2月5日,0.8.1版本发布,修复了一些BUG,如使 Hive 可以同时运行在 Hadoop0.20.x 与 0.23.0

2012年4月30日,0.9.0版本发布,重大改进版本,增加了对Hadoop 1.0.0的支持、实现BETWEEN等特性。

1.7 Hive的未来发展

增加更多类似传统数据库的功能,如存储过程

提高转换成的MapReduce性能

拥有真正的数据仓库的能力

UI部分加强

2 软件准备与环境规划

2.1 Hadoop环境介绍

Hadoop安装路径:/home/test/Desktop/hadoop-1.0.0/

Hadoop元数据存放目录:/home/test/data/core/namenode

Hadoop数据存放路径:/home/test/data/core/datanode

Hive安装路径:/home/test/Desktop/

Hive数据存放路径:/user/hive/warehouse

Hive元数据

第三方数据库:derby mysql

2.2 软件准备

OS

ubuntu

JDK

java 1.6.0_27

Hadoop

hadoop-1.0.0.tar

Hive

hive-0.8.1.tar

2.3 项目结构

2.4 Hive配置文件介绍

1、Hive配置文件介绍

hive-site.xml hive的配置文件

hive-env.sh hive的运行环境文件

hive-default.xml.template 默认模板

hive-env.sh.template hive-env.sh默认配置

hive-exec-log4j.properties.template exec默认配置

hive-log4j.properties.template log默认配置

2、hive-site.xml

< property>

javax.jdo.option.ConnectionURL

jdbc:mysql://localhost:3306/hive?createDatabaseIfNotExist=true

JDBC connectstring for a JDBC metastore

​ javax.jdo.option.ConnectionDriverName

​ com.mysql.jdbc.Driver

​ Driver classname for a JDBC metastore

​ javax.jdo.option.ConnectionUserName

​ root

​ username touse against metastore database

javax.jdo.option.ConnectionPassword

test

password touse against metastore database

3、hive-env.sh

配置Hive的配置文件路径:export HIVE_CONF_DIR= your path

配置Hadoop的安装路径:HADOOP_HOME=your hadoop home

2.5 使用Derby数据库的安装方式

1、什么是Derby安装方式

ApacheDerby是一个完全用java编写的数据库,所以可以跨平台,但需要在JVM中运行

Derby是一个Open source的产品,基于Apache License 2.0分发

即将元数据存储在Derby数据库中,也是Hive默认的安装方式。

2、安装Hive

解压Hive:tar zxvf hive-0.8.1.tar /home/test/Desktop

建立软连接:ln –s hive-0.8.1 hive

添加环境变量

export HIVE_HOME=/home/test/Desktop/hive

export PATH=….HIVE_HOME/bin:$PATH:.

3、配置Hive

进入hive/conf目录

依据hive-env.sh.template,创建hive-env.sh文件

cp hive-env.sh.template hive-env.sh

修改hive-env.sh

指定hive配置文件的路径

export HIVE_CONF_DIR=/home/test/Desktop/hive/conf

指定Hadoop路径

HADOOP_HOME=/home/test/Desktop/hadoop

4、hive-site.xml

javax.jdo.option.ConnectionURL

jdbc:derby:;databaseName=metastore_db;create=true

JDBCconnect string for a JDBC metastore

javax.jdo.option.ConnectionDriverName

org.apache.derby.jdbc.EmbeddedDriver

Driver class name for a JDBC metastore

javax.jdo.option.ConnectionUserName

APP

username to use against metastoredatabase

javax.jdo.option.ConnectionPassword

mine

password to use against metastoredatabase

5、启动hive

命令行键入

Hive

显示

WARNING: org.apache.hadoop.metrics.jvm.EventCounter is deprecated. Pleaseuse org.apache.hadoop.log.metrics.EventCounter in all the log4j.propertiesfiles.

Logging initialized using configuration injar:file:/home/test/Desktop/hive-0.8.1/lib/hive-common-0.8.1.jar!/hive-log4j.properties

Hive historyfile=/tmp/test/hive_job_log_test_201208260529_167273830.txt

hive>

5、测试语句

​ 建立测试表test

createtable test (key string);

showtables;

2.6 使用MySQL数据库的安装方式

1、安装MySQL

Ubuntu 采用apt-get安装

sudo apt-get install mysql-server

建立数据库hive

create database hive

创建hive用户,并授权

grant all on hive.* to hive@’%’ identified by ‘hive’;

flush privileges;

2、安装Hive

解压Hive:

tar zxvf hive-0.8.1.tar /home/test/Desktop

建立软连接:

ln –s hive-0.8.1 hive

添加环境变量

exportHIVE_HOME=/home/test/Desktop/hive

exportPATH=….HIVE_HOME/bin:$PATH:.

3、修改hive-site.xml

javax.jdo.option.ConnectionURL

jdbc:mysql://localhost:3306/hive

javax.jdo.option.ConnectionDriverName

com.mysql.jdbc.Driver

javax.jdo.option.ConnectionPassword

hive

hive.hwi.listen.port

9999

This is the port the Hive Web Interface will listenon

datanucleus.autoCreateSchema

false

datanucleus.fixedDatastore

true

​ hive.metastore.local

​ true

​ controls whether toconnect to remove metastore server or open a new metastore server in HiveClient JVM

4、启动Hive

命令行键入:Hive

显示

WARNING: org.apache.hadoop.metrics.jvm.EventCounter isdeprecated. Please use org.apache.hadoop.log.metrics.EventCounter in all thelog4j.properties files.

Logging initialized using configuration injar:file:/home/test/Desktop/hive-0.8.1/lib/hive-common-0.8.1.jar!/hive-log4j.properties

Hive historyfile=/tmp/test/hive_job_log_test_201208260529_167273830.txt

hive>

5、测试语句

建立测试表test

create table test (key string);

show tables;

3 Hive内建操作符与函数开发

3.1 关系运算符

等值比较: =

不等值比较: <>

小于比较: <

小于等于比较: <=

大于比较: >

大于等于比较: >=

空值判断: IS NULL

非空判断: IS NOT NULL

LIKE比较: LIKE

JAVA的LIKE操作: RLIKE

REGEXP操作: REGEXP

等值比较: =

语法:A=B

操作类型:所有基本类型

描述: 如果表达式A与表达式B相等,则为TRUE;否则为FALSE

举例:hive> select 1 from dual where 1=1;

不等值比较: <>

语法: A <> B

操作类型: 所有基本类型

描述: 如果表达式A为NULL,或者表达式B为NULL,返回NULL;如果表达式A与表达式B不相等,则为TRUE;否则为FALSE

举例:hive> select 1 from dual where 1 <> 2;

小于比较: <

语法: A < B

操作类型: 所有基本类型

描述: 如果表达式A为NULL,或者表达式B为NULL,返回NULL;如果表达式A小于表达式B,则为TRUE;否则为FALSE

举例:hive> select 1 from dual where 1 < 2;

小于等于比较: <=

语法: A <= B

操作类型: 所有基本类型

描述: 如果表达式A为NULL,或者表达式B为NULL,返回NULL;如果表达式A小于或者等于表达式B,则为TRUE;否则为FALSE

举例:hive> select 1 from dual where 1 <= 1;

大于等于比较: >=

语法: A >= B

操作类型: 所有基本类型

描述: 如果表达式A为NULL,或者表达式B为NULL,返回NULL;如果表达式A大于或者等于表达式B,则为TRUE;否则为FALSE

举例:hive> select 1 from dual where 1 >= 1;

空值判断: IS NULL

语法: A IS NULL

操作类型: 所有类型

描述: 如果表达式A的值为NULL,则为TRUE;否则为FALSE

举例:hive> select 1 from dual where null is null;

非空判断: IS NOT NULL

语法: A IS NOT NULL

操作类型: 所有类型

描述: 如果表达式A的值为NULL,则为FALSE;否则为TRUE

举例:hive> select 1 from dual where 1 is not null;

LIKE比较: LIKE

语法: A LIKE B

操作类型: strings

描述: 如果字符串A或者字符串B为NULL,则返回NULL;如果字符串A符合表达式B 的正则语法,则为TRUE;否则为FALSE。B中字符”_”表示任意单个字符,而字符”%”表示任意数量的字符。

举例:hive> select 1 from dual where ‘key’ like ‘foot%’;

hive> select 1 from dual where ‘key ’ like’foot____’;

注意:否定比较时候用 NOT A LIKE B

hive> select 1 from dual where NOT ‘key ’ like ‘fff%’;

JAVA的LIKE操作: RLIKE

语法: A RLIKE B

操作类型: strings

描述: 如果字符串A或者字符串B为NULL,则返回NULL;如果字符串A符合JAVA正则表达式B的正则语法,则为TRUE;否则为FALSE。

举例:hive> select 1 from dual where ‘footbar’ rlike’^f.*r$’;

注意:判断一个字符串是否全为数字:

hive>select 1 from dual where ‘123456’ rlike ‘^d+$’;

hive> select 1 from dual where ‘123456aa’ rlike’^d+$’;

REGEXP操作: REGEXP

语法: A REGEXP B

操作类型: strings

描述: 功能与RLIKE相同

举例:hive> select 1 from dual where ‘key’ REGEXP’^f.*r$’;

3.2 逻辑运算与数学运算

加法操作: +

减法操作: -

乘法操作: *

除法操作: /

取余操作: %

位与操作: &

位或操作: |

位异或操作: ^

位取反操作: ~

逻辑与操作: AND

逻辑或操作: OR

逻辑非操作: NOT

取整函数: round

指定精度取整函数: round

向下取整函数: floor

向上取整函数: ceil

向上取整函数: ceiling

取随机数函数: rand

自然指数函数: exp

以10为底对数函数: log10

以2为底对数函数: log2

对数函数: log

幂运算函数: pow

幂运算函数: power

开平方函数: sqrt

二进制函数: bin

十六进制函数: hex

反转十六进制函数: unhex

进制转换函数: conv

绝对值函数: abs

正取余函数: pmod

正弦函数: sin

反正弦函数: asin

余弦函数: cos

反余弦函数: acos

positive函数: positive

negative函数: negative

UNIX时间戳转日期函数: from_unixtime

获取当前UNIX时间戳函数: unix_timestamp

日期转UNIX时间戳函数: unix_timestamp

指定格式日期转UNIX时间戳函数: unix_timestamp

日期时间转日期函数: to_date

日期转年函数: year

日期转月函数: month

日期转天函数: day

日期转小时函数: hour

日期转分钟函数: minute

日期转秒函数: second

日期转周函数: weekofyear

日期比较函数: datediff

日期增加函数: date_add

日期减少函数: date_sub

If函数: if

非空查找函数: COALESCE

条件判断函数:CASE

字符串长度函数:length

字符串反转函数:reverse

字符串连接函数:concat

带分隔符字符串连接函数:concat_ws

字符串截取函数:substr,substring

字符串截取函数:substr,substring

字符串转大写函数:upper,ucase

字符串转小写函数:lower,lcase

去空格函数:trim

左边去空格函数:ltrim

右边去空格函数:rtrim

正则表达式替换函数:regexp_replace

正则表达式解析函数:regexp_extract

URL解析函数:parse_url

json解析函数:get_json_object

空格字符串函数:space

重复字符串函数:repeat

首字符ascii函数:ascii

左补足函数:lpad

右补足函数:rpad

分割字符串函数: split

集合查找函数: find_in_set

Map类型构建: map

Struct类型构建: struct

array类型构建: array

array类型访问: A[n]

map类型访问: M[key]

struct类型访问: S.x

Map类型长度函数: size(Map

原文地址:https://www.cnblogs.com/jpfss/p/10288377.html