Guest User

Untitled

a guest
Nov 24th, 2017
402
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.23 KB | None | 0 0
  1. package org.example.graph.orientdb;
  2.  
  3. import java.io.BufferedInputStream;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.FileNotFoundException;
  7. import java.io.IOException;
  8. import java.io.InputStream;
  9. import java.net.URI;
  10.  
  11. import org.apache.log4j.Logger;
  12.  
  13. import com.orientechnologies.orient.client.remote.OEngineRemote;
  14. import com.orientechnologies.orient.client.remote.OServerAdmin;
  15. import com.orientechnologies.orient.core.Orient;
  16. import com.orientechnologies.orient.core.command.OCommandOutputListener;
  17. import com.orientechnologies.orient.core.db.document.ODatabaseDocument;
  18. import com.orientechnologies.orient.core.db.tool.ODatabaseImport;
  19. import com.tinkerpop.blueprints.pgm.impls.orientdb.OrientGraph;
  20.  
  21. public class GraphDatabaseImporter {
  22.  
  23.     protected static final Logger LOGGER = Logger.getLogger(GraphDatabaseExporter.class);
  24.    
  25.     private static final String DATABASE_URL = "remote:iot-at-work1/tinkerpop";
  26.    
  27.     private static final String USERNAME = "admin";
  28.    
  29.     private static final String PASSWORD = "admin";
  30.    
  31.     private static final String ADMINISTRATOR_NAME = "root";
  32.    
  33.     private static final String ADMINISTRATOR_PWD = "EB61C596EAA0239700B02171DF791F2F423CFABF6A890FB032218EB8F22565E9";
  34.  
  35.  
  36.     /**
  37.      * @param args
  38.      */
  39.     public static void main(String[] args) {
  40.         //REGISTER THE ENGINE
  41.         Orient.instance().registerEngine(new OEngineRemote());
  42.        
  43.         OServerAdmin adminTool = null;
  44.         try {
  45.             adminTool = new OServerAdmin(DATABASE_URL).connect(ADMINISTRATOR_NAME,ADMINISTRATOR_PWD);
  46.             LOGGER.debug("Successful connection to " + DATABASE_URL);  
  47.             adminTool.dropDatabase();
  48.             adminTool.createDatabase("local");
  49.             adminTool.close();
  50.             LOGGER.debug("The database has been deleted.");
  51.         } catch (Exception e) {
  52.             LOGGER.error("Exception during the deletion of the database " + DATABASE_URL,e);
  53.                 System.exit(1);
  54.             }
  55.                
  56.         final URI fileURI = new File("D:\\eclipse workspaces\\iot-at-work\\OrientDBGraphDBExample\\export\\exp_db.json").toURI();
  57.         //check the parameters
  58.         if (fileURI == null) {
  59.             IllegalArgumentException topicNullExcep = new IllegalArgumentException(
  60.                 "The URI of the file to be imported into the database '" + DATABASE_URL +
  61.                 "' to cannot be null.");
  62.             LOGGER.error(topicNullExcep.getMessage(),topicNullExcep);
  63.             throw topicNullExcep;
  64.         }
  65.        
  66.         File file = new File (fileURI);
  67.         InputStream fileStream = null;
  68.         try {
  69.             fileStream = new BufferedInputStream(new FileInputStream(file));
  70.         } catch (FileNotFoundException e) {
  71.             LOGGER.error("Error in opening the file " + fileURI.toString() + "(database import)", e);
  72.             System.exit(1);
  73.         }
  74.         OrientGraph graph = new OrientGraph(
  75.                 DATABASE_URL, USERNAME,PASSWORD);
  76.         //graph.clear(); THROWS EXCEPTION
  77.         ODatabaseImport importManager = null;
  78.         try {
  79.            
  80.             ODatabaseDocument database = graph.getRawGraph();
  81.             importManager = new ODatabaseImport(database  ,fileStream,new OCommandOutputListener () {
  82.                 public void onMessage(String iText) {
  83.                     LOGGER.debug("Importing database " + DATABASE_URL +
  84.                         " to " + fileURI.toString() + " - " + iText);
  85.                 }
  86.             });
  87.             importManager.importDatabase();
  88.             database.close();
  89.         } catch (IOException e) {
  90.             LOGGER.error("Exception during the import of the file " + fileURI.toString() +
  91.                 " into the database " + DATABASE_URL,e);
  92.             System.exit(1);
  93.         }
  94.     }
  95.  
  96. }
Add Comment
Please, Sign In to add comment