Course Contents Hive Hive Hive Hive Hive Hive

  • Slides: 36
Download presentation

课程目录 Course Contents Hive原理与介绍 – – – Hive 架构与介绍 Hive 安装与部署 Hive 基本操作 Hive

课程目录 Course Contents Hive原理与介绍 – – – Hive 架构与介绍 Hive 安装与部署 Hive 基本操作 Hive 基本函数 Hive Map. Reduce过程 Hive 使用经验 部署php. Hiveadmin edu. 51 cto. com

Hive原理与介绍 l Hive 架构与介绍 edu. 51 cto. com

Hive原理与介绍 l Hive 架构与介绍 edu. 51 cto. com

Apache Hive 架构介绍 edu. 51 cto. com

Apache Hive 架构介绍 edu. 51 cto. com

Apache Hive 与普通数据库之间的关系 Hive RDBMS 查询语言 HQL SQL 数据存储 HDFS Raw Device or Local

Apache Hive 与普通数据库之间的关系 Hive RDBMS 查询语言 HQL SQL 数据存储 HDFS Raw Device or Local FS 索引 无 有 执行 Map. Reduce Excutor 执行延迟 高 低 处理数据规模 大 小 edu. 51 cto. com

Hive原理与介绍 l Hive 安装与部署 edu. 51 cto. com

Hive原理与介绍 l Hive 安装与部署 edu. 51 cto. com

Hive原理与介绍 l Hive 基本操作 edu. 51 cto. com

Hive原理与介绍 l Hive 基本操作 edu. 51 cto. com

Apache Hive 基本操作 :创建表 create table 语法: CREATE [EXTERNAL] TABLE [IF NOT EXISTS] table_name

Apache Hive 基本操作 :创建表 create table 语法: 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, . . . ) [SORTED BY (col_name [ASC|DESC], . . . )] INTO num_buckets BUCKETS] [ [ROW FORMAT row_format] [STORED AS file_format] | STORED BY 'storage. handler. class. name' [ WITH SERDEPROPERTIES (. . . ) ] ] [LOCATION hdfs_path] [TBLPROPERTIES (property_name=property_value, . . . )] [AS select_statement] edu. 51 cto. com

Apache Hive 基本操作 :创建表 基本例子 create table cto. stock_everydate_detail ( stock_name string, stock_type string,

Apache Hive 基本操作 :创建表 基本例子 create table cto. stock_everydate_detail ( stock_name string, stock_type string, trans_time string, price double, up_or_down_price double, trans_count bigint, trans_nmoney bigint, trans_type string ) PARTITIONED BY(dt String) ROW FORMAT DELIMITED FIELDS TERMINATED BY 't' edu. 51 cto. com

Apache Hive 基本操作 :创建外部表 基本例子 create table cto. stock_everydate_detail_extand ( stock_name string, stock_type string,

Apache Hive 基本操作 :创建外部表 基本例子 create table cto. stock_everydate_detail_extand ( stock_name string, stock_type string, trans_time string, price double, up_or_down_price double, trans_count bigint, trans_nmoney bigint, trans_type string ) PARTITIONED BY(dt String) ROW FORMAT DELIMITED FIELDS TERMINATED BY 't'; LOCATION '/user/hadoop/cto_extand/stock_data'; edu. 51 cto. com

Apache Hive 基本操作: 修改表 alter table ALTER TABLE table_name ADD [IF NOT EXISTS] partition_spec

Apache Hive 基本操作: 修改表 alter table ALTER TABLE table_name ADD [IF NOT EXISTS] partition_spec [ LOCATION 'location 1' ] partition_spec [ LOCATION 'location 2' ]. . . 示例: alter table stock_everydate_detail_extand add partition (dt='201312') location '201312'; alter table dcs_travel_erveryday_extand drop partition (dt='201312'); edu. 51 cto. com

Apache Hive 基本操作: 修改表名 alter table 语法: ALTER TABLE table_name RENAME TO new_table_name 示例:

Apache Hive 基本操作: 修改表名 alter table 语法: ALTER TABLE table_name RENAME TO new_table_name 示例: ALTER TABLE stock_everydate_detail RENAME TO stock_everydate_detail_01 edu. 51 cto. com

Apache Hive 基本操作:分区操作 alter table 语法: ALTER TABLE table_name ADD [IF NOT EXISTS] partition_spec

Apache Hive 基本操作:分区操作 alter table 语法: ALTER TABLE table_name ADD [IF NOT EXISTS] partition_spec [ LOCATION 'location 1' ] partition_spec [ LOCATION 'location 2' ]. . . 示例: alter table stock_everydate_detail_extand add partition (dt='201312') location '201312'; alter table dcs_travel_erveryday_extand drop partition (dt='201312'); edu. 51 cto. com

Apache Hive 基本操作:字段操作 alter table 语法: ALTER TABLE table_name ADD|REPLACE COLUMNS (col_name data_type [COMMENT

Apache Hive 基本操作:字段操作 alter table 语法: ALTER TABLE table_name ADD|REPLACE COLUMNS (col_name data_type [COMMENT col_comment], . . . ) 示例: ALTER TABLE stock_everydate_detail ADD COLUMNS (insert_time string); ALTER TABLE stock_everydate_detail REPLACE COLUMNS (stock_name string, stock_type string, trans_time string, price double, up_or_down_price double, trans_count bigint, trans_nmoney bigint, trans_type string ) edu. 51 cto. com

Apache Hive 基本操作:加载数据 Load data 语法: LOAD DATA [LOCAL] INPATH 'filepath' [OVERWRITE] INTO TABLE

Apache Hive 基本操作:加载数据 Load data 语法: LOAD DATA [LOCAL] INPATH 'filepath' [OVERWRITE] INTO TABLE tablename [PARTITION (partcol 1=val 1, partcol 2=val 2. . . )] 示例: LOAD DATA INPATH '/user/hadoop/cto_data/stock_data. csv' INTO TABLE stock_everydate_detail OVERWRITE PARTITION (dt='201312'); edu. 51 cto. com

Apache Hive 基本操作:HQL操作 select 语法: SELECT [ALL | DISTINCT] select_expr, . . . FROM

Apache Hive 基本操作:HQL操作 select 语法: SELECT [ALL | DISTINCT] select_expr, . . . FROM table_reference [WHERE where_condition] [GROUP BY col_list] [ CLUSTER BY col_list | [DISTRIBUTE BY col_list] [SORT BY col_list] ] [LIMIT number] 示例: Select * from stock_everydate_detail_extand whre dt='201312’ limit 10; edu. 51 cto. com

Apache Hive 基本操作:HQL操作 join命令 语法: table_reference JOIN table_factor [join_condition] | table_reference {LEFT|RIGHT|FULL} [OUTER] JOIN

Apache Hive 基本操作:HQL操作 join命令 语法: table_reference JOIN table_factor [join_condition] | table_reference {LEFT|RIGHT|FULL} [OUTER] JOIN table_reference 示例: Select a. stock_name, b. trans_time , b. price from stock_everydate_detail a Left outer join stock_name b on a. stock_name = b. stock_code where dt='201312’ limit 10; edu. 51 cto. com

Hive原理与介绍 l Hive 基本函数 edu. 51 cto. com

Hive原理与介绍 l Hive 基本函数 edu. 51 cto. com

Apache Hive 基本函数:日期函数 返回值类型 名称 描述 string from_unixtime(int unixtime) 将时间戳(unix epoch秒数)转换为日期时间字符串, 例如from_unixtime(0)="1970 -01 -01

Apache Hive 基本函数:日期函数 返回值类型 名称 描述 string from_unixtime(int unixtime) 将时间戳(unix epoch秒数)转换为日期时间字符串, 例如from_unixtime(0)="1970 -01 -01 00: 00" bigint unix_timestamp() unix_timestamp(string date) bigint to_date(string timestamp) string year(string date) int month(string date) int day(string date) dayofmonth(date) int int hour(string date) minute(string date) second(string date) weekofyear(string date) datediff(string enddate, startdate) int int 获得当前时间戳 获得date表示的时间戳 返回日期字符串,例如to_date("1970 -01 -01 00: 00") = "1970 -01 -01" 返 回 年 , 例 如 year("1970 -01 -01 00: 00") = 1970, year("1970 -01 -01") = 1970 string 返 回 enddate和 startdate的 天 数 的 差 , 例 如 datediff('2009 -03 -01', '2009 -02 -27') = 2 加 days天 数 到 startdate: date_add('2008 -12 -31', 1) = date_add(string startdate, int days) '2009 -01 -01' 减 days天 数 到 startdate: date_sub('2008 -12 -31', 1) = date_sub(string startdate, int days) '2008 -12 -30' edu. 51 cto. com

Apache Hive 基本函数:条件函数 返回值类型 名称 描述 - if(boolean test. Condition, T value. True, T

Apache Hive 基本函数:条件函数 返回值类型 名称 描述 - if(boolean test. Condition, T value. True, T value. False. Or. Null) 当 test. Condition为 真 时 返 回 value. True, test. Condition为 假 或 NULL时返回value. False. Or. Null - COALESCE(T v 1, T v 2, . . . ) 返回列表中的第一个非空元素, 如果列表元素都为空则返回NULL - CASE a WHEN b THEN c [WHEN d THEN e]* [ELSE f] END a = b,返回c;a = d,返回e;否 则返回f - CASE WHEN a THEN b [WHEN c THEN d]* [ELSE e] END a 为真,返回b;c为真,返回d; 否则e edu. 51 cto. com

Apache Hive 基本函数:字符串函数 返回值类型 Int String 名称 length(string A) reverse(string A) concat(string A, string

Apache Hive 基本函数:字符串函数 返回值类型 Int String 名称 length(string A) reverse(string A) concat(string A, string B. . . ) 描述 返回字符串长度 反转字符串 合 并 字 符 串 , 例 如 concat('foo', 'bar')='foobar'。注意这一函数可以接 受任意个数的参数 substr(string A, int start) 返 回 子 串 , 例 如 substr('foobar', substring(string A, int start) 4)='bar' String substr(string A, int start, int len) 返 回 限 定 长 度 的 子 串 , 例 如 substring(string A, int start, int substr('foobar', 4, 1)='b' len) String upper(string A) ucase(string A) 转换为大写 String lower(string A) lcase(string A) 转换为小写 edu. 51 cto. com

Apache Hive 基本函数:字符串函数 String String trim(string A) ltrim(string A) regexp_replace(string. A, Returns the string

Apache Hive 基本函数:字符串函数 String String trim(string A) ltrim(string A) regexp_replace(string. A, Returns the string resulting from replacing all string B, string C) substrings in B that match the Java regular expression syntax(See Java regular expressions syntax) with C e. g. regexp_replace("foobar", "oo|ar", "") returns 'fb. ' Note that some care is necessary in using predefined character classes: using 's' as the second argument will match the letter s; '\s' is necessary to match whitespace, etc. regexp_extract(string 返回使用正则表达式提取的子字串。例如, subject, string pattern, regexp_extract('foothebar', 'foo(. *? )(bar)', 2)='bar'。 intex) 注意使用特殊字符的规则:使用's'代表的是字符 's';空白字符需要使用'\s',以此类推。 edu. 51 cto. com

Apache Hive 基本函数:字符串函数 String parse_url(stri ng url. String, string part. To. Extract ) 解析URL字符串,part.

Apache Hive 基本函数:字符串函数 String parse_url(stri ng url. String, string part. To. Extract ) 解析URL字符串,part. To. Extract的可选项有:HOST, PATH, QUERY, REF, PROTOCOL, FILE, AUTHORITY, USERINFO。 例如, parse_url('http: //facebook. com/path/p 1. php? query=1', 'HOST')='facebook. com' parse_url('http: //facebook. com/path/p 1. php? query=1', 'PATH')='/path/p 1. php' parse_url('http: //facebook. com/path/p 1. php? query=1', 'QUERY')='query=1', 可 以 指 定 key来 返 回 特 定 参 数 , key的 格 式 是 QUERY: <KEY_NAME>,例如QUERY: k 1 parse_url('http: //facebook. com/path/p 1. php? query=1&field=2', 'QUERY', 'q uery')='1'可以用来取出外部渲染参数key对应的value值 parse_url('http: //facebook. com/path/p 1. php? query=1&field=2', 'QUERY', 'fi eld')='2' parse_url('http: //facebook. com/path/p 1. php? query=1#Ref', 'REF')='Ref' parse_url('http: //facebook. com/path/p 1. php? query=1#Ref', 'PROTOCOL')='http' edu. 51 cto. com

Hive原理与介绍 l Hive Map. Reduce过程 edu. 51 cto. com

Hive原理与介绍 l Hive Map. Reduce过程 edu. 51 cto. com

Apache Hive Map. Reduce过程 JOIN SELECT pv. pageid, u. age FROM page_view pv JOIN

Apache Hive Map. Reduce过程 JOIN SELECT pv. pageid, u. age FROM page_view pv JOIN user u ON (pv. userid = u. userid); edu. 51 cto. com

Apache Hive Map. Reduce过程 GROUP BY SELECT pageid, age, count(1) FROM pv_users GROUP BY

Apache Hive Map. Reduce过程 GROUP BY SELECT pageid, age, count(1) FROM pv_users GROUP BY pageid, age; edu. 51 cto. com

DISTINCT SELECT age, count(distinct pageid) FROM pv_users GROUP BY age edu. 51 cto. com

DISTINCT SELECT age, count(distinct pageid) FROM pv_users GROUP BY age edu. 51 cto. com

php. Hive. Admin 介绍与安装 edu. 51 cto. com

php. Hive. Admin 介绍与安装 edu. 51 cto. com

Thank You ! edu. 51 cto. com

Thank You ! edu. 51 cto. com