您的位置:首页 > 运维架构

sqoop基本操作

2017-12-06 12:42 375 查看
1. 查询mysql中的数据库

sqoop list-databases --connect jdbc:mysql://haoguan-HP-Compaq-Pro-6380-MT:3306 --username root --password 123456


 

2. import从mysql中导入数据到hdfs

create database testdb;
use testdb;
create table user(
id int not null auto_increment,
account varchar(255) default null,
password varchar(255) default null,
primary key(id)
);

insert into user(account, password) values('aaa', '123');
insert into user(account, password) values('bbb', '123');
insert into user(account, password) values('ccc', '123');
insert into user(account, password) values('ddd', '123');
insert into user(account, password) values('eee', '123');
insert into user(account, password) values('fff', '123');
insert into user(account, password) values('ggg', '123');
insert into user(account, password) values('hhh', '123');


 
注:--direct不能同时与 --as-sequencefile --as-avrodatafile  --as-parquetfile连用 
Parameters --as-sequencefile --as-avrodatafile and --as-parquetfile are not supported with --direct params in MySQL case. 

 

 
1. MySql -> Hdfs -> Hive, parquet文件 

(1) sqoop导入数据到hdfs中

sqoop import \
--connect jdbc:mysql://haoguan-HP-Compaq-Pro-6380-MT:3306/testdb \
--username root \
--password 123456 \
--table user \
--target-dir /sqoop/import/user_parquet \
--delete-target-dir \
--num-mappers 1 \
--as-parquetfile


(2) 利用导入的数据直接在hive中建表,无需再指定row format与fields terminated by

create external table user_from_mysql(
id int,
account string,
password string)
stored as parquet
location '/sqoop/import/user_parquet';


2. query,columns,where条件导入 

(1) query, where子句必须有$CONDITIONS(固定写法)

sqoop import \
--connect jdbc:mysql://haoguan-HP-Compaq-Pro-6380-MT:3306/testdb \
--username root \
--password 123456 \
--query 'select id,account from from user where account="ddd" and $CONDITIONS' \
--target-dir /user/haoguan/sqoop/import/user_parquet \
--delete-target-dir \
--fields-terminated-by '\t' \
--num-mappers 1 \
--as-parquetfile


(2) columns指定导入的字段 

注:--columns account,password字段之间不能有空格 

   用-P代替--password是为了不用明文指定数据库连接密码

sqoop import \
--connect jdbc:mysql://109.123.121.104:3306/testdb \
--username root \
-P \
--table user \
--columns account,password \
--target-dir /sqoop/import/user_column \
--delete-target-dir \
--fields-terminated-by '\t' \
--num-mappers 1 \
--direct


(3)where子句 

sqoop import \
--connect jdbc:mysql://109.123.121.104:3306/testdb \
--username root \
--password 123456 \
--table user \
--where 'id > 5 and account like "f%"' \
--target-dir /sqoop/import/user_where \
--delete-target-dir \
--fields-terminated-by '\t' \
-m 1 \
--direct


3. MySql -> Hive 
注: Mysql直接导入数据到Hive表中,不支持--as-parquetfile,即使在hive表创建的时候指定stored as parquet也无效 
    只能间接通过"MySql -> Hdfs -> Hive, parquet文件"转换 

创建hive表user_to_hive 

失败:

drop table if exists user_to_hive_parquet;
create table user_to_hive_parquet(
id int,
account string,
password string)
row format delimited fields terminated by '\t'
stored as parquet;


sqoop import \
--connect jdbc:mysql://109.123.121.104:3306/testdb \
--username root \
--password 123456 \
--table user \
--hive-import \
--hive-database testdb \
--hive-table user_to_hive_parquet \
--delete-target-dir \
--fields-terminated-by '\t' \
-m 1 \
--as-parquetfile


成功:只能普通文本文件导入

drop table if exists user_to_hive;
create table user_to_hive(
id int,
account string,
password string)
row format delimited fields terminated by '\t';


sqoop import \
--connect jdbc:mysql://109.123.121.104:3306/testdb \
--username root \
--password 123456 \
--table user \
--hive-import \
--hive-database testdb \
--hive-table user_to_hive \
--delete-target-dir \
--fields-terminated-by '\t' \
-m 1


4. Hdfs/Hive -> MySql 

(1) 导入文本文件表 

创建mysql中的表

create table user_from_hive like user;


导出数据到MySql

sqoop export \
--connect jdbc:mysql://109.123.121.104:3306/testdb \
--username root \
--password 123456 \
--table user_from_hive \
--export-dir /sqoop/import/user \
--input-fields-terminated-by '\t' \
-m 1


 

(2) 导入parquet文件表到Mysql 

创建mysql中的表 

create table user_from_hive_parquet like user; 

导出数据到MySql 

注:如果hive中文件是parquet格式,无需指定--input-fields-terminated-by '\t'  

sqoop export \
--connect jdbc:mysql://109.123.121.104:3306/testdb \
--username root \
--password 123456 \
--table user_from_hive_parquet \
--export-dir /sqoop/import/user_parquet \
-m 1


 
5.增量导入到hdfs 

注:从原表中id=5后面一行开始 

(1) 增量导入文本文件textfile

sqoop import \
--connect jdbc:mysql://109.123.121.104:3306/testdb \
--username root \
--password 123456 \
--table user \
--target-dir /sqoop/import/user \
--fields-terminated-by '\t' \
-m 1 \
--check-column id \
--incremental append \
--last-value 5


(2) 增量导入parquet文件 

方式一:不支持按指定条件追加数据,只能固定以某个列为标准追加 

--check-column id      以ID列为标准 

--incremental append   追加方式 

--last-value 5         id = 5后面一行开始追加

sqoop import \
--connect jdbc:mysql://109.123.121.104:3306/testdb \
--username root \
--password 123456 \
--table user \
--target-dir /sqoop/import/user_parquet \
--fields-terminated-by '\t' \
--as-parquetfile \
-m 1 \
--check-column id \
--incremental append \
--last-value 5


方式二: 可以按指定条件把查询结果追加到表中 

--append

sqoop import \
--connect jdbc:mysql://haoguan-HP-Compaq-Pro-6380-MT:3306/testdb \
--username root \
--password 123456 \
--query 'select * from user where id=5 and $CONDITIONS' \
--target-dir /sqoop/import/user \
--num-mappers 1 \
--append


(3)给hive表追加数据 

给hive表导入数据 

注:最好使用--delete-target-dir,否则在导入数据过程中hive会在/user/haoguan/user目录中生成_SUCCESS文件,下次再操作的时候,会报/user/haoguan/user目录已经存在

sqoop import \
--connect jdbc:mysql://109.123.121.104:3306/testdb \
--username root \
--password 123456 \
--table user \
--delete-target-dir \
--hive-import \
--hive-database testdb \
--hive-table user_to_hive \
--fields-terminated-by '\t' \
-m 1


可以在hive表目录中直接追加数据,也就相当于给hive追加数据了 

sqoop import \
--connect jdbc:mysql://109.123.121.104:3306/testdb \
--username root \
--password 123456 \
--query 'select * from user where id=5 and $CONDITIONS' \
--fields-terminated-by '\t' \
-m 1 \
--append \


不支持在hive表中直接追加数据,以下操作会报异常

sqoop import \
--connect jdbc:mysql://109.123.121.104:3306/testdb \
--username root \
--password 123456 \
--query 'select * from user where id=5 and $CONDITIONS' \
--hive-import \
--hive-database testdb \
--hive-table user_to_hive \
--fields-terminated-by '\t' \
-m 1 \
--append
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: