现在的位置: 首页 > 搜索技术 > 黄专家专栏 > 正文

Hadoop I/O 上 SequenceFile 类在 Pipes 上的应用

2014年10月30日 搜索技术, 黄专家专栏 ⁄ 共 3830字 ⁄ 字号 评论关闭

有的时候,我们在 hadoop 上的输入可能不是一些基于行的文本,是希望自定义一些结构化的数据。这种情况,一般会选用工具将结构化的数据序列化成字节流,存储在磁盘上。然后在 maper 中读取进来,反序列化即可得到原来的数据。

我们使用 google protobuf 作为这种结构化的信息传递的工具。

首先可以先定义 person.proto 文件

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
option java_package = "com.hackerlight.proto";
option java_outer_classname = "ProtobufMessage";

message Person {
  required string name = 1;
  required int32 id = 2;
  optional string email = 3;

  enum PhoneType {
    MOBILE = 0;
    HOME = 1;
    WORK = 2;
  }

  message PhoneNumber {
    required string number = 1;
    optional PhoneType type = 2 [default = HOME];
  }

  repeated PhoneNumber phone = 4;

  message CountryInfo {
          required string name = 1;
          required string code = 2;
          optional int32 number = 3;
  }
}

生成 protobuf 的 java 代码

1
protoc --java_out=./ test.proto

java 代码会生成在 当前目录下的 com/hackerlight/proto 下.

生成 SequenceFile 结构的代码如下:

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
41
42
43
44
45
46
47
48
package com.hackerlight.writer;

import java.io.FileOutputStream;
import java.net.URI;

import com.hackerlight.proto.ProtobufMessage;
import com.hackerlight.proto.ProtobufMessage.Person;

import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.BytesWritable;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.SequenceFile;

public class Writer {
public static void main(String[] args) throws Exception {
  String uri = args[0];

  org.apache.hadoop.conf.Configuration conf = new org.apache.hadoop.conf.Configuration();
  FileSystem fs = FileSystem.get(URI.create(uri), conf);
  Path path = new Path(uri);

  Person.Builder builder = Person.newBuilder();
  builder.setEmail("moon@hackerlight.com");
  builder.setId(1024);
  builder.setName("Moon");

  Person.PhoneNumber.Builder p = Person.PhoneNumber.newBuilder();
  p.setNumber("18900000000");
  builder.addPhone(p);

  byte[] serialize_string = builder.build().toByteArray();
  IntWritable key = new IntWritable();
  BytesWritable value = new BytesWritable();
  SequenceFile.Writer write = null;
  try {
    write = SequenceFile.createWriter(fs, conf, path, key.getClass(), value.getClass());
    for (int i = 0; i < 100; ++i) {
      key.set(i);
      value.set(serialize_string, 0, serialize_string.length);
      write.append(key, value);
    }
  } finally {
    IOUtils.closeStream(write);
  }
}
}

可以写一个读取的代码反序列化写入的结构数据

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
package com.hackerlight.reader;

import java.io.FileOutputStream;
import java.net.URI;

import com.hackerlight.proto.ProtobufMessage;
import com.hackerlight.proto.ProtobufMessage.Person;

import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.BytesWritable;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.SequenceFile;
import org.apache.hadoop.io.SequenceFile.Writer;
import org.apache.hadoop.io.Writable;
import org.apache.hadoop.util.ReflectionUtils;

public class Reader {
public static void main(String[] args) throws Exception {
  String uri = args[0];
  org.apache.hadoop.conf.Configuration conf = new org.apache.hadoop.conf.Configuration();
  FileSystem fs = FileSystem.get(URI.create(uri), conf);
  Path path = new Path(uri);
  SequenceFile.Reader reader = null;
  try {
    reader = new SequenceFile.Reader(fs, path, conf);
    Writable key = (Writable)ReflectionUtils.newInstance(reader.getKeyClass(), conf);
    BytesWritable value = (BytesWritable)ReflectionUtils.newInstance(reader.getValueClass(), conf);
    while (reader.next(key, value)) {
      System.out.println("key : " + key);
      Person new_person = Person.PARSER.parseFrom(value.getBytes(), 0, value.getLength());
      System.out.println("name  : " + new_person.getName());
      System.out.println("email : " + new_person.getEmail());
    }
  } finally {
    IOUtils.closeStream(reader);
  }
}
}

在 Pipes 上可以这样读取

1
2
3
4
5
6
7
8
9
10
class MyMap: public HadoopPipes::Mapper {
 public:
   WordCountMap(HadoopPipes::TaskContext& context) {}
   void map(HadoopPipes::MapContext& context) {
     const std::string& key = context.getInputKey();
     const std::string& value = context.getInputValue();
     Person person;
     assert(person.ParseFromString(value));
   }
};

执行 hadoop 命令的时候一定要记得加上参数 -inputformat org.apache.hadoop.mapred.SequenceFileInputFormat

抱歉!评论已关闭.