导入 MySQL 数据到 HDFS
1.普通导入
sqoop import \
--connect jdbc:mysql://localhost:3306/mysql \
--username root \
--password 123456 \
--table help_keyword \
-m 1
2.指定分隔符和导入路径
sqoop import \
--connect jdbc:mysql://localhost:3306/mysql \
--username root \
--password 123456 \
--table help_keyword \
--target-dir /user/centos/my_help_keyword \
--fields-terminated-by '\t' \
-m 1
3.导入 where 条件数据
sqoop import \
--connect jdbc:mysql://localhost:3306/mysql \
--username root \
--password 123456 \
--where "name='DATE' " \
--table help_keyword \
--target-dir /user/centos/my_help_keyword01 \
-m 1
4.导入 Query 结果数据
sqoop import \
--connect jdbc:mysql://localhost:3306/mysql \
--username root \
--password 123456 \
--target-dir /user/centos/my_help_keyword02 \
--query 'select help_keyword_id,name from help_keyword WHERE name = "STRING" and
$CONDITIONS' \
--split-by help_keyword_id \
--fields-terminated-by '\t' \
-m 1
sqoop import \
--connect jdbc:mysql://hadoop02:3306/mysql \
--username root \
--password root \
--target-dir /user/hadoop/myimport22 \
--query "select help_keyword_id,name from help_keyword WHERE name = 'STRING' and
\$CONDITIONS" \
--split-by help_keyword_id \
--fields-terminated-by '\t' \
-m 1
导入 MySQL 到 HIVE
sqoop import \
--connect jdbc:mysql://localhost:3306/mysql \
--username root \
--password 123456 \
--table help_relation \
--hive-import \
-m 1
sqoop import \
--connect jdbc:mysql://localhost:3306/mysql \
--username root \
--password root \
--table help_keyword01 \
--fields-terminated-by "\t" \
--lines-terminated-by "\n" \
--hive-import \
--hive-overwrite \
--create-hive-table \
--hive-table mydb_test.new_help_keyword \
--delete-target-dir
增量导入
sqoop import \
--connect jdbc:mysql://hadoop02:3306/mysql \
--username root \
--password root \
--table help_keyword \
--target-dir /user/hadoop/myimport3 \
--incremental append \
--check-column help_keyword_id \
--last-value 500 \
-m 1
导入 MySQL 数据到 HBase
sqoop import \
--connect jdbc:mysql://localhost:3306/mysql \
--username root \
--password 123456 \
--table help_keyword \
--hbase-table new_help_keyword \
--column-family person \
--hbase-row-key help_keyword_id
导出 HDFS 数据到 MySQL
第一步,先看需要导出到 mysql 的数据:
第二步,需要首先在 mysql 数据库中新建表:
create database sqoopdb default character set utf8 COLLATE utf8_general_ci;
use sqoopdb;
CREATE TABLE sqoopstudent (
id INT NOT NULL PRIMARY KEY,
name VARCHAR(20),
sex VARCHAR(20),
age INT,
department VARCHAR(20)
);
第三步,执行导出:
sqoop export \
--connect jdbc:mysql://localhost:3306/sqoopdb \
--username root \
--password 123456 \
--table sqoopstudent \
--export-dir /sqoopdata \
--fields-terminated-by ','
导出 HIVE 数据到 到 MySQL
sqoop export \
--connect jdbc:mysql://localhost:3306/sqoopdb \
--username root \
--password 123456 \
--table uv_info \
--export-dir /user/hive/warehouse/uv/dt=2011-08-03 \
--input-fields-terminated-by '\t'
导出 HBase 数据到 MySQL
一般采用如下 3 种方法:
1、将 Hbase 数据,扁平化成 HDFS 文件,然后再由 sqoop 导入
2、将 Hbase 数据导入 Hive 表中,然后再导入 mysql
3、直接使用 Hbase 的 JavaAPI 读取表数据,直接向 mysql 导入,不需要使用 sqoop