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.accumulo.edgemarker;
19  
20  import org.apache.accumulo.core.data.Key;
21  import org.apache.accumulo.core.data.Value;
22  import org.apache.giraph.edge.Edge;
23  import org.apache.giraph.edge.EdgeFactory;
24  import org.apache.giraph.graph.Vertex;
25  import org.apache.giraph.io.VertexReader;
26  import org.apache.giraph.io.accumulo.AccumuloVertexInputFormat;
27  import org.apache.hadoop.conf.Configuration;
28  import org.apache.hadoop.io.Text;
29  import org.apache.hadoop.mapreduce.InputSplit;
30  import org.apache.hadoop.mapreduce.RecordReader;
31  import org.apache.hadoop.mapreduce.TaskAttemptContext;
32  
33  import com.google.common.collect.Lists;
34  
35  import java.io.IOException;
36  import java.util.List;
37  import java.util.regex.Pattern;
38  
39  /*
40   Example subclass which reads in Key/Value pairs to construct vertex objects.
41   */
42  public class AccumuloEdgeInputFormat
43      extends AccumuloVertexInputFormat<Text, Text, Text> {
44    @Override public void checkInputSpecs(Configuration conf) { }
45  
46    private static final Text uselessEdgeValue = new Text();
47    public VertexReader<Text, Text, Text>
48    createVertexReader(InputSplit split, TaskAttemptContext context)
49        throws IOException {
50      try {
51  
52        return new AccumuloEdgeVertexReader(
53            accumuloInputFormat.createRecordReader(split, context)) {
54        };
55      } catch (InterruptedException e) {
56        throw new IOException(e);
57      }
58  
59    }
60    /*
61        Reader takes Key/Value pairs from the underlying input format.
62     */
63    public static class AccumuloEdgeVertexReader
64        extends AccumuloVertexReader<Text, Text, Text> {
65  
66      public static final Pattern commaPattern = Pattern.compile("[,]");
67  
68      public AccumuloEdgeVertexReader(RecordReader<Key, Value> recordReader) {
69        super(recordReader);
70      }
71  
72  
73      public boolean nextVertex() throws IOException, InterruptedException {
74        return getRecordReader().nextKeyValue();
75      }
76  
77      /*
78     Each Key/Value contains the information needed to construct the vertices.
79       */
80      public Vertex<Text, Text, Text> getCurrentVertex()
81          throws IOException, InterruptedException {
82        Key key = getRecordReader().getCurrentKey();
83        Value value = getRecordReader().getCurrentValue();
84        Vertex<Text, Text, Text> vertex =
85            getConfiguration().createVertex();
86        Text vertexId = key.getRow();
87        List<Edge<Text, Text>> edges = Lists.newLinkedList();
88        String edge = new String(value.get());
89        Text edgeId = new Text(edge);
90        edges.add(EdgeFactory.create(edgeId, uselessEdgeValue));
91        vertex.initialize(vertexId, new Text(), edges);
92  
93        return vertex;
94      }
95    }
96  }