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.io.formats;
1920import org.apache.giraph.edge.Edge;
21import org.apache.giraph.edge.EdgeFactory;
22import org.apache.hadoop.io.LongWritable;
23import org.apache.hadoop.io.NullWritable;
24import org.apache.hadoop.io.Text;
25import org.apache.hadoop.mapreduce.InputSplit;
26import org.apache.hadoop.mapreduce.TaskAttemptContext;
2728import com.google.common.collect.Lists;
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 long ids.37 *38 * Each line consists of: vertex neighbor1 neighbor2 ...39 */40publicclassLongLongNullTextInputFormatextends41 TextVertexInputFormat<LongWritable, LongWritable, NullWritable> {
42/** Separator of the vertex and neighbors */43privatestaticfinal Pattern SEPARATOR = Pattern.compile("[\t ]");
4445 @Override
46publicTextVertexReader createVertexReader(InputSplit split,
47 TaskAttemptContext context)
48throws IOException {
49returnnewLongLongNullVertexReader();
50 }
5152/**53 * Vertex reader associated with {@link LongLongNullTextInputFormat}.54 */55publicclassLongLongNullVertexReaderextends56 TextVertexReaderFromEachLineProcessed<String[]> {
57/** Cached vertex id for the current line */58private LongWritable id;
5960 @Override
61protected String[] preprocessLine(Text line) throws IOException {
62 String[] tokens = SEPARATOR.split(line.toString());
63 id = new LongWritable(Long.parseLong(tokens[0]));
64return tokens;
65 }
6667 @Override
68protected LongWritable getId(String[] tokens) throws IOException {
69return id;
70 }
7172 @Override
73protected LongWritable getValue(String[] tokens) throws IOException {
74return id;
75 }
7677 @Override
78protected Iterable<Edge<LongWritable, NullWritable>> getEdges(
79 String[] tokens) throws IOException {
80 List<Edge<LongWritable, NullWritable>> edges =
81 Lists.newArrayListWithCapacity(tokens.length - 1);
82for (int n = 1; n < tokens.length; n++) {
83 edges.add(EdgeFactory.create(
84new LongWritable(Long.parseLong(tokens[n]))));
85 }
86return edges;
87 }
88 }
89 }