Sqoop:mysql,hive,hbase,hdfs之间完成文件传输

**

mysql---->hdfs

注意:最后一行不要
代码不需要进去mysql中,就在xshell中直接粘贴执行就可以
**
1)整表导入

sqoop import \
--connect jdbc:mysql://singer:3306/kb06mysqltestdb \
--username root \
--password kb10 \
--table student \
--target-dir /kb10/hdfs_sqoop \
--delete-target-dir \
--num-mappers 1    \
--fields-terminated-by '\t' \
--columns sid,sname,class_id

查看hdfs上的文件:

hdfs dfs -cat /kb10/hdfs_sqoop/part-m-00000

2)加where语句筛选数据导入

sqoop import \
--connect jdbc:mysql://singer:3306/retail_db \
--table orders \
--where "order_id<500" \
--username root \
--password kb10 \
--delete-target-dir \
--target-dir /data/orders

3)指定列导入

sqoop import \
--connect jdbc:mysql://singer:3306/retail_db \
--table orders \
--columns "order_id,order_date,order_customer_id" \
--username root \
--password kb10 \
--delete-target-dir \
--target-dir /data/orders_col \
-m 3

4)query查询语句信息导入

sqoop import \
--connect jdbc:mysql://singer:3306/retail_db \
--query "select * from orders where order_status != 'CLOSED' and \$CONDITIONS" \
--username root \
--password kb10 \
--split-by order_status \
--delete-target-dir \
--target-dir /data/orders_query \
-m 3

5)append追加

sqoop import \
--connect jdbc:mysql://singer:3306/retail_db \
--driver com.mysql.jdbc.Driver \
--table orders \
--username root \
--password kb10 \
--incremental append \
--check-column order_date \
--last-value '2014-04-15' \
--target-dir /data/retail_db/orders_append \
-m 3

**

hdfs---->mysql:

**
先在mysql上创建一张新表用来存放下面hdfs导过来的数据:

create table hdfs_student (
	id int,
	name varchar (50),
	cid int
	) ;

然后用sqoop将hdfs上的数据导出到mysql上:

sqoop export \
--connect jdbc:mysql://singer:3306/kb06mysqltestdb \
--username root \
--password kb10 \
--table hdfs_student \
--num-mappers 1 \
--export-dir /kb10/hdfs_sqoop/part-m-00000 \
--input-fields-terminated-by '\t'

mysql数据库查询原先创建的表,表中就会有数据

select * from hdfs_student;

**

mysql---->hive

1)整表导入,先在hive下创建库retails

sqoop import \
--connect jdbc:mysql://singer:3306/retail_db \
--driver com.mysql.jdbc.Driver \
--table orders \
--username root \
--password kb10 \
--hive-import \
--create-hive-table \
--hive-database retails \
--hive-table orders \
--hive-overwrite \
-m 3

创建的是一个内部表orders,路径是在hive的默认目录下: /opt/software/hadoop/hive110/warehouse/retails.db/orders

2)导入数据到hive分区表

sqoop import \
--connect jdbc:mysql://singer:3306/retail_db \
--query "select order_id,order_status from orders where order_date>='2014-07-01' and order_date<'2014-07-02' and \$CONDITIONS" \
--username root \
--password kb10 \
--target-dir /data/retail_db/orders_partition \
--hive-import \
--hive-table retails.orders_partition \
--hive-partition-key 'order_date' \
--hive-partition-value '2014-07-01' \
-m 1

hdfs路径/opt/software/hadoop/hive110/warehouse/retails.db/orders_partition下出现表数据

mysql---->hbase

**

hbase下创建一个表用来存放mysql中导过来的数据:

create 'kb10:mysql_stu','info','score';
sqoop import \
--connect jdbc:mysql://singer:3306/kb06mysqltestdb \  	//连接到mysql
--username root \
--password kb10 \
--table score  \					//导出mysql中表score的数据
--hbase-table kb10:mysql_stu \		//导入到hbase中命名空间kb10下的mysql_stu表
--column-family score  \			//mysql_stu表的列族是score
--hbase-create-table \				
--hbase-row-key sid \
--hbase-bulkload					//用sqoop完成hdfs路径的转移和合并

然后在hbase下查看原先创建的表,表中就会有数据了

scan 'kb10:mysql_stu'

hive---->mysql

在mysql上建表用来存放导过来的数据

create table dm_users(
userid varchar(100),
locale varchar(100),
birthyear varchar(100),
gender varchar(100),
joinedat varchar(100),
location varchar(100),
timezone varchar(100)
);
sqoop export \
--connect jdbc:mysql://dw:3306/ms_dm_intes \
--username root \
--password root \
--table dm_users \
--hcatalog-database dwd_intes \
--hcatalog-table dwd_users \
-m 10

若执行的之后报错类似找不到hcatalog,需要在环境变量中添加以下内容:
vi /etc/profile

#HCAT_HOME
export HCAT_HOME=/opt/soft/hive110/hcatalog
export PATH=$PATH:$HCAT_HOME/bin

激活:source /etc/profile

hbase---->hive

利用hbase作为hive的数据源,因为hbase延时低,数据吞吐量大,用hive来查询分析数据信息

create external table hbase_student(sid string,student_id int,course_id int,score int)
stored by 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'
with serdeproperties("hbase.columns.mapping" = ":key,score:student_id,score:course_id,score:score")//这边得和上面hbase_student(sid string,student_id int,course_id int,score int)中顺序对应
tblproperties("hbase.table.name" = "kb10:mysql_stu");

然后在hive中就可以利用窗口函数,with语句等方便的查询表hbase_student

select * from hbase_student

创建job

sqoop job \
--create ordersHDFS \
-- import \
--connect jdbc:mysql://singer:3306/retail_db \
--driver com.mysql.jdbc.Driver \
--table orders \
--username root \
--password kb10 \
--target-dir /data/retail_db/orders_job \
--delete-target-dir \
-m 3

sqoop job --exec ordersHDFS
执行完上述语句之后之后才会导入数据

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值