Flume - 有没有办法将avro事件(标题和正文)存储到hdfs中?

时间:2015-10-27 22:14:52

标签: hadoop hdfs flume flume-ng

新的水槽......

我正在接收avro事件并将它们存储到HDFS中。

据我所知,默认情况下,只有事件正文存储在HDFS中。我也知道有avro_event serializer。但我不知道这个序列化器实际上在做什么?它如何影响接收器的最终输出?

另外,我无法弄清楚如何将事件转储到HDFS中以保留其标头信息。我需要编写自己的序列化程序吗?

1 个答案:

答案 0 :(得分:0)

事实证明,序列化程序avro_event确实存储了标题和文件。文件中的正文。

以下是我设置接收器的方法:

a1.sinks.i1.type=hdfs
a1.sinks.i1.hdfs.path=hdfs://localhost:8020/user/my-name
a1.sinks.i1.hdfs.rollInterval=0
a1.sinks.i1.hdfs.rollSize=1024
a1.sinks.i1.hdfs.rollCount=0
a1.sinks.i1.serializer=avro_event
a1.sinks.i1.hdfs.fileType=DataStream

我使用打包的代理avro-client发送了事件,使用-R headerFile选项注入了标题。

headerFile的内容:

machine=localhost
user=myName

最后使用我从这个posting中偷走的简单Java应用程序测试了结果:

final FileSystem fs = FileSystem.get(getConf());
        final Path path = new Path(fs.getHomeDirectory(), "FlumeData.1446072877536");

        printWriter.write(path + "-exists: " + fs.exists(path));

        final SeekableInput input = new FsInput(path, getConf());
        final DatumReader<GenericRecord> reader = new GenericDatumReader<GenericRecord>();
        final FileReader<GenericRecord> fileReader = DataFileReader.openReader(input, reader);

        for (final GenericRecord datum : fileReader) {
            printWriter.write("value = " + datum);
        }

        fileReader.close(); 

我确实看到每条记录的标题,这里有一行:

value = {"headers": {"machine": "localhost", "user": "myName"}, "body": {"bytes": "set -x"}}

还有一个其他序列化程序也会发出标题,即 header_and_text 序列化程序生成的文件是一个人类可读的文本文件。以下是一个示例行:

{machine=localhost, user=userName} set -x

最后在Apache Flume - Hadoop的分布式日志集合中,提到了header_and_text序列号,但我无法使用它。