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 java.io.IOException;
2223import org.apache.giraph.edge.Edge;
24import org.apache.hadoop.io.Text;
25import org.apache.hadoop.io.Writable;
26import org.apache.hadoop.io.WritableComparable;
27import org.apache.hadoop.mapreduce.TaskAttemptContext;
2829importstatic org.apache.giraph.conf.GiraphConstants.GIRAPH_TEXT_OUTPUT_FORMAT_SEPARATOR;
30importstatic org.apache.giraph.conf.GiraphConstants.GIRAPH_TEXT_OUTPUT_FORMAT_REVERSE;
3132/**33 * Write out Edge Value with Source and Destination ID, but not the vertex34 * value.35 * This is a demostration output format to show the possibility to separately36 * output edges from vertices.37 *38 * @param <I> Vertex index value39 * @param <V> Vertex value40 * @param <E> Edge value41 */42 @SuppressWarnings("rawtypes")
43publicclass SrcIdDstIdEdgeValueTextOutputFormat<I extends WritableComparable,
44 V extends Writable, E extends Writable>
45extends TextEdgeOutputFormat<I, V, E> {
4647 @Override
48publicTextEdgeWriter createEdgeWriter(TaskAttemptContext context) {
49returnnewSrcIdDstIdEdgeValueEdgeWriter();
50 }
5152/**53 * Edge writer used with {@link SrcIdDstIdEdgeValueTextOutputFormat}.54 */55protectedclass SrcIdDstIdEdgeValueEdgeWriter<I extends WritableComparable,
56 V extends Writable, E extends Writable>
57extends TextEdgeWriterToEachLine<I, V, E> {
5859/** Saved delimiter */60private String delimiter;
61/** Cached reserve option */62privateboolean reverseOutput;
6364 @Override
65publicvoid initialize(TaskAttemptContext context)
66throws IOException, InterruptedException {
67super.initialize(context);
68 delimiter = GIRAPH_TEXT_OUTPUT_FORMAT_SEPARATOR.get(getConf());
69 reverseOutput = GIRAPH_TEXT_OUTPUT_FORMAT_REVERSE.get(getConf());
70 }
7172 @Override
73protected Text convertEdgeToLine(I sourceId, V sourceValue, Edge<I, E> edge)
74throws IOException {
75 StringBuilder msg = new StringBuilder();
76if (reverseOutput) {
77 msg.append(edge.getValue().toString());
78 msg.append(delimiter);
79 msg.append(edge.getTargetVertexId().toString());
80 msg.append(delimiter);
81 msg.append(sourceId.toString());
82 } else {
83 msg.append(sourceId.toString());
84 msg.append(delimiter);
85 msg.append(edge.getTargetVertexId().toString());
86 msg.append(delimiter);
87 msg.append(edge.getValue().toString());
88 }
89returnnew Text(msg.toString());
90 }
91 }
92 }