下面讲一下如何使用elephant-bird的代码生成器生成自己的 Hadoop+protocolBuffer+lzo的代码。
编写自己的 *.proto 文件
生成代码的命令:
|
1
2
3
4
5
6 |
protoc –proto_path=/home/admin/yewang/kevinweil-elephant-bird-3bdbafa/examples/src/proto –java_out=/home/admin/yewang/kevinweil-elephant-bird-3bdbafa/examples/src/gen-java –twadoop_out=/home/admin/yewang/kevinweil-elephant-bird-3bdbafa/examples/src/gen-java/ –proto_path=/home/admin/yewang/kevinweil-elephant-bird-3bdbafa/examples/src/proto /home/admin/yewang/kevinweil-elephant-bird-3bdbafa/examples/src/proto/address_book.proto |
–twadoop_out 指定了2个意义:
1. 生成的hadoopProto***.java 文件放置的路径
2. protoc 要去调用一个外部的脚本 , 名字为 protoc-gen-twadoop 。
注意是根据名字 protoc_gen_ 的前缀 加上 twadoop 组合成一个名字 去调用 protoc-gen-twadoop 脚本的
此处非常诡异,大家使用时一定要注意
protoc-gen-twadoop 的内容为 :
/home/admin/jdk1.6.0_13/bin/java
-cp /home/admin/yewang/kevinweil-elephant-bird-3bdbafa/lib/*:/home/admin/yewang/kevinweil-elephant-bird-3bdbafa/dist/elephant-bird-1.0.jar
com.twitter.elephantbird.proto.HadoopProtoCodeGenerator
config-twadoop.yml
-
注意此处的红色的-不是可有可无的!!!
下载的版本中的jar包 是基于hadoop 0.20+开发的。如果你选用的是 hadoop 0.20+ 的版本 可以直接用 ,名字是 elephant-bird-1.0.jar
如果是 hadoop-0.19 请下载我提供的hadoop-lzo版本 ,解压后进行ant编译即可, 链接是 https://docs.google.com/leaf id=0B_QnJPSut6_SMmJmYzEyMWYtMTJmYi00OGZkLWE5N2QtYzY3M2M3MTAzMjll&hl=zh_CN (需要翻墙)
elephant-bird 里面包含了如下的这些类, 我大概说明一下他们的用途
mapred/input/LzoProtobufBlockB64InputFormat.java 读取proto+lzo的数据文件, 做Base64转换成文本输出, 主要是为streaming提供的
mapred/input/LzoProtobufBlockInputFormat.java 读取proto+lzo的数据文件,解析成proto对象, 主要是mapreduce程序使用
mapred/input/ProtobufBlockInputFormat.java 读取proto 的数据文件,解析成proto对象, 主要是mapreduce程序使用
mapred/io/ProtobufBlockReader.java 真正执行读取的类
mapred/io/ProtobufBlockWriter.java 真正执行输出的类
mapred/io/ProtobufWritable.java
mapred/output/LzoProtobufBlockB64OutputFormat.java 获取base64转换后的文本 ,做Base64转换后解析成proto对象,存储成proto+lzo的数据文件,主要是为streaming提供的
mapred/output/LzoProtobufBlockMultiOutputFormat.java 获取proto对象,存储成proto+lzo的数据文件, 可以根据key的值,改变输出part的前缀,一次输出多种命名文件。
mapred/output/LzoProtobufBlockOutputFormat.java 获取proto对象,存储成proto+lzo的数据文件。 主要是mapreduce程序使用
mapred/output/ProtobufBlockOutputFormat.java 获取proto对象,存储成proto的数据文件。 主要是mapreduce程序使用
重启集群 全部配置完成
运行使用
压缩命令:
|
1 |
hadoop jar ~/hadoop_sta/hadoop/contrib/streaming/hadoop-0.19.2-dev-streaming.jar -input /app/aa.txt -output /test-lzo -mapper cat -reducer cat -jobconf mapred.output.compress=true -jobconf mapred.output.compression.codec=org.apache.hadoop.io.compress.LzoCodec |
给lzo压缩文件加索引:
|
1 |
hadoop jar /home/admin/hadoop_sta/hadoop/lib/hadoop-lzo-0.4.4.jar com.hadoop.compression.lzo.LzoIndexer /test-lzo/ |
给lzo文件加索引的目的是为了让lzo支持 splitable, 这样hadoop可以并行处理, 所以这一步很关键, 生成的文件后缀 .index
我们在 hadoop-lzo-0.4.4.jar 另一个mapreduce版本的 创建索引的工具 DistributedLzoIndexer
解压命令:
|
1
2
3 |
hadoop jar ~/hadoop_sta/hadoop/contrib/streaming/hadoop-0.19.2-dev-streaming.jar -inputformat org.apache.hadoop.mapred.LzoTextInputFormat -input /test-lzo -output /test-txt -mapper cat -reducer cat |
如何编写 读取 写出 protocolBuffer + lzo 文件的mapreduce程序
编写.proto文件
具体语法请参考protocolBuffer网站
例子:
|
1
2
3
4
5
6
7
8
9 |
<package com.taobao.proto; message auction { optional string id = 1; optional bytes title = 2; optional string user = 3; optional string pict_url = 4; optional uint32 category = 5; } |
使用protoc程序生成java代码
protoc –java_out=. auction.proto
生成的文件是 com/taobao/proto/Auction.java 文件
实现定制的inputFormat和outputFormat
主要是下面的3个类, 相关的代码 在 我提供elephant-bird下载包的 taobao 目录下都有
AuctionProtobufWritable.java
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 |
package com.taobao.proto.mapred.io; import com.taobao.proto.Auction.auction; import com.twitter.elephantbird.mapred.io.ProtobufWritable; import com.twitter.elephantbird.util.TypeRef; public class AuctionProtobufWritable extends ProtobufWritable<auction> { public AuctionProtobufWritable() { super(new TypeRef<auction>(){}); } } |
AuctionLzoProtobufBlockInputFormat.java
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 |
AuctionLzoProtobufBlockInputFormat.java package com.taobao.proto.mapred.input; import com.taobao.proto.Auction.auction; import com.twitter.elephantbird.mapred.input.LzoProtobufBlockInputFormat; import com.taobao.proto.mapred.io.AuctionProtobufWritable; import com.twitter.elephantbird.util.TypeRef; public class AuctionLzoProtobufBlockInputFormat extends LzoProtobufBlockInputFormat<auction, AuctionProtobufWritab le> { public AuctionLzoProtobufBlockInputFormat() { setTypeRef(new TypeRef<auction>(){}); setProtobufWritable(new AuctionProtobufWritable()); } } |
AuctionLzoProtobufBlockOutputFormat.java
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 |
package com.taobao.proto.mapred.output; import com.taobao.proto.Auction.auction; import com.twitter.elephantbird.mapred.output.LzoProtobufBlockOutputFormat; import com.taobao.proto.mapred.io.AuctionProtobufWritable; import com.twitter.elephantbird.util.TypeRef; public class AuctionLzoProtobufBlockOutputFormat extends LzoProtobufBlockOutputFormat<auction, AuctionProtobufWrit able> { public AuctionLzoProtobufBlockOutputFormat() { setTypeRef(new TypeRef<auction>(){}); } } |
编写mapreduce程序
job 的设置:
|
1
2
3
4 |
job.setOutputKeyClass(NullWritable.class); job.setOutputValueClass(AuctionProtobufWritable.class); job.setInputFormat(AuctionLzoProtobufBlockInputFormat.class); job.setOutputFormat(AuctionLzoProtobufBlockOutputFormat.class); |
mapper和reduce类:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40 |
import com.google.protobuf.ByteString; import com.taobao.proto.Auction; import com.taobao.proto.Auction.auction; import com.taobao.proto.mapred.io.*; import com.taobao.proto.mapred.input.*; import com.taobao.proto.mapred.output.*; public static class proto2protoMapper extends MapReduceBase implements Mapper<LongWritable, AuctionProtobufWrit able, NullWritable, AuctionProtobufWritable> { @Override public void map(LongWritable key, AuctionProtobufWritable value, OutputCollector<NullWritable, AuctionProtobufWritable> outCollector, Reporter reporter) throws IOException { auction pa = value.get(); auction.Builder builder = auction.newBuilder(); if (pa.hasId()) builder.setId(pa.getId()); if (pa.hasTitle()) builder.setTitle(pa.getTitle()); ...... AuctionProtobufWritable pw = new AuctionProtobufWritable(); pw.set(builder.build()); outCollector.collect(NullWritable.get(), pw); } } |
编译成jar包
ant 编译
如何运行
|
1 |
hadoop jar dist/taobao-proto-auction-1.0.jar com.taobao.proto.proto2proto /yewang/xml2proto /yewang/proto2proto |
streaming调用方式,和map reduce处理程序 Python样例
编写.proto文件
复用上面的 例子一样
使用protoc 生成 python代码
|
1 |
protoc –python_out=../python/ auction.proto |
生成的文件是 auction_pb2.py
编写map reduce 脚本
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 |
#!/home/admin/Python/bin/python # -*- coding: utf-8 -*- # Filename: reducer.py # Author: yewang@taobao.com clm971910@gmail.com import auction_pb2 import sys import base64 pa = auction_pb2.auction() f = open("/dev/stdin", "rb") while True: line = f.readline(); if len(line) == 0: break; # 处理掉换行符 , (streaming.jar 帮我们加的) line = line.strip(); # 切分出keyValue, (streaming.jar 帮我们加的) keyValue = line.split("\t"); # base64 解码 value = base64.standard_b64decode(keyValue[1]); # 解析 成 proto 对象 pa.ParseFromString(value); # 输出部分内容, 需要带上key, \t分隔,用于选择合适的reducer # print "1\t" + pa.title; # 如果想要输出proto , 需要将proto对象转换成字符串, 然后base64编��� print "1\t" + base64.standard_b64encode(value); f.close() |
如何运行
|
1 |
hadoop jar -libjars dist/taobao-proto-auction-1.0.jar /home/admin/hadoop_sta/hadoop/contrib/streaming/hadoop-0.19.2-dev-streaming.jar -input /yewang/testproto -output /yewang/testStreaming4 -file /home/admin/yewang/elephant-bird/taobao/src/python/auction_pb2.py -file /home/admin/yewang/elephant-bird/taobao/src/python/mapper.py -file /home/admin/yewang/elephant-bird/taobao/src/python/reducer.py -inputformat com.taobao.proto.mapred.input.AuctionLzoProtobufBlockB64InputFormat -outputformat com.taobao.proto.mapred.output.AuctionLzoProtobufBlockB64OutputFormat -reducer reducer.py -mapper mapper.py |
测试结果
测试环境:
3 台虚拟机 (1G 内存 单核2G)
测试数据:
宝贝xml, 1.3 G
数据大小对比:
处理前: 1.3G 处理后: 285 M
数据减少为原始数据的 22%
处理速度对比
xml 读取加解析 1m26.595s
proto+lzo 读取加解析 0m21.833s
xml 读取加解析然后构造成xml输出 2m45.949s
proto+lzo 读取加解析然后构造成proto输出 0m40.267s
测试结论:
从测试的结果来看, 性能的提升比较明显。
