sqoop綜合案例
1.檢查Hadoop進程是否已經啓動
cd /apps/hadoop/sbin
./start-all.sh
jps
2.首先在Linux本地,新建data/case4目錄,用於存放所需文件。
mkdir -p /data/case4
在Linux中切換到data/case4目錄下,下載文本文件order_items。
首先在HDFS上新建/mycase4/目錄,然後將Linux本地/data/case4目錄下的order_items文件導入到HDFS的/mycase4/目錄中。
hadoop fs -mkdir -p /mycase4/
hadoop fs -put /data/case4/order_items /mycase4/
3.使用HDFS命令,查看HDFS目錄結果。
hadoop fs -ls -R /mycase4/
4.啓動Hive,首先查看下Mysql數據庫是否開啓,若沒有開啓,應先啓動Mysql, 然後啓動hive。
sudo service mysql status
sudo service mysql start
hive
創建myhive 數據庫
create database myhive;
查看hive中數據庫
show databases;
使用myhive數據庫
use myhive;
在myhive庫中創建訂單明細表(order_items),用於存儲hdfs上/mycase4/order文件中的數據。(寫下創建表語句)
create table order_items
(item_id string,
order_id string,
goods_id string,
goods_number string,
shop_price string,
goods_price string,
goods_amount string)
row format delimited
fields terminated by '\t'
stored as textfile;
將hdfs上/mycase4目錄下的order_items文件加載到hive的order_items表中。
load data inpath '/mycase4/' overwrite into table order_items;
5.統計order_items有多少行記錄。(寫出統計語句及結果)
select count(1) from order_items;
6.在Hive中創建mytb表,表字段爲(item_id ,order_id),字符類型爲string,以 "\t"爲分割符。
create table mytb (goods_id string, goods_number string) row format delimited fields terminated by '\t';
7.使用hive,統計每個商品ID(goods_id字段)數量;並將統計結果,插入到mytb表中。
insert into table mytb select goods_id, count(1) as num from order_items group by goods_id;
8.另開啓一個窗口,使用mysql -u root -p連接mysql數據庫
mysql -u root -p
在mysql中創建DD數據庫。
create database DD;
使用DD數據庫,並在DD數據庫中創建表,名爲items。
use DD;
create table items(goods_id varchar(100),goods_number varchar(100));
9.另開啓一個窗口,使用sqoop,將Hive中表mytb中的數據,導出到mysql中數據庫DD下的items中。
sqoop export --connect jdbc:mysql://localhost:3306/DD --username root --password strongs \
--table items --export-dir /user/hive/warehouse/myhive.db/mytb --input-fields-terminated-by '\t'
在mysql數據庫中用select語句查看items表是否導入數據,由於數據很多我們只查看前十條記錄。
select * from items limit 10;