首页 > 大数据 > 数据分析 > sqoop数据导入导出
sqoop:
1.配置
配置conf/sqoop-env-templete.sh,修改 HADOOP_COMMON_HOME、HADOOP_MAPRED_HOME、HIVE_HOME、HBASE_HOME
拷贝相应的jdbc驱动:例如,如果是mysql,则将mysqljdbc驱动拷贝到lib/
也可以直接配置HADOOP_HOME 这样 会根据hadoop_home寻找 HADOOP_COMMON_HOME、HADOOP_MAPRED_HOME
2.import导出数据到hdfs
./sqoop import --connect jdbc:mysql://192.168.249.133:3306/test --username root --password 123456 --table test --input-fields-terminated-by ',' --lines-terminated-by '\n' --warehouse-dir /sqoop -m 1
通过select语句指定特定的输出目录:
./sqoop import --connect jdbc:mysql://192.168.249.133:3306/test --username root --password 123456 --input-fields-terminated-by ',' --lines-terminated-by '\n' --target-dir /sqoop/test1 --query 'select *from test where id>2 and $CONDITIONS ;' -m 1
通过where指定查找条件:
./sqoop import --connect jdbc:mysql://192.168.249.133:3306/test --username root --password 123456 --input-fields-terminated-by ',' --lines-terminated-by '\n' --target-dir /sqoop/test2 --table test --where 'id<=3' -m 1
3.导出数据到hive(不用建表,自动生成目标表)
自动生成目标表
./sqoop import --connect jdbc:mysql://192.168.249.133:3306/test --username root --password 123456 --hive-import --hive-database test --create-hive-table --input-fields-terminated-by ',' --lines-terminated-by '\n' --table test --where 'id<=3' -m 1
1.这个时候可能会报错:Could not load org.apache.hadoop.hive.conf.HiveConf.
将hive lib/*添加到HADOOP_CLASSPATH 下
2.main ERROR Could not register mbeans java.security.AccessControlException: access denied ("javax.management.MBeanTrustPermission" "register")
解决方法:
Just add the following lines to your java.policy file unter <JRE_HOME>/lib/security.
grant {
// JMX Java Management eXtensions
permission javax.management.MBeanTrustPermission "register";
};
./sqoop import --connect jdbc:mysql://192.168.249.133:3306/test --username root --password 123456 --hive-import --hive-database test --input-fields-terminated-by ',' --lines-terminated-by '\n' --table test --where 'id<=3' -m 1
注:split-by id maptask根据id分片 与-m 相关
增量导入:
./sqoop import --connect jdbc:mysql://192.168.249.133:3306/test --username root --password 123456 --hive-import --hive-database test --input-fields-terminated-by ',' --lines-terminated-by '\n' --incremental append --check-column id --last-value 3 --table test -m 1
3.export从hdfs导出数据
./sqoop export --connect jdbc:mysql://192.168.249.133:3306/test --username root --password 123456 --export-dir /user/hive/warehouse/test.db/test --table test --fields-terminated-by ',' --lines-terminated-by '\n'
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/31506529/viewspace-2213877/ ,如需转载,请注明出处,否则将追究法律责任。