2017-06-17 92 views
0

我使用Java创建了一个OrientDB数据库。现在我需要插入一个数据集(一个文本文件)。我需要帮助。如何将图形数据集插入到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(); 
     } 
    } 
} 
+0

你尝试过OrientDB ETL吗? – Lvca

+0

如何使用ETL?我以前没听过这个词。你可以详细解释一下如何使用ETL?我写了一个java代码,如何将ETL添加到这个和我的整个系统? 在此先感谢:) –

回答

相关问题