Hive-1.2.1_03_DDL操作

来源:https://www.cnblogs.com/zhanglianghhh/archive/2018/07/22/9349078.html
-Advertisement-
Play Games

Hive官方文檔:Home-UserDocumentation Hive DDL官方文檔:LanguageManual DDL 參考文章:Hive 用戶指南 註意:各個語句的版本時間,有的是在 hive-1.2.1 之後才有的,這些語句我們在hive-1.2.1中是不能使用的。 註意:本文寫的都是常 ...


 

Hive官方文檔:Home-UserDocumentation

Hive DDL官方文檔:LanguageManual DDL

參考文章:Hive 用戶指南

 

  註意:各個語句的版本時間,有的是在 hive-1.2.1 之後才有的,這些語句我們在hive-1.2.1中是不能使用的。

  註意:本文寫的都是常用的知識點,更多細節請參考官網。

 

常用命令

1 select current_database();        # 當前使用哪個庫
2 show databases;        # 顯示所有庫名
3 show tables;        # 顯示當前庫有哪些表
4 describe extended talbe_name;   # 表的擴展信息  和 describe formatted talbe_name; 類似
5 describe formatted talbe_name;   # 推薦使用★★★  如:describe formatted t_sz01; # 描述表信息,也可知道是 MANAGED_TABLE 還是 EXTERNAL_TABLE
6 describe database test_db;  # 查看資料庫信息
7 show partitions t_sz03_part;  # 查看分區表的分區信息

 

說明

1 本文章使用Hive操作時,可能是 HIVE的命令行,也可能是beeline
2 所以看到2種風格的命令行,請不要驚訝。

 

 

1. DDL- Database操作

1.1. Create Database

1 CREATE (DATABASE|SCHEMA) [IF NOT EXISTS] database_name
2   [COMMENT database_comment]
3   [LOCATION hdfs_path]
4   [WITH DBPROPERTIES (property_name=property_value, ...)];

 

創建庫

 

 1 # 創建庫
 2 create database if not exists test_db
 3 comment 'my frist db'; 
 4 
 5 0: jdbc:hive2://mini01:10000> describe database test_db;  # 建庫信息 
 6 +----------+--------------+----------------------------------------------------+-------------+-------------+-------------+--+
 7 | db_name  |   comment    |                      location                      | owner_name  | owner_type  | parameters  |
 8 +----------+--------------+----------------------------------------------------+-------------+-------------+-------------+--+
 9 | test_db  | my frist db  | hdfs://mini01:9000/user/hive/warehouse/test_db.db  | yun         | USER        |             |
10 +----------+--------------+----------------------------------------------------+-------------+-------------+-------------+--+

 

 

1.2. Drop Database

1 DROP (DATABASE|SCHEMA) [IF EXISTS] database_name [RESTRICT|CASCADE];
2 # 預設為 RESTRICT,如果庫下有表就不能刪除庫;如果使用 CASCADE 那麼即使庫下有表,也會刪除庫下的表和庫。

 

1.3. Use Database

1 USE database_name;        # 使用哪個庫
2 USE DEFAULT;            # 使用預設庫

 

例如:

 

 1 hive (default)> show databases;        # 查詢有哪些庫
 2 OK
 3 default
 4 test001
 5 test_db
 6 zhang
 7 Time taken: 0.016 seconds, Fetched: 4 row(s)
 8 hive (default)> use test_db;    # 使用哪個庫
 9 OK
10 Time taken: 0.027 seconds
11 hive (test_db)> select current_database();    # 當前使用的是哪個庫
12 OK
13 test_db
14 Time taken: 1.232 seconds, Fetched: 1 row(s)

 

 

2. DDL-Table

2.1. Create Table

CREATE [TEMPORARY] [EXTERNAL] TABLE [IF NOT EXISTS] [db_name.]table_name 
  [(col_name data_type [COMMENT col_comment], ... [constraint_specification])]
  [COMMENT table_comment]
  [PARTITIONED BY (col_name data_type [COMMENT col_comment], ...)]	# PARTITIONED 分割
  [CLUSTERED BY (col_name, col_name, ...) [SORTED BY (col_name [ASC|DESC], ...)] INTO num_buckets BUCKETS]  # CLUSTERED 分群 BUCKETS 桶
  [SKEWED BY (col_name, col_name, ...) 
     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 (...)]  
  ]
  [LOCATION hdfs_path]
  [TBLPROPERTIES (property_name=property_value, ...)]   # TBLPROPERTIES 表屬性
  [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)
  | JSONFILE    -- (Note: Available in Hive 4.0.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建表時,如果該表或視圖已存在,那麼會報錯。可以使用IF NOT EXISTS 跳過該錯誤。

 

  •  表名和列名大小寫不明感,但是SerDe(Serializer/Deserializer的簡寫。hive使用Serde進行行對象的序列與反序列化)和property (屬性)名大小寫敏感。
  •  表和列註釋是字元串文本(需要單引號)。
  •  一個表的創建沒有使用EXTERNAL子句,那麼被稱為managed table(托管表);因為Hive管理它的數據。查看一個表是托管的還是外部的,可以通過 DESCRIBE EXTENDED 或者 table_name 查看tableType獲知。【或者通過describe formatted table_name; 查看】  【參見示例1】
  • TBLPROPERTIES 子句允許你使用自己的元數據key/value【鍵/值對】去標記表定義。一些預定義的表屬性也存在,比如last_modified_user和last_modified_time,它們是由Hive自動添加和管理的。【參見示例1】

 

 1 # 示例1
 2 0: jdbc:hive2://mini01:10000> create table t_sz05 (id int, name string) tblproperties ('key001'='value1', 'key200'='value200');  # 創建表
 3 No rows affected (0.224 seconds)
 4 0: jdbc:hive2://mini01:10000> describe formatted t_sz05;  # 查看表信息
 5 +-------------------------------+-------------------------------------------------------------+-----------------------+--+
 6 |           col_name            |                          data_type                          |        comment        |
 7 +-------------------------------+-------------------------------------------------------------+-----------------------+--+
 8 | # col_name                    | data_type                                                   | comment               |
 9 |                               | NULL                                                        | NULL                  |
10 | id                            | int                                                         |                       |
11 | name                          | string                                                      |                       |
12 |                               | NULL                                                        | NULL                  |
13 | # Detailed Table Information  | NULL                                                        | NULL                  |
14 | Database:                     | zhang                                                       | NULL                  |
15 | Owner:                        | yun                                                         | NULL                  |
16 | CreateTime:                   | Sat Jul 07 20:13:53 CST 2018                                | NULL                  |
17 | LastAccessTime:               | UNKNOWN                                                     | NULL                  |
18 | Protect Mode:                 | None                                                        | NULL                  |
19 | Retention:                    | 0                                                           | NULL                  |
20 | Location:                     | hdfs://mini01:9000/user/hive/warehouse/zhang.db/t_sz05      | NULL                  |
21 | Table Type:                   | MANAGED_TABLE 【# 如果是外部表,則為EXTERNAL_TABLE】        | NULL                  |
22 | Table Parameters:             | NULL                                                        | NULL                  |
23 |                               | key001 【# 自定義】                                         | value1                |
24 |                               | key200 【# 自定義】                                         | value200              |
25 |                               | transient_lastDdlTime                                       | 1530965633            |
26 |                               | NULL                                                        | NULL                  |
27 | # Storage Information         | NULL                                                        | NULL                  |
28 | SerDe Library:                | org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe          | NULL                  |
29 | InputFormat:                  | org.apache.hadoop.mapred.TextInputFormat                    | NULL                  |
30 | OutputFormat:                 | org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat  | NULL                  |
31 | Compressed:                   | No                                                          | NULL                  |
32 | Num Buckets:                  | -1                                                          | NULL                  |
33 | Bucket Columns:               | []                                                          | NULL                  |
34 | Sort Columns:                 | []                                                          | NULL                  |
35 | Storage Desc Params:          | NULL                                                        | NULL                  |
36 |                               | serialization.format                                        | 1                     |
37 +-------------------------------+-------------------------------------------------------------+-----------------------+--+
38 29 rows selected (0.153 seconds)

 

2.1.1. Managed Table

建表

 

 1 # 可以通過 describe formatted table_name;  # 查看表信息
 2 hive (test_db)> create table t_sz01 (id int, name string comment 'person name')
 3                 comment 'a table of name'
 4                 row format delimited fields terminated by ',';
 5 OK
 6 Time taken: 0.311 seconds
 7 hive (test_db)> show tables;
 8 OK
 9 t_sz01
10 Time taken: 0.031 seconds, Fetched: 1 row(s)

 

 

       通過 desc formatted t_sz01; 中的信息可知: Location: 為 hdfs://mini01:9000/user/hive/warehouse/test_db.db/t_sz01

 

添加表數據

 

 1 [yun@mini01 hive]$ pwd
 2 /app/software/hive
 3 [yun@mini01 hive]$ cat t_sz01.dat 
 4 1,zhnagsan
 5 3,李四
 6 5,wangwu
 7 7,趙六
 8 2,sunqi
 9 4,周八
10 6,kkkkk
11 8,zzzzz
12 [yun@mini01 hive]$ cp -a t_sz01.dat t_sz01.dat2  # 複製一份
13 [yun@mini01 hive]$ hadoop fs -put t_sz01.dat /user/hive/warehouse/test_db.db/t_sz01  # 數據上傳
14 [yun@mini01 hive]$ hadoop fs -put t_sz01.dat2 /user/hive/warehouse/test_db.db/t_sz01 # 數據上傳
15 [yun@mini01 hive]$ hadoop fs -ls /user/hive/warehouse/test_db.db/t_sz01  # 表下可以有多個數據文件
16 Found 2 items
17 -rw-r--r--   2 yun supergroup         71 2018-07-12 21:58 /user/hive/warehouse/test_db.db/t_sz01/t_sz01.dat
18 -rw-r--r--   2 yun supergroup         71 2018-07-12 22:30 /user/hive/warehouse/test_db.db/t_sz01/t_sz01.dat2

 

通過hive查看數據

 1 0: jdbc:hive2://mini01:10000> select * from t_sz01;
 2 +------------+--------------+--+
 3 | t_sz01.id  | t_sz01.name  |
 4 +------------+--------------+--+
 5 | 1          | zhnagsan     |
 6 | 3          | 李四         |
 7 | 5          | wangwu       |
 8 | 7          | 趙六         |
 9 | 2          | sunqi        |
10 | 4          | 周八         |
11 | 6          | kkkkk        |
12 | 8          | zzzzz        |
13 | 1          | zhnagsan     |
14 | 3          | 李四         |
15 | 5          | wangwu       |
16 | 7          | 趙六         |
17 | 2          | sunqi        |
18 | 4          | 周八         |
19 | 6          | kkkkk        |
20 | 8          | zzzzz        |
21 +------------+--------------+--+
22 16 rows selected (0.159 seconds)

 

2.1.2. External Tables

       外表就是自己提供一個LOCATION,而不使用預設的表位置。並且刪除該表時,表中的數據是不會刪除的。

 

建表

 

 1 # 其中location也可省略 hdfs://mini01:9000  改為 /user02/hive/database/ext_table   
 2 # 其中如果location目錄不存在,那麼hive會創建
 3 hive (test_db)> create external table t_sz02_ext (id int, name string)
 4                 comment 'a ext table'
 5                 row format delimited fields terminated by '\t'
 6                 location 'hdfs://mini01:9000/user02/hive/database/ext_table';
 7 OK
 8 Time taken: 0.065 seconds
 9 hive (test_db)> show tables;
10 OK
11 t_sz01
12 t_sz02_ext
13 Time taken: 0.03 seconds, Fetched: 2 row(s)
14 0: jdbc:hive2://mini01:10000> select * from t_sz02_ext;   # 無數據
15 +----------------+------------------+--+
16 | t_sz02_ext.id  | t_sz02_ext.name  |
17 +----------------+------------------+--+
18 +----------------+------------------+--+
19 No rows selected (0.094 seconds)
20 
21 # 通過desc formatted t_sz02_ext; 可能查詢表的Location,得到下麵的信息
22 # hdfs://mini01:9000/user02/hive/database/ext_table

 

 

添加表數據

 

 1 [yun@mini01 hive]$ pwd
 2 /app/software/hive
 3 [yun@mini01 hive]$ ll
 4 total 12
 5 -rw-rw-r-- 1 yun yun 56 Jul  3 21:26 sz.dat
 6 -rw-rw-r-- 1 yun yun 71 Jul 12 21:53 t_sz01.dat
 7 -rw-rw-r-- 1 yun yun 79 Jul 12 22:15 t_sz02_ext.dat
 8 [yun@mini01 hive]$ cat t_sz02_ext.dat # 最後有一行空行 
 9 1    劉晨
10 2    王敏
11 3    張立
12 4    劉剛
13 5    孫慶
14 6    易思玲
15 7    李娜
16 8    夢圓圓
17 
18 [yun@mini01 hive]$ hadoop fs -put t_sz02_ext.dat /user02/hive/database/ext_table  # 上傳數據
19 [yun@mini01 hive]$ hadoop fs -ls /user02/hive/database/ext_table
20 Found 1 items
21 -rw-r--r--   2 yun supergroup         79 2018-07-12 22:16 /user02/hive/database/ext_table/t_sz02_ext.dat

 

通過hive查看數據

 

 1 0: jdbc:hive2://mini01:10000> select * from t_sz02_ext;
 2 +----------------+------------------+--+
 3 | t_sz02_ext.id  | t_sz02_ext.name  |
 4 +----------------+------------------+--+
 5 | 1              | 劉晨               |
 6 | 2              | 王敏               |
 7 | 3              | 張立               |
 8 | 4              | 劉剛               |
 9 | 5              | 孫慶               |
10 | 6              | 易思玲             |
11 | 7              | 李娜               |
12 | 8              | 夢圓圓             |
13 | NULL           | NULL             | # 原因是數據中,最後一行為空行
14 +----------------+------------------+--+
15 9 rows selected (0.14 seconds)

 

 

2.1.3. Partitioned Tables

       分區表(Partitioned tables)可以使用 PARTITIONED BY 子句。一個表可以有一個或多個分區列,並且為每個分區列中的不同值組合創建一個單獨的數據目錄。

       當創建一個分區表,你得到這樣的錯誤:“FAILED: Error in semantic analysis: Column repeated in partitioning columns,【失敗:語義分析錯誤:分區列重覆列】”。意味著你試圖在表的本身數據中包含分區列。你可能確實定義了列。但是,您創建的分區可以生成一個可以查詢的偽列,因此必須將表的列重命名為其他(那樣用戶不會查詢)。

 

       例如,假設原始未分區表有三列:id、date和name。

1 id     int,
2 date   date,
3 name   varchar

 

       現在要按日期分區。你的Hive定義可以使用 "dtDontQuery"作為列名,以便 "date" 可以被用作分區(和查詢)。

1 create table table_name (
2   id                int,
3   dtDontQuery       string,
4   name              string
5 )
6 partitioned by (date string)

 

       現在你的用戶仍然查詢"where date = '...'",但是第二列dtDontQuery將保存原始值。

 

建表

1 # 不能使用date作為表的欄位【列】,因為date是關鍵字
2 hive (test_db)> create table t_sz03_part (id int, name string)
3                 comment 'This is a partitioned table'
4                 partitioned by (dt string, country string)
5                 row format delimited fields terminated by ',';

 

添加數據

 

 1 [yun@mini01 hive]$ pwd
 2 /app/software/hive
 3 [yun@mini01 hive]$ cat t_sz03_20180711.dat1
 4 1,張三_20180711
 5 2,lisi_20180711
 6 3,Wangwu_20180711
 7 [yun@mini01 hive]$ cat t_sz03_20180711.dat2
 8 11,Tom_20180711
 9 12,Dvid_20180711
10 13,cherry_20180711
11 [yun@mini01 hive]$ cat t_sz03_20180712.dat1
12 1,張三_20180712
13 2,lisi_20180712
14 3,Wangwu_20180712
15 [yun@mini01 hive]$ cat t_sz03_20180712.dat2
16 11,Tom_20180712
17 12,Dvid_20180712
18 13,cherry_20180712
19 #### 在Hive中導入數據
20 hive (test_db)> load data local inpath '/app/software/hive/t_sz03_20180711.dat1' into table t_sz03_part partition (dt='20180711', country='CN');
21 Loading data to table test_db.t_sz03_part partition (dt=20180711, country=CN)
22 Partition test_db.t_sz03_part{dt=20180711, country=CN} stats: [numFiles=1, numRows=0, totalSize=52, rawDataSize=0]
23 OK
24 Time taken: 0.406 seconds
25 hive (test_db)> load data local inpath '/app/software/hive/t_sz03_20180711.dat2' into table t_sz03_part partition (dt='20180711', country='US');
26 Loading data to table test_db.t_sz03_part partition (dt=20180711, country=US)
27 Partition test_db.t_sz03_part{dt=20180711, country=US} stats: [numFiles=1, numRows=0, totalSize=52, rawDataSize=0]
28 OK
29 Time taken: 0.453 seconds
30 hive (test_db)> load data local inpath '/app/software/hive/t_sz03_20180712.dat1' into table t_sz03_part partition (dt='20180712', country='CN');
31 Loading data to table test_db.t_sz03_part partition (dt=20180712, country=CN)
32 Partition test_db.t_sz03_part{dt=20180712, country=CN} stats: [numFiles=1, numRows=0, totalSize=52, rawDataSize=0]
33 OK
34 Time taken: 0.381 seconds
35 hive (test_db)> load data local inpath '/app/software/hive/t_sz03_20180712.dat2' into table t_sz03_part partition (dt='20180712', country='US');
36 Loading data to table test_db.t_sz03_part partition (dt=20180712, country=US)
37 Partition test_db.t_sz03_part{dt=20180712, country=US} stats: [numFiles=1, numRows=0, totalSize=52, rawDataSize=0]
38 OK
39 Time taken: 0.506 seconds

 

 

瀏覽器訪問

 

 

 

通過hive查看數據

 

 1 0: jdbc:hive2://mini01:10000> select * from t_sz03_part;
 2 +-----------------+-------------------+-----------------+----------------------+--+
 3 | t_sz03_part.id  | t_sz03_part.name  | t_sz03_part.dt  | t_sz03_part.country  |
 4 +-----------------+-------------------+-----------------+----------------------+--+
 5 | 1               | 張三_20180711     | 20180711        | CN                   |
 6 | 2               | lisi_20180711     | 20180711        | CN                   |
 7 | 3               | Wangwu_20180711   | 20180711        | CN                   |
 8 | 11              | Tom_20180711      | 20180711        | US                   |
 9 | 12              | Dvid_20180711     | 20180711        | US                   |
10 | 13              | cherry_20180711   | 20180711        | US                   |
11 | 1               | 張三_20180712     | 20180712        | CN                   |
12 | 2               | lisi_20180712     | 20180712        | CN                   |
13 | 3               | Wangwu_20180712   | 20180712        | CN                   |
14 | 11              | Tom_20180712      | 20180712        | US                   |
15 | 12              | Dvid_20180712     | 20180712        | US                   |
16 | 13              | cherry_20180712   | 20180712        | US                   |
17 +-----------------+-------------------+-----------------+----------------------+--+
18 12 rows selected (0.191 seconds)
19 0: jdbc:hive2://mini01:10000> show partitions t_sz03_part;  # 查看分區表的分區信息
20 +-------------------------+--+
21 |        partition        |
22 +-------------------------+--+
23 | dt=20180711/country=CN  |
24 | dt=20180711/country=US  |
25 | dt=20180712/country=CN  |
26 | dt=20180712/country=US  |
27 +-------------------------+--+
28 4 rows selected (0.164 seconds)

 

 

2.1.4. Create Table As Select (CTAS)

       表還可以由一個create-table-as-select (CTAS)語句中的查詢結果創建和填充。CTAS創建的表是原子的,這意味著在填充所有查詢結果之前,其他用戶看不到該表。因此,其他用戶要麼看到表的完整結果,要麼根本看不到表。

 

CTAS限制:

       目標表不能是分區表

       目標表不能是外部表

       目標表不能是分桶表

這裡的目標表指的是要創建的表。

1 # 示例:
2 CREATE TABLE new_key_value_store
3    ROW FORMAT SERDE "org.apache.hadoop.hive.serde2.columnar.ColumnarSerDe"
4    STORED AS RCFile
5    AS
6 SELECT (key % 1024) new_key, concat(key, value) key_value_pair
7 FROM key_value_store
8 SORT BY new_key, key_value_pair;

 

實例

 

 1 hive (test_db)> create table t_sz02_ext_new
 2                    row format delimited fields terminated by '#'
 3                    AS
 4                 SELECT id , concat(name, '_', id) name2 
 5                 FROM t_sz02_ext
 6                 SORT BY name2;
 7 
 8 0: jdbc:hive2://mini01:10000> show tables;
 9 +-----------------+--+
10 |    tab_name     |
11 +-----------------+--+
12 | t_sz01          |
13 | t_sz02_ext      |
14 | t_sz02_ext_new  |
15 | t_sz03_part     |
16 | t_sz100_ext     |
17 | t_sz101_ext     |
18 +-----------------+--+
19 6 rows selected (0.069 seconds)
20 0: jdbc:hive2://mini01:10000> select * from t_sz02_ext_new;
21 +--------------------+-----------------------+--+
22 | t_sz02_ext_new.id  | t_sz02_ext_new.name2  |
23 +--------------------+-----------------------+--+
24 | NULL               | NULL                  |
25 | 4                  | 劉剛_4                |
26 | 1                  | 劉晨_1                |
27 | 5                  | 孫慶_5                |
28 | 3                  | 張立_3                |
29 | 6                  | 易思玲_6              |
30 | 7                  | 李娜_7                |
31 | 8                  | 夢圓圓_8              |
32 | 2                  | 王敏_2                |
33 +--------------------+-----------------------+--+
34 9 rows selected (0.094 seconds)
35 
36 # 其中路徑location可以通過desc formatted t_sz02_ext_new; 獲取
37 hive (test_db)> dfs -ls /user/hive/warehouse/test_db.db/t_sz02_ext_new/;
38 Found 1 items
39 -rwxr-xr-x   2 yun supergroup        100 2018-07-12 23:50 /user/hive/warehouse/test_db.db/t_sz02_ext_new/000000_0
40 hive (test_db)> dfs -cat /user/hive/warehouse/test_db.db/t_sz02_ext_new/000000_0;
41 \N#\N
42 4#劉剛_4
43 1#劉晨_1
44 5#孫慶_5
45 3#張立_3
46 6#易思玲_6
47 7#李娜_7
48 8#夢圓圓_8
49 2#王敏_2

 

 

hdfs查看

 

 1 # 其中路徑location可以通過desc formatted t_sz02_ext_new; 獲取
 2 [yun@mini01 hive]$ hadoop fs -ls /user/hive/warehouse/test_db.db/t_sz02_ext_new;
 3 Found 1 items
 4 -rwxr-xr-x   2 yun supergroup        100 2018-07-12 23:44 /user/hive/warehouse/test_db.db/t_sz02_ext_new/000000_0
 5 [yun@mini01 hive]$ hadoop fs -cat /user/hive/warehouse/test_db.db/t_sz02_ext_new/000000_0
 6 \N#\N
 7 4#劉剛_4
 8 1#劉晨_1
 9 5#孫慶_5
10 3#張立_3
11 6#易思玲_6
12 7#李娜_7
13 8#夢圓圓_8
14 2#王敏_2

 

 

2.1.5. Create Table Like

       拷貝一個已存在的表結構作為一個新表

1 CREATE TABLE empty_key_value_store
2 LIKE key_value_store [TBLPROPERTIES (property_name=property_value, ...)];

 

實例

 

 1 hive (test_db)> create table t_sz03_part_new
 2                 like t_sz03_part tblproperties ('proper1'='value1', 'proper2'='value2');
 3 OK
 4 Time taken: 0.083 seconds
 5 0: jdbc:hive2://mini01:10000> select * from t_sz03_part_new;  # 只複製表結構,不複製內容 
 6 +---------------------+-----------------------+---------------------+--------------------------+--+
 7 | t_sz03_part_new.id  | t_sz03_part_new.name  | t_sz03_part_new.dt  | t_sz03_part_new.country  |
 8 +---------------------+-----------------------+---------------------+--------------------------+--+
 9 +---------------------+-----------------------+---------------------+--------------------------+--+
10 No rows selected (0.087 seconds)
11 hive (test_db)> create table t_sz04_like
12                 like t_sz02_ext  tblproperties ('proper1'='value1', 'proper2'='value2');
13 No rows affected (0.153 seconds)
14 
15 0: jdbc:hive2://mini01:10000> select * from t_sz04_like;
16 +-----------------+-------------------+--+
17 | t_sz04_like.id  | t_sz04_like.name  |
18 +-----------------+-------------------+--+
19 +-----------------+----------------

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 返回 "ProxySQL系列文章:http://www.cnblogs.com/f ck need u/p/7586194.html"   1.理解鏈式規則 在mysql_query_rules表中,有兩個特殊欄位" flagIN "和" flagOUT ",它們分別用來定義規則的入口和出 ...
  • 參考 SQL Server 2012編程入門經典(第4版) SQL Server 自帶的數據類型 整型: 貨幣 近似小數 日期/時間 特殊數字 字元 Unicode 二進位 其他 ...
  • 1. 數據準備 2. 用戶一個月總金額 3. 將月總金額表 自己連接 自己連接 4. 累計報表 4.1. 類似數據在MySQL資料庫查詢 4.2. Hive中運行 ...
  • 1.返回 每月最後一天訂單 使用EMONTH 對輸入的日期返回月末日期 類似動態條件 DATEDIFF(month, '19991231', orderdate) 相差多少月 從19991231到 orderdate之間先查多少月 DATEADD(month, DATEDIFF(month, '19 ...
  • 1. 建庫建表 2. 數據準備 相關數據 數據導入 3. 常用操作 3.1. 學生表基本查詢 查詢學號為95001, 95005, 95008的學生信息 查詢學號中有9500字元串的學生信息 查詢全體學生的學號與姓名 查詢學生的總人數 3.2. 成績表相關查詢 查詢選修了課程的學生姓名 計算1號課程 ...
  • 簡介 將查詢語句查詢的結果集作為數據插入到數據表中。 一、通過INSERT SELECT語句形式向表中添加數據 例如,創建一張新表AddressList來存儲班級學生的通訊錄信息,然後這些信息恰好存在學生表中,則可以從學生表中提取相關的數據插入建好的AddressList表中。 T-SQL語句如下: ...
  • 問題說明:使用的MySQL是5.1.37版本,用的mysql-connector-java-5.0.4.jar版本,在java文件中定義的欄位是Date類型,MySQL中定義的欄位類型是datetime類型的, 嘗試了以下方式都不成功,報的錯誤還是一個,方法如下: 1.第一個方法: // Date ...
  • Hive官方文檔:Home-UserDocumentation Hive DML官方文檔:LanguageManual DML 參考文章:Hive 用戶指南 1. Loading files into tables 當我們做Load操作是,hive不會做任何數據轉換,只是純複製/移動操作,將數據文件 ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...