Advertisement
ImpariTuriddu

OrientDB Blueprints Graph Export

Sep 13th, 2011
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.55 KB | None | 0 0
  1. package org.example.graph.orientdb;
  2.  
  3. import java.io.BufferedOutputStream;
  4. import java.io.File;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8. import java.io.OutputStream;
  9. import java.net.URI;
  10.  
  11. import org.apache.log4j.Logger;
  12.  
  13. import com.orientechnologies.orient.core.command.OCommandOutputListener;
  14. import com.orientechnologies.orient.core.db.record.ODatabaseRecord;
  15. import com.orientechnologies.orient.core.db.tool.ODatabaseExport;
  16. import com.tinkerpop.blueprints.pgm.impls.orientdb.OrientGraph;
  17.  
  18. /**
  19.  * @author Salvatore Piccione
  20.  *
  21.  */
  22. public class GraphDatabaseExporter {
  23.    
  24.     protected static final Logger LOGGER = Logger.getLogger(GraphDatabaseExporter.class);
  25.    
  26.     private static final String DATABASE_URL = "remote:orientdb-host/tinkerpop";
  27.    
  28.     private static final String USERNAME = "admin";
  29.    
  30.     private static final String PASSWORD = "admin";
  31.  
  32.     /**
  33.      * @param args
  34.      */
  35.     public static void main(String[] args) {
  36.         final URI fileURI = new File("C:\\path\\to\\export\\dir\\exp_db.json").toURI();
  37.         //check the parameters
  38.         if (fileURI == null) {
  39.             IllegalArgumentException topicNullExcep = new IllegalArgumentException(
  40.                 "The URI of the file exporting the database '" + DATABASE_URL +
  41.                 "' to cannot be null.");
  42.             LOGGER.error(topicNullExcep.getMessage(),topicNullExcep);
  43.             throw topicNullExcep;
  44.         }
  45.        
  46.         File file = new File (fileURI);
  47.         OutputStream fileStream = null;
  48.         try {
  49.             fileStream = new BufferedOutputStream(new FileOutputStream(file));
  50.         } catch (FileNotFoundException e) {
  51.             LOGGER.error("Error in opening the file " + fileURI.toString() + "(database export)", e);
  52.             System.exit(1);
  53.         }
  54.         OrientGraph graph = new OrientGraph(
  55.                 DATABASE_URL, USERNAME,PASSWORD);
  56.         ODatabaseExport exportManager = null;
  57.         ODatabaseRecord database = null;
  58.         try {
  59.             database = graph.getRawGraph();
  60.             exportManager = new ODatabaseExport(database ,fileStream,new OCommandOutputListener () {
  61.                 public void onMessage(String iText) {
  62.                     LOGGER.debug("Exporting database " + DATABASE_URL +
  63.                         " to " + fileURI.toString() + " - " + iText);
  64.                 }
  65.             });
  66.  
  67.             exportManager.exportDatabase();
  68.             /*
  69.             exportManager.exportRecords();*/
  70.             exportManager.close();
  71.             database.close();
  72.         } catch (IOException e) {
  73.             LOGGER.error("Exception during the export of the database " + DATABASE_URL
  74.                 + " to the file " + fileURI.toString(), e);
  75.             if (exportManager != null)
  76.                 exportManager.close();
  77.             if (database != null)
  78.                 database.close();
  79.             System.exit(1);
  80.         }
  81.     }
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement