一、高速入门
(一)下载安装
1、下载并解压
wget http://archive.cloudera.com/cdh5/cdh/5/sqoop-1.4.4-cdh5.1.2.tar.gz
tar -zxvf sqoop-1.4.4-cdh5.1.2.tar.gz
2、将sqoop中的命令加入至$PATH中
export PATH=$PATH:/home/jediael/setupfile/sqoop-1.4.4-cdh5.1.2/bin
3、验证安装
jediael@jediael:~/setupfile/sqoop-1.4.4-cdh5.1.2/bin$ source /etc/profile
……..
15/05/15 09:47:37 INFO sqoop.Sqoop: Running Sqoop version: 1.4.4-cdh5.1.2
Sqoop 1.4.4-cdh5.1.2
……..
(二)在mysql中准备数据
1、创建数据库交授权
create database sqoop;
use sqoop;
GRANT ALL PRIVILEGES ON sqoop.* TO ''@'localhost';
2、创建表
create table widgets(
id int not null primary key auto_increment,
widget_name varchar(64) not null,
price decimal(10,2),
design_date date,
version int,
design_comment varchar(100));
3、插入数据
insert into widgets values(null, 'sprocket', 0.25, '2010-02-10', 1, 'Connects two gizmos');
insert into widgets values(null, 'gizmos', 4.00, '2009-11-30', 4, null);
insert into widgets values(null, 'gadget', 99.99, '193-08-13', 13, 'Our flagship product');
(三)将mysql中的数据导入hdfs
1、准备jdbc connector,并将之入到sqoop/lib文件夹下
wget http://cdn.mysql.com/Downloads/Connector-J/mysql-connector-java-5.1.35.tar.gz
tar -zxvf mysql-connector-java-5.1.35.tar.gz
cp mysql-connector-java-5.1.35-bin.jar ../sqoop-1.4.4-cdh5.1.2/lib/
2、导入数据
sqoop import --connect jdbc:mysql://localhost/sqoop --table widgets -m 1
3、查看数据
$ hadoop fs -cat widgets/part-m-00000
15/05/15 10:29:29 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
1,sprocket,0.25,2010-02-10,1,Connects two gizmos
2,gizmos,4.00,2009-11-30,4,null
3,gadget,99.99,0193-08-13,13,Our flagship product
4、import除了将数据导入hdfs中,还在当前文件夹生成了widgets.java文件。
还能够使用下面命令仅仅生成java类,而不进行实际的导入:
sqoop import --connect jdbc:mysql://localhost/sqoop --table widgets --class-name widget
二、sqoop与hive
(一)将数据导入hive
1、若仅须要将mysql中的数据导入sqoop中。则能够运行下面命令
sqoop import --connect jdbc:mysql://localhost/sqoop --table widgets -m 1 --hive-import
验证数据:
hive> select * from widgets;
OK
1 sprocket 0.25 2010-02-10 1 Connects two gizmos
2 gizmos 4.0 2009-11-30 4 null
3 gadget 99.99 0193-08-13 13 Our flagship product
Time taken: 1.299 seconds, Fetched: 3 row(s)
2、若须要先将mysql中的数据导入hdfs。再从hdfs中导入hive
(1)将数据导入hdfs:见上面。
(2)创建表
sqoop create-hive-table --connect jdbc:mysql://localhost/sqoop --table widgets --fields-terminated-by ‘,'
这会依据mysql中的表结构直接创建一个hive表。
(3)将hdfs中的数据导入hive中
hive> load data inpath "widgets" into table widgets;
此时数据存储在/user/hive/warehouse/widgets
(二)在hive中进行数据分析
1、创建hive表。用于保存分析结果
create table widgets_sum(sum int);
2、分析
hive> insert into table widgets_sum select count(1) from widgets;
(三)将数据导出到mysql
1、在mysql中创建表。用于保存从hive中导出的结果
create table widgets_sum(sum int);
注意,sqoop可以依据mysql的结构自己主动生成hive表,但反之不行,必须手工建表
2、sqoop export --connect jdbc:mysql://localhost/sqoop -m 1 --table widgets_sum --export-dir /user/hive/warehouse/widgets_sum --input-fields-terminated-by ' 001
异常:
1、若没运行GRANT ALL PRIVILEGES ON sqoop.* TO ''@'localhost’;
则会出现下面异常:
15/05/15 10:25:28 ERROR manager.SqlManager: Error executing statement: java.sql.SQLException: Access denied for user ''@'localhost' (using password: NO)
java.sql.SQLException: Access denied for user ''@'localhost' (using password: NO)