This repository has been archived by the owner on May 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Kafka 0.9.0 output operators and unit tests.
1. Abstract Base class 2. Kafka Output operator 3. Exactly Once output operator Key in the Kafka message is used by the operator to track the tuples written by it.
- Loading branch information
Showing
5 changed files
with
944 additions
and
2 deletions.
There are no files selected for viewing
122 changes: 122 additions & 0 deletions
122
kafka/src/main/java/org/apache/apex/malhar/kafka/AbstractKafkaOutputOperator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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 org.apache.apex.malhar.kafka; | ||
|
||
import java.util.Properties; | ||
import javax.validation.constraints.NotNull; | ||
|
||
import org.apache.kafka.clients.producer.KafkaProducer; | ||
import org.apache.kafka.clients.producer.Producer; | ||
import com.datatorrent.api.Context; | ||
import com.datatorrent.api.Operator; | ||
|
||
/** | ||
* This is the base implementation of a Kafka output operator(0.9.0), which writes data to the Kafka message bus. | ||
* | ||
* @displayName Abstract Kafka Output | ||
* @category Messaging | ||
* @tags output operator | ||
* | ||
*/ | ||
@org.apache.hadoop.classification.InterfaceStability.Evolving | ||
public abstract class AbstractKafkaOutputOperator<K, V> implements Operator | ||
{ | ||
private transient Producer<K, V> producer; | ||
@NotNull | ||
private String topic; | ||
private Properties properties = new Properties(); | ||
|
||
@Override | ||
public void setup(Context.OperatorContext context) | ||
{ | ||
producer = new KafkaProducer<K, V>(properties); | ||
} | ||
|
||
/** | ||
* Implement Component Interface. | ||
*/ | ||
@Override | ||
public void teardown() | ||
{ | ||
producer.close(); | ||
} | ||
|
||
/** | ||
* Implement Operator Interface. | ||
*/ | ||
@Override | ||
public void beginWindow(long windowId) | ||
{ | ||
} | ||
|
||
/** | ||
* Implement Operator Interface. | ||
*/ | ||
@Override | ||
public void endWindow() | ||
{ | ||
} | ||
|
||
public Properties getProperties() | ||
{ | ||
return properties; | ||
} | ||
|
||
/** | ||
* Set the Kafka producer properties. | ||
* | ||
* @param properties Producer properties | ||
*/ | ||
public void setProperties(Properties properties) | ||
{ | ||
this.properties.putAll(properties); | ||
} | ||
|
||
/** | ||
* Set the Kafka producer property. | ||
* | ||
* @param key Producer Property name | ||
* @param val Producer Property value | ||
*/ | ||
public void setProperty(Object key, Object val) | ||
{ | ||
properties.put(key,val); | ||
} | ||
|
||
public String getTopic() | ||
{ | ||
return topic; | ||
} | ||
|
||
/** | ||
* Set the Kafka topic | ||
* @param topic Kafka topic for which the data is sent | ||
*/ | ||
public void setTopic(String topic) | ||
{ | ||
this.topic = topic; | ||
} | ||
|
||
protected Producer<K, V> getProducer() | ||
{ | ||
return producer; | ||
} | ||
} | ||
|
Oops, something went wrong.