【大数据学习笔记】sqoop

2 篇文章 0 订阅
2 篇文章 0 订阅

1、简介

将关系型数据库与hadoop数据进行转换的工具

官网文档地址:http://sqoop.apache.org/docs/1.4.6/SqoopUserGuide.html

我用的是1.4.6版本,集群使用Ambari搭建,在任意一台服务节点上安装sqoop客户端;

sqoopclient端直接接入hadoop,任务通过解析生成对应的MapRecue执行

sqoop中导入与导出相对于hdfs来讲,即数据流入HDFS为导入,数据从HDFS流出为导出;

2、安装

安装成功后,使用sqoop version测试

如果有警告信息,可以注释相关代码去除警告

进入sqoop的安装路径下的bin目录,通过Ambari安装的路径:/usr/hdp/current/sqoop-client/bin

vim configure-sqoop

在vim中搜索$ACCUMULO(直接输入/$ACCUMULO即为搜索),注释掉如下4行代码,如果还有其他警告信息,可以依次注释相关代码即可;

#if [ ! -d "${ACCUMULO_HOME}" ]; then
#  echo "Warning: $ACCUMULO_HOME does not exist! Accumulo imports will fail."
#  echo 'Please set $ACCUMULO_HOME to the root of your Accumulo installation.'
#fi

3、简单使用

3.1 直接输入命令,执行sqoop任务,列出MySQL中所有数据库列表

[root@server2 bin]# sqoop list-databases -connect jdbc:mysql://your_mysql_ip:3306/ -username root -password your_password
19/03/29 18:25:42 INFO sqoop.Sqoop: Running Sqoop version: 1.4.6.2.6.4.0-91
19/03/29 18:25:42 WARN tool.BaseSqoopTool: Setting your password on the command-line is insecure. Consider using -P instead.
19/03/29 18:25:42 INFO manager.MySQLManager: Preparing to use a MySQL streaming resultset.
Fri Mar 29 18:25:42 CST 2019 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
information_schema
ambari
hive
mysql
oozie
performance_schema
sys

3.2 将置顶数据库中数据导入hdfs中

su hdfs
sqoop import --connect jdbc:mysql://your_mysql_ip:3306/your_dbname --username root --password your_password --table your_table --columns id,your_column -m 1 --target-dir /sqoop

因为涉及到HDFS文件的写入,务必使用拥有HDFS操作权限的用户执行以上命令,否则会报权限不足的错误,导致任务失败;

这里我使用hdfs用户进行操作;

3.3 使用配置文件的方式

3.3.1 MySQL -> HDFS

从MySQL数据库的result_db数据库中的dimension_date表中所有的数据,导入到HDFS中的/sqoop路径下

cd /opt/data/sqoop
vim option
import 
--connect 
jdbc:mysql://server1:3306/result_db 
--username 
root 
--password 
your_password
--table 
dimension_date 
--columns 
id,calendar 
-m 
1 
--target-dir 
/sqoop
--delete-target-dir
su hdfs
sqoop --options-file option

导入成功后,HDFS上/sqoop路径下如下图所示:

3.3.2 MySQL -> HDFS

从MySQL数据库result_db数据库中的dimension_date表中符合条件的数据(where字句必须配合$CONDITIONS使用),导入到HDFS中的/sqoop路径下

vim option2
import
--connect
jdbc:mysql://server1:3306/result_db
--username
root
--password
your_password
-e
select * from dimension_date where id = 2 and $CONDITIONS
-m
1
--target-dir
/sqoop
--delete-target-dir
su hdfs
sqoop --options-file option2

3.3.3 MySQL -> HDFS

从MySQL数据库result_db数据库中的dimension_date表中符合条件的数据(where字句必须配合$CONDITIONS使用),导入到HDFS中的/sqoop路径下

vim option3
import
--connect
jdbc:mysql://server1:3306/result_db
--username
root
--password
your_password
-e
select * from dimension_date where $CONDITIONS
-m
1
--target-dir
/sqoop
--delete-target-dir
su hdfs
sqoop --options-file option3

3.3.4 MySQL -> HIVE

从MySQL数据库result_db数据库中的dimension_date表中符合条件的数据(where字句必须配合$CONDITIONS使用),导入到Hive中的abc表中

vim option4
import 
--connect 
jdbc:mysql://server1:3306/result_db 
--username 
root 
--password 
your_password
--table 
dimension_date 
--columns 
id,calendar,year 
--target-dir
/sqoop/tmp
-m 
1
--hive-import
--create-hive-table
--hive-table
abc
su hdfs
sqoop --options-file option4

自动创建的abc表详细信息:

hive> desc formatted abc;
OK
# col_name            	data_type           	comment             
	 	 
id                  	int                 	                    
year                	int                 	                    
season              	int                 	                    
month               	int                 	                    
week                	int                 	                    
day                 	int                 	                    
calendar            	string              	                    
type                	string              	                    
	 	 
# Detailed Table Information	 	 
Database:           	default             	 
Owner:              	hdfs                	 
CreateTime:         	Mon Apr 01 11:14:39 CST 2019	 
LastAccessTime:     	UNKNOWN             	 
Protect Mode:       	None                	 
Retention:          	0                   	 
Location:           	hdfs://lonzh/apps/hive/warehouse/abc	 
Table Type:         	MANAGED_TABLE       	 
Table Parameters:	 	 
	comment             	Imported by sqoop on 2019/04/01 11:14:30
	numFiles            	1                   
	numRows             	0                   
	rawDataSize         	0                   
	totalSize           	64                  
	transient_lastDdlTime	1554088480          
	 	 
# Storage Information	 	 
SerDe Library:      	org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe	 
InputFormat:        	org.apache.hadoop.mapred.TextInputFormat	 
OutputFormat:       	org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat	 
Compressed:         	No                  	 
Num Buckets:        	-1                  	 
Bucket Columns:     	[]                  	 
Sort Columns:       	[]                  	 
Storage Desc Params:	 	 
	field.delim         	\u0001              
	line.delim          	\n                  
	serialization.format	\u0001              
Time taken: 0.428 seconds, Fetched: 40 row(s)

3.3.5 MySQL -> HBase

从MySQL数据库result_db数据库中的dimension_date表中符合条件的数据(where字句必须配合$CONDITIONS使用),导入到HBase中的abc表中

vim option5
import 
--connect 
jdbc:mysql://server1:3306/result_db 
--username 
root 
--password 
your_password 
--table 
dimension_date 
--columns 
id,calendar,year,month
-m 
1 
--column-family
base
--hbase-create-table
--hbase-table
abc
su hdfs
sqoop --options-file option5

3.3.6 HDFS -> MySQL

vim option6
export
--connect
jdbc:mysql://server1:3306/result_db
--username
root
--password
your_password
-m
1
--columns
id,year,season,month
--export-dir
/sqoop/
--table
h_abc

在操作前,需要在MySQL中先创建h_abc表 

su hdfs
sqoop --options-file option6

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值