-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
LocalJsonDestination.java
234 lines (195 loc) · 8.39 KB
/
LocalJsonDestination.java
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
* MIT License
*
* Copyright (c) 2020 Airbyte
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package io.airbyte.integrations.destination.local_json;
import com.fasterxml.jackson.databind.JsonNode;
import com.google.common.collect.ImmutableMap;
import io.airbyte.commons.json.Jsons;
import io.airbyte.commons.resources.MoreResources;
import io.airbyte.integrations.base.Destination;
import io.airbyte.integrations.base.DestinationConsumer;
import io.airbyte.integrations.base.FailureTrackingConsumer;
import io.airbyte.integrations.base.IntegrationRunner;
import io.airbyte.integrations.base.SQLNamingResolvable;
import io.airbyte.integrations.base.StandardSQLNaming;
import io.airbyte.protocol.models.AirbyteConnectionStatus;
import io.airbyte.protocol.models.AirbyteConnectionStatus.Status;
import io.airbyte.protocol.models.AirbyteMessage;
import io.airbyte.protocol.models.ConfiguredAirbyteCatalog;
import io.airbyte.protocol.models.ConfiguredAirbyteStream;
import io.airbyte.protocol.models.ConnectorSpecification;
import io.airbyte.protocol.models.SyncMode;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class LocalJsonDestination implements Destination {
private static final Logger LOGGER = LoggerFactory.getLogger(LocalJsonDestination.class);
static final String FIELD_DATA = "data";
static final String FIELD_AB_ID = "ab_id";
static final String FIELD_EMITTED_AT = "emitted_at";
static final String DESTINATION_PATH_FIELD = "destination_path";
private final SQLNamingResolvable namingResolver;
public LocalJsonDestination() {
namingResolver = new StandardSQLNaming();
}
@Override
public ConnectorSpecification spec() throws IOException {
final String resourceString = MoreResources.readResource("spec.json");
return Jsons.deserialize(resourceString, ConnectorSpecification.class);
}
@Override
public AirbyteConnectionStatus check(JsonNode config) {
try {
FileUtils.forceMkdir(getDestinationPath(config).toFile());
} catch (Exception e) {
return new AirbyteConnectionStatus().withStatus(Status.FAILED).withMessage(e.getMessage());
}
return new AirbyteConnectionStatus().withStatus(Status.SUCCEEDED);
}
@Override
public SQLNamingResolvable getNamingResolver() {
return namingResolver;
}
/**
* @param config - destination config.
* @param catalog - schema of the incoming messages.
* @return - a consumer to handle writing records to the filesystem.
* @throws IOException - exception throw in manipulating the filesystem.
*/
@Override
public DestinationConsumer<AirbyteMessage> write(JsonNode config, ConfiguredAirbyteCatalog catalog) throws IOException {
final Path destinationDir = getDestinationPath(config);
FileUtils.forceMkdir(destinationDir.toFile());
final Map<String, WriteConfig> writeConfigs = new HashMap<>();
for (final ConfiguredAirbyteStream stream : catalog.getStreams()) {
final String streamName = stream.getStream().getName();
final Path finalPath = destinationDir.resolve(getNamingResolver().getRawTableName(streamName) + ".jsonl");
final Path tmpPath = destinationDir.resolve(getNamingResolver().getTmpTableName(streamName) + ".jsonl");
final boolean isIncremental = stream.getSyncMode() == SyncMode.INCREMENTAL;
if (isIncremental && finalPath.toFile().exists()) {
Files.copy(finalPath, tmpPath, StandardCopyOption.REPLACE_EXISTING);
}
final Writer writer = new FileWriter(tmpPath.toFile(), isIncremental);
writeConfigs.put(stream.getStream().getName(), new WriteConfig(writer, tmpPath, finalPath));
}
return new JsonConsumer(writeConfigs, catalog);
}
/**
* Extract provided path.
*
* @param config - config object
* @return absolute path where to write files.
*/
private Path getDestinationPath(JsonNode config) {
Path destinationPath = Paths.get(config.get(DESTINATION_PATH_FIELD).asText());
if (!destinationPath.startsWith("/local"))
destinationPath = Path.of("/local").resolve(destinationPath);
return destinationPath;
}
/**
* This consumer writes individual records to temporary files. If all of the messages are written
* successfully, it moves the tmp files to files named by their respective stream. If there are any
* failures, nothing is written.
*/
private static class JsonConsumer extends FailureTrackingConsumer<AirbyteMessage> {
private final Map<String, WriteConfig> writeConfigs;
private final ConfiguredAirbyteCatalog catalog;
public JsonConsumer(Map<String, WriteConfig> writeConfigs, ConfiguredAirbyteCatalog catalog) {
LOGGER.info("initializing consumer.");
this.catalog = catalog;
this.writeConfigs = writeConfigs;
}
@Override
protected void acceptTracked(AirbyteMessage message) throws Exception {
// ignore other message types.
if (message.getType() == AirbyteMessage.Type.RECORD) {
if (!writeConfigs.containsKey(message.getRecord().getStream())) {
throw new IllegalArgumentException(
String.format("Message contained record from a stream that was not in the catalog. \ncatalog: %s , \nmessage: %s",
Jsons.serialize(catalog), Jsons.serialize(message)));
}
final Writer writer = writeConfigs.get(message.getRecord().getStream()).getWriter();
writer.write(Jsons.serialize(ImmutableMap.of(
FIELD_AB_ID, UUID.randomUUID(),
FIELD_EMITTED_AT, message.getRecord().getEmittedAt(),
FIELD_DATA, message.getRecord().getData())));
writer.write(System.lineSeparator());
}
}
@Override
protected void close(boolean hasFailed) throws IOException {
LOGGER.info("finalizing consumer.");
for (final Map.Entry<String, WriteConfig> entries : writeConfigs.entrySet()) {
try {
entries.getValue().getWriter().flush();
entries.getValue().getWriter().close();
} catch (Exception e) {
hasFailed = true;
LOGGER.error("failed to close writer for: {}.", entries.getKey());
}
}
// do not persist the data, if there are any failures.
if (!hasFailed) {
for (final WriteConfig writeConfig : writeConfigs.values()) {
Files.move(writeConfig.getTmpPath(), writeConfig.getFinalPath(), StandardCopyOption.REPLACE_EXISTING);
}
}
// clean up tmp files.
for (final WriteConfig writeConfig : writeConfigs.values()) {
Files.deleteIfExists(writeConfig.getTmpPath());
}
}
}
private static class WriteConfig {
private final Writer writer;
private final Path tmpPath;
private final Path finalPath;
public WriteConfig(Writer writer, Path tmpPath, Path finalPath) {
this.writer = writer;
this.tmpPath = tmpPath;
this.finalPath = finalPath;
}
public Writer getWriter() {
return writer;
}
public Path getTmpPath() {
return tmpPath;
}
public Path getFinalPath() {
return finalPath;
}
}
public static void main(String[] args) throws Exception {
new IntegrationRunner(new LocalJsonDestination()).run(args);
}
}