flume使用实例

1、监听端口a1.sources.r1.type = netcat

配置文件nc-flume-console.conf

# Name the components on this agent a1 表示jvm进程名

a1.sources = r1

a1.sinks = k1

a1.channels = c1

# Describe/configure the source

a1.sources.r1.type = netcat

a1.sources.r1.bind = node1

a1.sources.r1.port = 44444

# Describe the sink

a1.sinks.k1.type = logger

# Use a channel which buffers events in memory

a1.channels.c1.type = memory

a1.channels.c1.capacity = 1000 #1000个event

a1.channels.c1.transactionCapacity = 100

# Bind the source and sink to the channel

a1.sources.r1.channels = c1

a1.sinks.k1.channel = c1

flume-ng agent -n a1 -c conf/ -f /export/server/flume/job/nc-flume-console.conf

 参数-n 表示jvm进程名 -c表示本次启动读取的配置文件conf目录下的文件 -f 表示具体执行的文件

另开窗口输入内容后控制台会自动返回OK

2、实时监控单个追加文件

配置文件 flume-exec-logger.conf

#Agent_name

a1.sources = r1

a1.sinks = k1

a1.channels = c1

#Sources

a1.sources.r1.type = exec

a1.sources.r1.command = tail -F /export/server/hive/logs/hive.log

#Channel

a1.channels.c1.type = memory

a1.channels.c1.capacity = 1000

a1.channels.c1.transactionCapacity = 100

#sinks

a1.sinks.k1.type = logger

#组合

a1.sources.r1.channels = c1

a1.sinks.k1.channel = c1

启动flume监听

flume-ng agent -c conf/ -f job/flume-exec-logger.conf -n a1

手动追加数据到hive.log文件 并查看监控窗口

echo INFO [main] spark.HiveSparkClientFactory >> logs/hive.log

动态添加数据到hive.log

连接hive 观察flume监控变化

 beeline -u jdbc:hive2://node1:10000 -n ljr

 show databases;

  由此可见当我们操作hive的时候 hive.log 就更新,由于我们监控了hive.log文件所以当有新数据追加到hive.log的时候 就会监听到 并打印到控制台

3、实时监控单个追加文件,并将数据输出到hdfs

配置文件 flume-hivelogs-hdfs.con

# Name the components on this agent

a2.sources = r2

a2.sinks = k2

a2.channels = c2

# Describe/configure the source

a2.sources.r2.type = exec

a2.sources.r2.command = tail -F /export/server/hive/logs/hive.log

# Describe the sink

a2.sinks.k2.type = hdfs

a2.sinks.k2.hdfs.path = hdfs://node1:8020/flume/%Y%m%d/%H

#上传文件的前缀

a2.sinks.k2.hdfs.filePrefix = logs-

#是否按照时间滚动文件夹

a2.sinks.k2.hdfs.round = true

#多少时间单位创建一个新的文件夹

a2.sinks.k2.hdfs.roundValue = 1

#重新定义时间单位

a2.sinks.k2.hdfs.roundUnit = hour

#是否使用本地时间戳

a2.sinks.k2.hdfs.useLocalTimeStamp = true

#积攒多少个 Event 才 flush 到 HDFS 一次

a2.sinks.k2.hdfs.batchSize = 100

#设置文件类型,可支持压缩

a2.sinks.k2.hdfs.fileType = DataStream

#多久生成一个新的文件

a2.sinks.k2.hdfs.rollInterval = 60

#设置每个文件的滚动大小

a2.sinks.k2.hdfs.rollSize = 134217700

#文件的滚动与 Event 数量无关

a2.sinks.k2.hdfs.rollCount = 0

# Use a channel which buffers events in memory

a2.channels.c2.type = memory

a2.channels.c2.capacity = 1000

a2.channels.c2.transactionCapacity = 100

# Bind the source and sink to the channel

a2.sources.r2.channels = c2

a2.sinks.k2.channel = c2

启动flume监听,操作hive

flume-ng agent -n a2 -c conf/ -f flume-hivelogs-hdfs.con

查看hdfs,有新文件产生

使用 Flume 监听整个目录(a3.sources.r3.type = TAILDIR)

的实时追加文件,并上传至 HDFS

实现步骤:

【1】创建被监控目录

我这里监控data目录  此目录需要提前创建

mkdir data

cd data

touch file1.txt

touch file2.txt

touch log2.txt

toch log1.txt

【2】创建文件 flume-taildir-hdfs.conf

a3.sources = r3

a3.sinks = k3

a3.channels = c3

# Describe/configure the source

a3.sources.r3.type = TAILDIR

#记录最后监控文件的断点的文件,此文件位置可不改

a3.sources.r3.positionFile =  /export/server/flume/data /tail_dir.json

a3.sources.r3.filegroups = f1 f2

a3.sources.r3.filegroups.f1 = /export/server/flume/data/.*file.*

a3.sources.r3.filegroups.f2 =/export/server/flume/data/.*log.*

# Describe the sink

a3.sinks.k3.type = hdfs

# hdfs://node1:8020 可省略

a3.sinks.k3.hdfs.path = hdfs://node1:8020/flume/upload2/%Y%m%d/%H

#上传文件的前缀

a3.sinks.k3.hdfs.filePrefix = upload-

#是否按照时间滚动文件夹

a3.sinks.k3.hdfs.round = true

#多少时间单位创建一个新的文件夹

a3.sinks.k3.hdfs.roundValue = 1

#重新定义时间单位

a3.sinks.k3.hdfs.roundUnit = hour

#是否使用本地时间戳

a3.sinks.k3.hdfs.useLocalTimeStamp = true

#积攒多少个 Event 才 flush 到 HDFS 一次

a3.sinks.k3.hdfs.batchSize = 100

#设置文件类型,可支持压缩

a3.sinks.k3.hdfs.fileType = DataStream

#多久生成一个新的文件,单位是秒

a3.sinks.k3.hdfs.rollInterval = 3600

#设置每个文件的滚动大小大概是 128M,单位是byte

a3.sinks.k3.hdfs.rollSize = 134217700

#文件的滚动与 Event 数量无关

a3.sinks.k3.hdfs.rollCount = 0

# Use a channel which buffers events in memory

a3.channels.c3.type = memory

a3.channels.c3.capacity = 1000

a3.channels.c3.transactionCapacity = 100

# Bind the source and sink to the channel

a3.sources.r3.channels = c3

a3.sinks.k3.channel = c3

【3】启动flume监控

        bin/flume-ng agent -c conf -f datas/flume-taildir-hdfs.conf -n a3

【4】向文件中追加内容

  • 26
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 使用Flume采集某个接口的代码,需要以下步骤: 1. 安装Flume:请按照Flume的官方文档进行安装。 2. 配置Flume:在Flume的配置文件中,指定采集源、汇聚点、输出点等信息。 3. 编写采集代码:需要使用Flume的Java API,来连接到接口并采集数据。 以下是一个示例代码: ``` import org.apache.flume.*; import org.apache.flume.conf.Configurable; import org.apache.flume.source.AbstractSource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class HttpSource extends AbstractSource implements Configurable, PollableSource { private static final Logger logger = LoggerFactory.getLogger(HttpSource.class); private String urlStr; @Override public void configure(Context context) { urlStr = context.getString("url"); if (urlStr == null) { throw new ConfigurationException("HttpSource: url must be specified."); } } @Override public Status process() throws EventDeliveryException { Status result = Status.READY; HttpURLConnection conn = null; try { URL url = new URL(urlStr); conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setDoOutput(true); conn.setDoInput(true); conn.setUseCaches(false); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); conn.connect(); BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line; while ((line = reader.readLine()) != null) { Event event = EventBuilder.withBody(line.getBytes()); getChannelProcessor().processEvent(event); } } catch (Exception e) { logger.error("HttpSource process error.", e); result = Status.BACKOFF; } finally { if (conn != null) { conn.disconnect(); } } return result; } @Override public long getBackOffSleepIncrement() { return 0; } @Override public long getMaxBackOff ### 回答2: 使用Flume采集某个接口的代码写法如下: 首先,需要安装和配置Flume,确保Flume的相关环境已经设置好。 1. 创建一个Flume配置文件:例如,命名为`flume_config.conf`。 2. 在配置文件中定义一个数据源,通常使用HTTP Source来获取接口数据。配置示例如下: ``` # 配置Source sourceAgent.sources = httpSource sourceAgent.sources.httpSource.type = org.apache.flume.source.http.HTTPSource sourceAgent.sources.httpSource.bind = 0.0.0.0 sourceAgent.sources.httpSource.port = <监听端口> ``` 3. 配置Sink,将获取的数据传输到相应的目标位置,这里可以选择将数据写入到文件、Kafka、HDFS等。以下是写入到文件Sink的示例配置: ``` # 配置Sink sourceAgent.sinks = fileSink sourceAgent.sinks.fileSink.type = hdfs sourceAgent.sinks.fileSink.hdfs.path = <目标文件路径> ``` 4. 配置Channel,用于在Source和Sink之间缓存接收到的数据。 ``` # 配置Channel sourceAgent.channels = memoryChannel sourceAgent.channels.memoryChannel.type = memory sourceAgent.channels.memoryChannel.capacity = <缓存容量> sourceAgent.channels.memoryChannel.transactionCapacity = <事务容量> ``` 5. 将Source和Sink以及Channel进行连接: ``` # 将Source与Sink以及Channel连接 sourceAgent.sources.httpSource.channels = memoryChannel sourceAgent.sinks.fileSink.channel = memoryChannel ``` 6. 启动Flume agent,并指定配置文件路径: ``` $ bin/flume-ng agent --conf conf --conf-file <配置文件路径> --name sourceAgent -Dflume.root.logger=INFO,console ``` 以上就是通过Flume采集某个接口的代码编写步骤。根据实际需求,需要根据具体情况调整配置文件中的参数和定义更多的Source、Sink和Channel。 ### 回答3: 使用Flume采集某个接口的代码可以按照以下步骤进行编写: 1. 引入所需的Flume依赖库,如Flume的核心库、日志库等。 2. 创建一个Flume配置文件,其中指定数据源、数据目的地和数据传输方式等配置项。 3. 在Flume配置文件中定义数据源,可以使用Flume提供的HTTP Source组件,通过监听特定端口获取接口数据。 4. 在Flume配置文件中定义数据目的地,可以是本地文件、HDFS、Kafka等存储方式,根据需求选择适合的目的地。 5. 在Flume配置文件中定义数据传输方式,可以是使用Flume提供的Sink组件,将数据传输到目的地。 6. 根据Flume配置文件的配置,编写Java代码,创建一个Flume Agent实例,并将配置文件路径作为参数传递。 7. 启动Flume Agent,它将根据配置文件的定义,采集特定接口的数据,并将其传输到指定的目的地。 8. 监控Flume Agent的采集情况和数据传输情况,根据需要进行日志记录、错误处理等操作。 需要注意以下几点: - 确保Flume依赖库正确引入,并且版本兼容。 - 需要根据具体接口的数据格式和传输要求,进行Flume配置文件的编写。可以参考Flume官方文档进行配置项的定义。 - 在编写Java代码时,应根据Flume的API文档,了解如何创建Flume Agent实例、启动Agent等操作。 - 在启动Flume Agent之前,要确保接口服务正常运行,并且Flume所监听的端口没有被其他程序占用。 以上是一个基本的框架,根据具体需求和接口特点,还可以进行配置文件的其他定制化设置,如添加过滤器、数据转换等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值