定位:数据的搬运工 Sqoop 是hdfs/hive/hbase与传统RDBMS之间进行数据转换的专业化工具 select > 文件(sql.txt) hdfs dfs -put sql.txt dest_path 将数据从rdbms中转移到hdfs的这个过程,称之为导入import 反之为导出export 注意:Sqoop的底层就是MapReduce DbInputFormat DbOutputFormat 安装配置: 约定:安装目录为/opt/ 下载地址:https://mirrors.tuna./apache/sqoop/1.4.6/ 下载之后进行解压: tar -zxvf sqoop-1.4.6.bin__hadoop-2.0.4-alpha.tar.gz -C /opt/ 重命名: mv sqoop-1.4.6.bin__hadoop-2.0.4-alpha sqoop 配置SQOOP_HOME到环境变量中 export SQOOP_HOME=/opt/sqoop 配置$SQOOP_HOME/conf/sqoop-env.sh export HADOOP_COMMON_HOME=/opt/hadoop export HBASE_HOME=/opt/hbase export HIVE_HOME=/opt/hive export ZOOCFGDIR=/opt/zookeeper/conf 注意: 1、数据库驱动: 在执行sqoop命里的受需要拷贝相关数据库驱动jar包到$SQOOP_HOME/lib目录下,例如mysql需要mysql-connector-java-5.1.32-bin.jar以上版本支持。 2、JDK版本 JDK版本最好1.7以上。 Import 1、将mysql中的people中的数据迁移到hdfs中 sqoop import --connect jdbc:mysql://192.168.43.1:3306/test --username root --password root --table people 2、强制将现有数据导入到目录已经存在的hdfs中--->--delete-target-dir sqoop import --connect jdbc:mysql://192.168.43.1:3306/test --username root --password root --table people --delete-target-dir 3、在hdfs现有数据的基础之上进行数据的追加--->--append sqoop import --connect jdbc:mysql://192.168.43.1:3306/test --username root --password root --table people --append 4、将mysql中的数据导入到指定的hdfs路径中 sqoop import --connect jdbc:mysql://192.168.43.1:3306/test --username root --password root --table people --target-dir hdfs://ns1/input/sqoop/people 5、指定sqoop命令在运行期间的map的个数--->-m n(具体的数字) sqoop import --connect jdbc:mysql://192.168.43.1:3306/test --username root --password root --table people --target-dir hdfs://ns1/input/sqoop/people -m 1 6、按照指定的条件导入hdfs--->--where "条件" 年龄小于40的同学 sqoop import --connect jdbc:mysql://192.168.43.1:3306/test --username root --password root --table people --target-dir hdfs://ns1/input/sqoop/people -m 1 --where "age < 40" 7、使用sql统计的结果导入hdfs--->--query "sql语句" 注意:既然使用sql语句,就不要再使用--table 重复 sqoop import --connect jdbc:mysql://192.168.43.1:3306/test --username root --password root --target-dir hdfs://ns1/input/sqoop/people -m 1 --query "SELECT id, name, age, height FROM people where age > 40 and \$CONDITIONS" --fields-terminated-by "," --split-by "," 异常问题:ERROR tool.ImportTool: Error during import: No primary key could be found for table 在使用import进行导入的时候,mysql中的表必须要有主键 Export 需要注意的就是: 1、中文 在url中加上useUnicode=true&characterEncoding=utf-8 2、分隔符的问题(mysql操作中默认的分隔符是",") --input-fields-terminated-by '\001' sqoop export --connect "jdbc:mysql://172.16.160.246:3306/test?useUnicode=true&characterEncoding=utf-8" --username root --password root --table people --export-dir /user/hive/warehouse/people --input-fields-terminated-by '\001' 作业: 学习shell脚本的基本使用,完成以下任务: 每天向hive的分区表中使用sqoop导入一批数据 mysql中表:people() CREATE TABLE `people` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(20) COLLATE utf8_bin NOT NULL, `age` int(11) DEFAULT NULL, `height` double(5,2) DEFAULT NULL, `data_date` date DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 COLLATE=utf8_bin hive中的表: create table people( id int, name string, age int, height double, ) partitioned by (dt date) fields terminated by ','; 每天通过sqoop从mysql中获取昨天所有数据,加载hive表people对应的日期分区中 Flume 做海量日志数据的分布式采集,cloudera(CDH)共享到apache linux的快捷键: ctrl+A 跳到行首 ctrl+E 跳到行尾 ctrl+B 向前移动一位光标 ctrl+U 删除光标前面的命令 ctrl+K 删除光标后面的命令 ctrl+r 搜索历史命令 |
|