Search in sources :

Example 1 with TransactionWork

use of org.neo4j.driver.TransactionWork in project hop by apache.

the class PipelineLoggingExtensionPoint method logPipelineMetadata.

private void logPipelineMetadata(final ILogChannel log, final Session session, final NeoConnection connection, final IPipelineEngine<PipelineMeta> pipeline) throws HopException {
    log.logDetailed("Logging pipeline metadata to Neo4j connection : " + connection.getName());
    final PipelineMeta pipelineMeta = pipeline.getPipelineMeta();
    synchronized (session) {
        session.writeTransaction((TransactionWork<Void>) transaction -> {
            try {
                Map<String, Object> transPars = new HashMap<>();
                transPars.put("pipelineName", pipelineMeta.getName());
                transPars.put("description", pipelineMeta.getDescription());
                transPars.put("filename", pipelineMeta.getFilename());
                StringBuilder transCypher = new StringBuilder();
                transCypher.append("MERGE (pipeline:Pipeline { name : $pipelineName } ) ");
                transCypher.append("SET pipeline.filename = $filename, pipeline.description = $description ");
                transaction.run(transCypher.toString(), transPars);
                log.logDetailed("Pipeline cypher : " + transCypher);
                for (TransformMeta transformMeta : pipelineMeta.getTransforms()) {
                    Map<String, Object> transformPars = new HashMap<>();
                    transformPars.put("pipelineName", pipelineMeta.getName());
                    transformPars.put("transformName", transformMeta.getName());
                    transformPars.put("description", transformMeta.getDescription());
                    transformPars.put("pluginId", transformMeta.getPluginId());
                    transformPars.put("copies", transformMeta.getCopies(pipeline));
                    transformPars.put("locationX", transformMeta.getLocation().x);
                    transformPars.put("locationY", transformMeta.getLocation().y);
                    StringBuilder transformCypher = new StringBuilder();
                    transformCypher.append("MATCH (pipeline:Pipeline { name : $pipelineName } ) ");
                    transformCypher.append("MERGE (transform:Transform { pipelineName : $pipelineName, name : $transformName } ) ");
                    transformCypher.append("SET ");
                    transformCypher.append("  transform.description = $description ");
                    transformCypher.append(", transform.pluginId = $pluginId ");
                    transformCypher.append(", transform.copies = $copies ");
                    transformCypher.append(", transform.locationX = $locationX ");
                    transformCypher.append(", transform.locationY = $locationY ");
                    transformCypher.append("MERGE (transform)-[rel:TRANSFORM_OF_PIPELINE]->(pipeline) ");
                    log.logDetailed("Transform '" + transformMeta.getName() + "' cypher : " + transformCypher);
                    transaction.run(transformCypher.toString(), transformPars);
                }
                for (int i = 0; i < pipelineMeta.nrPipelineHops(); i++) {
                    PipelineHopMeta hopMeta = pipelineMeta.getPipelineHop(i);
                    Map<String, Object> hopPars = new HashMap<>();
                    hopPars.put("fromTransform", hopMeta.getFromTransform().getName());
                    hopPars.put("toTransform", hopMeta.getToTransform().getName());
                    hopPars.put("pipelineName", pipelineMeta.getName());
                    StringBuilder hopCypher = new StringBuilder();
                    hopCypher.append("MATCH (from:Transform { pipelineName : $pipelineName, name : $fromTransform }) ");
                    hopCypher.append("MATCH (to:Transform { pipelineName : $pipelineName, name : $toTransform }) ");
                    hopCypher.append("MERGE (from)-[rel:PRECEDES]->(to) ");
                    transaction.run(hopCypher.toString(), hopPars);
                }
                transaction.commit();
            } catch (Exception e) {
                transaction.rollback();
                log.logError("Error logging pipeline metadata", e);
            }
            return null;
        });
    }
}
Also used : PipelineHopMeta(org.apache.hop.pipeline.PipelineHopMeta) Session(org.neo4j.driver.Session) org.apache.hop.pipeline.transform(org.apache.hop.pipeline.transform) Driver(org.neo4j.driver.Driver) java.util(java.util) ILogChannel(org.apache.hop.core.logging.ILogChannel) HopLogStore(org.apache.hop.core.logging.HopLogStore) ExtensionPoint(org.apache.hop.core.extension.ExtensionPoint) IVariables(org.apache.hop.core.variables.IVariables) Defaults(org.apache.hop.neo4j.logging.Defaults) HopException(org.apache.hop.core.exception.HopException) SimpleDateFormat(java.text.SimpleDateFormat) LoggingObjectType(org.apache.hop.core.logging.LoggingObjectType) TransactionWork(org.neo4j.driver.TransactionWork) IPipelineEngine(org.apache.hop.pipeline.engine.IPipelineEngine) Transaction(org.neo4j.driver.Transaction) LoggingHierarchy(org.apache.hop.core.logging.LoggingHierarchy) IExtensionPoint(org.apache.hop.core.extension.IExtensionPoint) NeoConnection(org.apache.hop.neo4j.shared.NeoConnection) Pipeline(org.apache.hop.pipeline.Pipeline) Result(org.apache.hop.core.Result) LoggingCore(org.apache.hop.neo4j.logging.util.LoggingCore) PipelineMeta(org.apache.hop.pipeline.PipelineMeta) PipelineHopMeta(org.apache.hop.pipeline.PipelineHopMeta) HopException(org.apache.hop.core.exception.HopException) PipelineMeta(org.apache.hop.pipeline.PipelineMeta)

Example 2 with TransactionWork

use of org.neo4j.driver.TransactionWork in project hop by apache.

the class ModelValidator method readConstraintsData.

private void readConstraintsData(Session session) {
    constraintsList = session.readTransaction(new TransactionWork<List<ConstraintDetails>>() {

        @Override
        public List<ConstraintDetails> execute(Transaction transaction) {
            List<ConstraintDetails> list = new ArrayList<>();
            Result result = transaction.run("call db.constraints()");
            while (result.hasNext()) {
                list.add(new ConstraintDetails(result.next()));
            }
            return list;
        }
    });
}
Also used : TransactionWork(org.neo4j.driver.TransactionWork) Transaction(org.neo4j.driver.Transaction) ArrayList(java.util.ArrayList) Result(org.neo4j.driver.Result)

Example 3 with TransactionWork

use of org.neo4j.driver.TransactionWork in project neo4j-java-driver by neo4j.

the class ResultRetainExample method addEmployees.

// tag::result-retain[]
public int addEmployees(final String companyName) {
    try (Session session = driver.session()) {
        int employees = 0;
        List<Record> persons = session.readTransaction(new TransactionWork<List<Record>>() {

            @Override
            public List<Record> execute(Transaction tx) {
                return matchPersonNodes(tx);
            }
        });
        for (final Record person : persons) {
            employees += session.writeTransaction(tx -> {
                Result result = tx.run("MATCH (emp:Person {name: $person_name}) " + "MERGE (com:Company {name: $company_name}) " + "MERGE (emp)-[:WORKS_FOR]->(com)", parameters("person_name", person.get("name").asString(), "company_name", companyName));
                result.consume();
                return 1;
            });
        }
        return employees;
    }
}
Also used : Session(org.neo4j.driver.Session) List(java.util.List) Values.parameters(org.neo4j.driver.Values.parameters) TransactionWork(org.neo4j.driver.TransactionWork) Record(org.neo4j.driver.Record) Result(org.neo4j.driver.Result) Transaction(org.neo4j.driver.Transaction) Transaction(org.neo4j.driver.Transaction) Record(org.neo4j.driver.Record) List(java.util.List) Session(org.neo4j.driver.Session) Result(org.neo4j.driver.Result)

Example 4 with TransactionWork

use of org.neo4j.driver.TransactionWork in project hop by apache.

the class WorkflowLoggingExtensionPoint method logEndOfWorkflow.

private void logEndOfWorkflow(final ILogChannel log, final Session session, final NeoConnection connection, final IWorkflowEngine<WorkflowMeta> workflow) throws HopException {
    log.logDetailed("Logging execution end of workflow to Neo4j connection : " + connection.getName());
    final WorkflowMeta workflowMeta = workflow.getWorkflowMeta();
    synchronized (session) {
        session.writeTransaction((TransactionWork<Void>) transaction -> {
            try {
                ILogChannel channel = workflow.getLogChannel();
                Result workflowResult = workflow.getResult();
                String workflowLogChannelId = workflow.getLogChannelId();
                String workflowLoggingText = HopLogStore.getAppender().getBuffer(workflowLogChannelId, true).toString();
                Date endDate = new Date();
                workflow.getExtensionDataMap().put(WORKFLOW_END_DATE, new Date());
                Date startDate = (Date) workflow.getExtensionDataMap().get(WORKFLOW_START_DATE);
                Map<String, Object> workflowPars = new HashMap<>();
                workflowPars.put("workflowName", workflowMeta.getName());
                workflowPars.put("type", EXECUTION_TYPE_WORKFLOW);
                workflowPars.put("id", channel.getLogChannelId());
                workflowPars.put("executionEnd", new SimpleDateFormat("yyyy/MM/dd'T'HH:mm:ss").format(endDate));
                workflowPars.put("durationMs", endDate.getTime() - startDate.getTime());
                workflowPars.put("errors", workflowResult.getNrErrors());
                workflowPars.put("linesInput", workflowResult.getNrLinesInput());
                workflowPars.put("linesOutput", workflowResult.getNrLinesOutput());
                workflowPars.put("linesRead", workflowResult.getNrLinesRead());
                workflowPars.put("linesWritten", workflowResult.getNrLinesWritten());
                workflowPars.put("linesRejected", workflowResult.getNrLinesRejected());
                workflowPars.put("loggingText", workflowLoggingText);
                workflowPars.put("result", workflowResult.getResult());
                workflowPars.put("nrResultRows", workflowResult.getRows().size());
                workflowPars.put("nrResultFiles", workflowResult.getResultFilesList().size());
                StringBuilder execCypher = new StringBuilder();
                execCypher.append("MERGE (e:Execution { name : $workflowName, type : $type, id : $id } ) ");
                execCypher.append("SET ");
                execCypher.append("  e.executionEnd = $executionEnd ");
                execCypher.append(", e.durationMs = $durationMs ");
                execCypher.append(", e.errors = $errors ");
                execCypher.append(", e.linesInput = $linesInput ");
                execCypher.append(", e.linesOutput = $linesOutput ");
                execCypher.append(", e.linesRead = $linesRead ");
                execCypher.append(", e.linesWritten = $linesWritten ");
                execCypher.append(", e.linesRejected = $linesRejected ");
                execCypher.append(", e.loggingText = $loggingText ");
                execCypher.append(", e.result = $result ");
                execCypher.append(", e.nrResultRows = $nrResultRows ");
                execCypher.append(", e.nrResultFiles = $nrResultFiles ");
                transaction.run(execCypher.toString(), workflowPars);
                StringBuilder relCypher = new StringBuilder();
                relCypher.append("MATCH (w:Workflow { name : $workflowName } ) ");
                relCypher.append("MATCH (e:Execution { name : $workflowName, type : $type, id : $id } ) ");
                relCypher.append("MERGE (e)-[r:EXECUTION_OF_WORKFLOW]->(w) ");
                transaction.run(relCypher.toString(), workflowPars);
                List<ActionResult> actionResults = workflow.getActionResults();
                for (ActionResult actionResult : actionResults) {
                    String actionLogChannelId = actionResult.getLogChannelId();
                    String transformLoggingText = HopLogStore.getAppender().getBuffer(actionLogChannelId, true).toString();
                    Result result = actionResult.getResult();
                    Map<String, Object> actionPars = new HashMap<>();
                    actionPars.put("workflowName", workflowMeta.getName());
                    actionPars.put("name", actionResult.getActionName());
                    actionPars.put("type", EXECUTION_TYPE_ACTION);
                    actionPars.put("id", actionLogChannelId);
                    actionPars.put("workflowId", workflowLogChannelId);
                    actionPars.put("comment", actionResult.getComment());
                    actionPars.put("reason", actionResult.getReason());
                    actionPars.put("loggingText", transformLoggingText);
                    actionPars.put("errors", result.getNrErrors());
                    actionPars.put("linesRead", result.getNrLinesRead());
                    actionPars.put("linesWritten", result.getNrLinesWritten());
                    actionPars.put("linesInput", result.getNrLinesInput());
                    actionPars.put("linesOutput", result.getNrLinesOutput());
                    actionPars.put("linesRejected", result.getNrLinesRejected());
                    StringBuilder actionExecCypher = new StringBuilder();
                    actionExecCypher.append("MERGE (e:Execution { name : $name, type : $type, id : $id } ) ");
                    actionExecCypher.append("SET ");
                    actionExecCypher.append("  e.workflowId = $workflowId ");
                    actionExecCypher.append(", e.loggingText = $loggingText ");
                    actionExecCypher.append(", e.comment = $comment ");
                    actionExecCypher.append(", e.reason = $reason ");
                    actionExecCypher.append(", e.linesRead = $linesRead ");
                    actionExecCypher.append(", e.linesWritten = $linesWritten ");
                    actionExecCypher.append(", e.linesInput = $linesInput ");
                    actionExecCypher.append(", e.linesOutput = $linesOutput ");
                    actionExecCypher.append(", e.linesRejected = $linesRejected ");
                    transaction.run(actionExecCypher.toString(), actionPars);
                    StringBuilder actionRelCypher = new StringBuilder();
                    actionRelCypher.append("MATCH (a:Action { workflowName : $workflowName, name : $name } ) ");
                    actionRelCypher.append("MATCH (e:Execution { name : $name, type : $type, id : $id } ) ");
                    actionRelCypher.append("MERGE (e)-[r:EXECUTION_OF_ACTION]->(a) ");
                    transaction.run(actionRelCypher.toString(), actionPars);
                }
                transaction.commit();
            } catch (Exception e) {
                log.logError("Error logging workflow end", e);
                transaction.rollback();
            }
            return null;
        });
    }
}
Also used : Session(org.neo4j.driver.Session) ActionMeta(org.apache.hop.workflow.action.ActionMeta) ExtensionPoint(org.apache.hop.core.extension.ExtensionPoint) IVariables(org.apache.hop.core.variables.IVariables) Date(java.util.Date) Defaults(org.apache.hop.neo4j.logging.Defaults) HopException(org.apache.hop.core.exception.HopException) WorkflowMeta(org.apache.hop.workflow.WorkflowMeta) SimpleDateFormat(java.text.SimpleDateFormat) HashMap(java.util.HashMap) ActionResult(org.apache.hop.workflow.ActionResult) TransactionWork(org.neo4j.driver.TransactionWork) Transaction(org.neo4j.driver.Transaction) NeoConnection(org.apache.hop.neo4j.shared.NeoConnection) Map(java.util.Map) LoggingCore(org.apache.hop.neo4j.logging.util.LoggingCore) IWorkflowEngine(org.apache.hop.workflow.engine.IWorkflowEngine) WorkflowHopMeta(org.apache.hop.workflow.WorkflowHopMeta) Driver(org.neo4j.driver.Driver) ILogChannel(org.apache.hop.core.logging.ILogChannel) HopLogStore(org.apache.hop.core.logging.HopLogStore) LoggingObjectType(org.apache.hop.core.logging.LoggingObjectType) LoggingHierarchy(org.apache.hop.core.logging.LoggingHierarchy) IExtensionPoint(org.apache.hop.core.extension.IExtensionPoint) List(java.util.List) Result(org.apache.hop.core.Result) IExecutionFinishedListener(org.apache.hop.pipeline.IExecutionFinishedListener) ILogChannel(org.apache.hop.core.logging.ILogChannel) Date(java.util.Date) HopException(org.apache.hop.core.exception.HopException) WorkflowMeta(org.apache.hop.workflow.WorkflowMeta) ActionResult(org.apache.hop.workflow.ActionResult) Result(org.apache.hop.core.Result) ActionResult(org.apache.hop.workflow.ActionResult) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) SimpleDateFormat(java.text.SimpleDateFormat)

Example 5 with TransactionWork

use of org.neo4j.driver.TransactionWork in project hop by apache.

the class WorkflowLoggingExtensionPoint method logWorkflowMetadata.

private void logWorkflowMetadata(final ILogChannel log, final Session session, final NeoConnection connection, final IWorkflowEngine<WorkflowMeta> workflow) throws HopException {
    log.logDetailed("Logging workflow metadata to Neo4j server : " + connection.getName());
    final WorkflowMeta workflowMeta = workflow.getWorkflowMeta();
    synchronized (session) {
        session.writeTransaction((TransactionWork<Void>) transaction -> {
            try {
                Map<String, Object> workflowPars = new HashMap<>();
                workflowPars.put("workflowName", workflowMeta.getName());
                workflowPars.put("description", workflowMeta.getDescription());
                workflowPars.put("filename", workflowMeta.getFilename());
                StringBuilder workflowCypher = new StringBuilder();
                workflowCypher.append("MERGE (w:Workflow { name : $workflowName} ) ");
                workflowCypher.append("SET w.filename = $filename, w.description = $description ");
                log.logDetailed("Workflow metadata cypher : " + workflowCypher);
                transaction.run(workflowCypher.toString(), workflowPars);
                for (ActionMeta actionMeta : workflowMeta.getActions()) {
                    Map<String, Object> actionPars = new HashMap<>();
                    actionPars.put("workflowName", workflowMeta.getName());
                    actionPars.put("name", actionMeta.getName());
                    actionPars.put("description", actionMeta.getDescription());
                    actionPars.put("pluginId", actionMeta.getAction().getPluginId());
                    actionPars.put("evaluation", actionMeta.isEvaluation());
                    actionPars.put("launchingParallel", actionMeta.isLaunchingInParallel());
                    actionPars.put("start", actionMeta.isStart());
                    actionPars.put("unconditional", actionMeta.isUnconditional());
                    actionPars.put("locationX", actionMeta.getLocation().x);
                    actionPars.put("locationY", actionMeta.getLocation().y);
                    StringBuilder actionCypher = new StringBuilder();
                    actionCypher.append("MATCH (w:Workflow { name : $workflowName } ) ");
                    actionCypher.append("MERGE (a:Action { workflowName : $workflowName, name : $name }) ");
                    actionCypher.append("MERGE (a)-[rel:ACTION_OF_WORKFLOW]->(w) ");
                    actionCypher.append("SET ");
                    actionCypher.append("  a.description = $description ");
                    actionCypher.append(", a.pluginId = $pluginId ");
                    actionCypher.append(", a.evaluation = $evaluation ");
                    actionCypher.append(", a.launchingParallel = $launchingParallel ");
                    actionCypher.append(", a.start = $start ");
                    actionCypher.append(", a.unconditional = $unconditional ");
                    actionCypher.append(", a.locationX = $locationX ");
                    actionCypher.append(", a.locationY = $locationY ");
                    log.logDetailed("Action copy '" + actionMeta.getName() + "' cypher : " + actionCypher);
                    transaction.run(actionCypher.toString(), actionPars);
                }
                for (int i = 0; i < workflowMeta.nrWorkflowHops(); i++) {
                    WorkflowHopMeta hopMeta = workflowMeta.getWorkflowHop(i);
                    Map<String, Object> hopPars = new HashMap<>();
                    hopPars.put("fromAction", hopMeta.getFromAction().getName());
                    hopPars.put("toAction", hopMeta.getToAction().getName());
                    hopPars.put("workflowName", workflowMeta.getName());
                    StringBuilder hopCypher = new StringBuilder();
                    hopCypher.append("MATCH (f:Action { workflowName : $workflowName, name : $fromAction}) ");
                    hopCypher.append("MATCH (t:Action { workflowName : $workflowName, name : $toAction}) ");
                    hopCypher.append("MERGE (f)-[rel:PRECEDES]->(t) ");
                    transaction.run(hopCypher.toString(), hopPars);
                }
                transaction.commit();
            } catch (Exception e) {
                transaction.rollback();
                log.logError("Error logging workflow metadata", e);
            }
            return null;
        });
    }
}
Also used : Session(org.neo4j.driver.Session) ActionMeta(org.apache.hop.workflow.action.ActionMeta) ExtensionPoint(org.apache.hop.core.extension.ExtensionPoint) IVariables(org.apache.hop.core.variables.IVariables) Date(java.util.Date) Defaults(org.apache.hop.neo4j.logging.Defaults) HopException(org.apache.hop.core.exception.HopException) WorkflowMeta(org.apache.hop.workflow.WorkflowMeta) SimpleDateFormat(java.text.SimpleDateFormat) HashMap(java.util.HashMap) ActionResult(org.apache.hop.workflow.ActionResult) TransactionWork(org.neo4j.driver.TransactionWork) Transaction(org.neo4j.driver.Transaction) NeoConnection(org.apache.hop.neo4j.shared.NeoConnection) Map(java.util.Map) LoggingCore(org.apache.hop.neo4j.logging.util.LoggingCore) IWorkflowEngine(org.apache.hop.workflow.engine.IWorkflowEngine) WorkflowHopMeta(org.apache.hop.workflow.WorkflowHopMeta) Driver(org.neo4j.driver.Driver) ILogChannel(org.apache.hop.core.logging.ILogChannel) HopLogStore(org.apache.hop.core.logging.HopLogStore) LoggingObjectType(org.apache.hop.core.logging.LoggingObjectType) LoggingHierarchy(org.apache.hop.core.logging.LoggingHierarchy) IExtensionPoint(org.apache.hop.core.extension.IExtensionPoint) List(java.util.List) Result(org.apache.hop.core.Result) IExecutionFinishedListener(org.apache.hop.pipeline.IExecutionFinishedListener) ActionMeta(org.apache.hop.workflow.action.ActionMeta) HashMap(java.util.HashMap) Map(java.util.Map) HopException(org.apache.hop.core.exception.HopException) WorkflowMeta(org.apache.hop.workflow.WorkflowMeta) WorkflowHopMeta(org.apache.hop.workflow.WorkflowHopMeta)

Aggregations

TransactionWork (org.neo4j.driver.TransactionWork)7 HopException (org.apache.hop.core.exception.HopException)5 NeoConnection (org.apache.hop.neo4j.shared.NeoConnection)5 Session (org.neo4j.driver.Session)5 Transaction (org.neo4j.driver.Transaction)5 Result (org.apache.hop.core.Result)4 IVariables (org.apache.hop.core.variables.IVariables)4 Driver (org.neo4j.driver.Driver)4 SimpleDateFormat (java.text.SimpleDateFormat)3 List (java.util.List)3 ExtensionPoint (org.apache.hop.core.extension.ExtensionPoint)3 IExtensionPoint (org.apache.hop.core.extension.IExtensionPoint)3 HopLogStore (org.apache.hop.core.logging.HopLogStore)3 ILogChannel (org.apache.hop.core.logging.ILogChannel)3 LoggingHierarchy (org.apache.hop.core.logging.LoggingHierarchy)3 LoggingObjectType (org.apache.hop.core.logging.LoggingObjectType)3 Defaults (org.apache.hop.neo4j.logging.Defaults)3 LoggingCore (org.apache.hop.neo4j.logging.util.LoggingCore)3 java.util (java.util)2 Date (java.util.Date)2