Reading Java CSV file and MongoDB

0

I need a help! I'm new to java and need to import from a CSV file and save inside MongoDB! Importing is already being done successfully, however, I need to save the values in mongoDB in the style of an array. Example: Each I have a given Father and his Children, in MongoDB, I need to save a line with the Father and inside the Father, I need to save the children. Here's the part of the code I'm doing it! the problem that is in loop.

// Relationships         try {             csvData = new BufferedReader (new FileReader (csvFile));

        // create array of relationships
        List<BasicDBObject> relationList = new ArrayList<BasicDBObject>();

        while ((line = csvData.readLine()) != null)
        {

            if (firstline) {
                firstline = false;
                continue; }

            String[] file = line.split(csvSeparetedField);

            //Filter *PGM and *SRVPGM
            //if (file[14].equals("*PGM") || (file[14].equals("*SRVPGM"))) {
            String newGuidId = UUID.randomUUID().toString();

            ProcessesField field = new ProcessesField();
            RelationshipsField relation = new RelationshipsField();

            //OPEN CONEXION MONGODB
            DB db = mongoClient.getDB("DataAccuracyDev");
            DBObject userData = new BasicDBObject();
            userData.put("id", field.getID());
            userData.put("name", field.getName());
            userData.put("description", field.getDescription());

            // toID -- Insert relationship - Column WHPNAM
            field.setID(newGuidId);
            field.setName(file[1]);
            //Test relation 1
            field.setType((file[14]));

            List<BasicDBObject> andRelationToID = new ArrayList<BasicDBObject>();
            andRelationToID.add(new BasicDBObject("name",field.getName()));
            andRelationToID.add(new BasicDBObject("type",field.getType()));

            DBObject query = new BasicDBObject();
            //query2.put("name", fieldTo.getName());
            query.put("$and", andRelationToID);

            //query.put("name", field.getName());

            DBCursor cursor = db.getCollection("Objects").find(query);
            System.out.println(query);
            // end Test relation 2

            String fromProcesses = "";
            if (cursor.hasNext()) {

                DBObject obj = cursor.next();
                fromProcesses = obj.get("id").toString();

            }

            // fromID -- Insert relationship - Column WHPNAM
            ProcessesField fieldTo = new ProcessesField();
            fieldTo.setName(file[5]);
            fieldTo.setType(file[14]);


            DBObject relationship = new BasicDBObject();

            //check if exists... if not exist insert
            //Test relation 2
            List<BasicDBObject> andRelationFromID = new ArrayList<BasicDBObject>();
            andRelationFromID.add(new BasicDBObject("name",fieldTo.getName()));
            andRelationFromID.add(new BasicDBObject("type",fieldTo.getType()));

            DBObject query2 = new BasicDBObject();
            //query2.put("name", fieldTo.getName());
            query2.put("$and", andRelationFromID);



            DBCursor cursor3 = db.getCollection("Objects").find(query2);

            System.out.println(query2);
            //end test

            if (cursor3.hasNext()) {

                DBObject obj = cursor3.next();
                fieldTo.setID(obj.get("id").toString());

                // * test
                List<BasicDBObject> andFields = new ArrayList<BasicDBObject>();
                andFields.add(new BasicDBObject("fromId", fromProcesses));
                andFields.add(new BasicDBObject("toId", fieldTo.getID()));

                DBObject queryTest = new BasicDBObject();
                queryTest.put("$and", andFields);

                DBCursor list = db.getCollection("Relationships").find(queryTest);

                if (list.hasNext()) {
                    continue;
                }
                // * end test
                newGuidId = UUID.randomUUID().toString();

                relationship.put("id", newGuidId);
                relationship.put("fromId", fromProcesses);
                relationship.put("toId", fieldTo.getID());
                relationship.put("action", "executes");//TO DO --after its necessary change the hard code

                // create a relationship
                BasicDBObject relationArray = new BasicDBObject();
                relationArray.put("id", newGuidId);
                relationArray.put("toId", fieldTo.getID());
                relationArray.put("action", "executes");

                // add the relationship in the list of relationships
                relationList.add(new BasicDBObject("relationship", relationArray));

                    // create the relationship to be save
                    DBObject relationshipInsert = new BasicDBObject();
                    relationshipInsert.put("fromId", fromProcesses);
                    relationshipInsert.put("relationships", relationList);
                    db.getCollection("Relationships").insert(relationshipInsert);


                    // End Array


                    //db.getCollection("Relationships").insert(relationship);

                System.out.println(fromProcesses + " , " + fieldTo.getID() + " Relationship created with success!");
            } else {
                System.out.println(fromProcesses + "  Relationship not exists");
            }
            //}
        }
        mongoClient.close();

The result is doubling the lines below!

    
asked by anonymous 18.02.2018 / 23:23

0 answers