This project has retired. For details please refer to its Attic page.
LongLongNullTextInputFormat 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.formats;
19  
20  import org.apache.giraph.edge.Edge;
21  import org.apache.giraph.edge.EdgeFactory;
22  import org.apache.hadoop.io.LongWritable;
23  import org.apache.hadoop.io.NullWritable;
24  import org.apache.hadoop.io.Text;
25  import org.apache.hadoop.mapreduce.InputSplit;
26  import org.apache.hadoop.mapreduce.TaskAttemptContext;
27  
28  import com.google.common.collect.Lists;
29  
30  import java.io.IOException;
31  import java.util.List;
32  import java.util.regex.Pattern;
33  
34  /**
35   * Simple text-based {@link org.apache.giraph.io.VertexInputFormat} for
36   * unweighted graphs with long ids.
37   *
38   * Each line consists of: vertex neighbor1 neighbor2 ...
39   */
40  public class LongLongNullTextInputFormat extends
41      TextVertexInputFormat<LongWritable, LongWritable, NullWritable> {
42    /** Separator of the vertex and neighbors */
43    private static final Pattern SEPARATOR = Pattern.compile("[\t ]");
44  
45    @Override
46    public TextVertexReader createVertexReader(InputSplit split,
47                                               TaskAttemptContext context)
48      throws IOException {
49      return new LongLongNullVertexReader();
50    }
51  
52    /**
53     * Vertex reader associated with {@link LongLongNullTextInputFormat}.
54     */
55    public class LongLongNullVertexReader extends
56        TextVertexReaderFromEachLineProcessed<String[]> {
57      /** Cached vertex id for the current line */
58      private LongWritable id;
59  
60      @Override
61      protected String[] preprocessLine(Text line) throws IOException {
62        String[] tokens = SEPARATOR.split(line.toString());
63        id = new LongWritable(Long.parseLong(tokens[0]));
64        return tokens;
65      }
66  
67      @Override
68      protected LongWritable getId(String[] tokens) throws IOException {
69        return id;
70      }
71  
72      @Override
73      protected LongWritable getValue(String[] tokens) throws IOException {
74        return id;
75      }
76  
77      @Override
78      protected Iterable<Edge<LongWritable, NullWritable>> getEdges(
79          String[] tokens) throws IOException {
80        List<Edge<LongWritable, NullWritable>> edges =
81            Lists.newArrayListWithCapacity(tokens.length - 1);
82        for (int n = 1; n < tokens.length; n++) {
83          edges.add(EdgeFactory.create(
84              new LongWritable(Long.parseLong(tokens[n]))));
85        }
86        return edges;
87      }
88    }
89  }