Search in sources :

Example 11 with StringSerializer

use of org.apache.kafka.common.serialization.StringSerializer in project druid by druid-io.

the class ITKafkaTest method testKafka.

@Test
public void testKafka() {
    LOG.info("Starting test: ITKafkaTest");
    // create topic
    try {
        int sessionTimeoutMs = 10000;
        int connectionTimeoutMs = 10000;
        String zkHosts = config.getZookeeperHosts();
        zkClient = new ZkClient(zkHosts, sessionTimeoutMs, connectionTimeoutMs, ZKStringSerializer$.MODULE$);
        int numPartitions = 1;
        int replicationFactor = 1;
        Properties topicConfig = new Properties();
        AdminUtils.createTopic(zkClient, TOPIC_NAME, numPartitions, replicationFactor, topicConfig);
    } catch (TopicExistsException e) {
    // it's ok if the topic already exists
    } catch (Exception e) {
        throw new ISE(e, "could not create kafka topic");
    }
    // set up kafka producer
    Properties properties = new Properties();
    properties.put("bootstrap.servers", config.getKafkaHost());
    LOG.info("Kafka bootstrap.servers: [%s]", config.getKafkaHost());
    properties.put("acks", "all");
    properties.put("retries", "3");
    KafkaProducer<String, String> producer = new KafkaProducer<>(properties, new StringSerializer(), new StringSerializer());
    DateTimeZone zone = DateTimeZone.forID("UTC");
    // format for putting into events
    DateTimeFormatter event_fmt = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss'Z'");
    // timestamp to put on events
    DateTime dt = new DateTime(zone);
    // timestamp of 1st event
    dtFirst = dt;
    // timestamp of last event
    dtLast = dt;
    // these are used to compute the expected aggregations
    int added = 0;
    int num_events = 10;
    // send data to kafka
    for (int i = 0; i < num_events; i++) {
        added += i;
        // construct the event to send
        String event = String.format(event_template, event_fmt.print(dt), i, 0, i);
        LOG.info("sending event: [%s]", event);
        try {
            // Send event to kafka
            producer.send(new ProducerRecord<String, String>(TOPIC_NAME, event)).get();
        } catch (Exception ioe) {
            throw Throwables.propagate(ioe);
        }
        dtLast = dt;
        dt = new DateTime(zone);
    }
    producer.close();
    String indexerSpec;
    // replace temp strings in indexer file
    try {
        LOG.info("indexerFile name: [%s]", INDEXER_FILE);
        indexerSpec = getTaskAsString(INDEXER_FILE).replaceAll("%%DATASOURCE%%", DATASOURCE).replaceAll("%%TOPIC%%", TOPIC_NAME).replaceAll("%%ZOOKEEPER_SERVER%%", config.getZookeeperHosts()).replaceAll("%%GROUP_ID%%", Long.toString(System.currentTimeMillis())).replaceAll("%%COUNT%%", Integer.toString(num_events));
        LOG.info("indexerFile: [%s]\n", indexerSpec);
    } catch (Exception e) {
        // log here so the message will appear in the console output
        LOG.error("could not read indexer file [%s]", INDEXER_FILE);
        throw new ISE(e, "could not read indexer file [%s]", INDEXER_FILE);
    }
    // start indexing task
    taskID = indexer.submitTask(indexerSpec);
    LOG.info("-------------SUBMITTED TASK");
    // wait for the task to finish
    indexer.waitUntilTaskCompletes(taskID, 20000, 30);
    // wait for segments to be handed off
    try {
        RetryUtil.retryUntil(new Callable<Boolean>() {

            @Override
            public Boolean call() throws Exception {
                return coordinator.areSegmentsLoaded(DATASOURCE);
            }
        }, true, 30000, 10, "Real-time generated segments loaded");
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
    LOG.info("segments are present");
    segmentsExist = true;
    // put the timestamps into the query structure
    String query_response_template = null;
    InputStream is = ITKafkaTest.class.getResourceAsStream(QUERIES_FILE);
    if (null == is) {
        throw new ISE("could not open query file: %s", QUERIES_FILE);
    }
    try {
        query_response_template = IOUtils.toString(is, "UTF-8");
    } catch (IOException e) {
        throw new ISE(e, "could not read query file: %s", QUERIES_FILE);
    }
    String queryStr = query_response_template.replaceAll("%%DATASOURCE%%", DATASOURCE).replace("%%TIMEBOUNDARY_RESPONSE_TIMESTAMP%%", TIMESTAMP_FMT.print(dtFirst)).replace("%%TIMEBOUNDARY_RESPONSE_MAXTIME%%", TIMESTAMP_FMT.print(dtLast)).replace("%%TIMEBOUNDARY_RESPONSE_MINTIME%%", TIMESTAMP_FMT.print(dtFirst)).replace("%%TIMESERIES_QUERY_START%%", INTERVAL_FMT.print(dtFirst)).replace("%%TIMESERIES_QUERY_END%%", INTERVAL_FMT.print(dtFirst.plusMinutes(MINUTES_TO_SEND + 2))).replace("%%TIMESERIES_RESPONSE_TIMESTAMP%%", TIMESTAMP_FMT.print(dtFirst)).replace("%%TIMESERIES_ADDED%%", Integer.toString(added)).replace("%%TIMESERIES_NUMEVENTS%%", Integer.toString(num_events));
    // this query will probably be answered from the realtime task
    try {
        this.queryHelper.testQueriesFromString(queryStr, 2);
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}
Also used : ZkClient(org.I0Itec.zkclient.ZkClient) KafkaProducer(org.apache.kafka.clients.producer.KafkaProducer) InputStream(java.io.InputStream) IOException(java.io.IOException) Properties(java.util.Properties) TopicExistsException(kafka.common.TopicExistsException) TopicExistsException(kafka.common.TopicExistsException) IOException(java.io.IOException) DateTimeZone(org.joda.time.DateTimeZone) DateTime(org.joda.time.DateTime) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) ISE(io.druid.java.util.common.ISE) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) Test(org.testng.annotations.Test)

Aggregations

StringSerializer (org.apache.kafka.common.serialization.StringSerializer)11 Properties (java.util.Properties)10 Test (org.junit.Test)5 KafkaProducer (org.apache.kafka.clients.producer.KafkaProducer)4 PrepareOnlyThisForTest (org.powermock.core.classloader.annotations.PrepareOnlyThisForTest)4 Cluster (org.apache.kafka.common.Cluster)3 Node (org.apache.kafka.common.Node)3 PartitionInfo (org.apache.kafka.common.PartitionInfo)3 ISE (io.druid.java.util.common.ISE)2 File (java.io.File)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 Map (java.util.Map)2 TopicExistsException (kafka.common.TopicExistsException)2 ZkClient (org.I0Itec.zkclient.ZkClient)2 Metadata (org.apache.kafka.clients.Metadata)2 ProducerRecord (org.apache.kafka.clients.producer.ProducerRecord)2 DateTime (org.joda.time.DateTime)2 DateTimeZone (org.joda.time.DateTimeZone)2 DateTimeFormatter (org.joda.time.format.DateTimeFormatter)2