This repository has been archived by the owner on Jul 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
HubspotBatchSource.java
87 lines (77 loc) · 3.36 KB
/
HubspotBatchSource.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
/*
* Copyright © 2020 Cask Data, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package io.cdap.plugin.hubspot.source.batch;
import com.google.gson.JsonElement;
import io.cdap.cdap.api.annotation.Description;
import io.cdap.cdap.api.annotation.Name;
import io.cdap.cdap.api.annotation.Plugin;
import io.cdap.cdap.api.data.batch.Input;
import io.cdap.cdap.api.data.format.StructuredRecord;
import io.cdap.cdap.api.data.schema.Schema;
import io.cdap.cdap.api.dataset.lib.KeyValue;
import io.cdap.cdap.etl.api.Emitter;
import io.cdap.cdap.etl.api.FailureCollector;
import io.cdap.cdap.etl.api.PipelineConfigurer;
import io.cdap.cdap.etl.api.batch.BatchSource;
import io.cdap.cdap.etl.api.batch.BatchSourceContext;
import io.cdap.plugin.common.IdUtils;
import io.cdap.plugin.common.LineageRecorder;
import io.cdap.plugin.hubspot.common.HubspotHelper;
import io.cdap.plugin.hubspot.common.SourceHubspotConfig;
import org.apache.hadoop.io.NullWritable;
import java.util.stream.Collectors;
/**
* Plugin reads Hubspot objects in batch.
*/
@Plugin(type = BatchSource.PLUGIN_TYPE)
@Name(HubspotBatchSource.NAME)
@Description("Plugin reads Hubspot objects in batch")
public class HubspotBatchSource extends BatchSource<NullWritable, JsonElement, StructuredRecord> {
private final SourceHubspotConfig config;
public static final String NAME = "Hubspot";
public HubspotBatchSource(SourceHubspotConfig config) {
this.config = config;
}
@Override
public void configurePipeline(PipelineConfigurer pipelineConfigurer) {
validateConfiguration(pipelineConfigurer.getStageConfigurer().getFailureCollector());
pipelineConfigurer.getStageConfigurer().setOutputSchema(config.getSchema());
}
/**
* Prepare Hubspot objects.
* @param context the batch source context
* @throws Exception on issues with reading the hubspot objects
*/
public void prepareRun(BatchSourceContext context) throws Exception {
validateConfiguration(context.getFailureCollector());
LineageRecorder lineageRecorder = new LineageRecorder(context, config.referenceName);
lineageRecorder.createExternalDataset(config.getSchema());
lineageRecorder.recordRead("Reads", "Reading Hubspot objects",
config.getSchema().getFields().stream()
.map(Schema.Field::getName)
.collect(Collectors.toList()));
context.setInput(Input.of(NAME, new HubspotInputFormatProvider(config)));
}
@Override
public void transform(KeyValue<NullWritable, JsonElement> input, Emitter<StructuredRecord> emitter) {
emitter.emit(HubspotHelper.transform(input.getValue().toString(), config));
}
private void validateConfiguration(FailureCollector failureCollector) {
IdUtils.validateReferenceName(config.referenceName, failureCollector);
config.validate(failureCollector);
failureCollector.getOrThrowException();
}
}