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 com.google.common.collect.Lists;
22import org.apache.giraph.edge.Edge;
23import org.apache.giraph.edge.EdgeFactory;
24import org.apache.hadoop.io.IntWritable;
25import org.apache.hadoop.io.NullWritable;
26import org.apache.hadoop.io.Text;
27import org.apache.hadoop.mapreduce.InputSplit;
28import org.apache.hadoop.mapreduce.TaskAttemptContext;
2930import java.io.IOException;
31import java.util.List;
32import java.util.regex.Pattern;
3334/**35 * Simple text-based {@link org.apache.giraph.io.VertexInputFormat} for36 * unweighted graphs with int ids.37 *38 * Each line consists of: vertex_id vertex_value neighbor1 neighbor2 ...39 */40publicclassIntIntNullTextVertexInputFormat41extends42 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 with55 * {@link org.apache.giraph.io.formats.IntIntNullTextVertexInputFormat}.56 */57publicclassIntIntNullVertexReaderextends58 TextVertexReaderFromEachLineProcessed<String[]> {
59/** Cached vertex id for the current line */60private IntWritable id;
61/** Cached vertex value for the current line */62private IntWritable value;
6364 @Override
65protected String[] preprocessLine(Text line) throws IOException {
66 String[] tokens = SEPARATOR.split(line.toString());
67 id = new IntWritable(Integer.parseInt(tokens[0]));
68 value = new IntWritable(Integer.parseInt(tokens[1]));
69return tokens;
70 }
7172 @Override
73protected IntWritable getId(String[] tokens) throws IOException {
74return id;
75 }
7677 @Override
78protected IntWritable getValue(String[] tokens) throws IOException {
79return value;
80 }
8182 @Override
83protected Iterable<Edge<IntWritable, NullWritable>> getEdges(
84 String[] tokens) throws IOException {
85 List<Edge<IntWritable, NullWritable>> edges =
86 Lists.newArrayListWithCapacity(tokens.length - 2);
87for (int n = 2; n < tokens.length; n++) {
88 edges.add(EdgeFactory.create(
89new IntWritable(Integer.parseInt(tokens[n]))));
90 }
91return edges;
92 }
93 }
94 }