org.waziup.waziupmongoconsumer.MongoDBConsumer.java Source code

Java tutorial

Introduction

Here is the source code for org.waziup.waziupmongoconsumer.MongoDBConsumer.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.waziup.waziupmongoconsumer;

import com.mongodb.MongoClient;
import com.mongodb.MongoException;
import com.mongodb.client.MongoDatabase;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.bson.Document;

/**
 *
 * @author constantin
 */
public class MongoDBConsumer {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        MongoClient waziupMongo = new MongoClient();
        try {

            //Get broker properties
            InputStream brokerConfigfile = new FileInputStream(
                    "./src/main/java/org/waziup/waziupmongoconsumer/smtpkafkaconsumer.properties");

            Properties brokerprops = new Properties();

            brokerprops.load(brokerConfigfile);

            KafkaConsumer consumer = new KafkaConsumer(brokerprops);

            consumer.subscribe(Arrays.asList("waziup"));

            MongoDatabase db = waziupMongo.getDatabase("waziup");

            while (true) {

                ConsumerRecords<String, String> records = consumer.poll(10);

                for (ConsumerRecord<String, String> record : records) {

                    System.out.println("Temperature => " + record.value());

                    db.getCollection("weather")
                            .insertOne(new Document().append("SensorID", "waziupOuaga").append("Location", "Ouaga")
                                    .append("dataTime", record.value().substring(0, record.value().indexOf("=")))
                                    .append("temperature",
                                            record.value().substring(record.value().indexOf("=") + 1,
                                                    record.value().lastIndexOf("=")))
                                    .append("humidity",
                                            record.value().substring(record.value().lastIndexOf("=") + 1)));

                    System.out.println("Data inserted");

                }
            }

        } catch (IOException ex) {
            Logger.getLogger(MongoDBConsumer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MongoException m) {
            Logger.getLogger(MongoDBConsumer.class.getName()).log(Level.SEVERE, null, m);
        }

    }

}