hive 中创建表的三种方式

Posted 数据咖啡

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hive 中创建表的三种方式相关的知识,希望对你有一定的参考价值。

1.create table

首先我们找到官网对创建表的描述如下: ’[]’ 表示可选,’|’ 表示几选一

 
   
   
 
  1. CREATE [TEMPORARY] [EXTERNAL] TABLE [IF NOT EXISTS] [db_name.]table_name    -- (Note: TEMPORARY available in Hive 0.14.0 and later)

  2.  [(col_name data_type [COMMENT col_comment], ... [constraint_specification])]

  3.  [COMMENT table_comment]

  4.  [PARTITIONED BY (col_name data_type [COMMENT col_comment], ...)]

  5.  [CLUSTERED BY (col_name, col_name, ...) [SORTED BY (col_name [ASC|DESC], ...)] INTO num_buckets BUCKETS]

  6.  [SKEWED BY (col_name, col_name, ...)                  -- (Note: Available in Hive 0.10.0 and later)]

  7.     ON ((col_value, col_value, ...), (col_value, col_value, ...), ...)

  8.     [STORED AS DIRECTORIES]

  9.  [

  10.   [ROW FORMAT row_format]

  11.   [STORED AS file_format]

  12.     | STORED BY 'storage.handler.class.name' [WITH SERDEPROPERTIES (...)]  -- (Note: Available in Hive 0.6.0 and later)

  13.  ]

  14.  [LOCATION hdfs_path]

  15.  [TBLPROPERTIES (property_name=property_value, ...)]   -- (Note: Available in Hive 0.6.0 and later)

  16.  [AS select_statement];   -- (Note: Available in Hive 0.5.0 and later; not supported for external tables)

  17. CREATE [TEMPORARY] [EXTERNAL] TABLE [IF NOT EXISTS] [db_name.]table_name

  18.  LIKE existing_table_or_view_name

  19.  [LOCATION hdfs_path];

  20. data_type

  21.  : primitive_type

  22.  | array_type

  23.  | map_type

  24.  | struct_type

  25.  | union_type  -- (Note: Available in Hive 0.7.0 and later)

  26. primitive_type

  27.  : TINYINT

  28.  | SMALLINT

  29.  | INT

  30.  | BIGINT

  31.  | BOOLEAN

  32.  | FLOAT

  33.  | DOUBLE

  34.  | DOUBLE PRECISION -- (Note: Available in Hive 2.2.0 and later)

  35.  | STRING

  36.  | BINARY      -- (Note: Available in Hive 0.8.0 and later)

  37.  | TIMESTAMP   -- (Note: Available in Hive 0.8.0 and later)

  38.  | DECIMAL     -- (Note: Available in Hive 0.11.0 and later)

  39.  | DECIMAL(precision, scale)  -- (Note: Available in Hive 0.13.0 and later)

  40.  | DATE        -- (Note: Available in Hive 0.12.0 and later)

  41.  | VARCHAR     -- (Note: Available in Hive 0.12.0 and later)

  42.  | CHAR        -- (Note: Available in Hive 0.13.0 and later)

  43. array_type

  44.  : ARRAY < data_type >

  45. map_type

  46.  : MAP < primitive_type, data_type >

  47. struct_type

  48.  : STRUCT < col_name : data_type [COMMENT col_comment], ...>

  49. union_type

  50.   : UNIONTYPE < data_type, data_type, ... >  -- (Note: Available in Hive 0.7.0 and later)

  51. row_format

  52.  : DELIMITED [FIELDS TERMINATED BY char [ESCAPED BY char]] [COLLECTION ITEMS TERMINATED BY char]

  53.        [MAP KEYS TERMINATED BY char] [LINES TERMINATED BY char]

  54.        [NULL DEFINED AS char]   -- (Note: Available in Hive 0.13 and later)

  55.  | SERDE serde_name [WITH SERDEPROPERTIES (property_name=property_value, property_name=property_value, ...)]

  56. file_format:

  57.  : SEQUENCEFILE

  58.  | TEXTFILE    -- (Default, depending on hive.default.fileformat configuration)

  59.  | RCFILE      -- (Note: Available in Hive 0.6.0 and later)

  60.  | ORC         -- (Note: Available in Hive 0.11.0 and later)

  61.  | PARQUET     -- (Note: Available in Hive 0.13.0 and later)

  62.  | AVRO        -- (Note: Available in Hive 0.14.0 and later)

  63.  | JSONFILE    -- (Note: Available in Hive 4.0.0 and later)

  64.  | INPUTFORMAT input_format_classname OUTPUTFORMAT output_format_classname

  65. constraint_specification:

  66.  : [, PRIMARY KEY (col_name, ...) DISABLE NOVALIDATE ]

  67.    [, CONSTRAINT constraint_name FOREIGN KEY (col_name, ...) REFERENCES table_name(col_name, ...) DISABLE NOVALIDATE

创建脚本

 
   
   
 
  1. create table user_info_t1(

  2.    id      int

  3.   ,name    string

  4.   ,hobby   array<string>

  5.   ,add     map<String,string>

  6. )

  7. STORED AS TEXTFILE

  8. row format delimited

  9. fields terminated by ','

  10. collection items terminated by '-'

  11. map keys terminated by ':'

  12. LOCATION '/hive/data/ ';

  13. ;

下面导入数据

 
   
   
 
  1. 1,xiaoming,book-TV-code,beijing:chaoyang-shagnhai:pudong

  2. 2,lilei,book-code,nanjing:jiangning-taiwan:taibei

  3. 3,lihua,music-book,heilongjiang:haerbin

 
   
   
 
  1. LOAD DATA [LOCAL] INPATH 'filepath' [OVERWRITE] INTO TABLE tablename [PARTITION (partcol1=val1, partcol2=val2 ...)]

导入脚本为

 
   
   
 
  1. LOAD DATA LOCAL INPATH '/data/hive/crate_table/user_info.txt'  INTO TABLE user_info_t1;

2. select 创建

 
   
   
 
  1. create table user_info_t2 AS select id,name from user_info_t1;

3. like 创建

 
   
   
 
  1. create table user_info_t3 like user_info_t1;

欢迎关注,更多福利


以上是关于hive 中创建表的三种方式的主要内容,如果未能解决你的问题,请参考以下文章

hive 表的创建的操作与测试

Hive创建表的三种形式

hive建表详注小记(备忘)

Hive的三种文件格式

Hive 协议缓冲区 - 在 Hive 中创建表时出现 NullPointerException

Hbase统计表总行数的三种方式