Sqoop数据传递

1.环境准备:打开Hadoop、Mysql
jps
cd /apps/hadoop/sbin
./start-all.sh
sudo service mysql start
mysql -u root -p

2.数据准备:
#本地下载数据文件
mkdir -p /data/sqoop2
cd /data/sqoop2 wget …
#导入到数据库中(在mysql命令行下)
create database mydb;
use mydb;
create table record(id varchar(100), buyer_id varchar(100), dt varchar(100), ip varchar(100), opt_type varch ar(100));
load data infile '/data/sqoop2/buyer_log' into table record fields terminated by ' ';
select * from record;

3.使用Sqoop查看Mysql数据库,检查Mysql是否可以正常使用
sqoop list-databases
--connect jdbc:mysql://localhost:3306/
--username root
--password strongs
#查看Mysql中的表
sqoop list-tables
--connect jdbc:mysql://localhost:3306/mydb
--username root
--password strongs

4.使用Sqoop将Mysql中数据导入到HDFS/mysqoop2目录里
sqoop import
--connect jdbc:mysql://localhost:3306/mydb
--username root
--password strongs
--table record -m 1
--target-dir /mysqoop2
#查看HDFS上/mysqoop2目录下文件内容
hadoop fs -cat /mysqoop2/part-m-00000

5.使用Sqoop将HDFS中数据存入到Mysql数据库中
#新建一张表
use mydb
create table recordfromhdfs like record;
#在另一个窗口开始导数据
sqoop export
--connect jdbc:mysql://localhost:3306/mydb?characterEncoding=UTF-8
--username root
--password strongs
--table recordfromhdfs
--export-dir hdfs://localhost:9000/mysqoop2/part-m-00000
#查看Mysql中的表
select * from recordfromhdfs;

6.使用Sqoop将Mysql中数据导入到HBase中
#启动HBase,并进入命令行模式
cd /apps/hbase/bin
./start-hbase.sh
hbase shell
#在另一个窗口开始导数据
sqoop import
--connect jdbc:mysql://localhost:3306/mydb?characterEncoding=UTF-8
--username root
--password strongs
--table record
--hbase-create-table
--hbase-table hbaserecord
--column-family mycf
--hbase-row-key dt -m 1
#查看HBase中有哪些表
list
#查看表中内容
scan ‘hbaserecord’

7.使用Sqoop将HBase中数据导出到Mysql中
暂时无法直接接口实现,需要借助其他途径去处理,比如:HBase=>HDFS=>Mysql或 HDFS=>Hive=>Mysql
hbase org.apache.hadoop.hbase.mapreduce.Export t1 /t2

8.使用Sqoop将Mysql表中数据导入到Hive中表
#使用vim编辑用户环境变量
vim ~/.bashrc
#hadoop
export HADOOP_HOME=/apps/hadoop
export PATH=$HADOOP_HOME/bin:$PATH
export HADOOP_CLASSPATH=$HADOOP_CLASSPATH:/apps/hive/lib/*
#执行source,使用户环境变量生效。
source /etc/profile
#开启Hive,创建表hiverecord
hive
create table hiverecord (id varchar(100),buyer_id varchar(100),dt varchar(100), ip varchar(100), opt_type varchar(100))
row format delimited fields terminated by ',' stored as textfile;
#在linux命令行下,使用Sqoop将Mysql中record表导入Hive中。
sqoop import
--connect jdbc:mysql://localhost:3306/mydb?characterEncoding=UTF-8
--username root
--password strongs
--table record
--hive-import
--hive-table hiverecord
--fields-terminated-by ',' -m 1
#在hive下,查看Hive中hiverecord表。
select * from hiverecord;

9.使用Sqoop将Hive表hiverecord表中的数据,导出到Mysql中的recordfromhive表中。
#首先在Mysql中创建表recordfromhive。
create table recordfromhive like record;
#在linux命令行下,使用sqoop开始导数据。
sqoop export
--connect jdbc:mysql://localhost:3306/mydb?characterEncoding=UTF-8
--username root
--password strongs
--table recordfromhive
--export-dir /user/hive/warehouse/hiverecord/part-m-00000
--input-fields-terminated-by ','
#导入完成,查看Mysql中recordfromhive表。
select * from recordfromhive;

10.增量数据导入方法
#使用Append模式(字段增长),增量导入
sqoop import --connect jdbc:mysql://localhost:3306/sqoop --username root -password strongs --table buyer -m 1
--incremental append --last-value 10005 --check-column buyer_id --target-dir /mysqoop/111
#使用Lastmodified模式(时间增长),增量导入
##将reg_date字符类型改为date类型。
alter table buyer modify reg_date date;
sqoop import --connect jdbc:mysql://localhost:3306/sqoop --username root --password strongs --table buyer -m 1
--incremental lastmodified --check-column reg_date --last-value '2008-10-21 15:31:33' --target-dir /mysqoop/222

原文地址:https://www.cnblogs.com/hannahzhao/p/11960084.html