首先简单说下sqoop的安装:
准备sqoop的安装包sqoop-1.4.5.bin__hadoop-0.23.tar.gz和MySQL的驱动mysql-connector-java-5.1.43-bin.jar。
安装包上传至Linux服务器。
解压sqoop-1.4.5.bin__hadoop-0.23.tar.gz后配置sqoop的环境变量。
修改sqoop的sqoop-env.sh的配置:
# See the License for the specific language governing permissions and # limitations under the License.
# included in all the hadoop scripts with source command # should not be executable directly # also should not be passed any arguments, since we need original $*
# Set Hadoop-specific environment variables here.
#Set path to where bin/hadoop is available export HADOOP_COMMON_HOME=/home/plus/hadoop/hadoop-2.7.3
#Set path to where hadoop-*-core.jar is available export HADOOP_MAPRED_HOME=/home/plus/hadoop/hadoop-2.7.3
#set the path to where bin/hbase is available #export HBASE_HOME=
#Set the path to where bin/hive is available export HIVE_HOME=/home/plus/hadoop/apache-hive-1.2.1-bin
#Set the path for where zookeper config dir is export ZOOCFGDIR=/home/plus/hadoop/zookeeper-3.4.6/conf |
将MySQL驱动放置到sqoop的lib目录里面。
sqoop将MySQL数据导入hive:
sqoop import --connect jdbc:mysql://plus111:3306/test --username root --password 123456 --table Persons -m 1 --hive-import --hive-table aa --fields-terminated-by '\t' |
--connect jdbc:mysql://plus111:3306/testjdbc连接MySQL,指定MySQL的ip和端口和数据库
--usernameMySQL的用户名
--passwordMySQL的密码
--tableMySQL的表
-m 1使用一个map
--hive-import数据导入hive
--hive-tablehive的表
--fields-terminated-byhive表的分隔符
sqoop将hive数据导出的MySQL:
sqoop-export --connect jdbc:mysql://plus111:3306/test --username root --password 123456 --table cs --export-dir /user/hive/warehouse/aa/part-m-00000 --input-fields-terminated-by '\t' |
--export-dir导出hive的表在hdfs中的绝对路径
--input-fields-terminated-by 指定分隔符
这样导出的数据里面没有中文的话是没有任何问题的,但是如果数据里面有中文会出现下面的问题:
MySQL没有识别数据中的中文,从而显示的是“?”
分析产生的原因:这是由于MySQL的编码和hive的编码不统一造成的。
解决方法:
查看MySQL字符集
show variables like 'character%'; 如果database和server都采用了latin1编码,不支持中文,我们需要把它修改为utf-8。 |
修改全局字符
set character_set_connection=utf8; set character_set_database=utf8; set character_set_results=utf8; set character_set_server=utf8; set character_set_system=utf8; set collation_connection=utf8; set collation_database=utf8; set collation_server=utf8;
|
导出的命令换以下的命令,即可解决中文乱码!
sqoop-export --connect "jdbc:mysql://plus111:3306/test?useUnicode=true&characterEncoding=utf-8" --username root --password 123456 --table rr --export-dir /user/hive/warehouse/qq/test.txt --input-fields-terminated-by '\t' |
VIP解答服务的流程:
报名VIP学习之后,通过售后老师加上解答微信或者QQ。
学习过程中遇到问题,在解答微信或者解答QQ进行文字,语音,图片等形式发来你的问题。
解答时间:每天的中午13:30到晚上的11:00,其余时间有问题的同学留言,看到会直接回复的。
解答方式:文字,语音,图片,远程操作你的电脑等方式。