hive的創建表語句

1.在hive的安裝包/apache-hive-2.3.4-bin/conf路徑下的hive-site.xml文件裏配置:
<property>
<name>hive.support.sql11.reserved.keywords</name>
<value>false</value>
</property>
此參數是關閉創建表字段有關鍵字段。也可以在sql語句之前設置
set hive.support.sql11.reserved.keywords=false;
sql語句中的關鍵字要用`,和mysql一樣。<br/>例如: <br/>CREATE TABLE IF NOT EXISTS traffic.monitor_flow_action( <br/>date` string ,
monitor_id string ,
camera_id string ,
car string ,
action_time string ,
speed string ,
road_id string,
area_id string
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' ;

2.從hdfs中加載數據到hive表中,加載完後hdfs裏的就自動刪除(其實是mv了,移動到/user/hive/warehouse下了)。
例如:
CREATE TABLE IF NOT EXISTS traffic.monitor_flow_action(
date string ,
monitor_id string ,
camera_id string ,
car string ,
action_time string ,
speed string ,
road_id string,
area_id string
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' ;
load data inpath 'hdfs://mycluster/myfile/monitor_flow_action' into table traffic.monitor_flow_action;

CREATE TABLE IF NOT EXISTS traffic.monitor_camera_info(
monitor_id string ,
camera_id string
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' ;
load data inpath 'hdfs://mycluster/myfile/monitor_camera_info' into table traffic.monitor_camera_info;

從本地路徑下加載文件數據到hive表中:
CREATE TABLE IF NOT EXISTS traffic.monitor_flow_action(
date string ,
monitor_id string ,
camera_id string ,
car string ,
action_time string ,
speed string ,
road_id string,
area_id string
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' ;
load data local inpath '/usr/java/monitor_flow_action' into table traffic.monitor_flow_action;

CREATE TABLE IF NOT EXISTS traffic.monitor_camera_info(
monitor_id string ,
camera_id string
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' ;
load data local inpath '/usr/java/monitor_camera_info' into table traffic.monitor_camera_info;

注意:從hdfs加數據和本地加載數據 load data 語法兩個一個沒有local,一個有local。
可以把sql文件放到一個文件裏比如testCreateHiveSql.sql,然後用命令: hive -f testCreateHiveSql.sql 執行即可創建表和加載數據

完整示列:
set hive.support.sql11.reserved.keywords=false;

CREATE TABLE IF NOT EXISTS traffic.monitor_flow_action(
date string ,
monitor_id string ,
camera_id string ,
car string ,
action_time string ,
speed string ,
road_id string,
area_id string
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' ;

load data local inpath '/root/test/monitor_flow_action' into table traffic.monitor_flow_action;

CREATE TABLE IF NOT EXISTS traffic.monitor_camera_info(
monitor_id string ,
camera_id string
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' ;

load data local inpath '/root/test/monitor_camera_info' into table traffic.monitor_camera_info;

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章