Example usage for com.mongodb.client MongoCollection insertMany

List of usage examples for com.mongodb.client MongoCollection insertMany

Introduction

In this page you can find the example usage for com.mongodb.client MongoCollection insertMany.

Prototype

void insertMany(List<? extends TDocument> documents);

Source Link

Document

Inserts one or more documents.

Usage

From source file:analisissentimientos.SentimentAnnotated.java

public static void procesarDatasetAnotado() {
    File archivo = null;//from w w  w.  j  av a 2  s. c  o m
    FileReader fr = null;
    BufferedReader br = null;
    List<Document> jArray = new ArrayList<Document>();
    try {
        archivo = new File(
                "D:\\Deisy universidad\\Big Data\\data\\01_debate08_sentiment_tweets\\debate08_sentiment_tweets_procesar.tsv");
        fr = new FileReader(archivo);
        br = new BufferedReader(fr);

        // Lectura del fichero
        String linea;
        while ((linea = br.readLine()) != null) {
            String[] csvValues = linea.toString().split("\t");

            Document json = new Document();
            json.put("idTweet", csvValues[0]);
            json.put("date", csvValues[1]);
            json.put("content", csvValues[2]);
            json.put("authorName", csvValues[3]);
            json.put("authorNickname", csvValues[4]);

            int j = 1;
            String nameAttribute = "";
            LinkedHashMap<Integer, Integer> ratings = new LinkedHashMap<Integer, Integer>();
            for (int i = 5; i < csvValues.length; i++) {
                nameAttribute = "rating" + j;
                int rating = Integer.parseInt(csvValues[i]);
                ratings.put(rating, ratings.containsKey(rating) ? (ratings.get(rating) + 1) : 1);
                json.put(nameAttribute, csvValues[i]);
                j++;
            }

            int score = 1;
            int maxValueInMap = (Collections.max(ratings.values()));
            for (Map.Entry<Integer, Integer> entry : ratings.entrySet()) {
                if (entry.getValue() == maxValueInMap) {
                    score = entry.getKey();
                    break;
                }
            }

            switch (score) {
            case 1:
                json.put("sentiment_score_annoted", "Negative");
                break;
            case 2:
                json.put("sentiment_score_annoted", "Positive");
                break;
            case 3:
                json.put("sentiment_score_annoted", "Mixed");
                break;
            case 4:
                json.put("sentiment_score_annoted", "Other");
                break;
            }

            //Mongo connection
            jArray.add(json);
        }

        MongoDatabase mongoDB = new MongoClient(DB_SERVER, DB_PORT).getDatabase(DB_NAME);
        MongoCollection mongoCollection = mongoDB.getCollection(COLLECTION_NAME);
        mongoCollection.insertMany(jArray);
        fr.close();
        br.close();
        ///tweet.id   pub.date.GMT   content   author.name   author.nickname   rating.1   rating.2   rating.3   rating.4   rating.5   rating.6   rating.7   rating.8

    } catch (Exception ex) {
        System.out.println("analisissentimientos.SentimentAnnotated.main() Error --->" + ex.toString());
    }
}

From source file:anuncius.singleton.AnunciusDAO.java

public boolean save(AbstractRequestList requestList) {
    if (requestList != null && requestList.hasContent()) {
        MongoCollection<Document> authCollection = mongo.getCollection(requestList.getCollectionName());
        List<Document> objectList = requestList.convertToMongoObject();
        authCollection.insertMany(objectList);
        return true;
    }/*from   w w w. j a va  2 s .  c  o m*/
    return false;
}

From source file:ch.hslu.dmg.InitDB.java

public void initProfs() {
    MongoClient mongo = new MongoClient("localhost", 27017);
    MongoDatabase database = mongo.getDatabase("unidb");
    MongoCollection<Document> prof = database.getCollection("professoren");
    prof.drop();//from   w w w. j  a  v  a2s  .  c  o  m

    ArrayList<Document> profs = new ArrayList<>();
    profs.add(new Document("PersNr", 2125).append("Name", "Sokrates").append("Rang", "C4").append("Raum", 226));
    profs.add(new Document("PersNr", 2126).append("Name", "Russel").append("Rang", "C4").append("Raum", 232));
    profs.add(
            new Document("PersNr", 2127).append("Name", "Kopernikus").append("Rang", "C3").append("Raum", 310));
    profs.add(new Document("PersNr", 2133).append("Name", "Popper").append("Rang", "C3").append("Raum", 52));
    profs.add(
            new Document("PersNr", 2134).append("Name", "Augustinus").append("Rang", "C3").append("Raum", 309));
    profs.add(new Document("PersNr", 2136).append("Name", "Curie").append("Rang", "C4").append("Raum", 36));
    profs.add(new Document("PersNr", 2137).append("Name", "Kant").append("Rang", "C4").append("Raum", 7));
    prof.insertMany(profs);

    mongo.close();
}

From source file:ch.hslu.dmg.InitDB.java

public void initStuds() {
    MongoClient mongo = new MongoClient("localhost", 27017);
    MongoDatabase database = mongo.getDatabase("unidb");
    MongoCollection<Document> stud = database.getCollection("studenten");
    stud.drop();//from   ww w . j a  v  a2  s .c  om

    ArrayList<Document> studs = new ArrayList<>();
    studs.add(new Document("Legi", 24002).append("Name", "Xenokrates").append("Semester", 18));
    studs.add(
            new Document("Legi", 25403).append("Name", "Jonas").append("Semester", 12).append("Hoeren", 5022));
    studs.add(
            new Document("Legi", 26120).append("Name", "Fichte").append("Semester", 10).append("Hoeren", 5001));
    studs.add(new Document("Legi", 26830).append("Name", "Aristoxenos").append("Semester", 8));
    BasicDBList hoeren = new BasicDBList();
    hoeren.add(5001);
    hoeren.add(4052);
    studs.add(new Document("Legi", 27550).append("Name", "Schopenhauer").append("Semester", 6).append("Hoeren",
            hoeren));
    BasicDBList hoeren2 = new BasicDBList();
    hoeren2.add(5041);
    hoeren2.add(5052);
    hoeren2.add(5216);
    hoeren2.add(5259);
    studs.add(new Document("Legi", 28106).append("Name", "Carnap").append("Semester", 3).append("Hoeren",
            hoeren2));
    BasicDBList hoeren3 = new BasicDBList();
    hoeren3.add(5001);
    hoeren3.add(5041);
    hoeren3.add(5049);
    studs.add(new Document("Legi", 29120).append("Name", "Theophrastos").append("Semester", 2).append("Hoeren",
            hoeren3));
    BasicDBList hoeren4 = new BasicDBList();
    hoeren4.add(5022);
    hoeren4.add(5001);
    studs.add(new Document("Legi", 29555).append("Name", "Feuerbach").append("Semester", 2).append("Hoeren",
            hoeren4));
    stud.insertMany(studs);

    mongo.close();
}

From source file:ch.hslu.dmg.InitDB.java

public void initVorlesungen() {
    MongoClient mongo = new MongoClient("localhost", 27017);
    MongoDatabase database = mongo.getDatabase("unidb");
    MongoCollection<Document> vorlesung = database.getCollection("vorlesungen");
    vorlesung.drop();/* ww w  .j  a  v  a 2s .  co  m*/

    ArrayList<Document> vorlesungen = new ArrayList<>();
    vorlesungen.add(new Document("VorlNr", 5001).append("Titel", "Grundzge").append("SWS", 4)
            .append("GelesenVon", 2137));
    vorlesungen.add(
            new Document("VorlNr", 5041).append("Titel", "Ethik").append("SWS", 4).append("GelesenVon", 2125));
    vorlesungen.add(new Document("VorlNr", 5043).append("Titel", "Erkentnistheorie").append("SWS", 3)
            .append("GelesenVon", 2126));
    vorlesungen.add(new Document("VorlNr", 5049).append("Titel", "Maeeutik").append("SWS", 2)
            .append("GelesenVon", 2125));
    vorlesungen.add(
            new Document("VorlNr", 4052).append("Titel", "Logik").append("SWS", 4).append("GelesenVon", 2125));
    vorlesungen.add(new Document("VorlNr", 5052).append("Titel", "Wissenschaftstheorie").append("SWS", 3)
            .append("GelesenVon", 2126));
    vorlesungen.add(new Document("VorlNr", 5216).append("Titel", "Bioethik").append("SWS", 2)
            .append("GelesenVon", 2126));
    vorlesungen.add(new Document("VorlNr", 5259).append("Titel", "Der Wiener Kreis").append("SWS", 2)
            .append("GelesenVon", 2133));
    vorlesungen.add(new Document("VorlNr", 5022).append("Titel", "Glaube und Wissen").append("SWS", 2)
            .append("GelesenVon", 2134));
    vorlesungen.add(new Document("VorlNr", 4630).append("Titel", "Die 3 Kritiken").append("SWS", 4)
            .append("GelesenVon", 2137));

    vorlesung.insertMany(vorlesungen);

    vorlesung.updateOne(eq("VorlNr", 5041), new Document("$set",
            new Document("Voraussetzung", vorlesung.find(eq("VorlNr", 5001)).iterator().next().get("_id"))));
    vorlesung.updateOne(eq("VorlNr", 5043), new Document("$set",
            new Document("Voraussetzung", vorlesung.find(eq("VorlNr", 5001)).iterator().next().get("_id"))));
    vorlesung.updateOne(eq("VorlNr", 5049), new Document("$set",
            new Document("Voraussetzung", vorlesung.find(eq("VorlNr", 5001)).iterator().next().get("_id"))));
    vorlesung.updateOne(eq("VorlNr", 5216), new Document("$set",
            new Document("Voraussetzung", vorlesung.find(eq("VorlNr", 5041)).iterator().next().get("_id"))));
    vorlesung.updateOne(eq("VorlNr", 5259), new Document("$set",
            new Document("Voraussetzung", vorlesung.find(eq("VorlNr", 5052)).iterator().next().get("_id"))));
    BasicDBList voraussetzungen = new BasicDBList();
    voraussetzungen.add(vorlesung.find(eq("VorlNr", 5043)).iterator().next().get("_id"));
    voraussetzungen.add(vorlesung.find(eq("VorlNr", 5041)).iterator().next().get("_id"));
    vorlesung.updateOne(eq("VorlNr", 5052),
            new Document("$set", new Document("Voraussetzung", voraussetzungen)));

    mongo.close();
}

From source file:ch.hslu.dmg.InitDB.java

public void initAssistenten() {
    MongoClient mongo = new MongoClient("localhost", 27017);
    MongoDatabase database = mongo.getDatabase("unidb");
    MongoCollection<Document> assistent = database.getCollection("assistenten");
    assistent.drop();/*from   w  ww. j  a  v a 2s  .  c om*/

    ArrayList<Document> assistenten = new ArrayList<>();
    assistenten.add(new Document("PersNr", 3002).append("Name", "Platon").append("Fachgebiet", "Ideenlehre")
            .append("Boss", 2125));
    assistenten.add(new Document("PersNr", 3003).append("Name", "Aristoteles")
            .append("Fachgebiet", "Syllogistik").append("Boss", 2125));
    assistenten.add(new Document("PersNr", 3004).append("Name", "Wittgenstein")
            .append("Fachgebiet", "Sprachtheorie").append("Boss", 2126));
    assistenten.add(new Document("PersNr", 3005).append("Name", "Rhetikus")
            .append("Fachgebiet", "Planetenbewegung").append("Boss", 2127));
    assistenten.add(new Document("PersNr", 3006).append("Name", "Newton")
            .append("Fachgebiet", "Keplersche Gesetze").append("Boss", 2127));
    assistenten.add(new Document("PersNr", 3007).append("Name", "Spinoza")
            .append("Fachgebiet", "Gott und Natur").append("Boss", 2134));

    assistent.insertMany(assistenten);

    mongo.close();
}

From source file:ch.hslu.dmg.InitDB.java

public void initPruefen() {
    MongoClient mongo = new MongoClient("localhost", 27017);
    MongoDatabase database = mongo.getDatabase("unidb");
    MongoCollection<Document> pruefungen = database.getCollection("pruefungen");
    pruefungen.drop();//from www  . j  a  v  a2 s.c o  m

    ArrayList<Document> pruefung = new ArrayList<>();
    pruefung.add(new Document("Legi", 28106).append("VorlNr", 5001).append("PersNr", 2126).append("Note", 1));
    pruefung.add(new Document("Legi", 25403).append("VorlNr", 5041).append("PersNr", 2125).append("Note", 2));
    pruefung.add(new Document("Legi", 27550).append("VorlNr", 4630).append("PersNr", 2137).append("Note", 2));

    pruefungen.insertMany(pruefung);

    mongo.close();
}

From source file:cn.edu.hfut.dmic.webcollector.example.WeiboCrawler.java

License:Open Source License

@Override
public void visit(Page page, CrawlDatums next) {
    int pageNum = Integer.valueOf(page.getMetaData("pageNum"));
    /*??*//*ww w  .  java2s. c  o  m*/
    Elements weibos = page.select("div.c");
    try {
        MongoClient mongoClient = new MongoClient("localhost", 27017);

        // ?
        MongoDatabase mongoDatabase = mongoClient.getDatabase("weibo_crawler");
        System.out.println("Connect to database successfully");
        MongoCollection<Document> collection = mongoDatabase.getCollection("webpage");
        //?  
        /** 
        * 1.  org.bson.Document ?key-value? 
        * 2. ?List<Document> 
        * 3. ???? mongoCollection.insertMany(List<Document>) ??? mongoCollection.insertOne(Document) 
        * */
        for (Element weibo : weibos) {
            Document document = new Document("content", "" + pageNum + "" + ":" + weibo.text());
            List<Document> documents = new ArrayList<Document>();
            documents.add(document);
            collection.insertMany(documents);
        }
        System.out.println("??");
        mongoClient.close();
    } catch (Exception e) {
        System.err.println(e.getClass().getName() + ": " + e.getMessage());
    }
}

From source file:cn.weibo.webcollector.spider.WeiboCrawler.java

License:Open Source License

@Override
public void visit(Page page, CrawlDatums next) {
    String inlink = page.meta("inlink");
    String title = page.select("title").text();
    String url = page.getUrl();/*w w w .  ja  v a 2 s .  co  m*/
    /*??*/
    Elements weibos = page.select("div[id].c");
    try {
        MongoClient mongoClient = new MongoClient("localhost", 27017);
        Logger mongoLogger = Logger.getLogger("org.mongodb.driver");
        mongoLogger.setLevel(Level.ERROR);

        // ?
        MongoDatabase mongoDatabase = mongoClient.getDatabase("weibo_crawler");
        System.out.println("Connect to database successfully");
        MongoCollection<Document> collection = mongoDatabase.getCollection("weibo_page");
        //?  
        /** 
        * 1.  org.bson.Document ?key-value? 
        * 2. ?List<Document> 
        * 3. ???? mongoCollection.insertMany(List<Document>) ??? mongoCollection.insertOne(Document) 
        * */
        for (Element weibo : weibos) {
            if (weibo.text().length() != 0) {
                Document document = new Document("url", url).append("title", title)
                        .append("content", weibo.text()).append("inlink", inlink);
                List<Document> documents = new ArrayList<Document>();
                documents.add(document);
                collection.insertMany(documents);
            }
        }
        System.out.println("??");
        mongoClient.close();
    } catch (Exception e) {
        System.err.println(e.getClass().getName() + ": " + e.getMessage());
    }
}

From source file:co.cask.hydrator.plugin.realtime.sink.MongoDBRealtimeSink.java

License:Apache License

@Override
public int write(Iterable<StructuredRecord> iterable, DataWriter dataWriter) throws Exception {
    int recordCount = 0;
    MongoCollection<Document> collection = mongoDatabase.getCollection(config.collectionName);
    List<Document> documentList = new ArrayList<>();
    for (StructuredRecord record : iterable) {
        Document document = new Document();
        for (Schema.Field field : record.getSchema().getFields()) {
            document.append(field.getName(), record.get(field.getName()));
        }//from  w w  w .  j  a v a  2  s  .c  o  m
        documentList.add(document);
        recordCount++;
    }
    collection.insertMany(documentList);
    return recordCount;
}