4 hive 執行命令方式,資料載入

2021-08-24 18:17:11 字數 2714 閱讀 7899

hive執行命令的方式

hive -e "hql" 命令列執行hsql

hive -e "select * from mydb2.c1"

hive -s 靜默模式,控制台不輸出logging資訊

hive -s -e "select * from mydb2.c1"

hive -v 詳細模式,會把執行的sql列印出來

hive -v -e "select * from mydb2.c1"

hive -f "path" 執行檔案

hive -f "/home/lz/createtable.txt"

hive -i 執行初始化xx自己實現

list jar;顯示快取了哪些jar包

list file;顯示快取了哪些檔案

hive 資料載入

建立表時載入

create table c3 as select * from c1;

建立表時指定資料位置

create table c4(id int,name string) location 'path';

本地資料載入

load data local inpath 'path' overwrite into table c5;

載入hdfs資料

load data inpath 'path' overwrite into table c6;

查詢語句載入資料

create table c4 like c2;(複製表結構)

1.insert into/overwrite table c4

select id,name

from c2

where id=1;

2.from c2

insert into/overwrite c5

select id,name

where id=2;

注意:字段對於和關係型資料庫不一樣的

hive在載入資料的時候不做字段檢查和字段型別檢查的

在查詢的時候才做檢查

分割槽表載入資料

同表載入資料一樣,只是需要指定分割槽分割槽

注意:資料存放的路徑層次要和表的分割槽一致

如果分割槽表沒有新增分割槽,即使目標路徑下已經有資料了,但依然查不到資料

本地載入資料

create table if not exists d1(

id int,

name string,

age int

)partitioned by(dt string)

row format delimited fields terminated by ','

stored as textfile;

1,tom,24

2,jack,25

3,lc,27

4,ljc,28

load data local inpath 'path' overwrite/into table d1 partition(dt='20180416');

載入hdfs資料

load data inpath 'path' overwrite/into table tablename partition(dt='20180416');

由查詢語句載入資料

create table if not exists d3(

id int,

name string

)partitioned by(dt string)

row format delimited fields terminated by ','

stored as textfile;

insert overwrite table d3 partition(dt='20180815') select name,id from d1;

insert overwrite table d2 partition(dt='20180815') select id,name,age from d1;

注意:由查詢語句載入資料,字段必須一樣多,d2表三個字段,那麼select d1表的三個字段才能插入

hive資料載入需要注意的問題

1.分隔符問題,分隔符預設是單個字元(因為預設只取第乙個字元)

2.load資料字段型別不能互相轉化是,查詢返回null

create table if not exists e1(id int,name string) row format delimited fields terminated by ',' stored as textfile;

1,tom

2,jack

load data local inpath '/home/lz/e1.txt' overwrite into table e1;

create table e2 like e1;

load data local inpath 'path' overwrite into table e2;

(這個查詢那麼欄位為null,string轉不了int)

3.select查詢插入,字段型別不能互相轉化時,插入資料為null

insert into table e2 select name,id from e1;

(這個id欄位為null,string轉不了int)

4.select查詢插入分割槽資料,字段值順序要預表中欄位順序一致,名稱可不一致

5.外部分割槽表需要新增分割槽才能看到資料(alter table add partition(xx=xx))

hive學習4(hive的指令碼執行)

hive e sql hvie f file例項 root spark1 hive e show tables 檢視有哪些表 root spark1 hive e show tables wujiadong 將檢視結果追加到wujiadong檔案中 root spark1 hive f 1.sql ...

hive 命令三種執行方式

20190831 hive sql語句hive show databases 顯示所有的資料庫 hive use base name 選擇資料庫 hive show tables 當前資料庫下所有的table view hive select from table name sql語句 hive q...

hive指令碼執行方式

hive指令碼的執行方式 hive指令碼的執行方式大致有三種 usage hive commands.e.g.d a b or define a b database specify the database to use e sql from command line f sql from fil...