private void checkExecutionEngine() { if ( queryEngine == null ) { queryEngine = new ExecutionEngine( graph ); } }
public Neo4jSingleInsertion(GraphDatabaseService neo4jGraph, File resultsPath) { super(GraphDatabaseType.NEO4J, resultsPath); this.neo4jGraph = neo4jGraph; engine = new ExecutionEngine(this.neo4jGraph); }
@Produces public ExecutionEngine createQueryEngine(GraphDatabaseService service) { return new ExecutionEngine(service); }
public static String executeCypher(GraphDatabaseService db, String cypher, Map<String, Object> params) { org.neo4j.cypher.javacompat.ExecutionEngine engine; engine = new org.neo4j.cypher.javacompat.ExecutionEngine(db); List<Map<String, Object>> results = new ArrayList<>(); org.neo4j.cypher.javacompat.ExecutionResult result; try ( Transaction tx = db.beginTx() ) { result = engine.execute(cypher, params); for (Map<String,Object> row : result) { results.add(new LinkedHashMap<>(row)); } tx.success(); } return new Gson().toJson(results); } }
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); } }
private void exportAllDistinctTaxa(ExportUtil.Appender writer, GraphDatabaseService graphDatabase) throws IOException { ExecutionEngine engine = new ExecutionEngine(graphDatabase); ExecutionResult results = engine.execute("START taxon = node:taxons('*:*') " + "MATCH taxon<-[:CLASSIFIED_AS]-specimen " + "WHERE has(taxon.externalId) AND taxon.externalId <> '" + PropertyAndValueDictionary.NO_MATCH + "' " + "AND has(taxon.name) AND taxon.name <> '" + PropertyAndValueDictionary.NO_MATCH + "' " + "RETURN distinct(taxon)" + ", taxon.name as scientificName" + ", taxon.path? as path" + ", taxon.pathNames? as pathNames" + ", taxon.rank? as rank" + ", taxon.externalId as taxonId"); Map<String, String> row = new HashMap<String, String>(); for (Map<String, Object> result : results) { resultsToRow(row, result); writeProperties(writer, row); row.clear(); } }
public Neo4j2Graph(final GraphDatabaseService rawGraph) { this.rawGraph = rawGraph; transactionManager = ((GraphDatabaseAPI) rawGraph).getDependencyResolver().resolveDependency(TransactionManager.class); cypher = new ExecutionEngine(rawGraph); init(); }
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")); } }
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); } }
/** * * @param graph */ public Neo4jStore(GraphDatabaseService graph) { transactions = new HashMap<Thread, Transaction>(); this.graph = graph; this.checkTransaction(); try { if (!graph.schema().getConstraints().iterator().hasNext()) { graph.schema().indexFor(NodeType.TERM); graph.schema().indexFor(NodeType.PREDICATE); // TODO manage constraints /*graph.schema().constraintFor(NodeType.TERM) .assertPropertyIsUnique("value").create(); graph.schema().constraintFor(NodeType.PREDICATE) .assertPropertyIsUnique("value").create();*/ } cypherEngine = new ExecutionEngine(graph); this.successTransaction(); } finally { this.reloadTransaction(); } }
public Neo4j2Graph(final String directory, final Map<String, String> configuration) { try { GraphDatabaseBuilder builder = new GraphDatabaseFactory().newEmbeddedDatabaseBuilder(directory); if (null != configuration) this.rawGraph = builder.setConfig(configuration).newGraphDatabase(); else this.rawGraph = builder.newGraphDatabase(); transactionManager = ((GraphDatabaseAPI) rawGraph).getDependencyResolver().resolveDependency(TransactionManager.class); cypher = new ExecutionEngine(rawGraph); init(); } catch (Exception e) { if (this.rawGraph != null) this.rawGraph.shutdown(); throw new RuntimeException(e.getMessage(), e); } }
@Before public void setup() throws IOException { graphdb = new ImpermanentGraphDatabase(); graphdb.cleanContent(true); engine = new ExecutionEngine( graphdb ); }
@Before public void setup() throws IOException { graphdb = new ImpermanentGraphDatabase(); graphdb.cleanContent( false ); engine = new ExecutionEngine( graphdb ); }
@BeforeClass public static void setup() throws IOException { graphdb = (ImpermanentGraphDatabase)new TestGraphDatabaseFactory().newImpermanentDatabase(); graphdb.cleanContent(); engine = new ExecutionEngine( graphdb ); }
@Override public void link() { LinkProgress progress = new LinkProgress(LOG::info, 10); progress.start(); boolean done; ExecutionEngine engine = new ExecutionEngine(graphDb); do { ExecutionResult result = engine.execute("START dataset = node:datasets('*:*')\n" + "MATCH dataset<-[:IN_DATASET]-study-[:REFUTES|SUPPORTS]->specimen\n" + "WHERE not(specimen<-[:HAS_PARTICIPANT]-())\n" + "WITH specimen, study, dataset LIMIT {batchSize}\n" + "MATCH specimen-[i:" + InteractUtil.allInteractionsCypherClause() + "]->otherSpecimen\n" + "WHERE not(has(i.inverted))\n" + "CREATE specimen<-[:HAS_PARTICIPANT]-interaction-[:DERIVED_FROM]->study" + ", interaction-[:HAS_PARTICIPANT]->otherSpecimen " + ", interaction-[:ACCESSED_AT]->dataset\n" + "RETURN id(interaction)", MapUtil.map("batchSize", this.batchSize)); done = !result.iterator().hasNext(); result.iterator().close(); progress.progress(); } while (!done); }
@Test public void shouldBeAbleToEmitJavaIterables() throws Exception { makeFriends( michaelaNode, andreasNode ); makeFriends( michaelaNode, johanNode ); ExecutionEngine engine = new ExecutionEngine( db ); ExecutionResult result = engine.execute( "START n=node(0) MATCH n-->friend RETURN collect(friend)" ); Iterable<Node> friends = (Iterable<Node>) result.columnAs( "collect(friend)" ).next(); assertThat( friends, hasItems( andreasNode, johanNode ) ); assertThat( friends, instanceOf( Iterable.class ) ); }
@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 }
ExecutionEngine engine = new ExecutionEngine( db );
@SuppressWarnings("deprecation") @Before public void setUp() throws IOException { db = new TestGraphDatabaseFactory().newImpermanentDatabaseBuilder().newGraphDatabase(); engine = new ExecutionEngine( db ); Transaction tx = db.beginTx(); michaelaNode = db.createNode(); andreasNode = db.createNode(); johanNode = db.createNode(); andreasNode.setProperty( "name", "Andreas" ); johanNode.setProperty( "name", "Johan" ); michaelaNode.setProperty( "name", "Michaela" ); index( andreasNode ); index( johanNode ); index( michaelaNode ); tx.success(); tx.finish(); }
@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()); }