1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.giraph.lib; |
20 | |
|
21 | |
|
22 | |
import org.apache.giraph.graph.BasicVertex; |
23 | |
import org.apache.giraph.graph.VertexWriter; |
24 | |
import org.apache.hadoop.io.Text; |
25 | |
import org.apache.hadoop.io.Writable; |
26 | |
import org.apache.hadoop.io.WritableComparable; |
27 | |
import org.apache.hadoop.mapreduce.RecordWriter; |
28 | |
import org.apache.hadoop.mapreduce.TaskAttemptContext; |
29 | |
|
30 | |
import java.io.IOException; |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
@SuppressWarnings("rawtypes") |
43 | 0 | public class IdWithValueTextOutputFormat<I extends WritableComparable, |
44 | |
V extends Writable, E extends Writable> |
45 | |
extends TextVertexOutputFormat<I, V, E> { |
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | 0 | static class IdWithValueVertexWriter<I extends WritableComparable, V extends |
55 | |
Writable, E extends Writable> extends TextVertexWriter<I, V, E> { |
56 | |
|
57 | |
public static final String LINE_TOKENIZE_VALUE = "output.delimiter"; |
58 | |
|
59 | |
public static final String LINE_TOKENIZE_VALUE_DEFAULT = "\t"; |
60 | |
|
61 | |
public static final String REVERSE_ID_AND_VALUE = "reverse.id.and.value"; |
62 | |
|
63 | |
public static final boolean REVERSE_ID_AND_VALUE_DEFAULT = false; |
64 | |
|
65 | |
private String delimiter; |
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
public IdWithValueVertexWriter(RecordWriter<Text, Text> recordWriter) { |
73 | 0 | super(recordWriter); |
74 | 0 | } |
75 | |
|
76 | |
@Override |
77 | |
public void writeVertex(BasicVertex<I, V, E, ?> vertex) throws IOException, |
78 | |
InterruptedException { |
79 | 0 | if (delimiter == null) { |
80 | 0 | delimiter = getContext().getConfiguration() |
81 | |
.get(LINE_TOKENIZE_VALUE, LINE_TOKENIZE_VALUE_DEFAULT); |
82 | |
} |
83 | |
|
84 | |
String first; |
85 | |
String second; |
86 | 0 | boolean reverseOutput = getContext().getConfiguration() |
87 | |
.getBoolean(REVERSE_ID_AND_VALUE, REVERSE_ID_AND_VALUE_DEFAULT); |
88 | |
|
89 | 0 | if (reverseOutput) { |
90 | 0 | first = vertex.getVertexValue().toString(); |
91 | 0 | second = vertex.getVertexId().toString(); |
92 | |
} else { |
93 | 0 | first = vertex.getVertexId().toString(); |
94 | 0 | second = vertex.getVertexValue().toString(); |
95 | |
} |
96 | |
|
97 | 0 | Text line = new Text(first + delimiter + second); |
98 | |
|
99 | 0 | getRecordWriter().write(line, null); |
100 | 0 | } |
101 | |
} |
102 | |
|
103 | |
@Override |
104 | |
public VertexWriter<I, V, E> createVertexWriter(TaskAttemptContext context) |
105 | |
throws IOException, InterruptedException { |
106 | 0 | return new IdWithValueVertexWriter<I, V, E> |
107 | |
(textOutputFormat.getRecordWriter(context)); |
108 | |
} |
109 | |
} |