hadoop(2): 安装&使用 sqoop
2015-08-18 17:42
323 查看
本文的原文连接是: /article/1665642.html 未经博主允许不得转载。
官网:http://sqoop.apache.org/
一个1.4.6 版本,一个是1.99版本(开发版还没有迁移完成,生产环境不推荐)
文档:
http://sqoop.apache.org/docs/1.4.6/
配置环境变量
sqoop启动的时候会检查hbase环境变量,不需要,直接注释
/data/sqoop/bin/configure-sqoop 的128 行到 147行。
hadoop和hive参考之前写的博客:
/article/1665643.html
sqoop命令主要就分为数据导入到hadoop,和数据从hadoop导出到 mysql。
首先创建mysql数据库blog,创建一个hadoop用户操作blog库,blog库里面创建一个msg表,插入6条记录
将mysql 数据导入到hive中,其实就是导入到hadoop上面,这里需要指定输出目录是hive的warehouse目录:
参数一大堆,设置分隔符的,设置null的。最后指定hive的warehouse目录。
但是这样hive还不识别这个表,必须在hive中创建下。
将hive 数据导入到 mysql中。
同样的需要配置export-dir,配置mysql的数据表msg_hive
检查结果:(分别在hive和mysql中查看数据)
sqoop提供了import和export功能,很方便的就可以迁移mysql和hive的数据。
业务数据需要迁移到hadoop上面进行计算,同时,将计算结果放到mysql数据库中进行统计显示。
数据可以方便的流动。
参考:
http://segmentfault.com/a/1190000002532293
写的参数很详细。
1,关于sqoop
Sqoop是一个用来将Hadoop和关系型数据库中的数据相互转移的工具,可以将一个关系型数据库(例如 : MySQL ,Oracle ,Postgres等)中的数据导入到Hadoop的HDFS中,也可以将HDFS的数据导入到关系型数据库中。官网:http://sqoop.apache.org/
一个1.4.6 版本,一个是1.99版本(开发版还没有迁移完成,生产环境不推荐)
文档:
http://sqoop.apache.org/docs/1.4.6/
2,安装
参考之前的文章hadoop和hive用的是2.0,所以sqoop这里也用2.0,不过是alpha。直接解压缩。配置环境变量
[code]export J***A_HOME=/usr/java/default export CLASS_PATH=$J***A_HOME/lib export PATH=$J***A_HOME/bin:$PATH export HADOOP_HOME=/data/hadoop export PATH=$HADOOP_HOME/bin:$PATH export HIVE_HOME=/data/apache-hive export PATH=$HIVE_HOME/bin:$PATH export SQOOP_HOME=/data/sqoop export PATH=$SQOOP_HOME/bin:$PATH
sqoop启动的时候会检查hbase环境变量,不需要,直接注释
/data/sqoop/bin/configure-sqoop 的128 行到 147行。
[code] 128 ## Moved to be a runtime check in sqoop. 129 #if [ ! -d "${HBASE_HOME}" ]; then 130 # echo "Warning: $HBASE_HOME does not exist! HBase imports will fail." 131 # echo 'Please set $HBASE_HOME to the root of your HBase installation.' 132 #fi 133 134 ## Moved to be a runtime check in sqoop. 135 #if [ ! -d "${HCAT_HOME}" ]; then 136 # echo "Warning: $HCAT_HOME does not exist! HCatalog jobs will fail." 137 # echo 'Please set $HCAT_HOME to the root of your HCatalog installation.' 138 #fi 139 140 #if [ ! -d "${ACCUMULO_HOME}" ]; then 141 # echo "Warning: $ACCUMULO_HOME does not exist! Accumulo imports will fail." 142 # echo 'Please set $ACCUMULO_HOME to the root of your Accumulo installation.' 143 #fi 144 #if [ ! -d "${ZOOKEEPER_HOME}" ]; then 145 # echo "Warning: $ZOOKEEPER_HOME does not exist! Accumulo imports will fail." 146 # echo 'Please set $ZOOKEEPER_HOME to the root of your Zookeeper installation.' 147 #fi
hadoop和hive参考之前写的博客:
/article/1665643.html
sqoop命令主要就分为数据导入到hadoop,和数据从hadoop导出到 mysql。
首先创建mysql数据库blog,创建一个hadoop用户操作blog库,blog库里面创建一个msg表,插入6条记录
[code]CREATE DATABASE blog DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; GRANT ALL PRIVILEGES ON blog.* TO sqoop@"%" IDENTIFIED BY "sqoop"; FLUSH PRIVILEGES; ##创建msg和 msg_hive数据表: CREATE TABLE `msg_hive` ( `id` bigint(20) NOT NULL, `gid` bigint(20) DEFAULT NULL , `content` varchar(4000), `create_time` datetime DEFAULT NULL COMMENT '创建时间', PRIMARY KEY (`id`,`gid`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 PARTITION BY KEY(`gid`); CREATE TABLE `msg` ( `id` bigint(20) NOT NULL, `gid` bigint(20) DEFAULT NULL , `content` varchar(4000), `create_time` datetime DEFAULT NULL COMMENT '创建时间', PRIMARY KEY (`id`,`gid`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 PARTITION BY KEY(`gid`); #插入测试数据。 insert into `msg`(id,gid,content,create_time) values(1,1,'zhang san 11',now()); insert into `msg`(id,gid,content,create_time) values(1,2,'zhang san 22',now()); insert into `msg`(id,gid,content,create_time) values(1,3,'zhang san 33',now()); insert into `msg`(id,gid,content,create_time) values(2,1,'li si 11',now()); insert into `msg`(id,gid,content,create_time) values(2,2,'li si 22',now()); insert into `msg`(id,gid,content,create_time) values(2,3,'li si 33',now());
3,sqoop使用,导入,导出
首先测试下数据库连接执行select。[code]sqoop eval --connect jdbc:mysql://127.0.0.1:3306/blog --username sqoop --password sqoop --query 'select now()' ##执行结果: ----------------------- | now() | ----------------------- | 2015-08-18 17:22:26.0 | -----------------------
将mysql 数据导入到hive中,其实就是导入到hadoop上面,这里需要指定输出目录是hive的warehouse目录:
[code]sqoop import --direct --connect jdbc:mysql://127.0.0.1:3306/blog --username sqoop --password sqoop --table msg \ --fields-terminated-by "\001" --lines-terminated-by "\n" --delete-target-dir --null-string '\\N' --null-non-string '\\N' --target-dir /user/hive/warehouse/msg
参数一大堆,设置分隔符的,设置null的。最后指定hive的warehouse目录。
但是这样hive还不识别这个表,必须在hive中创建下。
[code]sqoop create-hive-table --hive-table msg --connect jdbc:mysql://127.0.0.1:3306/blog --username sqoop --password sqoop --table msg
将hive 数据导入到 mysql中。
[code]sqoop export --direct --connect jdbc:mysql://127.0.0.1:3306/blog --username sqoop --password sqoop --table msg_hive \ --fields-terminated-by "\001" --lines-terminated-by "\n" --export-dir /user/hive/warehouse/msg
同样的需要配置export-dir,配置mysql的数据表msg_hive
检查结果:(分别在hive和mysql中查看数据)
[code]hive> select * from msg; OK 1 1 zhang san 11 2015-08-17 12:11:32 1 2 zhang san 22 2015-08-17 12:11:33 1 3 zhang san 33 2015-08-17 12:11:33 2 1 li si 11 2015-08-17 12:11:33 2 2 li si 22 2015-08-17 12:11:33 2 3 li si 33 2015-08-17 12:11:33 Time taken: 0.105 seconds, Fetched: 6 row(s) mysql> select * from msg_hive; +----+-----+--------------+---------------------+ | id | gid | content | create_time | +----+-----+--------------+---------------------+ | 2 | 1 | li si 11 | 2015-08-17 12:11:33 | | 2 | 2 | li si 22 | 2015-08-17 12:11:33 | | 2 | 3 | li si 33 | 2015-08-17 12:11:33 | | 1 | 3 | zhang san 33 | 2015-08-17 12:11:33 | | 1 | 1 | zhang san 11 | 2015-08-17 12:11:32 | | 1 | 2 | zhang san 22 | 2015-08-17 12:11:33 | +----+-----+--------------+---------------------+ 6 rows in set (0.00 sec)
4,总结
本文的原文连接是: /article/1665642.html 未经博主允许不得转载。sqoop提供了import和export功能,很方便的就可以迁移mysql和hive的数据。
业务数据需要迁移到hadoop上面进行计算,同时,将计算结果放到mysql数据库中进行统计显示。
数据可以方便的流动。
参考:
http://segmentfault.com/a/1190000002532293
写的参数很详细。
相关文章推荐
- Linux NTP配置详解 (Network Time Protocol)
- 利用Nginx_Resin_tomcat为网站分流
- Linux顶层目录下的Makefile作用解析
- Linux系统中对SSD硬盘优化的方法
- vmware下给linux添加硬盘
- 常用数码测评网站
- PAT 1044. Shopping in Mars (25)
- openerp/odoo权限设置总结
- Linux下rpm方式安装mysql(密码修改,远程登录)
- 初中高级LINUX运维所需具备技能
- 基于zabbix API添加监控主机
- linux总结应用之二
- Tomcat版本与Servlet、JSP等版本的支持关系
- Linux NFS服务器的安装与配置
- Hadoop/Hive简介
- tomcat报错Exception loading sessions from persistent storage解决方案
- linux系统下sublime text编译运行c++程序的配置文件
- tomcat数据库边接池配置
- tomcat 启动时 报 UnknownHostException
- 嵌入式 linux 查看内存