Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.example.graph.orientdb;
- import java.io.BufferedInputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.InputStream;
- import java.net.URI;
- import org.apache.log4j.Logger;
- import com.orientechnologies.orient.client.remote.OEngineRemote;
- import com.orientechnologies.orient.client.remote.OServerAdmin;
- import com.orientechnologies.orient.core.Orient;
- import com.orientechnologies.orient.core.command.OCommandOutputListener;
- import com.orientechnologies.orient.core.db.document.ODatabaseDocument;
- import com.orientechnologies.orient.core.db.tool.ODatabaseImport;
- import com.tinkerpop.blueprints.pgm.impls.orientdb.OrientGraph;
- public class GraphDatabaseImporter {
- protected static final Logger LOGGER = Logger.getLogger(GraphDatabaseExporter.class);
- private static final String DATABASE_URL = "remote:iot-at-work1/tinkerpop";
- private static final String USERNAME = "admin";
- private static final String PASSWORD = "admin";
- private static final String ADMINISTRATOR_NAME = "root";
- private static final String ADMINISTRATOR_PWD = "EB61C596EAA0239700B02171DF791F2F423CFABF6A890FB032218EB8F22565E9";
- /**
- * @param args
- */
- public static void main(String[] args) {
- //REGISTER THE ENGINE
- Orient.instance().registerEngine(new OEngineRemote());
- OServerAdmin adminTool = null;
- try {
- adminTool = new OServerAdmin(DATABASE_URL).connect(ADMINISTRATOR_NAME,ADMINISTRATOR_PWD);
- LOGGER.debug("Successful connection to " + DATABASE_URL);
- adminTool.dropDatabase();
- adminTool.createDatabase("local");
- adminTool.close();
- LOGGER.debug("The database has been deleted.");
- } catch (Exception e) {
- LOGGER.error("Exception during the deletion of the database " + DATABASE_URL,e);
- System.exit(1);
- }
- final URI fileURI = new File("D:\\eclipse workspaces\\iot-at-work\\OrientDBGraphDBExample\\export\\exp_db.json").toURI();
- //check the parameters
- if (fileURI == null) {
- IllegalArgumentException topicNullExcep = new IllegalArgumentException(
- "The URI of the file to be imported into the database '" + DATABASE_URL +
- "' to cannot be null.");
- LOGGER.error(topicNullExcep.getMessage(),topicNullExcep);
- throw topicNullExcep;
- }
- File file = new File (fileURI);
- InputStream fileStream = null;
- try {
- fileStream = new BufferedInputStream(new FileInputStream(file));
- } catch (FileNotFoundException e) {
- LOGGER.error("Error in opening the file " + fileURI.toString() + "(database import)", e);
- System.exit(1);
- }
- OrientGraph graph = new OrientGraph(
- DATABASE_URL, USERNAME,PASSWORD);
- //graph.clear(); THROWS EXCEPTION
- ODatabaseImport importManager = null;
- try {
- ODatabaseDocument database = graph.getRawGraph();
- importManager = new ODatabaseImport(database ,fileStream,new OCommandOutputListener () {
- public void onMessage(String iText) {
- LOGGER.debug("Importing database " + DATABASE_URL +
- " to " + fileURI.toString() + " - " + iText);
- }
- });
- importManager.importDatabase();
- database.close();
- } catch (IOException e) {
- LOGGER.error("Exception during the import of the file " + fileURI.toString() +
- " into the database " + DATABASE_URL,e);
- System.exit(1);
- }
- }
- }
Add Comment
Please, Sign In to add comment