/* * Helper method to convert an execution result to a collection * */ public Set<Node> nodeSetFromResult(ExecutionResult result, String column) { Set<Node> nodes = new HashSet<Node>(); Iterator<Node> columnIterator = result.columnAs(column); IteratorUtil.addToCollection(columnIterator, nodes); return nodes; }
@Override protected List<Usuario> montaLista(ExecutionResult result) { Iterator<Node> n_column = result.columnAs("n"); Usuario usuario = null; List<Usuario> usuarios = new ArrayList<>(); for (Node node : IteratorUtil.asIterable(n_column)) { usuario = new Usuario(); usuario.setNome((String) node.getProperty("nome")); usuario.setLogin((String) node.getProperty("login")); usuarios.add(usuario); } return usuarios; }
public static void main(String[] args) { GraphDatabaseService db = criaDatabase(); // 4 é a Maria String query = "start sdfjhsdkjfhsdk=sfgs n=node(4) match n-[a]->(cidade)<-[r]-(pessoas) where type(r) = \"VIAJOU_PARA\" or type(r) = \"MOROU_EM\" return distinct pessoas"; ExecutionEngine engine = new ExecutionEngine(db); ExecutionResult result = engine.execute(query); Iterator<Node> cidades = result.columnAs("pessoas"); while (cidades.hasNext()) { Node cidade = cidades.next(); System.out.println(cidade.getProperty("nome")); } }
@Test public void find_john_has_seen() { ExecutionEngine engine = new ExecutionEngine(graphDb); String cql = "start user=node:users(name = \"John Johnson\")" + "match (user)-[:HAS_SEEN]->(movie)" + "return movie;"; ExecutionResult result = engine.execute(cql); logger.info("Execution result:" + result.toString()); for (Map<String, Object> row : result) { logger.info("Row:" + row); } List<String> columns = result.columns(); for (String column : columns) { logger.info("Column:" + column); Iterator<Object> columnValues = result.columnAs(column); while (columnValues.hasNext()) { logger.info("Value:" + columnValues.next()); } } }
public static void main(String[] args) throws Exception { System.out.println("Starting Graph Database..."); graphDb = new GraphDatabaseFactory() .newEmbeddedDatabaseBuilder("/home/prhodes/workspace/granny/neo4j-db") .setConfig(GraphDatabaseSettings.node_keys_indexable, "name, uuid") .setConfig(GraphDatabaseSettings.relationship_keys_indexable, "name") .setConfig(GraphDatabaseSettings.node_auto_indexing, "true") .setConfig(GraphDatabaseSettings.relationship_auto_indexing, "true") .newGraphDatabase(); registerShutdownHook(graphDb); /* <option value="d70ee717-29ac-4326-9704-1bae1dfade62" name="Sally Sparrow"> … </option> <option value="235c5608-b240-438e-975e-6124e9e047f0" name="Jack Harkness"> … </option> <option value="79202f6e-eb8d-466a-a523-87757e2ad510" name="Rose Tyler"> … </option> */ ExecutionEngine engine = new ExecutionEngine(graphDb); // p, length(p), last(p) order by length(p) asc // String findFriendsQuery = "start n=node(*) MATCH p = (owner)-[:FRIEND*1..2]-(friend) where ( // (n = owner and friend = {userNode} ) OR (n = friend and owner = {userNode})) return distinct // p, length(p) order by length(p)"; String findFriendsQuery = "start n=node(*), person=node({userNode}) MATCH p = (person)-[:FRIEND*1..2]-(friend) return distinct p order by length(p)"; ReadableIndex<Node> autoNodeIndex = graphDb.index().getNodeAutoIndexer().getAutoIndex(); Map<String, Object> uuids = new HashMap<String, Object>(); uuids.put( "uuid", // "235c5608-b240-438e-975e-6124e9e047f0" // Jack // "79202f6e-eb8d-466a-a523-87757e2ad510" // Rose "d70ee717-29ac-4326-9704-1bae1dfade62" // Sally ); Node userNode = autoNodeIndex.get("uuid", uuids.get("uuid")).getSingle(); Map<String, Object> params = new HashMap<String, Object>(); params.put("userNode", userNode); ExecutionResult result = engine.execute(findFriendsQuery, params); Iterator<Path> friendNodes = result.columnAs("p"); while (friendNodes.hasNext()) { Path path = friendNodes.next(); System.out.println( "processing result in candidateFriendNodes: " + path.startNode().getProperty("name") + " -- " + path.endNode().getProperty("name") + " (length: " + path.length() + " )"); } }
public static void main(String[] args) throws Exception { System.out.println("Starting Graph Database..."); graphDb = new GraphDatabaseFactory() .newEmbeddedDatabaseBuilder("/home/prhodes/workspace/granny/neo4j-db") .setConfig(GraphDatabaseSettings.node_keys_indexable, "name, uuid") .setConfig(GraphDatabaseSettings.relationship_keys_indexable, "name") .setConfig(GraphDatabaseSettings.node_auto_indexing, "true") .setConfig(GraphDatabaseSettings.relationship_auto_indexing, "true") .newGraphDatabase(); registerShutdownHook(graphDb); /* <option value="d70ee717-29ac-4326-9704-1bae1dfade62" name="Sally Sparrow"> … </option> <option value="235c5608-b240-438e-975e-6124e9e047f0" name="Jack Harkness"> … </option> <option value="79202f6e-eb8d-466a-a523-87757e2ad510" name="Rose Tyler"> … </option> */ ExecutionEngine engine = new ExecutionEngine(graphDb); // find Address nodes that aren't me, and aren't already my friend // String findCandidateFriendsQuery = "start n=node(*) MATCH n:Address-[r?:FRIEND]->friend where // n.name <> {name} and ( r is null or friend.name <> {name}) return n"; // String findCandidateFriendsQuery = "start n=node(*) match n, (owner)-[r?:FRIEND]-(friend) // where ( NOT (id(n) IN [0])) AND n.uuid! <> {uuid} and ( NOT ( id( owner ) IN [0] ) ) and ( // NOT ( id( friend ) IN [0] ) ) " // + " and ( ( (owner.uuid <> {uuid} and friend = n) and (friend.uuid <> {uuid} and // owner = n) ) or (r is null ) ) return distinct n"; // String findCandidateFriendsQuery = "start n=node(*) match n, (owner)-[r?:FRIEND]-(friend) // where ( NOT (id(n) IN [0])) AND n.uuid! <> {uuid} and ( NOT ( id( owner ) IN [0] ) ) and ( // NOT ( id( friend ) IN [0] ) ) " // + " and (r is null AND n = owner and friend.uuid = {uuid} ) and (r is null AND n = // friend and owner.uuid = {uuid} ) or (r is null and (n <> owner and n <> friend)) return n"; // ( (r is null) or ( ( n = friend and owner.uuid <> {uuid} ) or (n = owner and friend.uuid <> // {uuid} ) ) ) String findCandidateFriendsQuery = "start n=node(*), person=node({personNode}) match (n)-[r?:FRIEND]->(person), (person)-[f?:FRIEND]->(n) where NOT (id(n) IN [0]) and (r is null and f is null ) and n <> person return n"; Map<String, Object> uuids = new HashMap<String, Object>(); uuids.put( "uuid", // "235c5608-b240-438e-975e-6124e9e047f0" // Jack // "79202f6e-eb8d-466a-a523-87757e2ad510" // Rose // "d70ee717-29ac-4326-9704-1bae1dfade62" // Sally "9d0cd66a-29e7-4fac-850b-de94b2d0a171" // Bracewell ); ReadableIndex<Node> autoNodeIndex = graphDb.index().getNodeAutoIndexer().getAutoIndex(); Node personNode = autoNodeIndex.get("uuid", uuids.get("uuid")).getSingle(); Map<String, Object> params = new HashMap<String, Object>(); params.put("personNode", personNode); ExecutionResult result = engine.execute(findCandidateFriendsQuery, params); Iterator<Node> candidateFriendNodes = result.columnAs("n"); /* Iterator<Node> tempNodes = result.columnAs("friend"); while( tempNodes.hasNext() ) { Node tempNode = tempNodes.next(); if( tempNode == null ) { System.out.println( "WTF - how can this be null???"); break; } System.out.println( "(temp) name: " + tempNode.getProperty("name")); } */ while (candidateFriendNodes.hasNext()) { Node candidateFriendNode = candidateFriendNodes.next(); try { System.out.println( "processing result in candidateFriendNodes: " + candidateFriendNode.getProperty("name")); } catch (Exception e) { } } }