Search in sources :

Example 1 with ODatabaseImportException

use of com.orientechnologies.orient.core.db.tool.ODatabaseImportException in project orientdb by orientechnologies.

the class ODatabaseImportRemote method importDatabase.

public void importDatabase() throws ODatabaseImportException {
    OStorageRemote storage = (OStorageRemote) ((ODatabaseDocumentInternal) getDatabase()).getStorage();
    File file = new File(getFileName());
    try {
        storage.importDatabase(options, new FileInputStream(file), file.getName(), getListener());
    } catch (FileNotFoundException e) {
        throw OException.wrapException(new ODatabaseImportException("Error importing the database"), e);
    }
}
Also used : FileNotFoundException(java.io.FileNotFoundException) ODatabaseImportException(com.orientechnologies.orient.core.db.tool.ODatabaseImportException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 2 with ODatabaseImportException

use of com.orientechnologies.orient.core.db.tool.ODatabaseImportException in project orientdb by orientechnologies.

the class OGraphMLReader method inputGraph.

/**
 * Input the GraphML stream data into the graph. More control over how data is streamed is provided by this method.
 *
 * @param inputGraph
 *          the graph to populate with the GraphML data
 * @param graphMLInputStream
 *          an InputStream of GraphML data
 * @param bufferSize
 *          the amount of elements to hold in memory before committing a transactions (only valid for TransactionalGraphs)
 * @param vertexIdKey
 *          if the id of a vertex is a <data/> property, fetch it from the data property.
 * @param edgeIdKey
 *          if the id of an edge is a <data/> property, fetch it from the data property.
 * @param edgeLabelKey
 *          if the label of an edge is a <data/> property, fetch it from the data property.
 * @throws IOException
 *           thrown when the GraphML data is not correctly formatted
 */
public OGraphMLReader inputGraph(final Graph inputGraph, final InputStream graphMLInputStream, int bufferSize, String vertexIdKey, String edgeIdKey, String edgeLabelKey) throws IOException {
    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    try {
        XMLStreamReader reader = inputFactory.createXMLStreamReader(graphMLInputStream);
        final OrientBaseGraph graph = (OrientBaseGraph) inputGraph;
        if (storeVertexIds)
            graph.setSaveOriginalIds(storeVertexIds);
        Map<String, String> keyIdMap = new HashMap<String, String>();
        Map<String, String> keyTypesMaps = new HashMap<String, String>();
        // <Mapped ID String, ID Object>
        // <Default ID String, Mapped ID String>
        Map<String, ORID> vertexMappedIdMap = new HashMap<String, ORID>();
        // Buffered Vertex Data
        String vertexId = null;
        Map<String, Object> vertexProps = null;
        boolean inVertex = false;
        // Buffered Edge Data
        String edgeId = null;
        String edgeLabel = null;
        String vertexLabel = null;
        // [0] = outVertex , [1] = inVertex
        Vertex[] edgeEndVertices = null;
        Map<String, Object> edgeProps = null;
        boolean inEdge = false;
        int bufferCounter = 0;
        long importedVertices = 0;
        long importedEdges = 0;
        while (reader.hasNext()) {
            Integer eventType = reader.next();
            if (eventType.equals(XMLEvent.START_ELEMENT)) {
                String elementName = reader.getName().getLocalPart();
                if (elementName.equals(GraphMLTokens.KEY)) {
                    String id = reader.getAttributeValue(null, GraphMLTokens.ID);
                    String attributeName = reader.getAttributeValue(null, GraphMLTokens.ATTR_NAME);
                    String attributeType = reader.getAttributeValue(null, GraphMLTokens.ATTR_TYPE);
                    keyIdMap.put(id, attributeName);
                    keyTypesMaps.put(id, attributeType);
                } else if (elementName.equals(GraphMLTokens.NODE)) {
                    vertexId = reader.getAttributeValue(null, GraphMLTokens.ID);
                    vertexLabel = reader.getAttributeValue(null, LABELS);
                    if (vertexLabel != null) {
                        if (vertexLabel.startsWith(":"))
                            // REMOVE : AS PREFIX
                            vertexLabel = vertexLabel.substring(1);
                        final String[] vertexLabels = vertexLabel.split(":");
                        // GET ONLY FIRST LABEL AS CLASS
                        vertexLabel = vertexId + ",class:" + vertexLabels[vertexLabelIndex];
                    } else
                        vertexLabel = vertexId;
                    inVertex = true;
                    vertexProps = new HashMap<String, Object>();
                } else if (elementName.equals(GraphMLTokens.EDGE)) {
                    edgeId = reader.getAttributeValue(null, GraphMLTokens.ID);
                    edgeLabel = reader.getAttributeValue(null, GraphMLTokens.LABEL);
                    edgeLabel = edgeLabel == null ? GraphMLTokens._DEFAULT : edgeLabel;
                    String[] vertexIds = new String[2];
                    vertexIds[0] = reader.getAttributeValue(null, GraphMLTokens.SOURCE);
                    vertexIds[1] = reader.getAttributeValue(null, GraphMLTokens.TARGET);
                    edgeEndVertices = new Vertex[2];
                    for (int i = 0; i < 2; i++) {
                        // i=0 => outVertex, i=1 => inVertex
                        if (vertexIdKey == null) {
                            edgeEndVertices[i] = null;
                        } else {
                            final Object vId = vertexMappedIdMap.get(vertexIds[i]);
                            edgeEndVertices[i] = vId != null ? graph.getVertex(vId) : null;
                        }
                        if (null == edgeEndVertices[i]) {
                            edgeEndVertices[i] = graph.addVertex(vertexLabel);
                            if (vertexIdKey != null) {
                                mapId(vertexMappedIdMap, vertexIds[i], (ORID) edgeEndVertices[i].getId());
                            }
                            bufferCounter++;
                            importedVertices++;
                            printStatus(reader, importedVertices, importedEdges);
                        }
                    }
                    inEdge = true;
                    vertexLabel = null;
                    edgeProps = new HashMap<String, Object>();
                } else if (elementName.equals(GraphMLTokens.DATA)) {
                    String key = reader.getAttributeValue(null, GraphMLTokens.KEY);
                    String attributeName = keyIdMap.get(key);
                    if (attributeName == null)
                        attributeName = key;
                    String value = reader.getElementText();
                    if (inVertex) {
                        if ((vertexIdKey != null) && (key.equals(vertexIdKey))) {
                            // Should occur at most once per Vertex
                            vertexId = value;
                        } else if (attributeName.equalsIgnoreCase(LABELS)) {
                        // IGNORE LABELS
                        } else {
                            final Object attrValue = typeCastValue(key, value, keyTypesMaps);
                            final OGraphMLImportStrategy strategy = vertexPropsStrategy.get(attributeName);
                            if (strategy != null) {
                                attributeName = strategy.transformAttribute(attributeName, attrValue);
                            }
                            if (attributeName != null)
                                vertexProps.put(attributeName, attrValue);
                        }
                    } else if (inEdge) {
                        if ((edgeLabelKey != null) && (key.equals(edgeLabelKey)))
                            edgeLabel = value;
                        else if ((edgeIdKey != null) && (key.equals(edgeIdKey)))
                            edgeId = value;
                        else {
                            final Object attrValue = typeCastValue(key, value, keyTypesMaps);
                            final OGraphMLImportStrategy strategy = edgePropsStrategy.get(attributeName);
                            if (strategy != null) {
                                attributeName = strategy.transformAttribute(attributeName, attrValue);
                            }
                            if (attributeName != null)
                                edgeProps.put(attributeName, attrValue);
                        }
                    }
                }
            } else if (eventType.equals(XMLEvent.END_ELEMENT)) {
                String elementName = reader.getName().getLocalPart();
                if (elementName.equals(GraphMLTokens.NODE)) {
                    ORID currentVertex = null;
                    if (vertexIdKey != null)
                        currentVertex = vertexMappedIdMap.get(vertexId);
                    if (currentVertex == null) {
                        final OrientVertex v = graph.addVertex(vertexLabel, vertexProps);
                        if (vertexIdKey != null)
                            mapId(vertexMappedIdMap, vertexId, v.getIdentity());
                        bufferCounter++;
                        importedVertices++;
                        printStatus(reader, importedVertices, importedEdges);
                    } else {
                        // UPDATE IT
                        final OrientVertex v = graph.getVertex(currentVertex);
                        v.setProperties(vertexProps);
                    }
                    vertexId = null;
                    vertexLabel = null;
                    vertexProps = null;
                    inVertex = false;
                } else if (elementName.equals(GraphMLTokens.EDGE)) {
                    Edge currentEdge = ((OrientVertex) edgeEndVertices[0]).addEdge(null, (OrientVertex) edgeEndVertices[1], edgeLabel, null, edgeProps);
                    bufferCounter++;
                    importedEdges++;
                    printStatus(reader, importedVertices, importedEdges);
                    edgeId = null;
                    edgeLabel = null;
                    edgeEndVertices = null;
                    edgeProps = null;
                    inEdge = false;
                }
            }
            if (bufferCounter > bufferSize) {
                graph.commit();
                bufferCounter = 0;
            }
        }
        reader.close();
        graph.commit();
    } catch (Exception xse) {
        throw OException.wrapException(new ODatabaseImportException("Error on importing GraphML"), xse);
    }
    return this;
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) OrientVertex(com.tinkerpop.blueprints.impls.orient.OrientVertex) XMLStreamReader(javax.xml.stream.XMLStreamReader) HashMap(java.util.HashMap) ODatabaseImportException(com.orientechnologies.orient.core.db.tool.ODatabaseImportException) OrientVertex(com.tinkerpop.blueprints.impls.orient.OrientVertex) OrientBaseGraph(com.tinkerpop.blueprints.impls.orient.OrientBaseGraph) ODatabaseImportException(com.orientechnologies.orient.core.db.tool.ODatabaseImportException) OException(com.orientechnologies.common.exception.OException) IOException(java.io.IOException) ORID(com.orientechnologies.orient.core.id.ORID) Edge(com.tinkerpop.blueprints.Edge) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 3 with ODatabaseImportException

use of com.orientechnologies.orient.core.db.tool.ODatabaseImportException in project orientdb by orientechnologies.

the class OGremlinConsole method exportDatabase.

@Override
@ConsoleCommand(description = "Export a database", splitInWords = false, onlineHelp = "Console-Command-Export")
public void exportDatabase(@ConsoleParameter(name = "options", description = "Export options") String iText) throws IOException {
    checkForDatabase();
    final List<String> items = OStringSerializerHelper.smartSplit(iText, ' ');
    final String fileName = items.size() <= 1 || items.get(1).charAt(0) == '-' ? null : items.get(1);
    if (fileName != null && (fileName.endsWith(".graphml") || fileName.endsWith(".xml"))) {
        message("\nExporting database in GRAPHML format to " + iText + "...");
        try {
            final OrientGraph g = (OrientGraph) OrientGraphFactory.getTxGraphImplFactory().getGraph(currentDatabase);
            g.setUseLog(false);
            g.setWarnOnForceClosingTx(false);
            // CREATE THE EXPORT FILE IF NOT EXIST YET
            final File f = new File(fileName);
            if (f.getParentFile() != null) {
                f.getParentFile().mkdirs();
            }
            f.createNewFile();
            new GraphMLWriter(g).outputGraph(fileName);
        } catch (ODatabaseImportException e) {
            printError(e);
        }
    } else
        // BASE EXPORT
        super.exportDatabase(iText);
}
Also used : OrientGraph(com.tinkerpop.blueprints.impls.orient.OrientGraph) GraphMLWriter(com.tinkerpop.blueprints.util.io.graphml.GraphMLWriter) ODatabaseImportException(com.orientechnologies.orient.core.db.tool.ODatabaseImportException) File(java.io.File) ConsoleCommand(com.orientechnologies.common.console.annotation.ConsoleCommand)

Example 4 with ODatabaseImportException

use of com.orientechnologies.orient.core.db.tool.ODatabaseImportException in project orientdb by orientechnologies.

the class OGraphSONReader method inputGraph.

/**
 * Input the JSON stream data into the graph. More control over how data is streamed is provided by this method.
 *
 * @param filename
 *          name of a file of JSON data
 * @param bufferSize
 *          the amount of elements to hold in memory before committing a transactions (only valid for TransactionalGraphs)
 * @throws IOException
 *           thrown when the JSON data is not correctly formatted
 */
public void inputGraph(final String filename, int bufferSize, final Set<String> edgePropertyKeys, final Set<String> vertexPropertyKeys) throws IOException {
    final File file = new File(filename);
    if (!file.exists())
        throw new ODatabaseImportException("File '" + filename + "' not found");
    inputSize = file.length();
    final FileInputStream fis = new FileInputStream(filename);
    try {
        inputGraph(fis, bufferSize, edgePropertyKeys, vertexPropertyKeys);
    } finally {
        fis.close();
    }
}
Also used : ODatabaseImportException(com.orientechnologies.orient.core.db.tool.ODatabaseImportException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 5 with ODatabaseImportException

use of com.orientechnologies.orient.core.db.tool.ODatabaseImportException in project orientdb by orientechnologies.

the class OGremlinConsole method importDatabase.

@Override
@ConsoleCommand(description = "Import a database into the current one", splitInWords = false)
public void importDatabase(@ConsoleParameter(name = "options", description = "Import options") String text) throws IOException {
    checkForDatabase();
    final List<String> items = OStringSerializerHelper.smartSplit(text, ' ');
    final String fileName = items.size() <= 0 || (items.get(1)).charAt(0) == '-' ? null : items.get(1);
    final String optionsAsString = fileName != null ? text.substring((items.get(0)).length() + (items.get(1)).length() + 1).trim() : text;
    final Map<String, List<String>> options = parseOptions(optionsAsString);
    final String format = options.containsKey("-format") ? options.get("-format").get(0) : null;
    if ((format != null && format.equalsIgnoreCase("graphml")) || (fileName != null && (fileName.endsWith(".graphml") || fileName.endsWith(".xml")))) {
        // GRAPHML
        message("\nImporting GRAPHML database from " + fileName + " with options (" + optionsAsString + ")...");
        try {
            final OrientGraph g = (OrientGraph) OrientGraphFactory.getTxGraphImplFactory().getGraph(currentDatabase);
            g.setUseLog(false);
            g.setWarnOnForceClosingTx(false);
            final long totalEdges = g.countEdges();
            final long totalVertices = g.countVertices();
            final File file = new File(fileName);
            if (!file.exists())
                throw new ODatabaseImportException("Input file '" + fileName + "' not exists");
            InputStream is = new FileInputStream(file);
            if (fileName.endsWith(".zip"))
                is = new ZipInputStream(is);
            else if (fileName.endsWith(".gz"))
                is = new GZIPInputStream(is);
            try {
                new OGraphMLReader(g).setOptions(options).setOutput(new OCommandOutputListener() {

                    @Override
                    public void onMessage(final String iText) {
                        System.out.print("\r" + iText);
                    }
                }).inputGraph(is);
                g.commit();
                currentDatabase.commit();
                message("\nDone: imported %d vertices and %d edges", g.countVertices() - totalVertices, g.countEdges() - totalEdges);
            } finally {
                is.close();
            }
        } catch (ODatabaseImportException e) {
            printError(e);
        }
    } else if ((format != null && format.equalsIgnoreCase("graphson")) || (fileName != null && (fileName.endsWith(".graphson")))) {
        // GRAPHSON
        message("\nImporting GRAPHSON database from " + fileName + " with options (" + optionsAsString + ")...");
        try {
            final OrientGraph g = (OrientGraph) OrientGraphFactory.getTxGraphImplFactory().getGraph(currentDatabase);
            g.setUseLog(false);
            g.setWarnOnForceClosingTx(false);
            final long totalEdges = g.countEdges();
            final long totalVertices = g.countVertices();
            final File file = new File(fileName);
            if (!file.exists())
                throw new ODatabaseImportException("Input file '" + fileName + "' not exists");
            InputStream is = new FileInputStream(file);
            if (fileName.endsWith(".zip")) {
                is = new ZipInputStream(is);
            } else if (fileName.endsWith(".gz")) {
                is = new GZIPInputStream(is);
            }
            try {
                new OGraphSONReader(g).setOutput(new OCommandOutputListener() {

                    @Override
                    public void onMessage(final String iText) {
                        System.out.print("\r" + iText);
                    }
                }).inputGraph(is, 10000);
                // new OGraphMLReader(g).setOptions(options).inputGraph(g, fileName);
                g.commit();
                currentDatabase.commit();
                message("\nDone: imported %d vertices and %d edges", g.countVertices() - totalVertices, g.countEdges() - totalEdges);
            } finally {
                is.close();
            }
        } catch (ODatabaseImportException e) {
            printError(e);
        }
    } else if (format == null)
        super.importDatabase(text);
    else
        throw new IllegalArgumentException("Format '" + format + "' is not supported");
}
Also used : OrientGraph(com.tinkerpop.blueprints.impls.orient.OrientGraph) GZIPInputStream(java.util.zip.GZIPInputStream) ZipInputStream(java.util.zip.ZipInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ODatabaseImportException(com.orientechnologies.orient.core.db.tool.ODatabaseImportException) FileInputStream(java.io.FileInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) ZipInputStream(java.util.zip.ZipInputStream) OGraphSONReader(com.orientechnologies.orient.graph.graphml.OGraphSONReader) OGraphMLReader(com.orientechnologies.orient.graph.graphml.OGraphMLReader) List(java.util.List) File(java.io.File) OCommandOutputListener(com.orientechnologies.orient.core.command.OCommandOutputListener) ConsoleCommand(com.orientechnologies.common.console.annotation.ConsoleCommand)

Aggregations

ODatabaseImportException (com.orientechnologies.orient.core.db.tool.ODatabaseImportException)5 File (java.io.File)4 FileInputStream (java.io.FileInputStream)3 ConsoleCommand (com.orientechnologies.common.console.annotation.ConsoleCommand)2 OrientGraph (com.tinkerpop.blueprints.impls.orient.OrientGraph)2 OException (com.orientechnologies.common.exception.OException)1 OCommandOutputListener (com.orientechnologies.orient.core.command.OCommandOutputListener)1 ORID (com.orientechnologies.orient.core.id.ORID)1 OGraphMLReader (com.orientechnologies.orient.graph.graphml.OGraphMLReader)1 OGraphSONReader (com.orientechnologies.orient.graph.graphml.OGraphSONReader)1 Edge (com.tinkerpop.blueprints.Edge)1 Vertex (com.tinkerpop.blueprints.Vertex)1 OrientBaseGraph (com.tinkerpop.blueprints.impls.orient.OrientBaseGraph)1 OrientVertex (com.tinkerpop.blueprints.impls.orient.OrientVertex)1 GraphMLWriter (com.tinkerpop.blueprints.util.io.graphml.GraphMLWriter)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 HashMap (java.util.HashMap)1 List (java.util.List)1