1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing, software13 * 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 and16 * limitations under the License.17 */18package org.apache.giraph.io.hbase.edgemarker;
1920import org.apache.giraph.io.hbase.HBaseVertexOutputFormat;
21import org.apache.giraph.graph.Vertex;
22import org.apache.giraph.io.VertexWriter;
23import org.apache.hadoop.hbase.client.Put;
24import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
25import org.apache.hadoop.hbase.util.Bytes;
26import org.apache.hadoop.io.Text;
27import org.apache.hadoop.io.Writable;
28import org.apache.hadoop.mapreduce.RecordWriter;
29import org.apache.hadoop.mapreduce.TaskAttemptContext;
3031import java.io.IOException;
32/*33 Test subclass for HBaseVertexOutputFormat34 */35publicclassTableEdgeOutputFormat36extends HBaseVertexOutputFormat<Text, Text, Text> {
373839public VertexWriter<Text, Text, Text>
40 createVertexWriter(TaskAttemptContext context)
41throws IOException, InterruptedException {
42returnnewTableEdgeVertexWriter(context);
43 }
4445/*46 For each vertex, write back to the configured table using47 the vertex id as the row key bytes.48 */49publicstaticclassTableEdgeVertexWriter50extends HBaseVertexWriter<Text, Text, Text> {
5152privatefinal byte[] CF = Bytes.toBytes("cf");
53privatefinal byte[] PARENT = Bytes.toBytes("parent");
5455publicTableEdgeVertexWriter(TaskAttemptContext context)
56throws IOException, InterruptedException {
57super(context);
58 }
59/*60 Record the vertex value as a the value for a new qualifier 'parent'.61 */62publicvoid writeVertex(
63 Vertex<Text, Text, Text> vertex)
64throws 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();
69if (value.toString().length() > 0) {
70 put.add(CF, PARENT, value.getBytes());
71 writer.write(new ImmutableBytesWritable(rowBytes), put);
72 }
73 }
74 }
75 }