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;
});
}
}
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;
}
});
}
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;
}
}
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;
});
}
}
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;
});
}
}
Aggregations