本文由云+社区发表

作者:皮皮熊

概述

Apache Flume是一个用于高效地从大量异构数据源收集、聚合、传输到一个集中式数据存储的分布式、高可靠、高可用的系统。

Apache Flume是Apache基金会的顶级项目。现在有两个代码版本线可以获取:0.9.x和1.x。本文档对应的是1.x版本。

数据流模型

Event是流经flume agent的最小数据单元。一个Event(由Event接口实现)从source流向channel,再到sink。Event包含了一个payload(byte array)和可选的header(string attributes)。一个flume agent就是一个jvm下的进程:控制着Events从一个外部的源头到一个外部的目的地。

Source消费着具有特殊格式的Events(这些Event传递到Source通过像Web server这样外在的数据源)。例如AvroSource可以被用于接收Avro的Events,从本客户端或者其他运行中的flume客户端。当一个Source接收到一个Event,它会把它插入到一个或者多个Channel里。Channel会被动地存储这些Event直到它们被一个Sink消费到。Flume中一种Channel是FileChannel,其使用文件系统来作为后端存储。Sink需要负责任地将一个Event从Channel中移除,并将其放入像hdfs一样的外部存储系统(例如HDFSEventSink),或者转发到传输中下一个节点的source中。Source和Sink在agent中异步地交互Channel中的Event。

可靠性

Event是存储在Flume agent的Channel里。Sink的责任就是传输Event到下一个agent或者最终的存储系统(像hdfs)。Sink只有当Event写入下一个agent的Channel 或者 存储到最终的系统时才会从channel里面删掉Event。这就是Flume如何在单跳消息传输中提供端到端的可靠性。Flume提供了一个事务性的方法来修复可靠传输中的Event。Source和Sink包含了Event的存储和重试(通过由channel提供的事务)。

构建Flume

获取源码

通过git

编译/测试 Flume

Flume使用maven来build。你可以通过标准的maven命令行来编译Flume。

  1. 仅编译:mvn clean compile
  2. 编译且运行单元测试:mvn clean test
  3. 运行独立的测试:mvn clean test -Dtest=,,... -DfailIfNoTests=false
  4. 打包:mvn clean install
  5. 打包(忽略单元测试):mvn clean install -DskipTests

注意:Flume build需要在path中有Google Protocol Buffers编译器。

更新Protocol Buffer版本

File channel依赖Protocol Buffer。当你想更新Protocol Buffer版本时,你需要如下更新使用到Protocol Buffer的data access类:

  1. 本机安装你想要的PB版本
  2. 更新pom.xml中PB的版本
  3. 生成flume中新的PB data access类:cd flume-ng-channels/flume-file-channel; mvn -P compile-proto clean package -DskipTests
  4. 在所有生成文件中加上Apache license(如果缺了的话)
  5. rebuild及测试Flume:cd ../..; mvn clean install

开发自定义部分

client

Client在Event产生时运转,并将他们传递到Flume的agent。Client通常运行在应用消费数据的进程空间中。Flume目前支持Avro, log4j, syslog, 以及 Http POST (with a JSON body)方式从外部数据源传输数据。同时ExecSource支持将本地进程的输出作为Flume的输入。

可能已有的方案是不够的。本案例中你可以使用自定义的方法来向flume发送数据。这里有两种方法来实现。第一:写一个自定义的客户端来和flume已有的source交互,像AvroSource 或者 SyslogTcpSource。此时Client需要将数据转换成这些Source能理解的message。另外一个方案:写一个自定义的Flume Source,通过IPC或者RPC,直接地和已有的client应用通信(需要将client的数据转换成Flume的Event)。注意这些存储在flume agent channel中的事件,必须以Flume Event形式存在。

Client SDK

尽管Flume包含了一系列内置的,用于接收数据的方法(即Source),人们常常想直接地通过flume和自定义的程序进行通信。Flume SDK 就是这样一个lib,它可以通过RPC直接地连接到Flume,并且发送到Flume的数据流。

RPC客户端接口

一个RPC客户端接口的实现,包含了支持Flume的RPC方法。用户的程序可以简单地调用Flume SDK客户端的append(Event)或者appendBatch(List)接口来发送数据,而不用考虑消息交互的细节。用户可以通过使用诸如SimpleEvent类,或者使用EventBuilder的 静态helper方法withBody(),便捷地实现直接提供事件接口所需的事件ARG。

Transaction(事务)接口

Transaction接口是Flume可靠性的基础。所有主要组件(即source,sink和channel)必须使用Flume Transaction。

Transaction在channel的实现中实现。每个source和sink连接到channel时必须要得到一个channnel的对象。Source使用channnelprocessor来管理transaction。sink明确地通过他们配置的channel来管理transaction。存储一个事件(把他们放入channnel中)或者抽取一个事件(从channnel中取出)在一个激活的transaction中完成。例如:

Channel ch = new MemoryChannel();
Transaction txn = ch.getTransaction();
txn.begin();
try {
// This try clause includes whatever Channel operations you want to do Event eventToStage = EventBuilder.withBody("Hello Flume!",
Charset.forName("UTF-8"));
ch.put(eventToStage);
// Event takenEvent = ch.take();
// ...
txn.commit();
} catch (Throwable t) {
txn.rollback(); // Log exception, handle individual exceptions as needed // re-throw all Errors
if (t instanceof Error) {
throw (Error)t;
}
} finally {
txn.close();
}

在这里,我们从channel获取transaction。在begin()返回后,Transaction现在处于活动/打开状态,然后将Event放入Channel中。如果put成功,则提交并关闭Transaction。

Sink

Sink的目的就是从Channel中提取事件并将其转发到传输中的下一个Flume Agent或将它们存储在外部存储库中。根据Flume属性文件中的配置,接收器只与一个通道关联。每个已配置的Sink都有一个SinkRunner实例,当Flume框架调用SinkRunner.start()时,会创建一个新线程来驱动Sink(使用SinkRunner.PollingRunner作为线程的Runnable),该线程管理Sink的生命周期。Sink需要实现start()和stop()方法作为LifecycleAware接口的一部分。

  • Sink.start()方法应初始化Sink并将其置于可将事件转发到其下一个目标的状态。
  • Sink.process()应该执行从Channel提取Event并转发它的核心处理过程。
  • Sink.stop()方法应该进行必要的清理(例如释放资源)。

Sink实现还需要实现Configurable接口来处理自己的配置设置。例如:

public class MySink extends AbstractSink implements Configurable {
private String myProp; @Override
public void configure(Context context) {
String myProp = context.getString("myProp", "defaultValue"); // Process the myProp value (e.g. validation) // Store myProp for later retrieval by process() method
this.myProp = myProp;
} @Override
public void start() {
// Initialize the connection to the external repository (e.g. HDFS) that
// this Sink will forward Events to ..
} @Override
public void stop () {
// Disconnect from the external respository and do any
// additional cleanup (e.g. releasing resources or nulling-out
// field values) ..
} @Override
public Status process() throws EventDeliveryException {
Status status = null; // Start transaction
Channel ch = getChannel();
Transaction txn = ch.getTransaction();
txn.begin();
try {
// This try clause includes whatever Channel operations you want to do Event event = ch.take(); // Send the Event to the external repository.
// storeSomeData(e); txn.commit();
status = Status.READY;
} catch (Throwable t) {
txn.rollback(); // Log exception, handle individual exceptions as needed status = Status.BACKOFF; // re-throw all Errors
if (t instanceof Error) {
throw (Error)t;
}
}
return status;
}
}

Source

Source的目的是从外部客户端接收数据并将其存储到已配置的Channels中。Source可以获取其自己的ChannelProcessor的实例来处理在Channel本地事务中提交的串行事件。在exception的情况下,需要Channels传播异常,则所有Channels将回滚其事务,但先前在其他Channel上处理的事件将保持提交。

与SinkRunner.PollingRunner Runnable类似,有一个PollingRunner Runnable,它在Flume框架调用PollableSourceRunner.start()时创建的线程上执行。每个配置的PollableSource都与自己运行PollingRunner的线程相关联。该线程管理PollableSource的生命周期,例如启动和停止。

  • PollableSource必须实现LifecycleAware接口中声明的start()和stop()方法。
  • PollableSource的运行器调用Source的process()方法。 process()方法应检查新数据并将其作为Flume事件存储到Channel中。

注意,实际上有两种类型的Source:已经提到过PollableSource,另一个是EventDrivenSource。与PollableSource不同,EventDrivenSource必须有自己的回调机制,捕获新数据并将其存储到Channel中。EventDrivenSources并不像PollableSources那样由它们自己的线程驱动。下面是一个自定义PollableSource的示例:

public class MySource extends AbstractSource implements Configurable, PollableSource {
private String myProp; @Override
public void configure(Context context) {
String myProp = context.getString("myProp", "defaultValue"); // Process the myProp value (e.g. validation, convert to another type, ...) // Store myProp for later retrieval by process() method
this.myProp = myProp;
} @Override
public void start() {
// Initialize the connection to the external client
} @Override
public void stop () {
// Disconnect from external client and do any additional cleanup
// (e.g. releasing resources or nulling-out field values) ..
} @Override
public Status process() throws EventDeliveryException {
Status status = null; try {
// This try clause includes whatever Channel/Event operations you want to do // Receive new data
Event e = getSomeData(); // Store the Event into this Source's associated Channel(s)
getChannelProcessor().processEvent(e); status = Status.READY;
} catch (Throwable t) {
// Log exception, handle individual exceptions as needed status = Status.BACKOFF; // re-throw all Errors
if (t instanceof Error) {
throw (Error)t;
}
} finally {
txn.close();
}
return status;
}
}

参考自(Flume 1.8.0 Developer Guide)

flume 1.8.0 文档完整翻译可见 https://blog.csdn.net/u013128262

此文已由腾讯云+社区在各渠道发布

获取更多新鲜技术干货,可以关注我们腾讯云技术社区-云加社区官方号及知乎机构号

flume 1.8.0 开发基础的更多相关文章

  1. 使用BleLib的轻松搞定Android低功耗蓝牙Ble 4.0开发具体解释

    转载请注明来源: http://blog.csdn.net/kjunchen/article/details/50909410 使用BleLib的轻松搞定Android低功耗蓝牙Ble 4.0开发具体 ...

  2. 从零3D基础入门XNA 4.0(1)——3D开发基础

    [题外话] 最近要做一个3D动画演示的程序,由于比较熟悉C#语言,再加上XNA对模型的支持比较好,故选择了XNA平台.不过从网上找到很多XNA的入门文章,发现大都需要一些3D基础,而我之前并没有接触过 ...

  3. 【翻译】Flume 1.8.0 User Guide(用户指南) source

    翻译自官网flume1.8用户指南,原文地址:Flume 1.8.0 User Guide 篇幅限制,分为以下5篇: [翻译]Flume 1.8.0 User Guide(用户指南) [翻译]Flum ...

  4. .NET基础拾遗(5)多线程开发基础

    Index : (1)类型语法.内存管理和垃圾回收基础 (2)面向对象的实现和异常的处理基础 (3)字符串.集合与流 (4)委托.事件.反射与特性 (5)多线程开发基础 (6)ADO.NET与数据库开 ...

  5. ASP.NET Core 1.0 开发记录

    官方资料: https://github.com/dotnet/core https://docs.microsoft.com/en-us/aspnet/core https://docs.micro ...

  6. .NET基础拾遗(6)ADO.NET与数据库开发基础

    Index : (1)类型语法.内存管理和垃圾回收基础 (2)面向对象的实现和异常的处理 (3)字符串.集合与流 (4)委托.事件.反射与特性 (5)多线程开发基础 (6)ADO.NET与数据库开发基 ...

  7. IOS开发基础知识碎片-导航

    1:IOS开发基础知识--碎片1 a:NSString与NSInteger的互换 b:Objective-c中集合里面不能存放基础类型,比如int string float等,只能把它们转化成对象才可 ...

  8. ASP.Net开发基础温故知新学习笔记

    申明:本文是学习2014版ASP.Net视频教程的学习笔记,仅供本人复习之用,也没有发布到博客园首页. 一.一般处理程序基础 (1)表单提交注意点: ①GET通过URL,POST通过报文体: ②需在H ...

  9. Flume官方文档翻译——Flume 1.7.0 User Guide (unreleased version)(二)

    Flume官方文档翻译--Flume 1.7.0 User Guide (unreleased version)(一) Logging raw data(记录原始数据) Logging the raw ...

随机推荐

  1. MyBatis3系列__06查询的几点补充

    关于查询的一点补充: 当查询部门信息时,希望查询该部门下的所有员工,下面会采取两种方式实现: 1.联合查询 public Department getDeptWithEmpById(Integer i ...

  2. leetcode-只出现一次的数字合并两个有序数组

    题目:合并两个有序数组 给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组. 说明: 初始化 nums1 和 nums2 的元素 ...

  3. [LeetCode] New 21 Game 新二十一点游戏

    Alice plays the following game, loosely based on the card game "21". Alice starts with 0 p ...

  4. Cmd命令 查看端口被占用

    1)第一步 打开cmd命令窗口,输入命令:netstat -ano|findstr 输入端口号 2)第二步 继续输入命令:tasklist|findstr  第一步查询到的进程号 3)第三步 根据第二 ...

  5. 让webstorm里提示nodejs智能补全

    webstorm里是默认没有nodejs只能提示的, 比如,输入requ到现在还不提示出require这个函数名,非常不方便. 设置方式: file  --> setting -->edi ...

  6. 简单工厂模式demo

    1. 简单工厂模式 domain的接口 public interface Color{ public void display(); } red public Class Red implements ...

  7. react-native模拟机调试步骤详解 ——亲测有效!!!!

    步骤 1 下载安装夜神模拟器,去夜神官网下载即可!然后安装完成!进入到初始化项目的目录,打开cmd命令,运行adb connect 127.0.0.1:62001 链接模拟器 2 链接完成之后,运行安 ...

  8. [Swift]LeetCode190. 颠倒二进制位 | Reverse Bits

    Reverse bits of a given 32 bits unsigned integer. Example 1: Input: 00000010100101000001111010011100 ...

  9. [Swift]LeetCode365. 水壶问题 | Water and Jug Problem

    You are given two jugs with capacities x and y litres. There is an infinite amount of water supply a ...

  10. [Swift]LeetCode720. 词典中最长的单词 | Longest Word in Dictionary

    Given a list of strings words representing an English Dictionary, find the longest word in words tha ...