Initial TCA commit into DCAEGEN2
[dcaegen2/analytics/tca.git] / dcae-analytics-cdap-plugins / src / main / java / org / openecomp / dcae / apod / analytics / cdap / plugins / batch / sink / dmaap / DMaaPMROutputFormat.java
1 /*
2  * ===============================LICENSE_START======================================
3  *  dcae-analytics
4  * ================================================================================
5  *    Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  *  Licensed under the Apache License, Version 2.0 (the "License");
8  *  you may not use this file except in compliance with the License.
9  *   You may obtain a copy of the License at
10  *
11  *          http://www.apache.org/licenses/LICENSE-2.0
12  *
13  *  Unless required by applicable law or agreed to in writing, software
14  *  distributed under the License is distributed on an "AS IS" BASIS,
15  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *  See the License for the specific language governing permissions and
17  *  limitations under the License.
18  *  ============================LICENSE_END===========================================
19  */
20
21 package org.openecomp.dcae.apod.analytics.cdap.plugins.batch.sink.dmaap;
22
23 import org.apache.hadoop.conf.Configuration;
24 import org.apache.hadoop.io.NullWritable;
25 import org.apache.hadoop.mapreduce.JobContext;
26 import org.apache.hadoop.mapreduce.OutputCommitter;
27 import org.apache.hadoop.mapreduce.OutputFormat;
28 import org.apache.hadoop.mapreduce.RecordWriter;
29 import org.apache.hadoop.mapreduce.TaskAttemptContext;
30 import org.openecomp.dcae.apod.analytics.cdap.plugins.utils.DMaaPSinkConfigMapper;
31 import org.openecomp.dcae.apod.analytics.dmaap.DMaaPMRFactory;
32 import org.openecomp.dcae.apod.analytics.dmaap.domain.config.DMaaPMRPublisherConfig;
33 import org.openecomp.dcae.apod.analytics.dmaap.service.publisher.DMaaPMRPublisher;
34
35 import java.io.IOException;
36
37 /**
38  * DMaaP MR Output format used by DMaaP MR Sink Plugin to create a MR Publisher and pass to custom {@link
39  * DMaaPMRRecordWriter}
40  * <p>
41  * @author Rajiv Singla . Creation Date: 1/27/2017.
42  */
43 public class DMaaPMROutputFormat extends OutputFormat<String, NullWritable> {
44
45     @Override
46     public RecordWriter<String, NullWritable> getRecordWriter(TaskAttemptContext context) throws IOException,
47             InterruptedException {
48         final Configuration configuration = context.getConfiguration();
49         final DMaaPMRPublisherConfig publisherConfig = DMaaPSinkConfigMapper.map(configuration);
50         final DMaaPMRPublisher publisher = DMaaPMRFactory.create().createPublisher(publisherConfig);
51         return new DMaaPMRRecordWriter(publisher);
52     }
53
54     @Override
55     public void checkOutputSpecs(JobContext context) throws IOException, InterruptedException {
56         // do nothing
57     }
58
59     @Override
60     public OutputCommitter getOutputCommitter(TaskAttemptContext context) throws IOException, InterruptedException {
61         return new NoOpOutputCommitter();
62     }
63
64     /**
65      * A dummy implementation for {@link OutputCommitter} that does nothing.
66      */
67     protected static class NoOpOutputCommitter extends OutputCommitter {
68
69         @Override
70         public void setupJob(JobContext jobContext) throws IOException {
71             // no op
72         }
73
74         @Override
75         public void setupTask(TaskAttemptContext taskContext) throws IOException {
76             // no op
77         }
78
79         @Override
80         public boolean needsTaskCommit(TaskAttemptContext taskContext) throws IOException {
81             return false;
82         }
83
84         @Override
85         public void commitTask(TaskAttemptContext taskContext) throws IOException {
86             // no op
87         }
88
89         @Override
90         public void abortTask(TaskAttemptContext taskContext) throws IOException {
91             // no op
92         }
93     }
94 }