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.graph.Vertex;
22import org.apache.hadoop.io.Text;
23import org.apache.hadoop.io.Writable;
24import org.apache.hadoop.io.WritableComparable;
25import org.apache.hadoop.mapreduce.TaskAttemptContext;
2627import java.io.IOException;
2829/**30 * OutputFormat to write out the graph nodes as text, value-separated (by31 * tabs, by default). With the default delimiter, a vertex is written out as:32 *33 * <VertexId><tab><Vertex Value><tab>34 * [<EdgeId><tab><EdgeValue>]+35 *36 * @param <I> Vertex index value37 * @param <V> Vertex value38 * @param <E> Edge value39 */40 @SuppressWarnings("rawtypes")
41publicclass AdjacencyListTextVertexOutputFormat<I extends WritableComparable,
42 V extends Writable, E extends Writable>
43extends TextVertexOutputFormat<I, V, E> {
44/** Split delimiter */45publicstaticfinal String LINE_TOKENIZE_VALUE = "output.delimiter";
46/** Default split delimiter */47publicstaticfinal String LINE_TOKENIZE_VALUE_DEFAULT =
48 AdjacencyListTextVertexInputFormat.LINE_TOKENIZE_VALUE_DEFAULT;
4950 @Override
51publicAdjacencyListTextVertexWriter createVertexWriter(
52 TaskAttemptContext context) {
53returnnewAdjacencyListTextVertexWriter();
54 }
5556/**57 * Vertex writer associated with {@link AdjacencyListTextVertexOutputFormat}.58 */59protectedclassAdjacencyListTextVertexWriterextends60TextVertexWriterToEachLine {
61/** Cached split delimeter */62private String delimiter;
6364 @Override
65publicvoid initialize(TaskAttemptContext context) throws IOException,
66 InterruptedException {
67super.initialize(context);
68 delimiter =
69 getConf().get(LINE_TOKENIZE_VALUE, LINE_TOKENIZE_VALUE_DEFAULT);
70 }
7172 @Override
73public Text convertVertexToLine(Vertex<I, V, E> vertex)
74throws IOException {
75 StringBuffer sb = new StringBuffer(vertex.getId().toString());
76 sb.append(delimiter);
77 sb.append(vertex.getValue());
7879for (Edge<I, E> edge : vertex.getEdges()) {
80 sb.append(delimiter).append(edge.getTargetVertexId());
81 sb.append(delimiter).append(edge.getValue());
82 }
8384returnnew Text(sb.toString());
85 }
86 }
8788 }