Mongo

Discussion in 'Big Data and Analytics' started by _3613, Mar 28, 2017.

  1. _3613

    _3613 New Member

    Joined:
    Dec 4, 2016
    Messages:
    1
    Likes Received:
    0
    /**
    *
    */
    package crudOperations;

    import static java.util.Arrays.asList;

    import java.util.ArrayList;
    import java.util.Collection;

    import org.bson.Document;

    import com.mongodb.MongoClient;
    import com.mongodb.client.MongoCollection;
    import com.mongodb.client.MongoDatabase;

    /**
    * @author Kaushal
    *
    */
    public class CallDataOperations {

    /**
    * @param args
    */
    public static void main(String[] args) {
    //Creating mongo client default connection host:localhost port:27017
    MongoClient mongoClient = new MongoClient();

    //fetching a database with name students
    MongoDatabase db = mongoClient.getDatabase("callData");

    //fetching collection scores from database students
    MongoCollection<Document> collection = db.getCollection("call_data");


    //find one record
    findOne(collection);

    //insert one record
    InsertOne(collection);

    //total call charges
    findTotalCallCharges(collection);

    //revenue generated from each location
    callsFromEachLocation(collection);

    //number of calls generated from each location
    numberOfCallsFromLocations(collection);

    //total calls made from roaming
    callFromRoaming(collection);
    }

    private static void callFromRoaming(MongoCollection<Document> collection) {
    Long count=collection.count(new Document("roaming","true"));
    System.out.println("Number of calls made from Roaming : " + count);

    }

    private static void numberOfCallsFromLocations(MongoCollection<Document> collection) {
    Collection<Document> callData = collection.aggregate(asList(new Document("$group", new Document("_id","$source_location").append("Total", new Document("$sum","$call_charge")).append("count", new Document("$sum",1))))).into(new ArrayList<Document>());
    System.out.println("Number of Calls made from each Location :");
    for(Document doc:callData){
    System.out.println(doc.toJson());
    }
    }

    private static void callsFromEachLocation(MongoCollection<Document> collection) {
    Collection<Document> callData = collection.aggregate(asList(new Document("$group", new Document("_id","$source_location").append("Total", new Document("$sum","$call_charge"))))).into(new ArrayList<Document>());
    System.out.println("Calls made from each Location :");
    for(Document doc:callData){
    System.out.println(doc.toJson());
    }

    }

    private static void findTotalCallCharges(MongoCollection<Document> collection) {
    Collection<Document> callData = collection.aggregate(asList(new Document("$group", new Document("_id",null).append("Total", new Document("$sum","$call_charge"))))).into(new ArrayList<Document>());
    for(Document doc:callData){
    System.out.println("Total Call Charges : " + doc.toJson());
    }

    }

    private static void InsertOne(MongoCollection<Document> collection) {
    collection.insertOne(new Document("source", "+919736723678").append("destination", "+919778093678").append("source_location", "Chennai").append("destination_location", "Jaipur").append("call_duration", 4.00).append("roaming", "true").append("call_charge", 8.00));

    }

    /** function to find a record
    input : mongodb collection */
    public static void findOne(MongoCollection<Document> collection){
    Document result=collection.find(new Document("source_location","Mumbai")).first();
    System.out.println("Result of find records : " + result.toJson());
    }

    }
    While executing this code in java i getting the error given below,please fix the issue
    Mar 28, 2017 8:17:52 PM com.mongodb.diagnostics.logging.JULLogger log
    INFO: Cluster created with settings {hosts=[127.0.0.1:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
    Mar 28, 2017 8:17:52 PM com.mongodb.diagnostics.logging.JULLogger log
    INFO: Opened connection [connectionId{localValue:1, serverValue:16}] to 127.0.0.1:27017
    Mar 28, 2017 8:17:52 PM com.mongodb.diagnostics.logging.JULLogger log
    INFO: No server chosen by ReadPreferenceServerSelector{readPreference=primary} from cluster description ClusterDescription{type=UNKNOWN, connectionMode=SINGLE, serverDescriptions=[ServerDescription{address=127.0.0.1:27017, type=UNKNOWN, state=CONNECTING}]}. Waiting for 30000 ms before timing out
    Mar 28, 2017 8:17:52 PM com.mongodb.diagnostics.logging.JULLogger log
    INFO: Monitor thread successfully connected to server with description ServerDescription{address=127.0.0.1:27017, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[3, 4, 2]}, minWireVersion=0, maxWireVersion=5, maxDocumentSize=16777216, roundTripTimeNanos=1309527}
    Mar 28, 2017 8:17:52 PM com.mongodb.diagnostics.logging.JULLogger log
    INFO: Opened connection [connectionId{localValue:2, serverValue:17}] to 127.0.0.1:27017
    Exception in thread "main" java.lang.NullPointerException
    at call.CallDataOperations.findOne(CallDataOperations.java:93)
    at call.CallDataOperations.main(CallDataOperations.java:35)
     
    #1
  2. Megha_42

    Megha_42 Well-Known Member
    Simplilearn Support

    Joined:
    Dec 15, 2016
    Messages:
    206
    Likes Received:
    9
    Hi,

    Thank you for reaching out.
    The error is caused due to missing Logger object in your code.
    Please add these lines to your code to fix the error,


    Logger mongoLogger =Logger.getLogger("org.mongodb.driver");
    mongoLogger.setLevel(Level.SEVERE);

    All the very best!

    Thanks and Regards
    Megha
     
    #2
  3. SOMESH BHARDWAJ

    SOMESH BHARDWAJ New Member

    Joined:
    Sep 27, 2016
    Messages:
    1
    Likes Received:
    0
    Hi, I am facing similar issue however not very expert in Java or programming, not able to figure out where to put suggested code.
     
    #3

Share This Page