cancel
Showing results for 
Search instead for 
Did you mean: 

Join the community at Nodes 2022, our free virtual event on November 16 - 17.

How to get driver result summary statement

Keith_gamble
Node Clone

I am new to the community so this is my first post, hopefully the answer is quite simple and I am just not great at reading java docs!

I am using the java driver to execute a series of write-only transactions and I would like to try and receive the update summary similar to what's provided in the browser.

I found that the result.consume() gives me a result summary but I am not sure if what I am looking for is something created from that, or something else.

I greatly appreciate any help!

What I have:

    public void updateQuery( final String cypher, Map<String,Object> params)
    {
        try ( Session session = driver.session() )
        {
            Transaction tx = session.beginTransaction();
            String[] commands = cypher.split(";");
            for (String command : commands)
            {
                Result result = tx.run(command, params);
                System.out.println(result.consume().toString());
            }
            tx.commit();
            return;
        }
        
    }

What I am looking for:

1 ACCEPTED SOLUTION

Hello and welcome!
You are looking for Result#consume and ResultSummary#counters.

View solution in original post

2 REPLIES 2

Hello and welcome!
You are looking for Result#consume and ResultSummary#counters.

Looks like that got me what I needed! Here is the script incase anyone wants it:

private String getResultSummaryString( ResultSummary summary) {

    SummaryCounters counters = summary.counters();
            
    List<String> resultList = new ArrayList<String>();

    long duration = summary.resultAvailableAfter(TimeUnit.MILLISECONDS);

    if ( counters.containsUpdates() ) {
        
        int nodesCreated = counters.nodesCreated();     
        if ( nodesCreated > 0 ) {
            resultList.add( "Created " + nodesCreated + " nodes" );
        }
        
        int nodesDeleted = counters.nodesDeleted();
        if ( nodesDeleted > 0 ) {
            resultList.add( "Deleted " + nodesDeleted + " nodes" );
        }

        int propertiesSet = counters.propertiesSet();

        if ( propertiesSet > 0 ) {
            resultList.add( "Set " + propertiesSet + " properties" );
        }

        int relationshipsCreated = counters.relationshipsCreated();
        if ( relationshipsCreated > 0 ) {
            resultList.add( "Created " + relationshipsCreated + " relationships" );
        }

        int relationshipsDeleted = counters.relationshipsDeleted();
        if ( relationshipsDeleted > 0 ) {
            resultList.add( "Deleted " + relationshipsDeleted + " relationships" );
        }
    } 

    resultList.add( "Completed in " + duration + "ms" );

    return String.join(", ", resultList);
}