Hive 基本操作

2021-09-26 19:57:34 字數 3040 閱讀 1602

1-hive hql基本操作

2-hive的內部表和外部表

3-分割槽表

create database test;
show databases;
create table student(classno string, stuno string, score int) row format delimited

fields terminated by ',';

row format delimited fields terminated by 『,』 指定了字段的分隔符為逗號,所以load資料的時候,load的文字也要為逗號,否則載入後為null。hive只支援單個字元的分隔符,hive預設的分隔符是\001

c01,n0101,82

c01,n0102,59

c01,n0103,65

c02,n0201,81

c02,n0202,82

c02,n0203,79

c03,n0301,56

c03,n0302,92

c03,n0306,72

load data local inpath '/root/tmp/student.txt'overwrite into table student;
這個命令將student.txt檔案複製到hive的warehouse目錄中,這個目錄由hive.metastore.warehouse.dir配置項設定,預設值為/user/hive/warehouse。overwrite選項將導致hive事先刪除student目錄下所有的檔案, 並將檔案內容對映到表中。 hive不會對student.txt做任何格式處理,因為hive本身並不強調資料的儲存格式。

hive>select * from student;
hive>select classno,count(score) from student where score>=60 group by classno;
從執行結果可以看出 hive把查詢的結果變成了mapreduce作業通過hadoop執行

load data local inpath '/root/tmp/student.txt' overwrite into table student2;
desc formatted table_name;
drop table student;
select * from student2;
建立分割槽表

tom,4300

jerry,12000

mike,13000

jake,11000

rob,10000

create table employee (name string,salary bigint) partitioned by (date1 string) row

format delimited fields terminated by ',' lines terminated by '\n' stored as textfile;

show partitions employee;
新增分割槽才會有分割槽

alter table employee add if not exists partition(date1='2018-12-01');
load data local inpath '/root/tmp/employee.txt' into table employee partition(date1='2018-12-01');
hadoop fs -mkdir /user/hive/warehouse/emp/dt=2018-12-04

hadoop fs -copyfromlocal /root/tmp/employee.txt /user/hive/warehouse/test.db/emp/dt=2018-12-04/employee.txt

alter table employee add if not exists partition(dt='2018-12-04');
此時再次檢視才能看到新加入的資料

動態分割槽

create table employee2 (name string,salary bigint) partitioned by (date1 string) row

format delimited fields terminated by ',' lines terminated by '\n' stored as textfile;

insert into table employee2 partition(date1) select name,salary,date1 from employee;
set hive.exec.dynamic.partition.mode=nonstrict;

hive基本操作

1.顯示所有資料庫 show databases 2.使用某個資料庫 use xx xx表示某個資料庫名 3.顯示某資料庫下的所有表 show tables 4.檢視表結構 顯示各欄位 desc 表名 5.資料匯出到本地 在hive中操作 insert overwrite local directo...

hive 基本操作

檢視表的詳細資訊 desc 表名 desc formatted 表名 建立外部表 create external table emp ext empno int,ename string,job string,mgr int,hiredate string,sal double,comm doubl...

HIVE基本操作

hive操作 一 建立分割槽 乙個表可以有多個分割槽,為避免過多的小檔案,建議只能對離散字段進行分割槽 create table if not exists stocks ymd date,price open float,price high float,price low float,price...