大数据课程D3——hadoop的Source

文章作者邮箱:[email protected] 地址:广东惠州

▲ 本章节目的

⚪ 掌握Source的AVRO Source;

⚪ 掌握Source的Exec Source;

⚪ 掌握Source的Spooling Directory Source;

⚪ 掌握Source的Netcat Source;

⚪ 掌握Source的Sequence Generator Source;

⚪ 掌握Source的HTTP Source;

⚪ 掌握Source的Custom Source;

一、AVRO Source

1. 概述

  1. AVRO Source监听指定的端口,接收其他节点发送来的被AVRO序列化的数据。

  2. AVRO Source结合AVRO Sink可以实现更多的流动模型,包括多级流动、扇入流动以及扇出流动。

2. 配置属性

|--------|----------------|
| 属性 | 解释 |
| type | 必须是avro |
| bind | 要监听的主机的主机名或者IP |
| port | 要监听的端口 |

3. 案例

  1. 编辑格式文件,在格式文件中需要添加指定内容:

a1.sources = s1

a1.channels = c1

a1.sinks = k1

配置AVRO Source

必须是avro

a1.sources.s1.type = avro

指定要监听的主机

a1.sources.s1.bind = hadoop01

指定要监听的端口

a1.sources.s1.port = 8090

a1.channels.c1.type = memory

a1.sinks.k1.type = logger

a1.sources.s1.channels = c1

a1.sinks.k1.channel = c1

  1. 启动Flume。

../bin/flume-ng agent -n a1 -c ../conf -f avrosource.conf -

Dflume.root.logger=INFO,console

  1. 在另一个窗口中,进入指定目录,编辑文件。

cd /home/software/apache-flume-1.9.0-bin/data

vim a.txt

  1. 运行AVRO客户端。

../bin/flume-ng avro-client -H hadoop01 -p 8090 -F a.txt

二、Exec Source

1. 概述

  1. Exec Source会运行指定的命令,然后将命令的执行结果作为日志进行收集。

  2. 利用这个Source可以实现对文件或者其他操作的实时监听。

2. 配置属性

|---------|-------------------------|
| 属性 | 解释 |
| type | 必须是exec |
| command | 要执行和监听的命令 |
| shell | 最好指定这个属性,表示指定Shell的运行方式 |

3. 案例

  1. 需求:实时监听/home/a.txt文件的变化。

  2. 编辑格式文件,添加如下内容:

a1.sources = s1

a1.channels = c1

a1.sinks = k1

配置Exec Source

必须是exec

a1.sources.s1.type = exec

指定要运行的命令

a1.sources.s1.command = tail -F /home/a.txt

指定Shell的运行方式/类型

a1.sources.s1.shell = /bin/bash -c

a1.channels.c1.type = memory

a1.sinks.k1.type = logger

a1.sources.s1.channels = c1

a1.sinks.k1.channel = c1

  1. 启动Flume。

../bin/flume-ng agent -n a1 -c ../conf -f execsource.conf -

Dflume.root.logger=INFO,console

  1. 修改文件内容。

echo "hello" >> a.txt

三、Spooling Directory Source

1. 概述

  1. Spooling Directory Source是监听指定的目录,自动将目录中出现的新文件的内容进行收集。

  2. 如果不指定,默认情况下,一个文件被收集之后,会自动添加一个后缀.COMPLETED,通过通过属性fileSuffix来修改。

2. 配置属性

|------------|---------------------------|
| 属性 | 解释 |
| type | 必须是spooldir |
| spoolDir | 要监听的目录 |
| fileSuffix | 收集之后添加的文件后缀,默认是.COMPLETED |

3. 案例

  1. 编辑格式文件,添加如下内容:

a1.sources = s1

a1.channels = c1

a1.sinks = k1

配置Spooling Directory Source

必须是spooldir

a1.sources.s1.type = spooldir

指定要监听的目录

a1.sources.s1.spoolDir = /home/flumedata

a1.channels.c1.type = memory

a1.sinks.k1.type = logger

a1.sources.s1.channels = c1

a1.sinks.k1.channel = c1

  1. 启动Flume。

../bin/flume-ng agent -n a1 -c ../conf -f spoolingdirsource.conf -

Dflume.root.logger=INFO,console

四、Netcat Source

1. 概述

  1. Netcat Source在Flume1.9之后分为Netcat TCP Source和Netcat UDP Source。

  2. 如果不指定,那么Netcat Source监听的是TCP请求。

2. 配置属性

|--------|----------------------------------------------|
| 属性 | 解释 |
| type | 如果监听TCP请求,那么使用netcat;如果监听UDP请求,那么使用netcatudp |
| bind | 要监听的主机的主机名或者IP |
| port | 要监听的端口 |

3. 案例

  1. 编辑格式文件,添加如下内容(以UDP为例):

a1.sources = s1

a1.channels = c1

a1.sinks = k1

配置Netcat UDP Source

必须是netcatudp

a1.sources.s1.type = netcatudp

指定要监听的主机

a1.sources.s1.bind = 0.0.0.0

指定要监听的端口

a1.sources.s1.port = 8090

a1.channels.c1.type = memory

a1.sinks.k1.type = logger

a1.sources.s1.channels = c1

a1.sinks.k1.channel = c1

  1. 启动Flume。

../bin/flume-ng agent -n a1 -c ../conf -f netcatudpsource.conf -

Dflume.root.logger=INFO,console

  1. 启动nc。

nc -u -k -l hadoop01 8090

五、Sequence Generator Source

1. 概述

  1. Sequence Generator Source本质上就是一个序列产生器,会从0开始每次递增1个单位。

  2. 如果不指定,默认情况下递增到Long.MAX_VALUE。

2. 配置属性

|-------------|---------|
| 属性 | 解释 |
| type | 必须是seq |
| totalEvents | 递增的结束范围 |

3. 案例

  1. 编辑格式文件,添加如下内容:

a1.sources = s1

a1.channels = c1

a1.sinks = k1

配置Sequence Generator Source

必须是seq

a1.sources.s1.type = seq

指定结束范围

a1.sources.s1.totalEvents = 10

a1.channels.c1.type = memory

a1.sinks.k1.type = logger

a1.sources.s1.channels = c1

a1.sinks.k1.channel = c1

  1. 启动Flume。

../bin/flume-ng agent -n a1 -c ../conf -f seqsource.conf -

Dflume.root.logger=INFO,console

六、HTTP Source

1. 概述

  1. HTTP Source用于监听HTTP请求,但是只能监听POST和GET请求。

  2. GET请求只用于试验阶段,所以实际过程中只用这个Source来监听POST请求。

2. 配置属性

|--------|---------|
| 属性 | 解释 |
| type | 必须是http |
| port | 要监听的端口 |

3. 案例

  1. 编辑格式文件,添加如下内容:

a1.sources = s1

a1.channels = c1

a1.sinks = k1

配置HTTP Source

必须是http

a1.sources.s1.type = http

指定要监听的端口

a1.sources.s1.port = 8090

a1.channels.c1.type = memory

a1.sinks.k1.type = logger

a1.sources.s1.channels = c1

a1.sinks.k1.channel = c1

  1. 启动Flume。

../bin/flume-ng agent -n a1 -c ../conf -f httpsource.conf -

Dflume.root.logger=INFO,console

  1. 发送POST请求。

curl -X POST -d '[{"headers":

{"kind":"test","class":"bigdata"},"body":"testing"}]'

http://hadoop01:8090

七、Custom Source

1. 概述

  1. 自定义Source:需要定义一个类实现Source接口的子接口:EventDrivenSource或者PollableSource。

a. EventDrivenSource:事件驱动源 - 被动型Source。需要自己定义线程来获取数据处理数据。

b. PollableSource:拉取源 - 主动型Source。提供了线程来获取数据,只需要考虑怎么处理数据即可。

  1. 除了实现上述两个接口之一,这个自定义的类一般还需要考虑实现Configurable接口,通过接口的方法来获取指定的属性。

2. 步骤

  1. 需要构建Maven工程,导入对应的POM依赖。

  2. 定义类继承AbstractSource,实现EventDrivenSource和Configurable接口。

  3. 覆盖configure,start和stop方法。

  4. 定义完成之后,需要将类打成jar包放到Flume安装目录的lib目录下。

  5. 编写格式文件,例如:

a1.sources = s1

a1.channels = c1

a1.sinks = k1

配置自定义Source

必须是类的全路径名

a1.sources.s1.type =cn.tedu.flume.source.AuthSource

指定结束范围

a1.sources.s1.end = 100

指定递增的步长

a1.sources.s1.step = 5

a1.channels.c1.type = memory

a1.sinks.k1.type = logger

a1.sources.s1.channels = c1

a1.sinks.k1.channel = c1

  1. 启动Flume。

../bin/flume-ng agent -n a1 -c ../conf -f authsource.conf -

Dflume.root.logger=INFO,console

相关推荐
心仪悦悦1 小时前
Spark缓存
大数据·缓存·spark
我叫珂蛋儿吖1 小时前
[redis进阶六]详解redis作为缓存&&分布式锁
运维·c语言·数据库·c++·redis·分布式·缓存
lix的小鱼1 小时前
安装Hadoop并运行WordCount程序
大数据·linux·hadoop
七七-d1 小时前
测试集群的功能-执行wordcount程序
大数据
INFINI Labs2 小时前
INFINI Console 纳管 Elasticsearch 9(一):指标监控、数据管理、DSL 语句执行
大数据·elasticsearch·搜索引擎·console
椰椰椰耶3 小时前
【RabbitMQ】工作队列和发布/订阅模式的具体实现
分布式·rabbitmq·ruby
猪猪果泡酒3 小时前
Spark,RDD中的行动算子
大数据·分布式·spark
2401_871290584 小时前
Spark处理过程-转换算子
大数据·分布式·spark
Betty_蹄蹄boo4 小时前
运行Spark程序-在Spark-shell——RDD
大数据·分布式·spark
李恒-聆机智能专精数采4 小时前
从零开始了解数据采集(二十七)——什么IIOT平台
大数据·人工智能·云计算·制造·数据采集·数据可视化