最近在用flume和sqoop来做非关系数据(日志)和关系数据(MYSQL)迁移到hdfs的工作,简单记录下使用过程,以此总结
一 flume的使用
使用flume把web的log日志数据导入到hdfs上
步骤
1 在 elephant 节点上
先安装flume sudo yum install --assumeyes flume-ng
2 创建配置文件
vi /etc/hadoop/conf/flume-conf.properties
tail1.sources = src1
tail1.channels = ch1
tail1.sinks = sink1
tail1.sources.src1.type = exec
tail1.sources.src1.command = tail -F /tmp/access_log
tail1.sources.src1.channels = ch1
tail1.channels.ch1.type = memory
tail1.channels.ch1.capacity = 500
tail1.sinks.sink1.type = avro
tail1.sinks.sink1.hostname = localhost
tail1.sinks.sink1.port = 6000
tail1.sinks.sink1.batch-size = 1
tail1.sinks.sink1.channel = ch1
##
collector1.sources = src1
collector1.channels = ch1
collector1.sinks = sink1
collector1.sources.src1.type = avro
collector1.sources.src1.bind = localhost
collector1.sources.src1.port = 6000
collector1.sources.src1.channels = ch1
collector1.channels.ch1.type = memory
collector1.channels.ch1.capacity = 500
collector1.sinks.sink1.type = hdfs
collector1.sinks.sink1.hdfs.path = flume/collector1
collector1.sinks.sink1.hdfs.filePrefix = access_log
collector1.sinks.sink1.channel = ch1
配置文件说明结构是
src取日志数据,通过内存传送到本地以avro文件格式保存,做中转,然后从avro文件,通过内存传送到hdfs上。hdfs保存路径是flume/collector1,
3 在hfds上创建保存目录
hadoop fs -mkdir -p flume/collector1
4 模拟产生大量日志文件,在log目录中
$ accesslog-gen.bash /tmp/access_log
5 启动日志收集器
flume-ng agent --conf /etc/hadoop/conf/ \
--conf-file /etc/hadoop/conf/flume-conf.properties \
--name collector1
6 启动日志采集器
$ flume-ng agent \
--conf-file /etc/hadoop/conf/flume-conf.properties \
--name tail1
二 sqoop的使用
使用sqoop把mysql中的表数据导入到hdfs
1安装sqoop
sudo yum install --assumeyes sqoop
2在sqoop的lib下创建一个mysql连接的驱动链接,也就是在sqoop的lib下面能找到mysql的驱动包
就是在/usr/lib/sqoop/lib目录,创建 $ sudo ln -s /usr/share/java/mysql-connector-java.jar /usr/lib/sqoop/lib/
3导入数据
sqoop help
用sqoop查看mysql中有哪些数据库
sqoop list-databases \
--connect jdbc:mysql://localhost \
--username training --password training
再看看库里有哪些表
sqoop list-tables \
--connect jdbc:mysql://localhost/movielens \
--username training --password training
开始导入命令表movie到hdfs,表中字段的数据用tab分割
sqoop import \
--connect jdbc:mysql://localhost/movielens \
--table movie --fields-terminated-by '\t' \
--username training --password training
4验证
hadoop fs -ls movie
hadoop fs -tail movie/part-m-00000
可以看到数据已文件形式保存到hdfs