Hive常用HiveQL

MySQL设置:

#添加一个数据库用户

insert into mysql.user(Host,User,Password) values("%","longsheng",password("longsheng1234"));

#数据库授权:

GRANT all ON hive.* TO [email protected]‘longV007‘ IDENTIFIED BY ‘sheng‘;

flush privileges;

#设置数据库日志格式

set global binlog_format=‘MIXED‘;

启动Hive

#集群方式启动Hive

hive --auxpath /usr/share/hive/lib/hive-hbase-handler-0.13.1.jar,/usr/share/hive/lib/zookeeper-3.4.6.jar

(可选参数 -hiveconf hive.root.logger=DEBUG,console)

创建表Hive-HBase关联表

#创建Hive和HBase的关联表:

* bidask_quote_hive是hive的表

* "hbase.table.name" = "bidask_quote"是hbase中已经存在的表

接下来将两者关联起来,命令如下:

#创建关联表命令

hive> CREATE EXTERNAL TABLE bidask_quote_hive(key string,ProdCode string,ProdName string,TradingDay string,ExchangeID string,ExchangeInstID string,LastPrice string,PreSettlementPrice string,PreClosePrice string,PreOpenInterest string,OpenPrice string,HighestPrice string,LowestPrice string,Volume string,Turnover string,TotalVolume string,TotalTurnover string,OpenInterest string,ClosePrice string,SettlementPrice string,UpperLimitPrice string,LowerLimitPrice string,PreDelta string,CurrDelta string,UpdateTime string,UpdateMillisec string,BidPrice1 string,BidVolume1 string,AskPrice1 string,AskVolume1 string,BidPrice2 string,BidVolume2 string,AskPrice2 string,AskVolume2 string,BidPrice3 string,BidVolume3 string,AskPrice3 string,AskVolume3 string,BidPrice4 string,BidVolume4 string,AskPrice4 string,AskVolume4 string,BidPrice5 string,BidVolume5 string,AskPrice5 string,AskVolume5 string,AveragePrice string,Equil string,CloseDate string,DecInPrice string,logDateTime string)

STORED BY ‘org.apache.hadoop.hive.hbase.HBaseStorageHandler‘

WITH SERDEPROPERTIES ("hbase.columns.mapping" = "info:ProdCode,info:ProdName,info:TradingDay,info:ExchangeID,info:ExchangeInstID,info:LastPrice,info:PreSettlementPrice,info:PreClosePrice,info:PreOpenInterest,info:OpenPrice,info:HighestPrice,info:LowestPrice,info:Volume,info:Turnover,info:TotalVolume,info:TotalTurnover,info:OpenInterest,info:ClosePrice,info:SettlementPrice,info:UpperLimitPrice,info:LowerLimitPrice,info:PreDelta,info:CurrDelta,info:UpdateTime,info:UpdateMillisec,info:BidPrice1,info:BidVolume1,info:AskPrice1,info:AskVolume1,info:BidPrice2,info:BidVolume2,info:AskPrice2,info:AskVolume2,info:BidPrice3,info:BidVolume3,info:AskPrice3,info:AskVolume3,info:BidPrice4,info:BidVolume4,info:AskPrice4,info:AskVolume4,info:BidPrice5,info:BidVolume5,info:AskPrice5,info:AskVolume5,info:AveragePrice,info:Equil,info:CloseDate,info:DecInPrice,info:logDateTime")

TBLPROPERTIES("hbase.table.name" = "bidask_quote"); 

我的表的字段很多,50多个。

说明:

1.bidask_quote_hive(key string,ProdCode string,string,ProdName string,....)是hive表的结构

2."hbase.columns.mapping" = "info:ProdCode,info:ProdName,info:TradingDay,...)是HBase中的列信息,这里现在只有一个列蔟。

查询语句

# 查看表是否创建成功

hive> show tables;

# 查询全字段

hive> select * from bidask_quote_hive limit 3;

# 查询所有条数

hive> select count(1) from bidask_quote_hive;

# 投影查询

hive> select key,ProdCode,TradingDay from bidask_quote_hive limit 3;

# 别名查询

别名查询对于单表查询没有多少意义,如果使用连接查询的话可以使用别名

hive> select b.ProdCode,b.TradingDay from bidask_quote_hive b limit 3;

# 正则表达式指定列

hive> select symbol,‘price.*‘ from stocks;

(说明:该条语句查询symbol列和以price为前缀的所有列)

# 使用列值进行计算

hive> select upper(name),deductions["Federal Taxes"],round(salary * (1 - deductions["Federal Taxes"])) from employees;

(说明:1:upper(name)函数对name字段的值全部转为大写

2:deductios是个Map类型的字段,我们只取出Federal Taxes的值

3:round()函数是做了一个四舍五入的运算)

# limit查询

hive> select * from bidask_quote_hive limit 3;

# 列别名

hive> select upper(name),deductions["Federal Taxes"] as fed_taxs from employees;

# 嵌套查询

hive> from(

> select upper(name),deductions["Federal Taxes"]  as fed_taxs,

> round(salary * (1 - deductions["Federal Taxes"])) as salary_minus_fed_taxes from employees;

> ) e

> select e.name, e.salary_minus_fed_taxes

> where e.salary_minus_fed_taxes > 7000;

# where过滤查询

hive> select * from employees where country = ‘US‘ and state = ‘CA‘

# like子句

示例1:

hive> select name,address.street from employees where address.street like ‘%Avo.‘;

John    1 Michigan Ave.

Todd    200 Chicago Ave.

示例2:

hive> select name,address.street from employees where address.street like ‘%Chi%‘;

Todd    200 Chicago Ave.

# Relike子句

Relike相当由于正则表达式,可以使用Java的正则表达式.例:

hive>select name, address.street from employees where address.street relike ‘.*(Chicago|Ontario).*‘

Mary     100 Ontario St.

todd      200 Chicago Ave.

说明:上面的正则表达式中点号(.)标识和任意字符匹配,星号(*)表示重复“左边的字符串”零次或无数次。表达式(x|y)表示x或y。

#group by

说明:group by 通常和聚合函数一起使用,按照一个或多个列进行分组,然后对每个组执行聚合操作。

示例1:存在交易表stocks,以下查询语句按照苹果公司股票(股票代码APPL)的年份对股票记录进行分组,然后计算每年的平均收盘价

hive> select year(ymd),avg(price_close) from stocks where exchange = ‘NASDAQ‘ AND symbol ‘APPL‘ GROUP BY year(ymd);

1984      25.5786

1985      20.1936

1987      53.8456

示例2:hiving字句

hive> select year(ymd),avg(price_close) from stocks where exchange = ‘NASDAQ‘ AND symbol ‘APPL‘ GROUP BY year(ymd)

> having avg(price_close) > 50.0;

JOIN语句

# INNER JOIN语句

内连接中,只有进行连接的两个表中都存在与连接标准相匹配。

示例1:对苹果公司(APPL)和IBM公司(IBM)的股价进行比较。股票表stocks进行自连接,连接条件是ymd(year-month-day)相等。

hive> select a.ymd, a.price_close, b.price_close

> from stocks a join stocks b on a.ymd = b.ymd

> where a.symbol = ‘APPL‘ and b.symbol = ‘IBM‘;

2010-01-04   210.21   132.45

2010-01-05   214.41   130.26

2010-01-06   211.61   134.87

2010-01-07   212.01   133.45

# JOIN语句

hive> select s.ymd, s.symbol, s.price_close, d.dividend

> from stocks s join dividend d on s.ymd = d.ymd and s.symbol = d.symbol

> where s.symbol = ‘APPL‘;

1987-05-11   APPL      77.01     0.015

1987-08-10   APPL      48.61     0.015

1987-11-17   APPL      78.01     0.002

3张表的JOIN:

hive> select a.ymd, a.price_close, b.price_close,c.price_close

> from stocks a join stocks b on a.ymd = b.ymd

>               join stocks c on a.ymd = c.ymd

> where a.symbol = ‘APPL‘ and b.symbol = ‘IBM‘ and c.symbol = ‘GE‘;

1987-05-11   APPL      77.01     0.015

1987-08-10   APPL      48.61     0.015

1987-11-17   APPL      78.01     0.002

# 查询优化

尽量将大表放在前面

# LEFT OUTER JOIN语句

做外连接:join操作左边表中符合where子句的所有记录将会被返回。join右边表中如果没有符合on后面连接条件的记录时,那么

右表列的值将会是NULL。例:

hive> select s.ymd, s.symbol, s.price_close, d.dividend

> from stocks s left outer join dividend d on s.ymd = d.ymd and s.symbol = d.symbol

> where s.symbol = ‘APPL‘;

ORDER BY 和 SORT BY语句

ORDER BY:全排序

SORT BY:只会在每个reducer中对数据进行排序,只进行局部排序,保证每个reducer输出的数据是有序的。例:

hive> select s.ymd, s.symbol, s.price_close

> from stocks s

> order|sort by s.ymd ASC, s.symbol DESC;

时间: 2024-08-28 16:48:36

Hive常用HiveQL的相关文章

Hive常用语句

1Hive简介 Hive对我来说就是一个基于HDFS的数据仓库,它提供了一个种类SQL语言(和SQL标准基本一样又有一些特殊的地方不一样),能让不精通Java语言而熟悉SQL语言的工程师,快速的对HDFS或其他存储文件系统如Amazon,S3,上的数据进行数据分析,是Hadoop生态系统中非常重要的一个工具.对于大数据分析师而言,HiveQL则是必须要掌握的一个工具. 2.Hive常用语句 2.1菜鸟建表法 1.直接建表,指定分隔符,默认存储为text,也可以指定存储格式! create tab

hive常用命令

hive常用命令 show tables; 列出hive里面所有数据表名 desc userProfile; 显示数据表userProfile的基本表字段及字段type desc extended trackinfo; 显示数据表trackinfo的详细信息,包括字段说明,数据表等 /usr/local/cloud/hive/bin/hive 进入hive数据库 select attribute_name from pms_attribute where attribute_id=21000 a

hive 常用UDF

Hive UDF整理 (可以直接在mysql上测试,hive中没有伪表,需要手动创建,反应慢)字符串函数 字符串长度函数:length 语法: length(string A) 返回值: int 说明:返回字符串A的长度 举例: hive> select length(‘abcedfg’) from dual; 7 字符串反转函数:reverse 语法: reverse(string A) 返回值: string 说明:返回字符串A的反转结果 举例: hive> select reverse(

Hive - 常用命令

1. 创建表 创建表的语句: Create [EXTERNAL] TABLE [IF NOT EXISTS] table_name [(col_name data_type [COMMENT col_comment], ...)] [COMMENT table_comment] [PARTITIONED BY (col_name data_type [COMMENT col_comment], ...)] [CLUSTERED BY (col_name, col_name, ...) [SORT

Hive[6] HiveQL 查询

6.1   SELECT ... FROM 语句 hive> SELECT name,salary FROM employees;    --普通查询 hive>SELECT e.name, e.salary FROM employees e;  --也支持别名查询 当用户选择的列是集合数据类型时,Hive会使用 JSON 语法应用于输出: hive> SELECT name,subordinates FROM employees;   显示  John Doe ["Mary

Hive[5] HiveQL 数据操作

5.1 向管理表中装载数据  Hive 没有行级别的数据插入更新和删除操作,那么往表中装载数据的唯一途径就是使用一种“大量”的数据装载操作,或者通过其他方式仅仅将文件写入到正确的目录下: LOAD DATA LOCAL INPATH '${env:HOME}/califonia-employees' OVERWRITE INOT TABLE employees PARTITON (country=''US, state='CA') ; 向管理表中装载数据,如果目录不存在的话, overwrite

Hive:常用的一些命令

1.一般可以通过beeline,代理方式登录hive; 2.使用数据库abc_hive_db:use abc_hive_db; 3.查看数据库中有哪些表:show tables :有哪些特定表 show tables like '*tb_site*'; 4.查看某张表的表结构:desc tablename; 5.创建表: --OID,MSISDN,StartTime,EndTime,AP_MAC,ApAliasName,HotSpotName,Longitude,Latitude,Floor 0

关于centos环境下Flume+hadoop+hive常用命令

centos命令 进入root用户su root 1. 复制 2.解压tar.gztar zxvf xxx.tar.gz 3.文件操作 -创建文件夹mkdir mkdir /usr/ mkdir 文件名 -移动文件mv [options] 源文件或目录 目标文件或目录 —删除一个文件rm —删除一个文件夹 rm /home/test rm -r /home/test —文件赋权限cmod 1.txt 4.修改文件夹权限chown -R Hadoop.Hadoop /增加可执行读写权限chmod

hive常用函数

来源:https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF Complex Type Constructors The following functions construct instances of complex types. Constructor Function Operands Description map (key1, value1, key2, value2, ...) Creates a m