【Flume-Demo】

案例一:使用telnet给flume发送信息,并把发送的信息以日志的形式打印在控制台上

1. 配置文件

# agent的三个组件
a1.sources = r1
a1.sinks = k1
a1.channels = c1

# sources配置
a1.sources.r1.type = netcat
a1.sources.r1.bind = node1
a1.sources.r1.port = 44444    

# sinks配置
a1.sinks.k1.type = logger

# Channels配置
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100

# 连接三个组件
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1

【提示】netcat 瑞士军刀。参考 https://www.jianshu.com/p/cb26a0f6c622

2. 启动Flume

$FLUME_HOME/bin/flume-ng agent \
--name a1 \
--conf $FLUME_HOME/conf \
--conf-file $FLUME_HOME/conf/example.conf \
-Dflume.root.logger=INFO,console

#$FLUME_HOME/bin/flume-ng agent  启动flume-ng的agent服务
#--name a1  指定服务名
#--conf $FLUME_HOME/conf  Flume配置文件目录flume-env.sh
#--conf-file $FLUME_HOME/conf/example.conf   我们编写的配置文件的目录
#-Dflume.root.logger=INFO,console   在控制台输出日志

3. 安装telnet服务

    https://blog.youkuaiyun.com/liupeifeng3514/article/details/79686740

 

4. 使用telnet测试

#启动telnet后在控制台输入内容即可
> telnet localhost 44444
Trying 127.0.0.1...
Connected to localhost.localdomain (127.0.0.1).
Escape character is '^]'.
Hello world! 

此时就可以在flume-ng agent的控制台看到如下输出

    12/06/19 15:32:19 INFO source.NetcatSource: Source starting
    12/06/19 15:32:19 INFO source.NetcatSource: Created serverSocket:sun.nio.ch.ServerSocketChannelImpl[/127.0.0.1:44444]
    12/06/19 15:32:34 INFO sink.LoggerSink: Event: { headers:{} body: 48 65 6C 6C 6F 20 77 6F 72 6C 64 21 0D          Hello world!. }

 

5. event

Event: { headers:{} body: 48 65 6C 6C 6F 20 77 6F 72 6C 64 21 0D Hello world!. }
Event是Flume传输数据的基本单元
Event = 可选的header + byte array
 

案例二:读取Hive日志信息,并把结果存储在HDFS上

1. 配置文件

# 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 /tmp/root/hive.log
a2.sources.r2.shell = /bin/bash -c   

# Use a channel which buffers events in memory
a2.channels.c2.type = memory
a2.channels.c2.capacity = 1000
a2.channels.c2.transactionCapacity = 100

# Describe the sink
a2.sinks.k2.type = hdfs
a2.sinks.k2.hdfs.path = hdfs://hadoop-001:9000/logs/%Y%m%d/%H0                                               
#上传文件的前缀
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 = 1000
#设置文件类型,可支持压缩
a2.sinks.k2.hdfs.fileType = DataStream
#多久生成一个新的文件 
a2.sinks.k2.hdfs.rollInterval = 600
#设置每个文件的滚动大小 
a2.sinks.k2.hdfs.rollSize = 134217700
#文件的滚动与Event数量无关
a2.sinks.k2.hdfs.rollCount = 0
#最小冗余数 
a2.sinks.k2.hdfs.minBlockReplicas = 1 


# Bind the source and sink to the channel
a2.sources.r2.channels = c2
a2.sinks.k2.channel = c2

2. 移动hdfs依赖包到flume的lib文件夹中

3.  进入flume根目录,启动

./bin/flume-ng agent --conf ./conf/ --name a2 --conf-file ./conf/flume-file-hdfs.conf

4. HDFS查看结果

 

案例三 Flume整合Kafka

   https://blog.youkuaiyun.com/a_drjiaoda/article/details/85003929

 

### 详细配置Apache Flume的Agent 在使用Apache Flume时,配置一个Agent(如`a1`)需要完成以下内容:创建配置文件、设置Source、Channel和Sink,并通过`flume-ng`命令启动Agent进行测试。 #### 创建配置文件 首先,创建一个名为`first_demo.conf`的配置文件。该文件用于定义Flume Agent的Source、Channel和Sink的具体参数。以下是配置文件的基本结构: ```properties # 定义Agent名称 agent.sources = source1 agent.channels = channel1 agent.sinks = sink1 # 配置Source agent.sources.source1.type = exec agent.sources.source1.command = tail -F /path/to/log/file.log agent.sources.source1.interceptors = i1 agent.sources.source1.interceptors.i1.type = timestamp # 配置Channel agent.channels.channel1.type = memory agent.channels.channel1.capacity = 1000 agent.channels.channel1.transactionCapacity = 100 # 配置Sink agent.sinks.sink1.type = logger agent.sinks.sink1.channel = channel1 ``` 上述配置中: - `source1` 是一个基于命令行的Source,它会监控指定的日志文件并将其作为数据源[^1]。 - `channel1` 是一个内存类型的Channel,容量为1000条事件[^3]。 - `sink1` 是一个Logger类型的Sink,用于将接收到的数据打印到控制台[^2]。 #### 设置Source、Channel和Sink 在Flume中,Source、Channel和Sink是三个核心组件,分别负责数据采集、数据传递和数据输出。 - **Source**:定义数据来源。例如,可以使用`exec`类型从命令行获取数据,或使用`netcat`类型从网络端口接收数据[^4]。 - **Channel**:作为Source和Sink之间的桥梁。常用的有`memory`和`file`两种类型。`memory`速度快但不持久化,`file`则提供持久化支持。 - **Sink**:定义数据输出目标。例如,`logger`用于日志输出,`hdfs`用于写入HDFS,`elasticsearch`用于写入Elasticsearch[^4]。 #### 启动Agent 配置完成后,可以通过以下命令启动Flume Agent: ```bash ./bin/flume-ng agent \ --conf conf/ \ --name a1 \ --conf-file ./first_demo.conf \ -Dflume.root.logger=INFO,console ``` 上述命令中: - `--conf` 指定Flume的配置目录。 - `--name` 指定Agent的名称(如`a1`)。 - `--conf-file` 指定配置文件路径(如`first_demo.conf`)。 - `-Dflume.root.logger` 设置日志级别和输出位置[^1]。 #### 测试与验证 启动后,可以通过观察控制台输出来验证数据是否正确流入并被处理。如果配置了`hdfs`或`elasticsearch`类型的Sink,则需要检查对应的目标系统是否接收到数据。 --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值