码迷,mamicode.com
首页 > 数据库 > 详细

【Hive学习之二】Hive SQL

时间:2019-01-29 18:00:39      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:操作   esc   0.12   name   man   无法   lines   图片   tiny   

环境
  虚拟机:VMware 10
  Linux版本:CentOS-6.5-x86_64
  客户端:Xshell4
  FTP:Xftp4
  jdk8
  hadoop-3.1.1
  apache-hive-3.1.1

 

参考:官网hive操作手册

一、DDL

1、数据类型

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)

 

2、数据库的创建、删除、修改;

3、表的创建、删除、修改;

举例:创建表

hive>CREATE TABLE person(
id INT,
name STRING,
age INT,
likes ARRAY<STRING>,
address MAP<STRING,STRING>
)
ROW FORMAT DELIMITED 
FIELDS TERMINATED BY , 
COLLECTION ITEMS TERMINATED BY -
MAP KEYS TERMINATED BY : 
LINES TERMINATED BY \n;

查看表结构:

hive> desc person;
OK
id                      int                                         
name                    string                                      
age                     int                                         
likes                   array<string>                               
address                 map<string,string>                          
Time taken: 0.095 seconds, Fetched: 5 row(s)
hive> desc formatted person;
OK
# col_name                data_type               comment             
id                      int                                         
name                    string                                      
age                     int                                         
likes                   array<string>                               
address                 map<string,string>                          
          
# Detailed Table Information          
Database:               default                  
OwnerType:              USER                     
Owner:                  root                     
CreateTime:             Tue Jan 29 11:41:12 CST 2019     
LastAccessTime:         UNKNOWN                  
Retention:              0                        
Location:               hdfs://PCS102:9820/root/hive_remote/warehouse/person     
Table Type:             MANAGED_TABLE            
Table Parameters:          
    COLUMN_STATS_ACCURATE    {\"BASIC_STATS\":\"true\",\"COLUMN_STATS\":{\"address\":\"true\",\"age\":\"true\",\"id\":\"true\",\"likes\":\"true\",\"name\":\"true\"}}
    bucketing_version       2                   
    numFiles                0                   
    numRows                 0                   
    rawDataSize             0                   
    totalSize               0                   
    transient_lastDdlTime    1548733272          
          
# Storage Information          
SerDe Library:          org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe     
InputFormat:            org.apache.hadoop.mapred.TextInputFormat     
OutputFormat:           org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat     
Compressed:             No                       
Num Buckets:            -1                       
Bucket Columns:         []                       
Sort Columns:           []                       
Storage Desc Params:          
    collection.delim        -                   
    field.delim             ,                   
    line.delim              \n                  
    mapkey.delim            :                   
    serialization.format    ,                   
Time taken: 0.157 seconds, Fetched: 39 row(s)

向表内加载数据:

data:
1,小明1,18,lol-book-movie,beijing:shangxuetang-shanghai:pudong
2,小明2,20,lol-book-movie,beijing:shangxuetang-shanghai:pudong
3,小明3,21,lol-book-movie,beijing:shangxuetang-shanghai:pudong
4,小明4,21,lol-book-movie,beijing:shangxuetang-shanghai:pudong
5,小明5,21,lol-book-movie,beijing:shangxuetang-shanghai:pudong
6,小明6,21,lol-book-movie,beijing:shangxuetang-shanghai:pudong
hive> LOAD DATA LOCAL INPATH ‘/root/data‘ INTO TABLE person;
Loading data to table default.person
OK
Time taken: 0.185 seconds
hive> select * from person;
OK
1    小明1    18    ["lol","book","movie"]    {"beijing":"shangxuetang","shanghai":"pudong"}
2    小明2    20    ["lol","book","movie"]    {"beijing":"shangxuetang","shanghai":"pudong"}
3    小明3    21    ["lol","book","movie"]    {"beijing":"shangxuetang","shanghai":"pudong"}
4    小明4    21    ["lol","book","movie"]    {"beijing":"shangxuetang","shanghai":"pudong"}
5    小明5    21    ["lol","book","movie"]    {"beijing":"shangxuetang","shanghai":"pudong"}
6    小明6    21    ["lol","book","movie"]    {"beijing":"shangxuetang","shanghai":"pudong"}
Time taken: 0.126 seconds, Fetched: 6 row(s)
hive> 

备注:向表导入数据最好按照表定义的结构来安排数据,如果不按照这个格式,文件也能上传到HDFS,这是通过hive select查看的时候查不出来,无法格式化输出。

技术分享图片

 

【Hive学习之二】Hive SQL

标签:操作   esc   0.12   name   man   无法   lines   图片   tiny   

原文地址:https://www.cnblogs.com/cac2020/p/10333910.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!