private boolean contains(Atom atom, Transaction transaction) { String query = containsAtomIntoCypherQuery(atom, true); if (LOGGER.isDebugEnabled()) { LOGGER.debug(query); } ResourceIterator<Node> result = this.cypherEngine.execute(query).columnAs("atom"); boolean res = result.hasNext(); result.close(); return res; }
public Iterator<Map<String,Object>> query(String query, Map<String,Object> params) { return cypher.execute(query,params==null ? Collections.<String,Object>emptyMap() : params).iterator(); }
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); } }
@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() ); }
Iterator<Node> n_column = result.columnAs( "n" ); for ( Node node : IteratorUtil.asIterable( n_column ) ) List<String> columns = result.columns(); resultString = engine.execute( "match (n {name: 'my node'}) return n, n.name" ).dumpToString(); columnsString = columns.toString(); db.shutdown();
@Test public void testColumnAreInTheRightOrder() throws Exception { createTenNodes(); String q = "start one=node(1), two=node(2), three=node(3), four=node(4), five=node(5), six=node(6), " + "seven=node(7), eight=node(8), nine=node(9), ten=node(10) " + "return one, two, three, four, five, six, seven, eight, nine, ten"; ExecutionResult result = engine.execute( q ); assertThat( result.dumpToString(), matchesPattern( "one.*two.*three.*four.*five.*six.*seven.*eight.*nine.*ten" ) ); }
@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 testAddToIndex() throws Exception { Map<String, String> config = TimelineIndexProvider.CONFIG; IndexManager indexMan = db.index(); Index<Node> index = indexMan.forNodes("timeline1", config); assertNotNull(index); Transaction tx = db.beginTx(); Node n1 = db.createNode(); n1.setProperty("time", 123); index.add(n1, "timestamp", 123L); Node n2 = db.createNode(); n2.setProperty("time", 123); index.add(n2, "timestamp", 123L); Node n3 = db.createNode(); n3.setProperty("time", 124); index.add(n3, "timestamp", 124L); tx.success(); tx.finish(); GraphvizWriter writer = new GraphvizWriter(); writer.emit(System.out, Walker.fullGraph(db)); IndexHits<Node> hits = index.get("timestamp", 123L); assertEquals(2, hits.size()); hits = index.query("[122 TO 125]"); assertEquals(3, hits.size()); ExecutionEngine engine = new ExecutionEngine(db); ExecutionResult result = engine.execute("start n=node:timeline1('[100 TO 200]') return n"); System.out.println(result.toString()); }
fw.append( "\nResulting in:\n\n" ); fw.append( AsciiDocGenerator.dumpToSeparateFileWithType( new File( DOCS_TARGET ), "intro.result", createQueryResultSnippet( engine.execute( query ).dumpToString() ) ) ); fw.append( "\nResulting in:\n\n" ); fw.append( AsciiDocGenerator.dumpToSeparateFileWithType( new File( DOCS_TARGET ), "intro.result", createQueryResultSnippet( engine.execute( query ).dumpToString() ) ) ); fw.close();
@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 ); }
@Override public CloseableIterator<Term> termsByPredicatePosition(Predicate p, int position) throws AtomSetException { StringBuilder sb = new StringBuilder(); sb.append("MATCH "); predicateToCypher(sb, p); sb.append("<-[rel_predicate:PREDICATE]-(atom)-[:TERM { index: ").append(position).append(" }]->(term) "); sb.append("RETURN DISTINCT term"); String query = sb.toString(); if (LOGGER.isDebugEnabled()) { LOGGER.debug(query); } ResourceIterator<Map<String, Object>> result = this.cypherEngine.execute(query).iterator(); return new Neo4jTermIterator(this.getTransaction(), result); }
private <T> List<T> toList( ExecutionResult result, String column ) { List<T> results = new ArrayList<T>(); IteratorUtil.addToCollection( result.<T>columnAs( column ), results ); return results; }
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 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() ); }
@Override public CloseableIterator<Atom> atomsByPredicate(Predicate p) throws AtomSetException { StringBuilder sb = new StringBuilder(); sb.append("MATCH "); predicateToCypher(sb, p); sb.append("<-[rel_predicate:PREDICATE]-(atom)"); sb.append("RETURN atom"); String query = sb.toString(); if (LOGGER.isDebugEnabled()) { LOGGER.debug(query); } ResourceIterator<Map<String, Object>> result = this.cypherEngine.execute(query).iterator(); return new Neo4jAtomIterator(this.getTransaction(), result); }
public static Node getReferenceNode(GraphDatabaseService db, String name) { if (engine == null || db != dbRef) { engine = new ExecutionEngine(db); ReferenceNodes.dbRef = db; } ExecutionResult result = engine.execute("MERGE (ref:ReferenceNode {name:{name}}) RETURN ref", map("name", name)); return IteratorUtil.single(result.<Node>columnAs("ref")); } }
@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 }
@Override public CloseableIterator<Atom> match(Atom atom) { String query = matchAtomIntoCypherQuery(atom); if (LOGGER.isDebugEnabled()) { LOGGER.debug(query); } ResourceIterator<Map<String, Object>> result = this.cypherEngine.execute(query).iterator(); return new Neo4jAtomIterator(this.getTransaction(), result); }
public Node getOrCreate(String nodeId) { Node result = null; try(final Transaction tx = ((GraphDatabaseAPI) neo4jGraph).tx().unforced().begin()) { try { String queryString = "MERGE (n:Node {nodeId: {nodeId}}) RETURN n"; Map<String, Object> parameters = new HashMap<String, Object>(); parameters.put("nodeId", nodeId); ResourceIterator<Node> resultIterator = engine.execute(queryString, parameters).columnAs("n"); result = resultIterator.next(); tx.success(); } catch (Exception e) { tx.failure(); throw new BenchmarkingException("unable to get or create node " + nodeId, e); } } return result; }