hdfs數據到hive中:
假設hdfs中已存在好了數據,路徑是hdfs:/localhost:9000/user/user_w/hive_g2park/user_center_enterprise_info/*
1.提前(在hive中)准備好表, user_center_enterprise_info2 ,用於接收hdfs數據。
CREATE TABLE user_center_enterprise_info2 ( `id`string , `name` string );
2.使用load data方式,加載數據,(已執行數據庫選擇命令 hive>use testdb;)
以下 相對/絕對 兩種路徑加載都行
hive>load data inpath 'hive_g2park/user_center_enterprise_info/*' into table user_center_enterprise_info2; hive>load data inpath '/user/user_w/hive_g2park/user_center_enterprise_info/*' into table user_center_enterprise_info2;
此時:
hdfs dfs -ls /user/user_w/hive_g2park/user_center_enterprise_info 發現里面內容沒了
3.把數據從hive寫回hdfs,讓數據出現在hdfs
數據是從hdfs的 hive_g2park/user_center_enterprise_info 寫到hive的,
現在寫道 hive_g2park/user_center_enterprise_info3 路徑下
-- 設置task數 set mapred.reduce.tasks = 1; 結果數據平均分區(分區數等於task數); set mapred.reduce.tasks = 1; insert overwrite directory 'hive_g2park/user_center_enterprise_info3' ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe' WITH SERDEPROPERTIES ( 'field.delim'='\t', 'serialization.format'= '', 'serialization.null.format'='' ) STORED AS TEXTFILE select * from user;
此時在hdfs下生成了新路徑,
hive_g2park/user_center_enterprise_info3
並且有數據在其文件夾下
我的理解數據消失:
hive本質是map-reduce,即操作hdfs數據的方式有:spark,mr,pig,hive。
而hive只是在mr上包了一層,hive操作的時候,本質上,是直接操作的hdfs數據,也就是說hdfs數據load后,和hive數據是同一份數據
而且load data inpath '/user/hive/os.txt' into table os;這種方式loca數據到hive辣么快,應該是修改了指針而已,而不是復制了一份數據到hive。
hdfs數據到hive就隱藏不見,這么設計,就是為了避免數據在被hive改動的同時,又被mr直接操作hdfs數據,刪除移動什么的,造成數據的不一致,所以數據丟hive里hdfs里就看不見了
sqoop export --connect jdbc:mysql://127.0.0.1:3306/parkdb --username xiaoming --password '123' --table t_vip_user --export-dir 'hive_g2park/vip/*' --fields-terminated-by "\t"
附錄:
sqoop的導出參數中,hive-import作用:本次導入到hive中
導入看得到hdfs文件夾范例
sqoop import --connect jdbc:mysql://localhost:3306/user --username h --password '123' \
--fields-terminated-by "\t" --table enterprise_info --delete-target-dir --target-dir 'hive_g2park/user_center_enterprise_info' \
--create-hive-table --hive-table g2park.user_center_enterprise_info
導入看不到hdfs文件夾范例
sqoop import --connect jdbc:mysql://localhost:3306/user --username h --password '123'
--fields-terminated-by "\t" --table enterprise_info --delete-target-dir
--hive-import --target-dir 'hive_g2park/user_center_enterprise_info'
--create-hive-table --hive-table g2park.user_center_enterprise_info