您的位置:首页 > 数据库 > SQL

sqoop 从mysql导入数据到hdfs、hive

2015-09-29 00:45 639 查看
1.上传sqoop安装包

2.安装和配置

在添加sqoop到环境变量

将数据库连接驱动拷贝到$SQOOP_HOME/lib里

3.使用

第一类:数据库中的数据导入到HDFS上

sqoop import --connect jdbc:mysql://hadoop07:3306/test --username root --password 123 --table user_info--columns 'id, account, income, expenses'

指定输出路径、指定数据分隔符

sqoop import --connect jdbc:mysql://192.168.19.116:3306/test --username root --password 123 --table user_info--target-dir '/sqoop/td' --fields-terminated-by '\t'

指定Map数量 -m

sqoop import --connect jdbc:mysql://192.168.19.116:3306/test --username root --password 123 --table user_info--target-dir '/sqoop/td1' --fields-terminated-by '\t' -m 2

增加where条件, 注意:条件必须用引号引起来

sqoop import --connect jdbc:mysql://192.168.19.116:3306/test --username root --password 123 --table user_info--where 'id>3' --target-dir '/sqoop/td2'

增加query语句(使用 \ 将语句换行)

sqoop import --connect jdbc:mysql://192.168.19.116:3306/test --username root --password 123 \

--query 'SELECT * FROM trade_detail where id > 2 AND $CONDITIONS' --split-by user_info.id --target-dir '/sqoop/td3'

注意:如果使用--query这个命令的时候,需要注意的是where后面的参数,AND $CONDITIONS这个参数必须加上

而且存在单引号与双引号的区别,如果--query后面使用的是双引号,那么需要在$CONDITIONS前加上\即\$CONDITIONS

如果设置map数量为1个时即-m 1,不用加上--split-by ${tablename.column},否则需要加上

第二类:将HDFS上的数据导出到数据库中(不要忘记指定分隔符)

sqoop export --connect jdbc:mysql://192.168.19.116:3306/test --username root --password 123 --export-dir '/td3' --table td_bak -m 1 --fields-terminated-by ','

4.配置mysql远程连接

GRANT ALL PRIVILEGES ON itcast.* TO 'root'@'192.168.19.116' IDENTIFIED BY '123' WITH GRANT OPTION;

FLUSH PRIVILEGES;

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123' WITH GRANT OPTION;

FLUSH PRIVILEGES

5.sqoop 导入到hive

在hive当中创建两张表

create table teacher_info (id bigint, account string, income double, expenses double, time string) row format delimited fields terminated by '\t';

create table user_info (id bigint, name string, age int) row format delimited fields terminated by '\t';

将mysq当中的数据直接导入到hive当中

sqoop import --connect jdbc:mysql://192.168.19.116:3306/test --username root --password 123 --table teacher_info --hive-import --hive-overwrite --hive-table teacher_info --fields-terminated-by '\t'

sqoop import --connect jdbc:mysql://192.168.19.116:3306/test --username root --password 123 --table user_info --hive-import --hive-overwrite --hive-table user_info --fields-terminated-by '\t'

6.报错情况:

a.可能是连接驱动版本不符合,更新相对应得版本

b.报连接不多historyserver 10020 端口

HistoryServer需要配置: yarn-site.xml

<property>

<name>mapred.job.history.server.embedded</name>

<value>true</value>

</property>

<property>

<name>mapreduce.jobhistory.address</name>

<value>historyserver:10020</value>

</property>

启动historyserver:sbin/mr-jobhistory-daemon.sh start historyserver

校验是否启动成功:jps 看到:JobHistoryServer 进程
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: