Search code examples
javahttporientdb

How can i create vertex and edge with HTTP in Orientdb?


Here is my code. I did a "GET" method for have a response of my DB.

Then I read my own file csv. All is ok in this point, but... I have not idea how can i do a "POST" method. i know that i need to use "addRequestProperty"method.

Any idea for create vertex and edge?

    public void run() throws MalformedURLException, JSONException, IOException {

        String viaURl = "http://xxxxxxxxxxxxxxxxxxxxxxxxxxx/mydb";
        URL url = new URL(viaURl);
        HttpURLConnection conexion = null;
        String texto = null;
        String json;
        BufferedReader in = null, in2 = null;
        int numDump = 5;
        String dato;
        String csvSplitBy = ";";
        int numApps = 0;
        OutputStreamWriter out;

        try {
            Authenticator.setDefault(new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication("xxxxx", "xxxxxxxxxxxxx.".toCharArray());
                }
            });
            conexion = (HttpURLConnection) url.openConnection();

            conexion.setRequestMethod("GET");

            conexion.connect();
            System.out.println("¡¡¡Conectado!!!");

            in = new BufferedReader(new InputStreamReader(conexion.getInputStream()));
            out = new OutputStreamWriter(conexion.getOutputStream());


            json = "";

            while ((texto = in.readLine()) != null) {
                json += texto;
            }

            in.close();
            System.out.println(json);

            conexion.setDoOutput(true);

            try {
                for (int i = 0; i < numDump; i++) {
                    String csvFile = "/home/danicroque/dump/dump_" + i;

                    try {

                        in2 = new BufferedReader(new FileReader(csvFile));
                        while ((dato = in2.readLine()) != null) {
                            numApps++;

                            String[] datos = dato.split(csvSplitBy, 15);

                            conexion.setRequestMethod("POST");

                            conexion.addRequestProperty("_id0" , datos[0]);

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

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            System.out.println("Fin");
        }
    }
}

Thank in advance.


Solution

  • You can use this POST methods to create class: http://your_host:2480/class/mydb/className

    to add property to a class http://your_host:2480/property/mydb/className/propertyName

    You can fine more detailed information here.

    Hope it helps, Alex.

    UPDATE: To insert use this POST method: http://your_host:2480/command/mydb/sql/insert into className(propertyName) values(“yourValue”)