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.Mutation;
24  import org.apache.hadoop.hbase.client.Put;
25  import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
26  import org.apache.hadoop.hbase.util.Bytes;
27  import org.apache.hadoop.io.Text;
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, Mutation>
66                    writer = getRecordWriter();
67                byte[] rowBytes = vertex.getId().getBytes();
68                Put put = new Put(rowBytes);
69                Text value = vertex.getValue();
70                if (value.toString().length() > 0)   {
71                   put.add(CF, PARENT, value.getBytes());
72                   writer.write(new ImmutableBytesWritable(rowBytes), put);
73                }
74          }
75      }
76  }