Search in sources :

Example 16 with ODocument

use of com.orientechnologies.orient.core.record.impl.ODocument in project orientdb by orientechnologies.

the class OGremlinHelper method execute.

public static Object execute(final OrientBaseGraph graph, final String iText, final Map<Object, Object> iConfiguredParameters, Map<Object, Object> iCurrentParameters, final List<Object> iResult, final OGremlinCallback iBeforeExecution, final OGremlinCallback iAfterExecution) {
    try {
        final ScriptEngine engine = getGremlinEngine(graph);
        try {
            final String output = OGremlinHelper.bindParameters(engine, iConfiguredParameters, iCurrentParameters);
            if (iBeforeExecution != null)
                if (!iBeforeExecution.call(engine, graph))
                    return null;
            if (iText == null) {
                return null;
            }
            final Object scriptResult = engine.eval(iText);
            if (iAfterExecution != null)
                if (!iAfterExecution.call(engine, graph))
                    return null;
            // - Map -> ODocument
            if (output != null) {
                if (scriptResult instanceof GremlinPipeline) {
                    Iterator<?> it = ((GremlinPipeline<?, ?>) scriptResult).iterator();
                    while (it.hasNext()) // ignore iCurrentRecord but traverse still required
                    it.next();
                }
                final Map<String, Object> map = (Map<String, Object>) engine.get(output);
                ODocument oDocument = new ODocument(map);
                iResult.add(oDocument);
                return oDocument;
            }
            // returned for this call in the last pipe
            if (scriptResult instanceof GremlinPipeline) {
                final Iterator<?> it = ((GremlinPipeline<?, ?>) scriptResult).iterator();
                Object finalResult = null;
                List<Object> resultCollection = null;
                while (it.hasNext()) {
                    Object current = it.next();
                    if (finalResult != null) {
                        if (resultCollection == null) {
                            // CONVERT IT INTO A COLLECTION
                            resultCollection = new ArrayList<Object>();
                            resultCollection.add(finalResult);
                        }
                        resultCollection.add(current);
                    } else
                        finalResult = current;
                }
                if (resultCollection != null) {
                    iResult.addAll(resultCollection);
                    return resultCollection;
                } else {
                    if (finalResult != null)
                        iResult.add(finalResult);
                    return finalResult;
                }
            } else if (scriptResult != null)
                iResult.add(scriptResult);
            return scriptResult;
        } catch (Exception e) {
            throw OException.wrapException(new OCommandExecutionException("Error on execution of the GREMLIN script"), e);
        } finally {
            OGremlinHelper.global().releaseEngine(engine);
        }
    } finally {
        OGremlinHelper.global().releaseGraph(graph);
    }
}
Also used : GremlinPipeline(com.tinkerpop.gremlin.java.GremlinPipeline) OCommandExecutionException(com.orientechnologies.orient.core.exception.OCommandExecutionException) HashMap(java.util.HashMap) Map(java.util.Map) GremlinGroovyScriptEngine(com.tinkerpop.gremlin.groovy.jsr223.GremlinGroovyScriptEngine) ScriptEngine(javax.script.ScriptEngine) OException(com.orientechnologies.common.exception.OException) OCommandExecutionException(com.orientechnologies.orient.core.exception.OCommandExecutionException) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 17 with ODocument

use of com.orientechnologies.orient.core.record.impl.ODocument in project orientdb by orientechnologies.

the class OLogTransformerTest method testPostfix.

@Test
public void testPostfix() throws Exception {
    ByteArrayOutputStream output = getByteArrayOutputStream();
    String cfgJson = "{source: { content: { value: 'id,text\n1,Hello\n2,Bye'} }, extractor : { csv : {} }, transformers : [{ log : {postfix:'-> '}}], loader : { test: {} } }";
    process(cfgJson);
    List<ODocument> res = getResult();
    ODocument doc = res.get(0);
    String[] stringList = output.toString().split(System.getProperty("line.separator"));
    assertEquals("[1:log] INFO {id:1,text:Hello}-> ", stringList[2]);
    assertEquals("[2:log] INFO {id:2,text:Bye}-> ", stringList[3]);
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) OETLBaseTest(com.orientechnologies.orient.etl.OETLBaseTest) Test(org.junit.Test)

Example 18 with ODocument

use of com.orientechnologies.orient.core.record.impl.ODocument in project orientdb by orientechnologies.

the class OLogTransformerTest method testPrefix.

@Test
public void testPrefix() throws Exception {
    ByteArrayOutputStream output = getByteArrayOutputStream();
    String cfgJson = "{source: { content: { value: 'id,text\n1,Hello\n2,Bye'} }, extractor : { csv: {} }, transformers : [{ log : {prefix:'-> '}}], loader : { test: {} } }";
    process(cfgJson);
    List<ODocument> res = getResult();
    ODocument doc = res.get(0);
    String[] stringList = output.toString().split(System.getProperty("line.separator"));
    assertEquals("[1:log] INFO -> {id:1,text:Hello}", stringList[2]);
    assertEquals("[2:log] INFO -> {id:2,text:Bye}", stringList[3]);
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) OETLBaseTest(com.orientechnologies.orient.etl.OETLBaseTest) Test(org.junit.Test)

Example 19 with ODocument

use of com.orientechnologies.orient.core.record.impl.ODocument in project orientdb by orientechnologies.

the class OGraphBatchInsert method createEdge.

/**
   * Creates a new edge between two vertices. If vertices do not exist, they will be created
   * 
   * @param from
   *          id of the vertex that is starting point of the edge
   * @param to
   *          id of the vertex that is end point of the edge
   */
public void createEdge(final Long from, final Long to, Map<String, Object> properties) {
    if (settingProperties) {
        throw new IllegalStateException("Cannot create new edges when already set properties on vertices");
    }
    if (from < 0) {
        throw new IllegalArgumentException(" Invalid vertex id: " + from);
    }
    if (to < 0) {
        throw new IllegalArgumentException(" Invalid vertex id: " + to);
    }
    if (useLightWeigthEdges && (properties == null || properties.size() == 0)) {
        last = last < from ? from : last;
        last = last < to ? to : last;
        putInList(from, out, to);
        putInList(to, in, from);
    } else {
        ODocument edgeDoc = new ODocument(edgeClass);
        edgeDoc.fromMap(properties);
        edgeDoc.field("out", new ORecordId(getClusterId(from), getClusterPosition(from)));
        edgeDoc.field("in", new ORecordId(getClusterId(to), getClusterPosition(to)));
        db.save(edgeDoc);
        ORecordId rid = (ORecordId) edgeDoc.getIdentity();
        putInList(from, out, rid);
        putInList(to, in, rid);
    }
}
Also used : ORecordId(com.orientechnologies.orient.core.id.ORecordId) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 20 with ODocument

use of com.orientechnologies.orient.core.record.impl.ODocument in project orientdb by orientechnologies.

the class OOrientDBLoader method configure.

@Override
public void configure(final OETLProcessor iProcessor, final ODocument conf, final OCommandContext iContext) {
    super.configure(iProcessor, conf, iContext);
    if (conf.containsField("dbURL"))
        dbURL = (String) resolve(conf.field("dbURL"));
    if (conf.containsField("dbUser"))
        dbUser = (String) resolve(conf.field("dbUser"));
    if (conf.containsField("dbPassword"))
        dbPassword = (String) resolve(conf.field("dbPassword"));
    if (conf.containsField("serverUser"))
        serverUser = (String) resolve(conf.field("serverUser"));
    if (conf.containsField("serverPassword"))
        serverPassword = (String) resolve(conf.field("serverPassword"));
    if (conf.containsField("dbType"))
        dbType = DB_TYPE.valueOf(conf.field("dbType").toString().toUpperCase());
    if (conf.containsField("tx"))
        tx = conf.<Boolean>field("tx");
    if (conf.containsField("wal"))
        wal = conf.<Boolean>field("wal");
    if (conf.containsField("txUseLog"))
        txUseLog = conf.<Boolean>field("txUseLog");
    if (conf.containsField("batchCommit"))
        batchCommitSize = conf.<Integer>field("batchCommit");
    if (conf.containsField("dbAutoCreate"))
        dbAutoCreate = conf.<Boolean>field("dbAutoCreate");
    if (conf.containsField("dbAutoDropIfExists"))
        dbAutoDropIfExists = conf.<Boolean>field("dbAutoDropIfExists");
    if (conf.containsField("dbAutoCreateProperties"))
        dbAutoCreateProperties = conf.<Boolean>field("dbAutoCreateProperties");
    if (conf.containsField("useLightweightEdges"))
        useLightweightEdges = conf.<Boolean>field("useLightweightEdges");
    if (conf.containsField("standardElementConstraints"))
        standardElementConstraints = conf.<Boolean>field("standardElementConstraints");
    if (conf.containsField("skipDuplicates"))
        skipDuplicates = conf.field("skipDuplicates");
    clusterName = conf.field("cluster");
    className = conf.field("class");
    indexes = conf.field("indexes");
    classes = conf.field("classes");
    if (conf.containsField("settings")) {
        final ODocument settings = conf.field("settings");
        settings.setAllowChainedAccess(false);
        for (String s : settings.fieldNames()) {
            final OGlobalConfiguration v = OGlobalConfiguration.findByKey(s);
            if (v != null)
                v.setValue(settings.field(s));
        }
    }
    // use wal or not
    OGlobalConfiguration.USE_WAL.setValue(wal);
    if (dbURL.startsWith("remote")) {
        manageRemoteDatabase();
    } else {
        switch(dbType) {
            case DOCUMENT:
                configureDocumentDB();
                break;
            case GRAPH:
                configureGraphDB();
                break;
        }
    }
}
Also used : OGlobalConfiguration(com.orientechnologies.orient.core.config.OGlobalConfiguration) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Aggregations

ODocument (com.orientechnologies.orient.core.record.impl.ODocument)2200 Test (org.testng.annotations.Test)651 OSQLSynchQuery (com.orientechnologies.orient.core.sql.query.OSQLSynchQuery)426 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)422 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)277 Test (org.junit.Test)267 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)257 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)244 ORID (com.orientechnologies.orient.core.id.ORID)196 ORecordId (com.orientechnologies.orient.core.id.ORecordId)139 OSchema (com.orientechnologies.orient.core.metadata.schema.OSchema)122 ArrayList (java.util.ArrayList)118 ORidBag (com.orientechnologies.orient.core.db.record.ridbag.ORidBag)103 HashMap (java.util.HashMap)103 HashSet (java.util.HashSet)96 ORecord (com.orientechnologies.orient.core.record.ORecord)80 Set (java.util.Set)76 OETLBaseTest (com.orientechnologies.orient.etl.OETLBaseTest)75 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)68 Collection (java.util.Collection)55