1717package io .cdap .plugin .elastic .sink ;
1818
1919import io .cdap .cdap .api .annotation .Description ;
20- import io .cdap .cdap .api .annotation .Macro ;
2120import io .cdap .cdap .api .annotation .Name ;
2221import io .cdap .cdap .api .annotation .Plugin ;
2322import io .cdap .cdap .api .data .batch .Output ;
2423import io .cdap .cdap .api .data .format .StructuredRecord ;
2524import io .cdap .cdap .api .dataset .lib .KeyValue ;
2625import io .cdap .cdap .etl .api .Emitter ;
26+ import io .cdap .cdap .etl .api .FailureCollector ;
27+ import io .cdap .cdap .etl .api .PipelineConfigurer ;
2728import io .cdap .cdap .etl .api .batch .BatchSink ;
2829import io .cdap .cdap .etl .api .batch .BatchSinkContext ;
2930import io .cdap .cdap .format .StructuredRecordStringConverter ;
30-
31- import io .cdap .plugin .common .ReferenceBatchSink ;
32- import io .cdap .plugin .common .ReferencePluginConfig ;
3331import io .cdap .plugin .common .batch .JobUtils ;
3432import io .cdap .plugin .common .batch .sink .SinkOutputFormatProvider ;
35- import io .cdap .plugin .elastic .ESProperties ;
3633import org .apache .hadoop .conf .Configuration ;
3734import org .apache .hadoop .io .Text ;
3835import org .apache .hadoop .io .Writable ;
5754@ Name ("Elasticsearch" )
5855@ Description ("Elasticsearch Batch Sink takes the structured record from the input source and converts it " +
5956 "to a JSON string, then indexes it in Elasticsearch using the index, type, and id specified by the user." )
60- public class BatchElasticsearchSink extends ReferenceBatchSink <StructuredRecord , Writable , Writable > {
61- private static final String INDEX_DESCRIPTION = "The name of the index where the data will be stored. " +
62- "If the index does not already exist, it will be created using Elasticsearch's default properties." ;
63- private static final String TYPE_DESCRIPTION = "The name of the type where the data will be stored. " +
64- "If it does not already exist, it will be created." ;
65- private static final String ID_DESCRIPTION = "The field that will determine the id for the document. " +
66- "It should match a fieldname in the structured record of the input." ;
67- private static final String HOST_DESCRIPTION = "The hostname and port for the Elasticsearch server; " +
68- "such as localhost:9200." ;
69- private final ESConfig config ;
57+ public class BatchElasticsearchSink extends BatchSink <StructuredRecord , Writable , Writable > {
58+ private final ElasticsearchSinkConfig config ;
7059
71- public BatchElasticsearchSink (ESConfig config ) {
72- super (config );
60+ public BatchElasticsearchSink (ElasticsearchSinkConfig config ) {
7361 this .config = config ;
7462 }
7563
64+ @ Override
65+ public void configurePipeline (PipelineConfigurer pipelineConfigurer ) {
66+ FailureCollector collector = pipelineConfigurer .getStageConfigurer ().getFailureCollector ();
67+ config .validate (collector );
68+ collector .getOrThrowException ();
69+ }
70+
7671 @ Override
7772 public void prepareRun (BatchSinkContext context ) throws IOException {
73+ FailureCollector collector = context .getFailureCollector ();
74+ config .validate (collector );
75+ collector .getOrThrowException ();
76+
7877 Job job = JobUtils .createInstance ();
7978 Configuration conf = job .getConfiguration ();
8079
8180 job .setSpeculativeExecution (false );
8281
83- conf .set ("es.nodes" , config .hostname );
84- conf .set ("es.resource.write" , String . format ( "%s/%s" , config .index , config . type ));
82+ conf .set ("es.nodes" , config .getHostname () );
83+ conf .set ("es.resource.write" , config .getResource ( ));
8584 conf .set ("es.input.json" , "yes" );
86- conf .set ("es.mapping.id" , config .idField );
85+ conf .set ("es.mapping.id" , config .getIdField () );
8786
8887 context .addOutput (Output .of (config .referenceName , new SinkOutputFormatProvider (EsOutputFormat .class , conf )));
8988 }
@@ -93,37 +92,4 @@ public void transform(StructuredRecord record, Emitter<KeyValue<Writable, Writab
9392 emitter .emit (new KeyValue <Writable , Writable >(new Text (StructuredRecordStringConverter .toJsonString (record )),
9493 new Text (StructuredRecordStringConverter .toJsonString (record ))));
9594 }
96-
97- /**
98- * Config class for BatchElasticsearchSink.java
99- */
100- public static class ESConfig extends ReferencePluginConfig {
101- @ Name (ESProperties .HOST )
102- @ Description (HOST_DESCRIPTION )
103- @ Macro
104- private String hostname ;
105-
106- @ Name (ESProperties .INDEX_NAME )
107- @ Description (INDEX_DESCRIPTION )
108- @ Macro
109- private String index ;
110-
111- @ Name (ESProperties .TYPE_NAME )
112- @ Description (TYPE_DESCRIPTION )
113- @ Macro
114- private String type ;
115-
116- @ Name (ESProperties .ID_FIELD )
117- @ Description (ID_DESCRIPTION )
118- @ Macro
119- private String idField ;
120-
121- public ESConfig (String referenceName , String hostname , String index , String type , String idField ) {
122- super (referenceName );
123- this .hostname = hostname ;
124- this .index = index ;
125- this .type = type ;
126- this .idField = idField ;
127- }
128- }
12995}
0 commit comments