kafka.producer.ProducerConfig Java Examples

The following examples show how to use kafka.producer.ProducerConfig. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SendSampleDataToKafka.java    From eagle with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws URISyntaxException, IOException {
    String data = "{" +
        "\"host\":\"localhost\", " +
        "\"timestamp\": 1480319109000, " +
        "\"metric\": \"hadoop.cpu.usage\", " +
        "\"component\": \"namenode\", " +
        "\"site\": \"test\", " +
        "\"value\": 0.98}";
    KafkaStreamSinkConfig config = new KafkaStreamProvider().getSinkConfig("HADOOP_JMX_METRIC_STREAM",ConfigFactory.load());
    Properties properties = new Properties();
    properties.put("metadata.broker.list", config.getBrokerList());
    properties.put("serializer.class", config.getSerializerClass());
    properties.put("key.serializer.class", config.getKeySerializerClass());
    // new added properties for async producer
    properties.put("producer.type", config.getProducerType());
    properties.put("batch.num.messages", config.getNumBatchMessages());
    properties.put("request.required.acks", config.getRequestRequiredAcks());
    properties.put("queue.buffering.max.ms", config.getMaxQueueBufferMs());
    ProducerConfig producerConfig = new ProducerConfig(properties);
    kafka.javaapi.producer.Producer producer = new kafka.javaapi.producer.Producer(producerConfig);
    try {
        producer.send(new KeyedMessage(config.getTopicId(), data));
    } finally {
        producer.close();
    }
}
 
Example #2
Source File: PulsarKafkaProducerTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Test
public void testPulsarKafkaProducerWithDefaultConfig() throws Exception {
    // https://kafka.apache.org/08/documentation.html#producerconfigs
    Properties properties = new Properties();
    properties.put(BROKER_URL, "http://localhost:8080/");

    ProducerConfig config = new ProducerConfig(properties);
    PulsarKafkaProducer<byte[], byte[]> producer = new PulsarKafkaProducer<>(config);
    ProducerBuilderImpl<byte[]> producerBuilder = (ProducerBuilderImpl<byte[]>) producer.getPulsarProducerBuilder();
    Field field = ProducerBuilderImpl.class.getDeclaredField("conf");
    field.setAccessible(true);
    ProducerConfigurationData conf = (ProducerConfigurationData) field.get(producerBuilder);
    System.out.println("getMaxPendingMessages= " + conf.getMaxPendingMessages());
    assertEquals(conf.getCompressionType(), CompressionType.NONE);
    assertEquals(conf.isBlockIfQueueFull(), true);
    assertEquals(conf.getMaxPendingMessages(), 1000);
    assertEquals(conf.getBatchingMaxPublishDelayMicros(), TimeUnit.MILLISECONDS.toMicros(1));
    assertEquals(conf.getBatchingMaxMessages(), 1000);
}
 
Example #3
Source File: IoTDataProducer.java    From iot-traffic-monitor with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
	//read config file
	Properties prop = PropertyFileReader.readPropertyFile();		
	String zookeeper = prop.getProperty("com.iot.app.kafka.zookeeper");
	String brokerList = prop.getProperty("com.iot.app.kafka.brokerlist");
	String topic = prop.getProperty("com.iot.app.kafka.topic");
	logger.info("Using Zookeeper=" + zookeeper + " ,Broker-list=" + brokerList + " and topic " + topic);

	// set producer properties
	Properties properties = new Properties();
	properties.put("zookeeper.connect", zookeeper);
	properties.put("metadata.broker.list", brokerList);
	properties.put("request.required.acks", "1");
	properties.put("serializer.class", "com.iot.app.kafka.util.IoTDataEncoder");
	//generate event
	Producer<String, IoTData> producer = new Producer<String, IoTData>(new ProducerConfig(properties));
	IoTDataProducer iotProducer = new IoTDataProducer();
	iotProducer.generateIoTEvent(producer,topic);		
}
 
Example #4
Source File: AbstractKafkaOutputOperator.java    From attic-apex-malhar with Apache License 2.0 6 votes vote down vote up
/**
 * setup producer configuration.
 * @return ProducerConfig
 */
protected ProducerConfig createKafkaProducerConfig()
{
  Properties prop = new Properties();
  for (String propString : producerProperties.split(",")) {
    if (!propString.contains("=")) {
      continue;
    }
    String[] keyVal = StringUtils.trim(propString).split("=");
    prop.put(StringUtils.trim(keyVal[0]), StringUtils.trim(keyVal[1]));
  }

  configProperties.putAll(prop);

  return new ProducerConfig(configProperties);
}
 
Example #5
Source File: ProducerDemo.java    From KafkaExample with Apache License 2.0 6 votes vote down vote up
private static Producer<String, String> initProducer() {
    Properties props = new Properties();
    props.put("metadata.broker.list", BROKER_LIST);
    // props.put("serializer.class", "kafka.serializer.StringEncoder");
    props.put("serializer.class", StringEncoder.class.getName());
    props.put("partitioner.class", HashPartitioner.class.getName());
//    props.put("compression.codec", "0");
    props.put("producer.type", "sync");
    props.put("batch.num.messages", "1");
    props.put("queue.buffering.max.messages", "1000000");
    props.put("queue.enqueue.timeout.ms", "20000000");

    
    ProducerConfig config = new ProducerConfig(props);
    Producer<String, String> producer = new Producer<String, String>(config);
    return producer;
  }
 
Example #6
Source File: KafkaTestProducer.java    From attic-apex-malhar with Apache License 2.0 6 votes vote down vote up
private ProducerConfig createProducerConfig(int cid)
  {
    Properties props = new Properties();
    props.setProperty("serializer.class", "kafka.serializer.StringEncoder");
    props.setProperty("key.serializer.class", "kafka.serializer.StringEncoder");
//    props.put("metadata.broker.list", "localhost:"+KafkaOperatorTestBase.TEST_KAFKA_BROKER1_PORT );
    if (hasPartition) {
      props.put("metadata.broker.list", "localhost:" + KafkaOperatorTestBase.TEST_KAFKA_BROKER_PORT[cid][0] + ",localhost:" + KafkaOperatorTestBase.TEST_KAFKA_BROKER_PORT[cid][1]);
      props.setProperty("partitioner.class", KafkaTestPartitioner.class.getCanonicalName());
    } else {
      props.put("metadata.broker.list", "localhost:" + KafkaOperatorTestBase.TEST_KAFKA_BROKER_PORT[cid][0] );
    }
    props.setProperty("topic.metadata.refresh.interval.ms", "20000");

    props.setProperty("producer.type", getProducerType());

    return new ProducerConfig(props);
  }
 
Example #7
Source File: KafkaProducer08.java    From datacollector with Apache License 2.0 6 votes vote down vote up
@Override
public void init() throws StageException {
  Properties props = new Properties();
  //metadata.broker.list
  props.put(METADATA_BROKER_LIST_KEY, metadataBrokerList);
  //producer.type
  props.put(PRODUCER_TYPE_KEY, PRODUCER_TYPE_DEFAULT);
  //key.serializer.class
  props.put(KEY_SERIALIZER_CLASS_KEY, STRING_ENCODER_CLASS);
  //partitioner.class
  configurePartitionStrategy(props, partitionStrategy);
  //serializer.class
  configureSerializer(props, producerPayloadType);
  //request.required.acks
  props.put(REQUEST_REQUIRED_ACKS_KEY, REQUEST_REQUIRED_ACKS_DEFAULT);

  addUserConfiguredProperties(props);

  ProducerConfig config = new ProducerConfig(props);
  producer = new Producer<>(config);
}
 
Example #8
Source File: KafkaUtils.java    From yuzhouwan with Apache License 2.0 6 votes vote down vote up
static Producer<String, byte[]> createProducer() {
        Properties props = new Properties();
        try {
//            props.put("zk.connect", p.getProperty("kafka.zk.connect"));   // not need zk in new version
            props.put("key.serializer.class", p.getProperty("kafka.key.serializer.class"));
            props.put("serializer.class", p.getProperty("kafka.serializer.class"));
            props.put("metadata.broker.list", p.getProperty("kafka.metadata.broker.list"));
            props.put("request.required.acks", p.getProperty("kafka.request.required.acks"));
            props.put("producer.type", p.getProperty("kafka.async"));
            props.put("partitioner.class", PARTITIONER_CLASS_NAME);

            props.put("queue.buffering.max.ms", p.getProperty("kafka.queue.buffering.max.ms"));
            props.put("queue.buffering.max.messages", p.getProperty("kafka.queue.buffering.max.messages"));
            props.put("queue.enqueue.timeout.ms", p.getProperty("kafka.queue.enqueue.timeout.ms"));
            // 41,0000,0000 / 24 / 60 / 60 = 47454 / 24 = 1977
            props.put("batch.num.messages", p.getProperty("kafka.batch.num.messages"));
            props.put("send.buffer.bytes", p.getProperty("kafka.send.buffer.bytes"));
//            props.put("compression.type", "lz4");
        } catch (Exception e) {
            _log.error("Connect with kafka failed {}!", e.getMessage());
            throw new RuntimeException(e);
        }
        _log.info("Connect with kafka successfully!");
        return new Producer<>(new ProducerConfig(props));
    }
 
Example #9
Source File: KafkaChannel.java    From flume-plugin with Apache License 2.0 6 votes vote down vote up
@Override
public void start() {
    try {
        LOGGER.info("Starting Kafka Channel: " + getName());
        producer = new Producer<String, byte[]>(new ProducerConfig(kafkaConf));
        // We always have just one topic being read by one thread
        LOGGER.info("Topic = " + topic.get());
        topicCountMap.put(topic.get(), 1);
        counter.start();
        super.start();
    } catch (Exception e) {
        LOGGER.error("Could not start producer");
        throw new FlumeException("Unable to create Kafka Connections. " +
                "Check whether Kafka Brokers are up and that the " +
                "Flume agent can connect to it.", e);
    }
}
 
Example #10
Source File: KafkaProducerMeta.java    From pentaho-kafka-producer with Apache License 2.0 6 votes vote down vote up
public void check(List<CheckResultInterface> remarks, TransMeta transMeta, StepMeta stepMeta, RowMetaInterface prev,
		String input[], String output[], RowMetaInterface info) {

	if (isEmpty(topic)) {
		remarks.add(new CheckResult(CheckResultInterface.TYPE_RESULT_ERROR,
				Messages.getString("KafkaProducerMeta.Check.InvalidTopic"), stepMeta));
	}
	if (isEmpty(messageField)) {
		remarks.add(new CheckResult(CheckResultInterface.TYPE_RESULT_ERROR,
				Messages.getString("KafkaProducerMeta.Check.InvalidMessageField"), stepMeta));
	}
	try {
		new ProducerConfig(kafkaProperties);
	} catch (IllegalArgumentException e) {
		remarks.add(new CheckResult(CheckResultInterface.TYPE_RESULT_ERROR, e.getMessage(), stepMeta));
	}
}
 
Example #11
Source File: PulsarKafkaProducerTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Test
public void testPulsarKafkaProducer() throws Exception {
    // https://kafka.apache.org/08/documentation.html#producerconfigs
    Properties properties = new Properties();
    properties.put(BROKER_URL, "http://localhost:8080/");
    properties.put(COMPRESSION_CODEC, "gzip"); // compression: ZLIB
    properties.put(QUEUE_ENQUEUE_TIMEOUT_MS, "-1"); // block queue if full => -1 = true
    properties.put(QUEUE_BUFFERING_MAX_MESSAGES, "6000"); // queue max message
    properties.put(QUEUE_BUFFERING_MAX_MS, "100"); // batch delay
    properties.put(BATCH_NUM_MESSAGES, "500"); // batch msg
    properties.put(CLIENT_ID, "test");
    ProducerConfig config = new ProducerConfig(properties);
    PulsarKafkaProducer<byte[], byte[]> producer = new PulsarKafkaProducer<>(config);
    ProducerBuilderImpl<byte[]> producerBuilder = (ProducerBuilderImpl<byte[]>) producer.getPulsarProducerBuilder();
    Field field = ProducerBuilderImpl.class.getDeclaredField("conf");
    field.setAccessible(true);
    ProducerConfigurationData conf = (ProducerConfigurationData) field.get(producerBuilder);
    assertEquals(conf.getCompressionType(), CompressionType.ZLIB);
    assertEquals(conf.isBlockIfQueueFull(), true);
    assertEquals(conf.getMaxPendingMessages(), 6000);
    assertEquals(conf.getBatchingMaxPublishDelayMicros(), TimeUnit.MILLISECONDS.toMicros(100));
    assertEquals(conf.getBatchingMaxMessages(), 500);
}
 
Example #12
Source File: KafkaStreamProxyProducerImpl.java    From eagle with Apache License 2.0 6 votes vote down vote up
public KafkaStreamProxyProducerImpl(String streamId, StreamSinkConfig streamConfig) {
    Preconditions.checkNotNull(streamConfig, "Stream sink config for " + streamId + " is null");
    this.streamId = streamId;
    this.config = (KafkaStreamSinkConfig) streamConfig;
    Properties properties = new Properties();
    Preconditions.checkNotNull(config.getBrokerList(), "brokerList is null");
    properties.put("metadata.broker.list", config.getBrokerList());
    properties.put("serializer.class", config.getSerializerClass());
    properties.put("key.serializer.class", config.getKeySerializerClass());
    // new added properties for async producer
    properties.put("producer.type", config.getProducerType());
    properties.put("batch.num.messages", config.getNumBatchMessages());
    properties.put("request.required.acks", config.getRequestRequiredAcks());
    properties.put("queue.buffering.max.ms", config.getMaxQueueBufferMs());
    ProducerConfig producerConfig = new ProducerConfig(properties);
    this.producer = new Producer(producerConfig);
}
 
Example #13
Source File: NativeProducer.java    From spring-kafka-demo with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
	String topic= "test";
	long events = 100;
       Random rand = new Random();

       Properties props = new Properties();
       props.put("metadata.broker.list", "localhost:9092");
       props.put("serializer.class", "kafka.serializer.StringEncoder");
       props.put("request.required.acks", "1");

       ProducerConfig config = new ProducerConfig(props);

       Producer<String, String> producer = new Producer<String, String>(config);

       for (long nEvents = 0; nEvents < events; nEvents++) {                
              String msg = "NativeMessage-" + rand.nextInt() ; 
              KeyedMessage<String, String> data = new KeyedMessage<String, String>(topic, nEvents + "", msg);
              producer.send(data);
       }
       producer.close();

}
 
Example #14
Source File: Bridge.java    From MQTTKafkaBridge with Apache License 2.0 6 votes vote down vote up
private void connect(String serverURI, String clientId, String zkConnect) throws MqttException {
	
	mqtt = new MqttAsyncClient(serverURI, clientId);
	mqtt.setCallback(this);
	IMqttToken token = mqtt.connect();
	Properties props = new Properties();
	
	//Updated based on Kafka v0.8.1.1
	props.put("metadata.broker.list", "localhost:9092");
       props.put("serializer.class", "kafka.serializer.StringEncoder");
       props.put("partitioner.class", "example.producer.SimplePartitioner");
       props.put("request.required.acks", "1");
	
	ProducerConfig config = new ProducerConfig(props);
	kafkaProducer = new Producer<String, String>(config);
	token.waitForCompletion();
	logger.info("Connected to MQTT and Kafka");
}
 
Example #15
Source File: KafkaProducerFactory.java    From mod-kafka with Apache License 2.0 6 votes vote down vote up
/**
 * Creates kafka producers based on given message serializer type.
 *
 * @param serializerType    message serializer type
 * @param properties        properties to be used to send a message
 * @return                  created kafka producer
 */
public Producer createProducer(MessageSerializerType serializerType, Properties properties) {
    Producer producer;

    switch (serializerType) {
            case BYTE_SERIALIZER:
                producer = new Producer<String, byte[]>(new ProducerConfig(properties));
                break;
            case STRING_SERIALIZER:
                producer = new Producer<String, String>(new ProducerConfig(properties));
                break;
            default:
                throw new IllegalArgumentException("Incorrect serialazier class specified...");
    }
    return producer;
}
 
Example #16
Source File: KafkaSink.java    From cep with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Creates a new KafkaSink.
 * This method initializes and starts a new Kafka producer that will be
 * used to produce messages to kafka topics.
 * @param properties KafkaSink propertiers. You should provide a kafka_broker property
 *                   set to the Kafka host address. If none is provided localhost will
 *                   be used
 */

public KafkaSink(Map<String, Object> properties) {
    super(properties);
    // The producer config attributes
    Properties props = new Properties();
    if (properties != null && properties.get("kafka_brokers") != null) {
        props.put("metadata.broker.list", properties.get("kafka_brokers"));
    } else {
        props.put("metadata.broker.list", "127.0.0.1:9092");
    }
    props.put("serializer.class", "kafka.serializer.StringEncoder");
    props.put("request.required.acks", "1");
    props.put("message.send.max.retries", "60");
    props.put("retry.backoff.ms", "1000");
    props.put("producer.type", "async");
    props.put("queue.buffering.max.messages", "10000");
    props.put("queue.buffering.max.ms", "500");
    props.put("partitioner.class", "net.redborder.cep.sinks.kafka.SimplePartitioner");

    // Initialize the producer
    ProducerConfig config = new ProducerConfig(props);
    producer = new Producer<>(config);
}
 
Example #17
Source File: KafkaEventPublisherClient.java    From product-cep with Apache License 2.0 6 votes vote down vote up
public static void publish(String url, String topic, String testCaseFolderName, String dataFileName) {
    log.info("Starting Kafka EventPublisher Client");

    Properties props = new Properties();
    props.put("metadata.broker.list", url);
    props.put("producer.type", "sync");
    props.put("serializer.class", "kafka.serializer.StringEncoder");

    ProducerConfig config = new ProducerConfig(props);
    Producer<String, Object> producer = new Producer<String, Object>(config);

    try {
        List<String> messagesList = readMsg(getTestDataFileLocation(testCaseFolderName, dataFileName));
        for (String message : messagesList) {
            log.info(String.format("Sending message: %s", message));
            KeyedMessage<String, Object> data = new KeyedMessage<String, Object>(topic, message);
            producer.send(data);
            Thread.sleep(100);
        }
        Thread.sleep(1000);
    } catch (Throwable t) {
        log.error("Error when sending the messages", t);
    } finally {
        producer.close();
    }
}
 
Example #18
Source File: kafkaProducer.java    From Transwarp-Sample-Code with MIT License 6 votes vote down vote up
/**
 * 读取配置文件,创建线程池,运行线程
 */
public void go() {
    Constant constant = new Constant();
    kafkaProperties kafkaProperties = new kafkaProperties();
    ProducerConfig config = new ProducerConfig(kafkaProperties.properties());

    ExecutorService executorService = Executors.newFixedThreadPool(Integer.parseInt(constant.THREAD_POOL_SIZE));

    String topic = constant.TOPIC_NAME;
    Task[] tasks = new Task[Integer.parseInt(constant.THREAD_NUM)];
    String[] folders = constant.FILE_FOLDERS.split(";");
    int batchSize = Integer.parseInt(constant.BATCH_SIZE);
    CopyOnWriteArrayList<String> fileList = addFiles(folders);

    for (int i = 0; i < tasks.length; ++i) {
        tasks[i] = new Task(i, topic, new Producer<String, String>(config), fileList, batchSize);
    }

    for (Task task : tasks) {
        executorService.execute(task);
    }
    executorService.shutdown();
}
 
Example #19
Source File: kafkaProducer.java    From Transwarp-Sample-Code with MIT License 6 votes vote down vote up
/**
 * 读取配置文件,创建线程池,运行线程
 */
public void go() {
    Constant constant = new Constant();
    kafkaProperties kafkaProperties = new kafkaProperties();
    ProducerConfig config = new ProducerConfig(kafkaProperties.properties());

    ExecutorService executorService = Executors.newFixedThreadPool(Integer.parseInt(constant.THREAD_POOL_SIZE));

    String topic = constant.TOPIC_NAME;
    Task[] tasks = new Task[Integer.parseInt(constant.THREAD_NUM)];
    String[] folders = constant.FILE_FOLDERS.split(";");
    int batchSize = Integer.parseInt(constant.BATCH_SIZE);
    CopyOnWriteArrayList<String> fileList = addFiles(folders);

    for (int i = 0; i < tasks.length; ++i) {
        tasks[i] = new Task(i, topic, new Producer<String, String>(config), fileList, batchSize);
    }

    for (Task task : tasks) {
        executorService.execute(task);
    }
    executorService.shutdown();
}
 
Example #20
Source File: SpringTest.java    From metrics-kafka with Apache License 2.0 6 votes vote down vote up
public void startKafkaReporter(){
	String hostName = "192.168.66.30";
	String topic = "test-kafka-reporter";
	Properties props = new Properties();
	props.put("metadata.broker.list", "192.168.90.147:9091");
	props.put("serializer.class", "kafka.serializer.StringEncoder");
	props.put("partitioner.class", "kafka.producer.DefaultPartitioner");
	props.put("request.required.acks", "1");

	String prefix = "test.";
	ProducerConfig config = new ProducerConfig(props);
	KafkaReporter kafkaReporter = KafkaReporter.forRegistry(metrics)
			.config(config).topic(topic).hostName(hostName).prefix(prefix).build();

	kafkaReporter.start(3, TimeUnit.SECONDS);
}
 
Example #21
Source File: KafkaSink.java    From mt-flume with Apache License 2.0 6 votes vote down vote up
@Override
  public synchronized void start() {
      super.start();

Properties props = new Properties();
  	props.put("metadata.broker.list", brokerList);
  	props.put("request.required.acks", String.valueOf(requestRequiredAcks));
  	props.put("request.timeout.ms", String.valueOf(requestTimeoutms));
props.put("serializer.class", serializerClass);
props.put("partitioner.class", partitionerClass);
props.put("producer.type", producerType);
props.put("batch.num.messages", String.valueOf(batchNumMessages));
props.put("queue.buffering.max.messages", String.valueOf(queueBufferingMaxMessages));
props.put("topic.metadata.refresh.interval.ms", "30000");

producer = new Producer<String, String>(new ProducerConfig(props));
  }
 
Example #22
Source File: IoTDataProducer.java    From lambda-arch with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
	//read config file
	Properties prop = PropertyFileReader.readPropertyFile();		
	String zookeeper = prop.getProperty("com.iot.app.kafka.zookeeper");
	String brokerList = prop.getProperty("com.iot.app.kafka.brokerlist");
	String topic = prop.getProperty("com.iot.app.kafka.topic");
	logger.info("Using Zookeeper=" + zookeeper + " ,Broker-list=" + brokerList + " and topic " + topic);

	// set producer properties
	Properties properties = new Properties();
	properties.put("zookeeper.connect", zookeeper);
	properties.put("metadata.broker.list", brokerList);
	properties.put("request.required.acks", "1");
	properties.put("serializer.class", "com.iot.app.kafka.util.IoTDataEncoder");
	//generate event
	Producer<String, IoTData> producer = new Producer<String, IoTData>(new ProducerConfig(properties));
	IoTDataProducer iotProducer = new IoTDataProducer();
	iotProducer.generateIoTEvent(producer,topic);		
}
 
Example #23
Source File: KafkaTransport.java    From incubator-retired-htrace with Apache License 2.0 6 votes vote down vote up
public Producer<byte[], byte[]> newProducer(HTraceConfiguration conf) {
  // https://kafka.apache.org/083/configuration.html
  Properties producerProps = new Properties();
  // Essential producer configurations
  producerProps.put("metadata.broker.list",
                    conf.get("zipkin.kafka.metadata.broker.list", "localhost:9092"));
  producerProps.put("request.required.acks",
                    conf.get("zipkin.kafka.request.required.acks", "0"));
  producerProps.put("producer.type",
                    conf.get("zipkin.kafka.producer.type", "async"));
  producerProps.put("serializer.class",
                    conf.get("zipkin.kafka.serializer.class", "kafka.serializer.DefaultEncoder"));
  producerProps.put("compression.codec",
                    conf.get("zipkin.kafka.compression.codec", "1"));

  Producer<byte[], byte[]> producer = new Producer<>(new ProducerConfig(producerProps));
  LOG.info("Connected to Kafka transport \n" +  producerProps);
  return producer;
}
 
Example #24
Source File: KafkaSink.java    From flume-ng-kafka-sink with Apache License 2.0 5 votes vote down vote up
@Override
public synchronized void start() {
    // instantiate the producer
    ProducerConfig config = new ProducerConfig(producerProps);
    producer = new Producer<String, String>(config);
    super.start();
}
 
Example #25
Source File: Kafka.java    From product-cep with Apache License 2.0 5 votes vote down vote up
public static void main(String args[]) {
    log.info("Command line arguments passed: " + Arrays.deepToString(args));
    log.info("Starting Kafka Client");

    String url = args[0];
    String topic = args[1];
    String filePath = args[2];
    String sampleNumber = args[3];

    Properties props = new Properties();
    props.put("metadata.broker.list", url);
    props.put("serializer.class", "kafka.serializer.StringEncoder");

    ProducerConfig config = new ProducerConfig(props);
    Producer<String, Object> producer = new Producer<String, Object>(config);

    try {

        filePath = KafkaUtil.getEventFilePath(sampleNumber, topic, filePath);
        readMsg(filePath);

        for (String message : messagesList) {
            System.out.println("Sending message:");
            System.out.println(message);
            KeyedMessage<String, Object> data = new KeyedMessage<String, Object>(topic, message);
            producer.send(data);
        }
        Thread.sleep(500);

    } catch (Throwable t) {
        log.error("Error when sending the messages", t);
    } finally {
        producer.close();
    }
}
 
Example #26
Source File: KafkaBrokerReporter.java    From metrics-kafka with Apache License 2.0 5 votes vote down vote up
synchronized public void init(VerifiableProperties props) {
    if (!initialized) {
        this.props = props;
        props.props().put("metadata.broker.list", String.format("%s:%d", "localhost", props.getInt("port")));

        final KafkaMetricsConfig metricsConfig = new KafkaMetricsConfig(props);

        this.underlying = new TopicReporter(Metrics.defaultRegistry(),
                new ProducerConfig(props.props()),
                "broker%s".format(props.getString("broker.id")));
        initialized = true;
        startReporter(metricsConfig.pollingIntervalSecs());
    }
}
 
Example #27
Source File: AvroOneM2MDataPublish.java    From SDA with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public AvroOneM2MDataPublish(String broker) {
	Properties props = new Properties();
	props.put("metadata.broker.list", Utils.BROKER_LIST);
	props.put("serializer.class", "kafka.serializer.DefaultEncoder");
	props.put("partitioner.class", "kafka.producer.DefaultPartitioner");
	props.put("request.required.acks", "1");
	producer = new Producer<String, byte[]>(new ProducerConfig(props));

}
 
Example #28
Source File: KafkaStreamSink.java    From eagle with Apache License 2.0 5 votes vote down vote up
@Override
public void prepare(Map stormConf, TopologyContext context, OutputCollector collector) {
    super.prepare(stormConf, context, collector);
    Properties properties = new Properties();
    properties.put("metadata.broker.list", config.getBrokerList());
    properties.put("serializer.class", config.getSerializerClass());
    properties.put("key.serializer.class", config.getKeySerializerClass());
    // new added properties for async producer
    properties.put("producer.type", config.getProducerType());
    properties.put("batch.num.messages", config.getNumBatchMessages());
    properties.put("request.required.acks", config.getRequestRequiredAcks());
    properties.put("queue.buffering.max.ms", config.getMaxQueueBufferMs());
    ProducerConfig producerConfig = new ProducerConfig(properties);
    producer = new Producer(producerConfig);
}
 
Example #29
Source File: SensorGridSimulation.java    From Decision with Apache License 2.0 5 votes vote down vote up
private ProducerConfig createProducerConfig() {
    Properties properties = new Properties();
    properties.put("serializer.class", "kafka.serializer.StringEncoder");
    properties.put("metadata.broker.list", brokerList);
    // properties.put("request.required.acks", "1");
    // properties.put("compress", "true");
    // properties.put("compression.codec", "gzip");
    // properties.put("producer.type", "sync");

    return new ProducerConfig(properties);
}
 
Example #30
Source File: KafkaUtilsTest.java    From storm-kafka-0.8-plus with Apache License 2.0 5 votes vote down vote up
private void createTopicAndSendMessage(String key, String value) {
    Properties p = new Properties();
    p.setProperty("metadata.broker.list", broker.getBrokerConnectionString());
    p.setProperty("serializer.class", "kafka.serializer.StringEncoder");
    ProducerConfig producerConfig = new ProducerConfig(p);
    Producer<String, String> producer = new Producer<String, String>(producerConfig);
    producer.send(new KeyedMessage<String, String>(config.topic, key, value));
}