如何将图表数据集插入Orientdb数据库?

时间:2017-06-17 20:33:14

标签: java orientdb

我使用Java创建了一个OrientDB数据库。现在我需要插入一个数据集(一个文本文件)。我需要帮助。

An example of file which i need to insert into my database.

我目前的代码:

package creationdbgraph;

import com.orientechnologies.orient.client.remote.OServerAdmin;
import com.orientechnologies.orient.core.metadata.schema.OClass;
import com.orientechnologies.orient.core.metadata.schema.OType;
import com.orientechnologies.orient.core.sql.OCommandSQL;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.blueprints.impls.orient.OrientGraph;
import com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx;
import com.tinkerpop.blueprints.impls.orient.OrientVertex;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class CreationDbGraph {
    public static void main(String[] args)throws FileNotFoundException, IOException {
        String nameDb="Graph";
        String currentPath="remote:localhost/"+nameDb;

        OServerAdmin serverAdmin;
        try {
            serverAdmin = new OServerAdmin(currentPath).connect("root", "19952916");
            if(!serverAdmin.existsDatabase()){
                serverAdmin.createDatabase(nameDb, "graph", "plocal");
                OrientGraphNoTx g = new OrientGraphNoTx(currentPath);
                OClass FromNode=g.createVertexType("FromNode", "V");
                FromNode.createProperty("ID", OType.STRING);
                OClass ToNode=g.createVertexType("ToNode", "V");
                ToNode.createProperty("ID", OType.STRING);
                g.createEdgeType("Edge", "E");
                g.shutdown();

                OrientGraph g1 = new OrientGraph(currentPath);
                File file = new File("C:\\Users\\USER\\Downloads\\orientdb-community-2.2.20\\dataset.txt");
                BufferedReader reader = null;
                reader = new BufferedReader(new FileReader(file));
                String text = null;
                while ((text = reader.readLine()) != null) {
                    Scanner scanner = new Scanner(text);
                    while (scanner.hasNext()) {
                        OrientVertex node1=g1.addVertex("class:FromNode");
                        OrientVertex node2=g1.addVertex("class:ToNode");
                        if(scanner.hasNextInt())
                        {
                           node1.setProperty("ID",scanner.nextInt());
                           continue;
                        }

                        node2.setProperty("ID",scanner.nextInt());
                        node1.addEdge("Edge", node2);
                    }
                }
                System.out.println(list);
                g1.shutdown();
            }
            serverAdmin.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您的情况非常简单,如果文件不是很大(<数百万行),您可以使用图表批量插入:http://orientdb.com/docs/2.2.x/Graph-Batch-Insert.html