Search code examples
javaneo4jnosqlgraph-databases

Java - How to parse JSON and CSV files in Java and store them in Neo4j Database


Current Situation: I have two files, one in JSON format and the other in CSV format. I would like to parse them in Java and store the results in a Neo4j DB for later use.

What I have till now: I have used Jackson Library to parse the JSON file and for the CSV file I have done as follows:


import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class CSVReader {

    public static void main(String[] args) {

        String csvFile = "path/to/file/data.csv";
        String line = "";
        String cvsSplitBy = ",";

        try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {

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


                String[] dataPoints = line.split(cvsSplitBy);

                System.out.println("Data [point1= " + dataPoints[4] + " , point3=" + dataPoints[3] + "]");

            }

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

Problem: I can display the results fine but I would like to store them in a Neo4j Database. I don't know how to proceed with that. Can anyone help or point me to the right direction? I just started with Neo4j so some extra clarification regarding the code/steps would be really helpful.

EDIT: For example, with MySQL I would do something like this to insert values into a table.

String query = " insert into users (first_name, last_name, dob, email_id)"
        + " values (?, ?, ?, ?)";

      // create the mysql insert preparedstatement
      PreparedStatement preparedStmt = conn.prepareStatement(query);

      // code to set values

      // execute the preparedstatement
      preparedStmt.execute();

What would be the equivalent in Neo4J?

Note: Downvoting a question without explaining what is wrong with it doesn't really achieve anything. There's criticism and then there's constructive criticism. Thanks!


Solution

  • Are you trying to find out how to interact with a database in Java? The native way is via JDBC, but there are other options. The Neo4J JDBC driver documentation (https://neo4j.com/blog/neo4j-jdbc-driver-3-3-1-release/) has some good pointers on how to proceed. As to parsing, the CSV parsing code you have here looks good and the Jackson library is a fine option for parsing JSON. Can you elaborate on what you're looking for?