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;
202122import java.io.IOException;
23import org.apache.giraph.graph.Vertex;
24import org.apache.hadoop.io.Text;
25import org.apache.hadoop.io.Writable;
26import org.apache.hadoop.io.WritableComparable;
27import org.apache.hadoop.mapreduce.TaskAttemptContext;
2829/**30 * Write out Vertices' IDs and values, but not their edges nor edges' values.31 * This is a useful output format when the final value of the vertex is32 * all that's needed. The boolean configuration parameter reverse.id.and.value33 * allows reversing the output of id and value.34 *35 * @param <I> Vertex index value36 * @param <V> Vertex value37 * @param <E> Edge value38 */39 @SuppressWarnings("rawtypes")
40publicclass IdWithValueTextOutputFormat<I extends WritableComparable,
41 V extends Writable, E extends Writable>
42extends TextVertexOutputFormat<I, V, E> {
4344/** Specify the output delimiter */45publicstaticfinal String LINE_TOKENIZE_VALUE = "output.delimiter";
46/** Default output delimiter */47publicstaticfinal String LINE_TOKENIZE_VALUE_DEFAULT = "\t";
48/** Reverse id and value order? */49publicstaticfinal String REVERSE_ID_AND_VALUE = "reverse.id.and.value";
50/** Default is to not reverse id and value order. */51publicstaticfinalboolean REVERSE_ID_AND_VALUE_DEFAULT = false;
5253 @Override
54publicTextVertexWriter createVertexWriter(TaskAttemptContext context) {
55returnnewIdWithValueVertexWriter();
56 }
5758/**59 * Vertex writer used with {@link IdWithValueTextOutputFormat}.60 */61protectedclassIdWithValueVertexWriterextendsTextVertexWriterToEachLine {
62/** Saved delimiter */63private String delimiter;
64/** Cached reserve option */65privateboolean reverseOutput;
6667 @Override
68publicvoid initialize(TaskAttemptContext context) throws IOException,
69 InterruptedException {
70super.initialize(context);
71 delimiter = getConf().get(
72 LINE_TOKENIZE_VALUE, LINE_TOKENIZE_VALUE_DEFAULT);
73 reverseOutput = getConf().getBoolean(
74 REVERSE_ID_AND_VALUE, REVERSE_ID_AND_VALUE_DEFAULT);
75 }
7677 @Override
78protected Text convertVertexToLine(Vertex<I, V, E> vertex)
79throws IOException {
8081 StringBuilder str = new StringBuilder();
82if (reverseOutput) {
83 str.append(vertex.getValue().toString());
84 str.append(delimiter);
85 str.append(vertex.getId().toString());
86 } else {
87 str.append(vertex.getId().toString());
88 str.append(delimiter);
89 str.append(vertex.getValue().toString());
90 }
91returnnew Text(str.toString());
92 }
93 }
94 }