上一篇文章写到flume实时抽取mysql数据到kafka和hdfs,但是之前没有考虑到在hdfs是在HA模式下的情况,如果在HA模式下,我们指定了写入地址为: hdfs://cdh2:8020/flume/oracle/topic/test_%Y%m%d,当cdh2是Active状态下是没有问题的,但是当cdh2变成Standby状态后,则数据无法正常写入;为了解决这种问题,我们在flume写入到hdfs时,地址写hdfs的nameservices;实现方式如下:

一,把hadoop的配置文件hdfs-site.xml,和core-site.xml复制到flume的conf目录下;

由于我的集群是CDH管控的,所以hadoop的配置文件,是存储在/etc/hadoop/conf/目录下的,不在hadoop的安装目录下,我复制如下:

cp /etc/hadoop/conf/hdfs-site.xml /opt/cloudera/parcels/CDH/lib/flume-ng/conf/
cp /etc/hadoop/conf/core-site.xml /opt/cloudera/parcels/CDH/lib/flume-ng/conf/

注:如果flume没有安装在hadoop所在的集群内,记得把上述两个配置文件移动到flume安装主机所在的conf目录下

 

二,配置flume的相关配置文件

a1.channels = ch-1
 a1.sources = src-1
 a1.sinks = HDFS
 ###########sql source#################
 # For each one of the sources, the type is defined
 a1.sources.src-1.type = org.keedio.flume.source.SQLSource
 a1.sources.src-1.hibernate.connection.url = jdbc:mysql://10.1.40.104:3306/ibrain
 # Hibernate Database connection properties
 a1.sources.src-1.hibernate.connection.user = root
 a1.sources.src-1.hibernate.connection.password = root
 #这个参数很重要,默认false,如果设为false就不会自动查询
 a1.sources.src-1.hibernate.connection.autocommit = true
 #声明mysql的hibernate方言
 a1.sources.src-1.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
 #声明mysql驱动
 a1.sources.src-1.hibernate.connection.driver_class = com.mysql.jdbc.Driver
 #查询间隔,单位毫秒
 a1.sources.src-1.run.query.delay=5000
 a1.sources.src-1.hibernate.columns.to.select = *
 #表里面的某个字段,用来判断增量
 a1.sources.src-1.hibernate.incremental.column.name =submit_time
 #声明保存flume状态的文件夹位置
 a1.sources.src-1.status.file.path = /opt/flume-test/#声明保存flume状态的文件位置
 a1.sources.src-1.status.file.name = sqlT.status
 # Custom query
 #第一次启动时的初始值,第二次启动时则去读取状态文件中的值
 a1.sources.src-1.start.from = 2012-07-28 00:00:0.0000000#sql语句自定义,但是要注意:增量只能针对查询字段的第一个字段,如下面的SUBMIT_TIME,经测试系统默认如此.
 #$@$表示增量列上一次查询的值,记录在status文件中
 #查询sql不能加";",不然会报错
 a1.sources.src-1.custom.query = select SUBMIT_TIME,ID,ENTRANCE_GUARD_ID,ENTRANCE_GUARD_TYPE,ENTRANCE_GUARD_STATUS,ID_CARD,NAME,EXAM_SITE_ID,FACE_IDENTIFY_RESULT,FACE_IDENTIFY_MESSAGE,FACE_BASE64,ENTRY_TYPE from T_ZCKJ_MJ_MJSJ  where  SUBMIT_TIME > to_timestamp('$@$','yyyy-mm-dd hh24:mi:ss.ff6')#设置分批参数
 a1.sources.src-1.batch.size = 1000
 a1.sources.src-1.max.rows = 1000#设置c3p0连接池参数
 a1.sources.src-1.hibernate.connection.provider_class = org.hibernate.connection.C3P0ConnectionProvider
 a1.sources.src-1.hibernate.c3p0.min_size=1
 a1.sources.src-1.hibernate.c3p0.max_size=10################################################################
 #设置通道为内存模式
 a1.channels.ch-1.type = memory
 a1.channels.ch-1.capacity = 1000
 a1.channels.ch-1.transactionCapacity = 1000
 a1.channels.ch-1.byteCapacityBufferPercentage = 20
 a1.channels.ch-1.byteCapacity = 68435456
 a1.channels.ch-1.keep-alive = 60
 a1.channels.ch-1.capacity = 1000000#配置hdfs sink    数据持久化备份
 a1.sinks.HDFS.type = hdfs
 #/**存放数据的hdfs目录*/
 a1.sinks.HDFS.hdfs.path = hdfs://nameserviceBackup/flume/oracle/test_topic1/test_%Y%m%d
 a1.sinks.HDFS.hdfs.filePrefix=prefix_%Y%m%d
 a1.sinks.HDFS.hdfs.fileType = DataStream
 a1.sinks.HDFS.hdfs.writeFormat = Text
 #设置文件存储数据多大的时候生成下一个文件,建议设置成128M和块大小相同
 a1.sinks.HDFS.hdfs.rollSize = 268435456
 #设置滚动时间,每隔多少时间生成一个文件.如果设置成0,则禁止滚动,可以使所有数据被写到一个文件中.单位是s
 a1.sinks.HDFS.hdfs.rollInterval = 3600
 #设置文件多少行时,滚动生成下一个文件,设置成0时禁止滚动
 a1.sinks.HDFS.hdfs.rollCount = 0#绑定sources sinks
 a1.sources.src-1.channels=ch-1
 a1.sinks.HDFS.channel = ch-1注:由于我的hdfs的nameservices为nameserviceBackup
三,启动程序
bin/flume-ng agent \
 -c conf \
 -n a1 \           #--配置文件中agent的名称
 -f /opt/flume.conf \
 -Dflume.root.logger=DEBUG,console

注:由于我上述的配置文件放在的目录为:/opt/flume.conf