-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathParquetWriter.java
More file actions
176 lines (146 loc) · 6.37 KB
/
ParquetWriter.java
File metadata and controls
176 lines (146 loc) · 6.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package blue.strategic.parquet;
import org.apache.hadoop.conf.Configuration;
import org.apache.parquet.column.ParquetProperties;
import org.apache.parquet.hadoop.api.WriteSupport;
import org.apache.parquet.hadoop.metadata.CompressionCodecName;
import org.apache.parquet.io.DelegatingPositionOutputStream;
import org.apache.parquet.io.OutputFile;
import org.apache.parquet.io.PositionOutputStream;
import org.apache.parquet.io.api.Binary;
import org.apache.parquet.io.api.RecordConsumer;
import org.apache.parquet.schema.LogicalTypeAnnotation;
import org.apache.parquet.schema.MessageType;
import org.apache.parquet.schema.PrimitiveType;
import java.io.Closeable;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Collections;
import static org.apache.parquet.hadoop.metadata.CompressionCodecName.SNAPPY;
public final class ParquetWriter<T> implements Closeable {
private final org.apache.parquet.hadoop.ParquetWriter<T> writer;
public static <T> ParquetWriter<T> writeFile(MessageType schema, File out, Dehydrator<T> dehydrator) throws IOException {
return writeFile(schema, out, dehydrator, SNAPPY);
}
public static <T> ParquetWriter<T> writeFile(MessageType schema, File out, Dehydrator<T> dehydrator, CompressionCodecName codecName) throws IOException {
OutputFile f = new OutputFile() {
@Override
public PositionOutputStream create(long blockSizeHint) throws IOException {
return createOrOverwrite(blockSizeHint);
}
@Override
public PositionOutputStream createOrOverwrite(long blockSizeHint) throws IOException {
FileOutputStream fos = new FileOutputStream(out);
return new DelegatingPositionOutputStream(fos) {
@Override
public long getPos() throws IOException {
return fos.getChannel().position();
}
};
}
@Override
public boolean supportsBlockSize() {
return false;
}
@Override
public long defaultBlockSize() {
return 1024L;
}
};
return writeOutputFile(schema, f, dehydrator, codecName);
}
private static <T> ParquetWriter<T> writeOutputFile(MessageType schema, OutputFile file, Dehydrator<T> dehydrator, CompressionCodecName codecName) throws IOException {
return new ParquetWriter<>(file, schema, dehydrator, codecName);
}
private ParquetWriter(OutputFile outputFile, MessageType schema, Dehydrator<T> dehydrator, CompressionCodecName codecName) throws IOException {
this.writer = new Builder<T>(outputFile)
.withType(schema)
.withDehydrator(dehydrator)
.withCompressionCodec(codecName)
.withWriterVersion(ParquetProperties.WriterVersion.PARQUET_2_0)
.build();
}
public void write(T record) throws IOException {
writer.write(record);
}
@Override
public void close() throws IOException {
this.writer.close();
}
public long getDataSize() {
return writer.getDataSize();
}
private static final class Builder<T> extends org.apache.parquet.hadoop.ParquetWriter.Builder<T, ParquetWriter.Builder<T>> {
private MessageType schema;
private Dehydrator<T> dehydrator;
private Builder(OutputFile file) {
super(file);
}
public ParquetWriter.Builder<T> withType(MessageType schema) {
this.schema = schema;
return this;
}
public ParquetWriter.Builder<T> withDehydrator(Dehydrator<T> dehydrator) {
this.dehydrator = dehydrator;
return this;
}
@Override
protected ParquetWriter.Builder<T> self() {
return this;
}
@Override
protected WriteSupport<T> getWriteSupport(Configuration conf) {
return new SimpleWriteSupport<>(schema, dehydrator);
}
}
private static class SimpleWriteSupport<T> extends WriteSupport<T> {
private final MessageType schema;
private final Dehydrator<T> dehydrator;
private final ValueWriter valueWriter = SimpleWriteSupport.this::writeField;
private RecordConsumer recordConsumer;
SimpleWriteSupport(MessageType schema, Dehydrator<T> dehydrator) {
this.schema = schema;
this.dehydrator = dehydrator;
}
@Override
public WriteContext init(Configuration configuration) {
return new WriteContext(schema, Collections.emptyMap());
}
@Override
public void prepareForWrite(RecordConsumer recordConsumer) {
this.recordConsumer = recordConsumer;
}
@Override
public void write(T record) {
recordConsumer.startMessage();
dehydrator.dehydrate(record, valueWriter);
recordConsumer.endMessage();
}
@Override
public String getName() {
return "blue.strategic.parquet.ParquetWriter";
}
private void writeField(String name, Object value) {
int fieldIndex = schema.getFieldIndex(name);
PrimitiveType type = schema.getType(fieldIndex).asPrimitiveType();
recordConsumer.startField(name, fieldIndex);
switch (type.getPrimitiveTypeName()) {
case INT32: recordConsumer.addInteger((int)value); break;
case INT64: recordConsumer.addLong((long)value); break;
case DOUBLE: recordConsumer.addDouble((double)value); break;
case BOOLEAN: recordConsumer.addBoolean((boolean)value); break;
case FLOAT: recordConsumer.addFloat((float)value); break;
case BINARY:
if (type.getLogicalTypeAnnotation() == LogicalTypeAnnotation.stringType()) {
recordConsumer.addBinary(Binary.fromString((String)value));
} else {
throw new UnsupportedOperationException("We don't support writing " + type.getLogicalTypeAnnotation());
}
break;
default:
throw new UnsupportedOperationException("We don't support writing " + type.getPrimitiveTypeName());
}
recordConsumer.endField(name, fieldIndex);
}
}
}