• Flume基础(五):企业开发案例(二)


     实时读取本地文件到 HDFS 案例

    1)案例需求:实时监控 Hive 日志,并上传到 HDFS 中
    2)需求分析:
    3)实现步骤:
    1.Flume 要想将数据输出到 HDFS,必须持有 Hadoop 相关 jar 包
    由于flume服务器没有安装hadoop,但是要依赖hadoop的相关类来将文件保存到hdfs的,所以要复制依赖的jar包到 flume。从hadoop安装目录 的 /share/common   、 /share/hdfs目录下找。
    将 commons-configuration-1.6.jar、
    hadoop-auth-2.7.2.jar、
    hadoop-common-2.7.2.jar、
    hadoop-hdfs-2.7.2.jar、
    commons-io-2.4.jar、
    htrace-core-3.1.0-incubating.jar
    拷贝到/opt/module/flume/lib 文件夹下。
    2.创建 flume-file-hdfs.conf 文件
    创建文件
    [atguigu@hadoop102 job]$ touch flume-file-hdfs.conf
    注:要想读取 Linux 系统中的文件,就得按照 Linux 命令的规则执行命令。由于 Hive日志在 Linux 系统中所以读取文件的类型选择:exec 即 execute 执行的意思。表示执行 Linux
    命令来读取文件。
    [atguigu@hadoop102 job]$ vim flume-file-hdfs.conf
    添加如下内容
     
    # 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 /opt/module/hive/logs/hive.log
    a2.sources.r2.shell = /bin/bash -c
    # Describe the sink
    a2.sinks.k2.type = hdfs
    a2.sinks.k2.hdfs.path = hdfs://hadoop102:9000/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 = 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
    # 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

    3.执行监控配置
    [atguigu@hadoop102 flume]$ bin/flume-ng agent -c conf/ -n a2 -f job/flume-file-hdfs.conf
    4.开启 Hadoop 和 Hive 并操作 Hive 产生日志 
    [atguigu@hadoop102 hadoop-2.7.2]$ sbin/start-dfs.sh
    [atguigu@hadoop103 hadoop-2.7.2]$ sbin/start-yarn.sh
    [atguigu@hadoop102 hive]$ bin/hive
    hive (default)>
    #注意保证存在/opt/module/hive/logs 文件夹
    5.在 HDFS 上查看文件。
     
     
     
     

    本文来自博客园,作者:秋华,转载请注明原文链接:https://www.cnblogs.com/qiu-hua/p/13378762.html

  • 相关阅读:
    产品设计步骤
    浅谈公司经营的事业发展线
    项目经理的眼:一切都是项目
    IT软件的编程方向
    后记:IT软件人员学习的书籍
    12、产品经理要阅读的书籍
    11、项目经理要阅读的书籍
    7、项目活动的开展
    6、项目组人员绩效考核
    5、项目组人员培训
  • 原文地址:https://www.cnblogs.com/qiu-hua/p/13378762.html
Copyright © 2020-2023  润新知