Search in sources :

Example 1 with OETLProcessHaltedException

use of com.orientechnologies.orient.etl.OETLProcessHaltedException in project orientdb by orientechnologies.

the class OLinkTransformer method executeTransform.

@Override
public Object executeTransform(final Object input) {
    if (!(input instanceof OIdentifiable)) {
        log(OETLProcessor.LOG_LEVELS.DEBUG, "skip because input value is not a record, but rather an instance of class: %s", input.getClass());
        return null;
    }
    final ODocument doc = ((OIdentifiable) input).getRecord();
    final Object joinRuntimeValue;
    if (joinFieldName != null)
        joinRuntimeValue = doc.field(joinFieldName);
    else if (joinValue != null)
        joinRuntimeValue = resolve(joinValue);
    else
        joinRuntimeValue = null;
    Object result;
    if (OMultiValue.isMultiValue(joinRuntimeValue)) {
        // RESOLVE SINGLE JOINS
        final Collection<Object> singleJoinsResult = new ArrayList<Object>();
        for (Object o : OMultiValue.getMultiValueIterable(joinRuntimeValue)) {
            singleJoinsResult.add(lookup(o, true));
        }
        result = singleJoinsResult;
    } else
        result = lookup(joinRuntimeValue, true);
    log(OETLProcessor.LOG_LEVELS.DEBUG, "joinRuntimeValue=%s, lookupResult=%s", joinRuntimeValue, result);
    if (result != null) {
        if (linkFieldType != null) {
            // CONVERT IT
            if (linkFieldType == OType.LINK) {
                if (result instanceof Collection<?>) {
                    if (!((Collection) result).isEmpty())
                        result = ((Collection) result).iterator().next();
                    else
                        result = null;
                }
            } else if (linkFieldType == OType.LINKSET) {
                if (!(result instanceof Collection)) {
                    final Set<OIdentifiable> res = new HashSet<OIdentifiable>();
                    res.add((OIdentifiable) result);
                    result = res;
                }
            } else if (linkFieldType == OType.LINKLIST) {
                if (!(result instanceof Collection)) {
                    final List<OIdentifiable> res = new ArrayList<OIdentifiable>();
                    res.add((OIdentifiable) result);
                    result = res;
                }
            }
        }
        if (result == null) {
            // APPLY THE STRATEGY DEFINED IN unresolvedLinkAction
            switch(unresolvedLinkAction) {
                case CREATE:
                    if (lookup != null) {
                        final String[] lookupParts = lookup.split("\\.");
                        final ODocument linkedDoc = new ODocument(lookupParts[0]);
                        linkedDoc.field(lookupParts[1], joinRuntimeValue);
                        linkedDoc.save();
                        log(OETLProcessor.LOG_LEVELS.DEBUG, "created new document=%s", linkedDoc.getRecord());
                        result = linkedDoc;
                    } else
                        throw new OConfigurationException("Cannot create linked document because target class is unknown. Use 'lookup' field");
                    break;
                case ERROR:
                    processor.getStats().incrementErrors();
                    log(OETLProcessor.LOG_LEVELS.ERROR, "%s: ERROR Cannot resolve join for value '%s'", getName(), joinRuntimeValue);
                    break;
                case WARNING:
                    processor.getStats().incrementWarnings();
                    log(OETLProcessor.LOG_LEVELS.INFO, "%s: WARN Cannot resolve join for value '%s'", getName(), joinRuntimeValue);
                    break;
                case SKIP:
                    return null;
                case HALT:
                    throw new OETLProcessHaltedException("[Link transformer] Cannot resolve join for value '" + joinRuntimeValue + "'");
            }
        }
    }
    // SET THE TRANSFORMED FIELD BACK
    doc.field(linkFieldName, result);
    log(OETLProcessor.LOG_LEVELS.DEBUG, "set %s=%s in document=%s", linkFieldName, result, input);
    return input;
}
Also used : OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) OETLProcessHaltedException(com.orientechnologies.orient.etl.OETLProcessHaltedException) OConfigurationException(com.orientechnologies.orient.core.exception.OConfigurationException) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 2 with OETLProcessHaltedException

use of com.orientechnologies.orient.etl.OETLProcessHaltedException in project orientdb by orientechnologies.

the class OEdgeTransformer method createEdge.

private List<OrientEdge> createEdge(final OrientVertex vertex, final Object joinCurrentValue, Object result) {
    log(OETLProcessor.LOG_LEVELS.DEBUG, "joinCurrentValue=%s, lookupResult=%s", joinCurrentValue, result);
    if (result == null) {
        // APPLY THE STRATEGY DEFINED IN unresolvedLinkAction
        switch(unresolvedLinkAction) {
            case CREATE:
                // Don't try to create a Vertex with a null value
                if (joinCurrentValue != null) {
                    if (lookup != null) {
                        final String[] lookupParts = lookup.split("\\.");
                        final OrientVertex linkedV = pipeline.getGraphDatabase().addTemporaryVertex(lookupParts[0]);
                        linkedV.setProperty(lookupParts[1], joinCurrentValue);
                        if (targetVertexFields != null) {
                            for (String f : targetVertexFields.fieldNames()) linkedV.setProperty(f, resolve(targetVertexFields.field(f)));
                        }
                        linkedV.save();
                        log(OETLProcessor.LOG_LEVELS.DEBUG, "created new vertex=%s", linkedV.getRecord());
                        result = linkedV.getIdentity();
                    } else {
                        throw new OConfigurationException("Cannot create linked document because target class is unknown. Use 'lookup' field");
                    }
                }
                break;
            case ERROR:
                processor.getStats().incrementErrors();
                log(OETLProcessor.LOG_LEVELS.ERROR, "%s: ERROR Cannot resolve join for value '%s'", getName(), joinCurrentValue);
                break;
            case WARNING:
                processor.getStats().incrementWarnings();
                log(OETLProcessor.LOG_LEVELS.INFO, "%s: WARN Cannot resolve join for value '%s'", getName(), joinCurrentValue);
                break;
            case SKIP:
                return null;
            case HALT:
                throw new OETLProcessHaltedException("Cannot resolve join for value '" + joinCurrentValue + "'");
            case NOTHING:
            default:
                return null;
        }
    }
    if (result != null) {
        final List<OrientEdge> edges;
        if (OMultiValue.isMultiValue(result)) {
            final int size = OMultiValue.getSize(result);
            if (size == 0)
                // NO EDGES
                return null;
            edges = new ArrayList<OrientEdge>(size);
        } else
            edges = new ArrayList<OrientEdge>(1);
        for (Object o : OMultiValue.getMultiValueIterable(result)) {
            OIdentifiable oid = (OIdentifiable) o;
            final OrientVertex targetVertex = pipeline.getGraphDatabase().getVertex(oid);
            try {
                // CREATE THE EDGE
                final OrientEdge edge;
                if (directionOut)
                    edge = (OrientEdge) vertex.addEdge(edgeClass, targetVertex);
                else
                    edge = (OrientEdge) targetVertex.addEdge(edgeClass, vertex);
                if (edgeFields != null) {
                    for (String f : edgeFields.fieldNames()) edge.setProperty(f, resolve(edgeFields.field(f)));
                }
                edges.add(edge);
                log(OETLProcessor.LOG_LEVELS.DEBUG, "created new edge=%s", edge);
            } catch (ORecordDuplicatedException e) {
                if (skipDuplicates) {
                    log(OETLProcessor.LOG_LEVELS.DEBUG, "skipped creation of new edge because already exists");
                    continue;
                } else {
                    log(OETLProcessor.LOG_LEVELS.ERROR, "error on creation of new edge because it already exists (skipDuplicates=false)");
                    throw e;
                }
            }
        }
        return edges;
    }
    // NO EDGES
    return null;
}
Also used : ArrayList(java.util.ArrayList) OrientVertex(com.tinkerpop.blueprints.impls.orient.OrientVertex) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) OrientEdge(com.tinkerpop.blueprints.impls.orient.OrientEdge) OETLProcessHaltedException(com.orientechnologies.orient.etl.OETLProcessHaltedException) OConfigurationException(com.orientechnologies.orient.core.exception.OConfigurationException) ORecordDuplicatedException(com.orientechnologies.orient.core.storage.ORecordDuplicatedException)

Aggregations

OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)2 OConfigurationException (com.orientechnologies.orient.core.exception.OConfigurationException)2 OETLProcessHaltedException (com.orientechnologies.orient.etl.OETLProcessHaltedException)2 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)1 ORecordDuplicatedException (com.orientechnologies.orient.core.storage.ORecordDuplicatedException)1 OrientEdge (com.tinkerpop.blueprints.impls.orient.OrientEdge)1 OrientVertex (com.tinkerpop.blueprints.impls.orient.OrientVertex)1 ArrayList (java.util.ArrayList)1