This project has retired. For details please refer to its Attic page.
TableEdgeOutputFormat xref
View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.giraph.io.hbase.edgemarker;
19  
20  import org.apache.giraph.io.hbase.HBaseVertexOutputFormat;
21  import org.apache.giraph.graph.Vertex;
22  import org.apache.giraph.io.VertexWriter;
23  import org.apache.hadoop.hbase.client.Put;
24  import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
25  import org.apache.hadoop.hbase.util.Bytes;
26  import org.apache.hadoop.io.Text;
27  import org.apache.hadoop.io.Writable;
28  import org.apache.hadoop.mapreduce.RecordWriter;
29  import org.apache.hadoop.mapreduce.TaskAttemptContext;
30  
31  import java.io.IOException;
32  /*
33   Test subclass for HBaseVertexOutputFormat
34   */
35  public class TableEdgeOutputFormat
36          extends HBaseVertexOutputFormat<Text, Text, Text> {
37  
38  
39      public VertexWriter<Text, Text, Text>
40      createVertexWriter(TaskAttemptContext context)
41              throws IOException, InterruptedException {
42          return new TableEdgeVertexWriter(context);
43      }
44  
45      /*
46       For each vertex, write back to the configured table using
47       the vertex id as the row key bytes.
48       */
49      public static class TableEdgeVertexWriter
50              extends HBaseVertexWriter<Text, Text, Text> {
51  
52          private final byte[] CF = Bytes.toBytes("cf");
53          private final byte[] PARENT =  Bytes.toBytes("parent");
54  
55          public TableEdgeVertexWriter(TaskAttemptContext context)
56            throws IOException, InterruptedException  {
57              super(context);
58          }
59          /*
60           Record the vertex value as a the value for a new qualifier 'parent'.
61           */
62          public void writeVertex(
63                  Vertex<Text, Text, Text> vertex)
64                  throws IOException, InterruptedException {
65                RecordWriter<ImmutableBytesWritable, Writable> writer = getRecordWriter();
66                byte[] rowBytes = vertex.getId().getBytes();
67                Put put = new Put(rowBytes);
68                Text value = vertex.getValue();
69                if (value.toString().length() > 0)   {
70                   put.add(CF, PARENT, value.getBytes());
71                   writer.write(new ImmutableBytesWritable(rowBytes), put);
72                }
73          }
74      }
75  }