欢迎投稿

今日深度:

hive 使用教程,

hive 使用教程,


1. hive 介绍

hive是一个基于hadoop的数据仓库系统,提供一种机制把数据映射为结构化的table;

 官方文档:https://cwiki.apache.org/confluence/display/Hive/Home#Home-ApacheHive

应用场景:非实时的海量数据分析/挖掘/建模

2.建表语句

  语法:


CREATE [TEMPORARY] [EXTERNAL] TABLE [IF NOT EXISTS] [db_name.]table_name    -- (Note: TEMPORARY available in Hive 0.14.0 and later)
  [(col_name data_type [COMMENT col_comment], ... [constraint_specification])]
  [COMMENT table_comment]
  [PARTITIONED BY (col_name data_type [COMMENT col_comment], ...)]
  [CLUSTERED BY (col_name, col_name, ...) [SORTED BY (col_name [ASC|DESC], ...)] INTO num_buckets BUCKETS]
  [SKEWED BY (col_name, col_name, ...)                  -- (Note: Available in Hive 0.10.0 and later)]
     ON ((col_value, col_value, ...), (col_value, col_value, ...), ...)
     [STORED AS DIRECTORIES]
  [
   [ROW FORMAT row_format] 
   [STORED AS file_format]
     | STORED BY 'storage.handler.class.name' [WITH SERDEPROPERTIES (...)]  -- (Note: Available in Hive 0.6.0 and later)
  ]
  [LOCATION hdfs_path]
  [TBLPROPERTIES (property_name=property_value, ...)]   -- (Note: Available in Hive 0.6.0 and later)
  [AS select_statement];   -- (Note: Available in Hive 0.5.0 and later; not supported for external tables)
 
CREATE [TEMPORARY] [EXTERNAL] TABLE [IF NOT EXISTS] [db_name.]table_name
  LIKE existing_table_or_view_name
  [LOCATION hdfs_path];
 
data_type
  : primitive_type
  | array_type
  | map_type
  | struct_type
  | union_type  -- (Note: Available in Hive 0.7.0 and later)
 
primitive_type
  : TINYINT
  | SMALLINT
  | INT
  | BIGINT
  | BOOLEAN
  | FLOAT
  | DOUBLE
  | DOUBLE PRECISION -- (Note: Available in Hive 2.2.0 and later)
  | STRING
  | BINARY      -- (Note: Available in Hive 0.8.0 and later)
  | TIMESTAMP   -- (Note: Available in Hive 0.8.0 and later)
  | DECIMAL     -- (Note: Available in Hive 0.11.0 and later)
  | DECIMAL(precision, scale)  -- (Note: Available in Hive 0.13.0 and later)
  | DATE        -- (Note: Available in Hive 0.12.0 and later)
  | VARCHAR     -- (Note: Available in Hive 0.12.0 and later)
  | CHAR        -- (Note: Available in Hive 0.13.0 and later)
 
array_type
  : ARRAY < data_type >
 
map_type
  : MAP < primitive_type, data_type >
 
struct_type
  : STRUCT < col_name : data_type [COMMENT col_comment], ...>
 
union_type
   : UNIONTYPE < data_type, data_type, ... >  -- (Note: Available in Hive 0.7.0 and later)
 
row_format
  : DELIMITED [FIELDS TERMINATED BY char [ESCAPED BY char]] [COLLECTION ITEMS TERMINATED BY char]
        [MAP KEYS TERMINATED BY char] [LINES TERMINATED BY char]
        [NULL DEFINED AS char]   -- (Note: Available in Hive 0.13 and later)
  | SERDE serde_name [WITH SERDEPROPERTIES (property_name=property_value, property_name=property_value, ...)]
 
file_format:
  : SEQUENCEFILE
  | TEXTFILE    -- (Default, depending on hive.default.fileformat configuration)
  | RCFILE      -- (Note: Available in Hive 0.6.0 and later)
  | ORC         -- (Note: Available in Hive 0.11.0 and later)
  | PARQUET     -- (Note: Available in Hive 0.13.0 and later)
  | AVRO        -- (Note: Available in Hive 0.14.0 and later)
  | INPUTFORMAT input_format_classname OUTPUTFORMAT output_format_classname
 
constraint_specification:
  : [, PRIMARY KEY (col_name, ...) DISABLE NOVALIDATE ]
    [, CONSTRAINT constraint_name FOREIGN KEY (col_name, ...) REFERENCES table_name(col_name, ...) DISABLE NOVALIDATE 



 建表实例:

create table contact(id bigint,f_name string,f_phone string)  ROW FORMAT DELIMITED fields terminated by '\t'
说明:对于文本数据一般指定列分隔符,不然会出现插入如都是空的数据:

hive> select * from default.contact;
OK
NULL    NULL    NULL
NULL    NULL    NULL
NULL    NULL    NULL
NULL    NULL    NULL
NULL    NULL    NULL
NULL    NULL    NULL
NULL    NULL    NULL
Time taken: 0.225 seconds, Fetched: 7 row(s)


样例数据:

[hadoop@master example]$ cat /home/hadoop/example/contact.txt
1       hxw     15965490730
2       wzx     15915485260
3       gcx     14515647620
4       qnm     13214636993
5       gdf     11423356897
6       lxp     14547532156
7       lyy     13914517852
[hadoop@master example]$ 



load 数据:

hive> Load data local inpath '/home/hadoop/example/contact.txt' OVERWRITE  into table contact;
Loading data to table test.contact
OK
Time taken: 0.441 seconds

查询数据:

hive> select id,f_name,f_phone from contact;
OK
1       hxw     15965490730
2       wzx     15915485260
3       gcx     14515647620
4       qnm     13214636993
5       gdf     11423356897
6       lxp     14547532156
7       lyy     13914517852
Time taken: 0.193 seconds, Fetched: 7 row(s)

hive 操作是类sql 的操作,感觉学习起来会很容易,还有分区表的概念,很多高深的东西,需要以后慢慢研究。。。

www.htsjk.Com true http://www.htsjk.com/hive/39835.html NewsArticle hive 使用教程, 1. hive 介绍 hive是一个基于hadoop的数据仓库系统,提供一种机制把数据映射为结构化的table;  官方文档:https://cwiki.apache.org/confluence/display/Hive/Home#Home-ApacheHive 应用场景:...
相关文章
    暂无相关文章
评论暂时关闭