Search in sources :

Example 1 with OGraphMLReader

use of com.orientechnologies.orient.graph.graphml.OGraphMLReader in project orientdb by orientechnologies.

the class OGremlinConsoleTest method testGraphMLImportIgnoreEAttribute.

@Test
public void testGraphMLImportIgnoreEAttribute() throws IOException {
    final String INPUT_FILE = "src/test/resources/graph-example-fromexport.xml";
    String dbUrl = "memory:testGraphMLImportIgnoreEAttribute";
    new OGraphMLReader(new OrientGraphNoTx(dbUrl)).defineEdgeAttributeStrategy("friend", new OIgnoreGraphMLImportStrategy()).inputGraph(INPUT_FILE);
    ODatabaseDocumentTx db = new ODatabaseDocumentTx(dbUrl);
    db.open("admin", "admin");
    try {
        List<ODocument> result = db.query(new OSQLSynchQuery<ODocument>("select from E"));
        Assert.assertFalse(result.isEmpty());
        for (ODocument d : result) {
            Assert.assertFalse(d.containsField("friend"));
        }
    } finally {
        db.close();
    }
}
Also used : OIgnoreGraphMLImportStrategy(com.orientechnologies.orient.graph.graphml.OIgnoreGraphMLImportStrategy) OGraphMLReader(com.orientechnologies.orient.graph.graphml.OGraphMLReader) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) OrientGraphNoTx(com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.junit.Test)

Example 2 with OGraphMLReader

use of com.orientechnologies.orient.graph.graphml.OGraphMLReader in project orientdb by orientechnologies.

the class OGremlinConsoleTest method testGraphMLImportRenameVAttribute.

@Test
public void testGraphMLImportRenameVAttribute() throws IOException {
    final String INPUT_FILE = "src/test/resources/graph-example-fromexport.xml";
    String dbUrl = "memory:testGraphMLImportRenameVAttribute";
    final OrientGraphNoTx graph = new OrientGraphNoTx(dbUrl);
    try {
        new OGraphMLReader(graph).defineVertexAttributeStrategy("__type__", new ORenameGraphMLImportStrategy("t")).inputGraph(INPUT_FILE);
        ODatabaseDocumentTx db = new ODatabaseDocumentTx(dbUrl);
        db.open("admin", "admin");
        try {
            List<ODocument> result = db.query(new OSQLSynchQuery<ODocument>("select from Person"));
            Assert.assertFalse(result.isEmpty());
            for (ODocument d : result) {
                Assert.assertTrue(d.containsField("t"));
                Assert.assertFalse(d.containsField("__type__"));
            }
        } finally {
            db.close();
        }
    } finally {
        graph.shutdown();
    }
}
Also used : ORenameGraphMLImportStrategy(com.orientechnologies.orient.graph.graphml.ORenameGraphMLImportStrategy) OGraphMLReader(com.orientechnologies.orient.graph.graphml.OGraphMLReader) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) OrientGraphNoTx(com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.junit.Test)

Example 3 with OGraphMLReader

use of com.orientechnologies.orient.graph.graphml.OGraphMLReader in project orientdb by orientechnologies.

the class OGremlinConsoleTest method testGraphMLImportDirect.

@Test
public void testGraphMLImportDirect() throws IOException {
    final String INPUT_FILE = "src/test/resources/graph-example-fromexport.xml";
    String dbUrl = "memory:testGraphMLImportDirect";
    new OGraphMLReader(new OrientGraphNoTx(dbUrl)).inputGraph(INPUT_FILE);
    ODatabaseDocumentTx db = new ODatabaseDocumentTx(dbUrl);
    db.open("admin", "admin");
    try {
        boolean foundTypeVAttr = false;
        boolean foundFriendEAttr = false;
        List<ODocument> result = db.query(new OSQLSynchQuery<ODocument>("select from V"));
        Assert.assertFalse(result.isEmpty());
        for (ODocument d : result) {
            if (d.containsField("__type__"))
                foundTypeVAttr = true;
        }
        Assert.assertTrue(foundTypeVAttr);
        result = db.query(new OSQLSynchQuery<ODocument>("select from E"));
        Assert.assertFalse(result.isEmpty());
        for (ODocument d : result) {
            if (d.containsField("friend"))
                foundFriendEAttr = true;
        }
        Assert.assertTrue(foundFriendEAttr);
    } finally {
        db.close();
    }
}
Also used : OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) OGraphMLReader(com.orientechnologies.orient.graph.graphml.OGraphMLReader) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) OrientGraphNoTx(com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.junit.Test)

Example 4 with OGraphMLReader

use of com.orientechnologies.orient.graph.graphml.OGraphMLReader in project orientdb by orientechnologies.

the class OGremlinConsoleTest method testGraphSONImport.

@Test
public void testGraphSONImport() throws IOException {
    final String INPUT_FILE = "src/test/resources/graph-example-fromexport.xml";
    String dbUrl1 = "memory:testGraphSONImport1";
    String dbUrl2 = "memory:testGraphSONImport2";
    final OrientGraphNoTx g1 = new OrientGraphNoTx(dbUrl1);
    new OGraphMLReader(g1).inputGraph(INPUT_FILE);
    // EXPORT IN GRAPHSON FORMAT
    final ByteArrayOutputStream output = new ByteArrayOutputStream();
    new GraphSONWriter(g1).outputGraph(output, null, null, GraphSONMode.NORMAL);
    final OrientGraphNoTx g2 = new OrientGraphNoTx(dbUrl2);
    ByteArrayInputStream is = new ByteArrayInputStream(output.toByteArray());
    new OGraphSONReader(g2).inputGraph(is);
    ODatabaseDocumentTx db = new ODatabaseDocumentTx(dbUrl2);
    db.open("admin", "admin");
    try {
        boolean foundTypeVAttr = false;
        boolean foundFriendEAttr = false;
        List<ODocument> result = db.query(new OSQLSynchQuery<ODocument>("select from V"));
        Assert.assertFalse(result.isEmpty());
        for (ODocument d : result) {
            if (d.containsField("__type__"))
                foundTypeVAttr = true;
        }
        Assert.assertTrue(foundTypeVAttr);
        result = db.query(new OSQLSynchQuery<ODocument>("select from E"));
        Assert.assertFalse(result.isEmpty());
        for (ODocument d : result) {
            if (d.containsField("friend"))
                foundFriendEAttr = true;
        }
        Assert.assertTrue(foundFriendEAttr);
    } finally {
        db.close();
    }
}
Also used : GraphSONWriter(com.tinkerpop.blueprints.util.io.graphson.GraphSONWriter) OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) OGraphSONReader(com.orientechnologies.orient.graph.graphml.OGraphSONReader) OGraphMLReader(com.orientechnologies.orient.graph.graphml.OGraphMLReader) OrientGraphNoTx(com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.junit.Test)

Example 5 with OGraphMLReader

use of com.orientechnologies.orient.graph.graphml.OGraphMLReader 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

OGraphMLReader (com.orientechnologies.orient.graph.graphml.OGraphMLReader)6 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)5 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)5 OrientGraphNoTx (com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx)5 Test (org.junit.Test)5 OSQLSynchQuery (com.orientechnologies.orient.core.sql.query.OSQLSynchQuery)2 OGraphSONReader (com.orientechnologies.orient.graph.graphml.OGraphSONReader)2 OIgnoreGraphMLImportStrategy (com.orientechnologies.orient.graph.graphml.OIgnoreGraphMLImportStrategy)2 ConsoleCommand (com.orientechnologies.common.console.annotation.ConsoleCommand)1 OCommandOutputListener (com.orientechnologies.orient.core.command.OCommandOutputListener)1 ODatabaseImportException (com.orientechnologies.orient.core.db.tool.ODatabaseImportException)1 ORenameGraphMLImportStrategy (com.orientechnologies.orient.graph.graphml.ORenameGraphMLImportStrategy)1 OrientGraph (com.tinkerpop.blueprints.impls.orient.OrientGraph)1 GraphSONWriter (com.tinkerpop.blueprints.util.io.graphson.GraphSONWriter)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 List (java.util.List)1 GZIPInputStream (java.util.zip.GZIPInputStream)1 ZipInputStream (java.util.zip.ZipInputStream)1