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 */1819package org.apache.giraph.io.formats;
2021import org.apache.giraph.edge.Edge;
22import org.apache.giraph.edge.EdgeFactory;
23import org.apache.hadoop.io.IntWritable;
24import org.apache.hadoop.io.NullWritable;
25import org.apache.hadoop.io.Text;
26import org.apache.hadoop.mapreduce.InputSplit;
27import org.apache.hadoop.mapreduce.TaskAttemptContext;
2829import com.google.common.collect.Lists;
3031import java.io.IOException;
32import java.util.List;
33import java.util.regex.Pattern;
3435/**36 * Simple text-based {@link org.apache.giraph.io.VertexInputFormat} for37 * unweighted graphs with int ids.38 *39 * Each line consists of: vertex neighbor1 neighbor2 ...40 */41publicclassIntIntNullTextInputFormatextends42 TextVertexInputFormat<IntWritable, IntWritable, NullWritable> {
43/** Separator of the vertex and neighbors */44privatestaticfinal Pattern SEPARATOR = Pattern.compile("[\t ]");
4546 @Override
47publicTextVertexReader createVertexReader(InputSplit split,
48 TaskAttemptContext context)
49throws IOException {
50returnnewIntIntNullVertexReader();
51 }
5253/**54 * Vertex reader associated with {@link IntIntNullTextInputFormat}.55 */56publicclassIntIntNullVertexReaderextends57 TextVertexReaderFromEachLineProcessed<String[]> {
58/**59 * Cached vertex id for the current line60 */61private IntWritable id;
6263 @Override
64protected String[] preprocessLine(Text line) throws IOException {
65 String[] tokens = SEPARATOR.split(line.toString());
66 id = new IntWritable(Integer.parseInt(tokens[0]));
67return tokens;
68 }
6970 @Override
71protected IntWritable getId(String[] tokens) throws IOException {
72return id;
73 }
7475 @Override
76protected IntWritable getValue(String[] tokens) throws IOException {
77return id;
78 }
7980 @Override
81protected Iterable<Edge<IntWritable, NullWritable>> getEdges(
82 String[] tokens) throws IOException {
83 List<Edge<IntWritable, NullWritable>> edges =
84 Lists.newArrayListWithCapacity(tokens.length - 1);
85for (int n = 1; n < tokens.length; n++) {
86 edges.add(EdgeFactory.create(
87new IntWritable(Integer.parseInt(tokens[n]))));
88 }
89return edges;
90 }
91 }
92 }