static void writeResults(Appender appender, GraphDatabaseService dbService, List<String> queries, HashMap<String, Object> params, boolean includeHeader) throws IOException { for (String query : queries) { ExecutionResult rows = new ExecutionEngine(dbService).execute(query, params); List<String> columns = rows.columns(); if (includeHeader && queries.indexOf(query) == 0) { final String[] values = columns.toArray(new String[columns.size()]); appender.append(Stream.of(values)); } appendRow(appender, rows, columns); } }
protected void export(GraphDatabaseService graphService, OutputStreamFactory fileFactory) throws StudyImporterException { String query = "START taxon = node:taxons('*:*') " + "MATCH taxon-[?:SAME_AS*0..1]->linkedTaxon " + "WHERE has(linkedTaxon.externalId) AND linkedTaxon.externalId =~ 'NCBI:.*'" + "RETURN distinct(linkedTaxon.externalId) as id"; ExecutionResult rows = new ExecutionEngine(graphService).execute(query, new HashMap<String, Object>()); int rowCount = 0; OutputStream os = null; try { List<String> columns = rows.columns(); for (Map<String, Object> row : rows) { if (rowCount % getLinksPerResourceFile() == 0) { close(os); os = null; } for (String column : columns) { String taxonId = row.get(column).toString(); String ncbiTaxonId = StringUtils.replace(taxonId, TaxonomyProvider.ID_PREFIX_NCBI, ""); String aLink = String.format( " <ObjId>%s</ObjId>\n" , ncbiTaxonId); IOUtils.write(aLink, os == null ? (os = open(fileFactory, rowCount)) : os, StandardCharsets.UTF_8); } rowCount++; } close(os); } catch (IOException e) { throw new StudyImporterException("failed to export ncbi resources", e); } }
@Test public void exampleWithParameterForNodeObject() throws Exception { // START SNIPPET: exampleWithParameterForNodeObject Map<String, Object> params = new HashMap<String, Object>(); params.put( "node", andreasNode ); String query = "START n=node({node}) RETURN n.name"; ExecutionResult result = engine.execute( query, params ); // END SNIPPET: exampleWithParameterForNodeObject assertThat( result.columns(), hasItem( "n.name" ) ); Iterator<Object> n_column = result.columnAs( "n.name" ); assertEquals( "Andreas", n_column.next() ); }
@Test public void exampleWithParameterForNodeId() throws Exception { // START SNIPPET: exampleWithParameterForNodeId Map<String, Object> params = new HashMap<String, Object>(); params.put( "id", 0 ); String query = "START n=node({id}) RETURN n.name"; ExecutionResult result = engine.execute( query, params ); // END SNIPPET: exampleWithParameterForNodeId assertThat( result.columns(), hasItem( "n.name" ) ); Iterator<Object> n_column = result.columnAs( "n.name" ); assertEquals( "Michaela", n_column.next() ); dumpToFile( "exampleWithParameterForNodeId", query, params ); }
@Test public void exampleWithParameterForSkipAndLimit() throws Exception { // START SNIPPET: exampleWithParameterForSkipLimit Map<String, Object> params = new HashMap<String, Object>(); params.put( "s", 1 ); params.put( "l", 1 ); String query = "MATCH (n) RETURN n.name SKIP {s} LIMIT {l}"; ExecutionResult result = engine.execute( query, params ); // END SNIPPET: exampleWithParameterForSkipLimit assertThat( result.columns(), hasItem( "n.name" ) ); Iterator<Object> n_column = result.columnAs( "n.name" ); assertEquals( "Andreas", n_column.next() ); dumpToFile( "exampleWithParameterForSkipLimit", query, params ); }
@Test public void exampleWithParameterRegularExpression() throws Exception { // START SNIPPET: exampleWithParameterRegularExpression Map<String, Object> params = new HashMap<String, Object>(); params.put( "regex", ".*h.*" ); String query = "MATCH (n) WHERE n.name =~ {regex} RETURN n.name"; ExecutionResult result = engine.execute( query, params ); // END SNIPPET: exampleWithParameterRegularExpression dumpToFile( "exampleWithParameterRegularExpression", query, params ); assertThat( result.columns(), hasItem( "n.name" ) ); Iterator<Object> n_column = result.columnAs( "n.name" ); assertEquals( "Michaela", n_column.next() ); assertEquals( "Johan", n_column.next() ); }
@Test public void exampleQuery() throws Exception { // START SNIPPET: JavaQuery ExecutionEngine engine = new ExecutionEngine( db ); ExecutionResult result = engine.execute( "START n=node(0) WHERE 1=1 RETURN n" ); assertThat( result.columns(), hasItem( "n" ) ); Iterator<Node> n_column = result.columnAs( "n" ); assertThat( asIterable( n_column ), hasItem( db.getNodeById( 0 ) ) ); // END SNIPPET: JavaQuery }
List<String> columns = result.columns();