数据集成工具之Flume的安装及使用

数据集成工具之Flume的安装及使用,第1张

数据集成工具之Flume的安装及使用

文章目录
  • Flume简介
    • 1. Flume
    • 2. Flume架构
    • 3. Flume组件
  • Flume的安装
    • 安装地址
    • 1、上传至虚拟机,并解压
    • 2、重命名并配置环境变量
    • 3、查看flume版本
    • 4、测试flume
  • Flume的使用

Flume简介 1. Flume

1. flume是一个分布式、可靠、和高可用的海量日志采集、聚合和传输的系统。支持在日志系统中定制各类数据发送方,用于收集数据;同时,Flume提供对数据进行简单处理,并写到各种数据接受方(比如文本、HDFS、Hbase等)的能力

2. flume的数据流由事件(Event)贯穿始终。事件是Flume的基本数据单位,它携带日志数据(字节数组形式)并且携带有头信息,这些Event由Agent外部的Source生成,当Source捕获事件后会进行特定的格式化,然后Source会把事件推入(单个或多个)Channel中。你可以把Channel看作是一个缓冲区,它将保存事件直到Sink处理完该事件。Sink负责持久化日志或者把事件推向另一个Source。

2. Flume架构

Flume 运行的核心是 Agent。Flume以agent为最小的独立运行单位。一个agent就是一个JVM。它是一个完整的数据收集工具,含有三个核心组件,分别是source、 channel、 sink。通过这些组件, Event 可以从一个地方流向另一个地方,如下图所示。

3. Flume组件

Client:Client生产数据,运行在一个独立的线程。
Event: 一个数据单元,消息头和消息体组成。(Events可以是日志记录、 avro 对象等。)
Flow: Event从源点到达目的点的迁移的抽象。
Agent: 一个独立的Flume进程,包含组件Source、 Channel、 Sink。(Agent使用JVM 运行Flume。每台机器运行一个agent,但是可以在一个agent中包含多个sources和sinks。)
Source: 数据收集组件。(source从Client收集数据,传递给Channel)
Channel: 中转Event的一个临时存储,保存由Source组件传递过来的Event。(Channel连接 sources 和 sinks ,这个有点像一个队列。)
Sink: 从Channel中读取并移除Event, 将Event传递到FlowPipeline中的下一个Agent(如果有的话)(Sink从Channel收集数据,运行在一个独立线程。)

Flume的安装 安装地址

1)Flume 官网地址:http://flume.apache.org/
2)文档查看地址:http://flume.apache.org/FlumeUserGuide.html
3)下载地址:http://archive.apache.org/dist/flume/

1、上传至虚拟机,并解压
tar -zxvf apache-flume-1.9.0-bin.tar.gz -C /usr/local/soft/

在环境变量中增加如下命令,可以使用 soft 快速切换到 /usr/local/soft

alias soft=‘cd /usr/local/soft/’

2、重命名并配置环境变量
mv apache-flume-1.9.0-bin/ flume-1.9.0
vim /etc/profile
source /etc/profile

3、查看flume版本
flume-ng version
[root@master soft]# flume-ng version
Flume 1.9.0
Source code repository: https://git-wip-us.apache.org/repos/asf/flume.git
Revision: d4fcab4f501d41597bc616921329a4339f73585e
Compiled by fszabo on Mon Dec 17 20:45:25 CET 2018
From source with checksum 35db629a3bda49d23e9b3690c80737f9

4、测试flume
  • 监控一个目录,将数据打印出来

    • 配置文件
    # 首先先给agent起一个名字 叫a1
    # 分别给source channel sink取名字
    a1.sources = r1
    a1.channels = c1
    a1.sinks = k1
    
    # 分别对source、channel、sink进行配置
    
    # 配置source
    # 将source的类型指定为 spooldir 用于监听一个目录下文件的变化
    # 因为每个组件可能会出现相同的属性名称,所以在对每个组件进行配置的时候 
    # 需要加上 agent的名字.sources.组件的名字.属性 = 属性值
    a1.sources.r1.type = spooldir
    a1.sources.r1.spoolDir = /root/data/
    a1.sources.r1.fileSuffix = .ok
    a1.sources.r1.fileHeader = true
    
    # 给r1这个souces配置一个拦截器并取名为 i1
    a1.sources.r1.interceptors = i1
    # 将拦截器i1的类型设置为timestamp 会将处理数据的时间以毫秒的格式插入event的header中
    # a1.sources.r1.interceptors.i1.type = timestamp
    # 将拦截器i1的类型设置为regex_filter 会根据正则表达式过滤数据
    a1.sources.r1.interceptors.i1.type = regex_filter
    # 配置正则表达式
    a1.sources.r1.interceptors.i1.regex = d{3,6}
    # excludeEvents = true 表示将匹配到的过滤,未匹配到的放行
    a1.sources.r1.interceptors.i1.excludeEvents = true
    
    # 配置sink
    # 使用logger作为sink组件,可以将收集到数据直接打印到控制台
    a1.sinks.k1.type = logger
    
    # 配置channel
    # 将channel的类型设置为memory,表示将event缓存在内存中
    a1.channels.c1.type = memory
    
    # 组装
    # 将sources的channels属性指定为c1
    a1.sources.r1.channels = c1
    
    # 将sinks的channel属性指定为c1
    a1.sinks.k1.channel = c1
    
    • 启动agent
    flume-ng agent -n a1 -f ./spoolingtest.conf -Dflume.root.logger=DEBUG,console
    
    • 新建/root/data目录
    mkdir /root/data
    
    • 在/root/data/目录下新建文件,输入内容,观察flume进程打印的日志
    # 随意在a.txt中加入一些内容
    vim /root/data/a.txt
    
Flume的使用
  • spoolingToHDFS.conf

    • 配置文件
    # a表示给agent命名为a
    # 给source组件命名为r1
    a.sources = r1
    # 给sink组件命名为k1
    a.sinks = k1 
    # 给channel组件命名为c1
    a.channels = c1
    #指定spooldir的属性
    a.sources.r1.type = spooldir 
    a.sources.r1.spoolDir = /root/data 
    a.sources.r1.fileHeader = true 
    a.sources.r1.interceptors = i1 
    a.sources.r1.interceptors.i1.type = timestamp
    #指定sink的类型
    a.sinks.k1.type = hdfs
    a.sinks.k1.hdfs.path = /flume/data/dir1
    # 指定文件名前缀
    a.sinks.k1.hdfs.filePrefix = student
    # 指定达到多少数据量写一次文件 单位:bytes
    a.sinks.k1.hdfs.rollSize = 102400
    # 指定多少条写一次文件
    a.sinks.k1.hdfs.rollCount = 1000
    # 指定文件类型为 流 来什么输出什么
    a.sinks.k1.hdfs.fileType = DataStream
    # 指定文件输出格式 为text
    a.sinks.k1.hdfs.writeFormat = text
    # 指定文件名后缀
    a.sinks.k1.hdfs.fileSuffix = .txt
    
    #指定channel
    a.channels.c1.type = memory 
    a.channels.c1.capacity = 1000
    # 表示sink每次会从channel里取多少数据
    a.channels.c1.transactionCapacity = 100
    # 组装
    a.sources.r1.channels = c1 
    a.sinks.k1.channel = c1
    
    • 在 /root/data/目录下准备数据
    The Zen of Python, by Tim Peters
    
    Beautiful is better than ugly.
    Explicit is better than implicit.
    Simple is better than complex.
    Complex is better than complicated.
    Flat is better than nested.
    Sparse is better than dense.
    Readability counts.
    Special cases aren't special enough to break the rules.
    Although practicality beats purity.
    Errors should never pass silently.
    Unless explicitly silenced.
    In the face of ambiguity, refuse the temptation to guess.
    There should be one-- and preferably only one --obvious way to do it.
    Although that way may not be obvious at first unless you're Dutch.
    Now is better than never.
    Although never is often better than *right* now.
    If the implementation is hard to explain, it's a bad idea.
    If the implementation is easy to explain, it may be a good idea.
    Namespaces are one honking great idea -- let's do more of those!
    
    • 启动agent
    flume-ng agent -n a -f ./spoolingToHDFS.conf -Dflume.root.logger=DEBUG,console
    
  • hbaseLogToHDFS

    • 配置文件
    # a表示给agent命名为a
    # 给source组件命名为r1
    a.sources = r1
    # 给sink组件命名为k1
    a.sinks = k1 
    # 给channel组件命名为c1
    a.channels = c1
    #指定exec的属性
    a.sources.r1.type = exec 
    a.sources.r1.command = tail -f /usr/local/soft/hbase-1.4.6/logs/hbase-root-master-master.log
    #指定sink的类型
    a.sinks.k1.type = hdfs
    a.sinks.k1.hdfs.path = /flume/data/dir2
    # 指定文件名前缀
    a.sinks.k1.hdfs.filePrefix = hbaselog
    # 指定达到多少数据量写一次文件 单位:bytes
    a.sinks.k1.hdfs.rollSize = 102400
    # 指定多少条写一次文件
    a.sinks.k1.hdfs.rollCount = 1000
    # 指定文件类型为 流 来什么输出什么
    a.sinks.k1.hdfs.fileType = DataStream
    # 指定文件输出格式 为text
    a.sinks.k1.hdfs.writeFormat = text
    # 指定文件名后缀
    a.sinks.k1.hdfs.fileSuffix = .txt
    
    #指定channel
    a.channels.c1.type = memory 
    a.channels.c1.capacity = 1000
    # 表示sink每次会从channel里取多少数据
    a.channels.c1.transactionCapacity = 100
    # 组装
    a.sources.r1.channels = c1 
    a.sinks.k1.channel = c1
    
  • hbaselogToHbase

    • 在hbase中创建log表
    create 'log','cf1'
    
    • 配置文件
    # a表示给agent命名为a
    # 给source组件命名为r1
    a.sources = r1
    # 给sink组件命名为k1
    a.sinks = k1 
    # 给channel组件命名为c1
    a.channels = c1
    #指定exec的属性
    a.sources.r1.type = exec 
    a.sources.r1.command = cat /usr/local/soft/hbase-1.4.6/logs/hbase-root-master-master.log
    #指定sink的类型
    a.sinks.k1.type = hbase
    a.sinks.k1.table = log
    a.sinks.k1.columnFamily = cf1
    
    #指定channel
    a.channels.c1.type = memory 
    a.channels.c1.capacity = 100000
    # 表示sink每次会从channel里取多少数据
    a.channels.c1.transactionCapacity = 100
    # 组装
    a.sources.r1.channels = c1 
    a.sinks.k1.channel = c1
    
  • netcatLogger

    监听telnet端口

    • 安装telnet
    yum install telnet
    
    • 配置文件
    # a表示给agent命名为a
    # 给source组件命名为r1
    a.sources = r1
    # 给sink组件命名为k1
    a.sinks = k1 
    # 给channel组件命名为c1
    a.channels = c1
    #指定netcat的属性
    a.sources.r1.type = netcat 
    a.sources.r1.bind = 0.0.0.0 
    a.sources.r1.port = 8888 
    
    #指定sink的类型
    a.sinks.k1.type = logger
    #指定channel
    a.channels.c1.type = memory 
    a.channels.c1.capacity = 1000
    # 表示sink每次会从channel里取多少数据
    a.channels.c1.transactionCapacity = 100
    # 组装
    a.sources.r1.channels = c1 
    a.sinks.k1.channel = c1
    
  • 启动

  • 先启动agent

    flume-ng agent -n a -f ./netcatToLogger.conf -Dflume.root.logger=DEBUG,console
    
    • 在启动telnet
    telnet master 8888
    
  • httpToLogger

    • 配置文件
    # a表示给agent命名为a
    # 给source组件命名为r1
    a.sources = r1
    # 给sink组件命名为k1
    a.sinks = k1 
    # 给channel组件命名为c1
    a.channels = c1
    #指定http的属性
    a.sources.r1.type = http
    a.sources.r1.port = 6666 
    
    #指定sink的类型
    a.sinks.k1.type = logger
    #指定channel
    a.channels.c1.type = memory 
    a.channels.c1.capacity = 1000
    # 表示sink每次会从channel里取多少数据
    a.channels.c1.transactionCapacity = 100
    # 组装
    a.sources.r1.channels = c1 
    a.sinks.k1.channel = c1
    
  • 启动

  • 先启动agent

flume-ng agent -n a -f ./httpToLogger.conf -Dflume.root.logger=DEBUG,console
  • 再使用curl发起一个http请求
http://master:6666

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/zaji/5669992.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-17
下一篇2022-12-16

发表评论

登录后才能评论

评论列表(0条)

    保存