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.examples.scc;
1920import java.io.IOException;
21import java.util.List;
22import java.util.regex.Pattern;
2324import org.apache.giraph.edge.Edge;
25import org.apache.giraph.edge.EdgeFactory;
26import org.apache.giraph.io.formats.TextVertexInputFormat;
27import org.apache.hadoop.io.LongWritable;
28import org.apache.hadoop.io.NullWritable;
29import org.apache.hadoop.io.Text;
30import org.apache.hadoop.mapreduce.InputSplit;
31import org.apache.hadoop.mapreduce.TaskAttemptContext;
3233import com.google.common.collect.Lists;
3435/**36 * Simple text-based {@link org.apache.giraph.io.VertexInputFormat} for37 * unweighted graphs with long ids.38 *39 * Each line consists of: vertex neighbor1 neighbor2 ...40 */41publicclassSccLongLongNullTextInputFormatextends42 TextVertexInputFormat<LongWritable, SccVertexValue, 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 {
50returnnewLongLongNullVertexReader();
51 }
5253/**54 * Vertex reader associated with {@link SccLongLongNullTextInputFormat}.55 */56publicclassLongLongNullVertexReaderextends57 TextVertexReaderFromEachLineProcessed<String[]> {
58/** Cached vertex id for the current line */59private LongWritable id;
6061 @Override
62protected String[] preprocessLine(Text line) throws IOException {
63 String[] tokens = SEPARATOR.split(line.toString());
64 id = new LongWritable(Long.parseLong(tokens[0]));
65return tokens;
66 }
6768 @Override
69protected LongWritable getId(String[] tokens) throws IOException {
70return id;
71 }
7273 @Override
74protectedSccVertexValue getValue(String[] tokens) throws IOException {
75returnnewSccVertexValue(Long.parseLong(tokens[0]));
76 }
7778 @Override
79protected Iterable<Edge<LongWritable, NullWritable>> getEdges(
80 String[] tokens) throws IOException {
81 List<Edge<LongWritable, NullWritable>> edges =
82 Lists.newArrayListWithCapacity(tokens.length - 1);
83for (int n = 1; n < tokens.length; n++) {
84 edges.add(EdgeFactory.create(
85new LongWritable(Long.parseLong(tokens[n]))));
86 }
87return edges;
88 }
89 }
90 }