建一個表,沒有任何數據,在hdfs 上也沒有對應的數據文件
hive> select * from product;
OK
id name
Time taken: 0.104 seconds
hive> dfs -ls /user/hive/warehouse/psi.db/product;
hive>
從本地加載一個文件到該表:
hive> load data local inpath 'product.txt' into table product;
Copying data from file:/root/product.txt
Copying file: file:/root/product.txt
Loading data to table psi.product
Table psi.product stats: [num_partitions: 0, num_files: 1, num_rows: 0, total_size: 25, raw_data_size: 0]
OK
Time taken: 0.398 seconds
查看數據:
hive> select * from product;
OK
id name
1 apple
2 samsung
3 moto
Time taken: 0.124 seconds, Fetched: 3 row(s)
查看HDFS,其實是吧文件從本地原原本本copy到hdfs下而已
hive> dfs -ls /user/hive/warehouse/psi.db/product;
Found 1 items
-rw-r--r-- 1 root supergroup 25 2013-09-05 02:21 /user/hive/warehouse/psi.db/product/product.txt
hive>
那么在load一次:
hive> load data local inpath 'product.txt' into table product;
Copying data from file:/root/product.txt
Copying file: file:/root/product.txt
Loading data to table psi.product
Table psi.product stats: [num_partitions: 0, num_files: 2, num_rows: 0, total_size: 50, raw_data_size: 0]
OK
Time taken: 0.346 seconds
hive>
再查看HDFS: 其實不是想象的把數據追加到原來的文件中而是又產生一個新文件product_copy_1.txt
hive> dfs -ls /user/hive/warehouse/psi.db/product;
Found 2 items
-rw-r--r-- 1 root supergroup 25 2013-09-05 02:21 /user/hive/warehouse/psi.db/product/product.txt
-rw-r--r-- 1 root supergroup 25 2013-09-05 02:23 /user/hive/warehouse/psi.db/product/product_copy_1.txt
hive>
把本地文件上傳到HDFS再從HDFS加載試一下:
hive> dfs -put /root/product.txt /usr;
hive> load data inpath '/usr/product.txt' into table product;
Loading data to table psi.product
Table psi.product stats: [num_partitions: 0, num_files: 3, num_rows: 0, total_size: 75, raw_data_size: 0]
OK
Time taken: 0.337 seconds
hive>
再看下HDFS: 同樣是吧文件拷貝到表的目錄下而已
hive> dfs -ls /user/hive/warehouse/psi.db/product;
Found 3 items
-rw-r--r-- 1 root supergroup 25 2013-09-05 02:21 /user/hive/warehouse/psi.db/product/product.txt
-rw-r--r-- 1 root supergroup 25 2013-09-05 02:23 /user/hive/warehouse/psi.db/product/product_copy_1.txt
-rw-r--r-- 1 root supergroup 25 2013-09-05 02:27 /user/hive/warehouse/psi.db/product/product_copy_2.txt
hive>
再來看用insert into
先復制一個表:
hive> create table product2 as select * from product;
hive> insert into table product select * from product2;
再來看HDFS: 多了一個000000_0的文件
hive> dfs -ls /user/hive/warehouse/psi.db/product;
Found 4 items
-rw-r--r-- 1 root supergroup 75 2013-09-05 02:30 /user/hive/warehouse/psi.db/product/000000_0
-rw-r--r-- 1 root supergroup 25 2013-09-05 02:21 /user/hive/warehouse/psi.db/product/product.txt
-rw-r--r-- 1 root supergroup 25 2013-09-05 02:23 /user/hive/warehouse/psi.db/product/product_copy_1.txt
-rw-r--r-- 1 root supergroup 25 2013-09-05 02:27 /user/hive/warehouse/psi.db/product/product_copy_2.txt
hive>
再執行一次:
hive> insert into table product select * from product2;
再來看HDFS: 又產生了一個新文件000000_0_copy_1
hive> dfs -ls /user/hive/warehouse/psi.db/product;
Found 5 items
-rw-r--r-- 1 root supergroup 75 2013-09-05 02:30 /user/hive/warehouse/psi.db/product/000000_0
-rw-r--r-- 1 root supergroup 75 2013-09-05 02:31 /user/hive/warehouse/psi.db/product/000000_0_copy_1
-rw-r--r-- 1 root supergroup 25 2013-09-05 02:21 /user/hive/warehouse/psi.db/product/product.txt
-rw-r--r-- 1 root supergroup 25 2013-09-05 02:23 /user/hive/warehouse/psi.db/product/product_copy_1.txt
-rw-r--r-- 1 root supergroup 25 2013-09-05 02:27 /user/hive/warehouse/psi.db/product/product_copy_2.txt
這就意味着hive的每次加載文件或者數據之后都會在hdfs上創立一個文件,然后把表指向這些文件而已;如果在做ETL很長時間之后,這些文件的量是可觀的,對namenode的壓力也是顯而易見的,那么如何定期去整理這些文件整合整一個?可以新建一張表
hive> create table temp as select * from product;
查看該表在HDFS上的存儲: 可以發現其實並不是把product表下的所有文具文件拷貝而是整合了,這樣子就好辦多了
hive> dfs -ls /user/hive/warehouse/psi.db/temp;
Found 1 items
-rw-r--r-- 1 root supergroup 225 2013-09-05 02:36 /user/hive/warehouse/psi.db/temp/000000_0
刪除所有product的數據:
hive> truncate table product;
OK
Time taken: 1.24 seconds
hive> dfs -ls /user/hive/warehouse/psi.db/product;
hive>
在hdfs上已經沒有了數據文件,然后再把temp表的數據整合到product然后刪除temp
hive> insert into table product select * from temp;
hive> drop table temp;
OK
Time taken: 0.96 seconds
hive>
hive> dfs -ls /user/hive/warehouse/psi.db/product;
Found 1 items
-rw-r--r-- 1 root supergroup 225 2013-09-05 02:41 /user/hive/warehouse/psi.db/product/000000_0
hive>
這樣就達到了整合的目的,當然如果原表有分區而且分區是規范的,比如說按照年份來分區則2012年的數據不會跑到2013年的文件中,這樣子的話從temp表插灰原表的話自然就可以用自動分區的方式,但是如果由於其他的目的在原表中的數據在分區中已經混亂的話自然無法達到原來同樣的混亂的數據
當然用hadoop的歸檔是一個很好的選擇