Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
Kafka 0.9.0 output operators and unit tests.
Browse files Browse the repository at this point in the history
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
sandeshh committed Jul 12, 2016
1 parent 85566a3 commit 0444ced
Show file tree
Hide file tree
Showing 5 changed files with 944 additions and 2 deletions.
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;
}
}

0 comments on commit 0444ced

Please sign in to comment.