直接將檔案put到Hdfs後建Hive表查詢資料

2021-08-18 16:40:11 字數 3435 閱讀 8496

直接將檔案put到hdfs後建hive表查詢資料

由於業務需要,有時需要將其他地方的檔案資料匯入hdfs,然後建hive表進行資料查詢或資料業務統計。這裡演示如何將本地檔案先put到hive已存在的空表中,然後查詢資料。

create table `hive_test.direct_load_file_into_table`(

`id` int,

`name` string)

partitioned by (`year` string comment '年', `month` string comment '月', `day` string comment '日')

row format delimited fields terminated by '\t'

stored as textfile;

然後show create table,確認下該該錶的hdfs路徑,比如/user/hive/warehouse/hive_test.db/direct_load_file_into_table

create table `direct_load_file_into_table`(

`id` int,

`name` string)

partitioned by (

`year` string comment '年',

`month` string comment '月',

`day` string comment '日')

row format serde

'org.apache.hadoop.hive.serde2.lazy.lazy******serde'

with serdeproperties (

'field.delim'='\t',

'serialization.format'='\t')

stored as inputformat

'org.apache.hadoop.mapred.textinputformat'

outputformat

'org.apache.hadoop.hive.ql.io.hiveignorekeytextoutputformat'

location

'hdfs://******:9000/user/hive/warehouse/hive_test.db/direct_load_file_into_table'

tblproperties (

'transient_lastddltime'='1523536404')

這裡將本地兩個檔案,put到上一步所建表的hdfs路徑下,同時演示資料夾迭代比如這裡除了分割槽2018/04/01,額外新增了一層資料夾01和02

hadoop fs -mkdir -p  /user/hive/warehouse/hive_test.db/direct_load_file_into_table/2018/04/01/01

hadoop fs -mkdir -p /user/hive/warehouse/hive_test.db/direct_load_file_into_table/2018/04/01/02

hadoop fs -ls /user/hive/warehouse/hive_test.db/direct_load_file_into_table/2018/04/01

drwxr-xr-x - hjw01 supergroup 0 2018-04-12 20:12 /user/hive/warehouse/hive_test.db/direct_load_file_into_table/2018/04/01/01

drwxr-xr-x - hjw01 supergroup 0 2018-04-12 20:12 /user/hive/warehouse/hive_test.db/direct_load_file_into_table/2018/04/01/02

本地檔案如下

[hjw01@f0d97899be95 data]$ more 01.txt

101 張三

102 李四

103 王五

[hjw01@f0d97899be95 data]$ more 02.txt

201 張三

202 李四

203 王五

將本地檔案put到hdfs中

hadoop fs -put  01.txt /user/hive/warehouse/hive_test.db/direct_load_file_into_table/2018/04/01/01

hadoop fs -put 02.txt /user/hive/warehouse/hive_test.db/direct_load_file_into_table/2018/04/01/02

建立分割槽就可以直接使用分割槽下的資料

alter table hive_test.direct_load_file_into_table add if not exists   partition(year='2018',month='04',day='01') location '2018/04/01';
這裡我們順帶演示了檔案多層迭代,需要set 引數,具體如下

set hive.mapred.supports.subdirectories=true;

set mapreduce.input.fileinputformat.input.dir.recursive=true;

select

* from

hive_test.direct_load_file_into_table

where

concat(year,month,day) = '20180401'

查詢結果

hive> set hive.mapred.supports.subdirectories=true;

hive> set mapreduce.input.fileinputformat.input.dir.recursive=true;

hive> select

> *

> from

> hive_test.direct_load_file_into_table

> where

> concat(year,month,day) = '20180401';

ok101 張三 2018 04 01

102 李四 2018 04 01

103 王五 2018 04 01

201 張三 2018 04 01

202 李四 2018 04 01

203 王五 2018 04 01

time taken: 1.775 seconds, fetched: 6 row(s)

將hdfs檔案匯入hive表

hive sql對hdfs的操作最終都會轉化為mr任務,下面介紹如何將已經存在的hdfs檔案 匯入 hive表,很簡單 條件及要求 1 hdfs檔案為經過lzo壓縮的seqfile 2 seqfile資料樣例 3 hive表是外在分割槽表 步驟1.建立hive表 其中external和partiti...

將hdfs檔案加入hive分割槽表中

先把檔案放入hdfs,或用flume採集到hdfs,參看另一篇,再把hdfs檔案載入到hive表中 alter table ods nshop.ods 01 releasedatas add partition bdp day 20191215 location hdfs hadoop01 9000...

將csv或者Excel檔案匯入到hive

1.將csv或excel檔案轉換為文字,以逗號或者製表符都可以 xigua.txt id,color,root,stroke,venation,umbilical,touch,status 1,青綠,蜷縮,濁響,清晰,凹陷,硬滑,是 2,烏黑,蜷縮,沉悶,清晰,凹陷,硬滑,是 3,烏黑,蜷縮,濁響,...