[Hive_4] Hive 插入數據



 

0. 說明

  Hive 插入數據的方法 && Hive 插入數據的順序 && 插入復雜數據的方法 && load 命令詳解

 

 


1. Hive 插入數據的方法

 

  Hive 插入數據不是使用 insert,而是 load

 

 


 

2. Hive 插入數據的順序

  2.1 先定義好表結構

create table employee(name string,
work_place array<string>,
sex_age struct<sex:string, age:int>,
score map<string, int>,
depart_title map<string, string>)
row format delimited
fields terminated by '|' 
collection items terminated by ',' 
map keys terminated by ':'
lines terminated by '\n' 
stored as textfile;

 



  2.2 准備數據。數據格式要和表結構對應 employee.txt

Michael|Montreal,Toronto|Male,30|DB:80|Product:Developer
Will|Montreal|Male,35|Perl:85|Product:Lead,Test:Lead
Shelley|New York|Female,27|Python:80|Test:Lead,COE:Architect
Lucy|Vancouver|Female,57|Sales:89,HR:94|Sales:Lead

 

  2.3 空表中使用 load 命令加載數據

load data local inpath '/home/centos/files/employee.txt' into table employee;

 

  2.4 取出所有的成員

# array獲取
select name ,work_place[0] from employee;
# 結構體獲取
select name ,sex_age.sex from employee;
# map成員獲取
select name, score['Python'] from employee;

 

 


 

3. 插入復雜類型數據 insert

  3.0 設置顯示表頭

  臨時修改命令如下,永久修改需要修改配置文件 hive-site.xml

set hive.cli.print.header=true;

 

  3.1 插入復雜類型使用轉儲

insert xxx select xxx

 

 

  通過 select 語句構造出 array 類型

# 通過 select 語句構造出 array 類型
select array('tom','tomas','tomson') ;

# 轉儲 array 類型數據
insert into employee(name,work_place) select 'tom',array('beijing','shanghai','guangzhou');

 

  通過 select 語句構造出 map 類型

# 通過 select 語句構造出 map 類型
select map('bigdata',100);

# 轉儲 map 類型數據
insert into employee(name,score) select 'tomas', map('bigdata',100);

 

  通過 select 語句構造出 struct 類型

# 通過 select 語句構造出 struct 類型
select struct('male',10);
select named_struct('sex','male','age',10);

# 轉儲 struct 類型數據
insert into employee(name,sex_age) select 'tomson',named_struct('sex','male','age',10);

 

 


4. load命令詳解

  4.0 前提:先建表

create table duowan(id int, name string, pass string, mail string, nickname string)
row format delimited
fields terminated by '\t' 
lines terminated by '\n' 
stored as textfile;

 

  4.1 使用 load

# load 本地數據,相當於上傳或者復制,源文件不變
load data local inpath '/home/centos/files/employee.txt' into table employee;

# load hdfs 數據,相當於移動
load data inpath '/duowan_user.txt' into table duowan;

# load 本地數據 + 覆蓋原始數據
load data local inpath '/home/centos/files/employee.txt' overwrite into table employee;

# load hdfs 數據 + 覆蓋原始數據
load data inpath '/duowan_user.txt' overwrite into table duowan;

 

 

 


 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM