CS257
Chris Pollett
Sep 30, 2020
db.orders.insertMany([ { _id: 1, cust_id: "Ant O. Knee", ord_date: new Date("2020-03-01"), price: 25, items: [ { sku: "oranges", qty: 5, price: 2.5 }, { sku: "apples", qty: 5, price: 2.5 } ], status: "A" }, { _id: 2, cust_id: "Ant O. Knee", ord_date: new Date("2020-03-08"), price: 70, items: [ { sku: "oranges", qty: 8, price: 2.5 }, { sku: "chocolates", qty: 5, price: 10 } ], status: "A" }, { _id: 3, cust_id: "Busby Bee", ord_date: new Date("2020-03-08"), price: 50, items: [ { sku: "oranges", qty: 10, price: 2.5 }, { sku: "pears", qty: 10, price: 2.5 } ], status: "A" }, { _id: 4, cust_id: "Busby Bee", ord_date: new Date("2020-03-18"), price: 25, items: [ { sku: "oranges", qty: 10, price: 2.5 } ], status: "A" }, { _id: 5, cust_id: "Busby Bee", ord_date: new Date("2020-03-19"), price: 50, items: [ { sku: "chocolates", qty: 5, price: 10 } ], status: "A"}, { _id: 6, cust_id: "Cam Elot", ord_date: new Date("2020-03-19"), price: 35, items: [ { sku: "carrots", qty: 10, price: 1.0 }, { sku: "apples", qty: 10, price: 2.5 } ], status: "A" }, { _id: 7, cust_id: "Cam Elot", ord_date: new Date("2020-03-20"), price: 25, items: [ { sku: "oranges", qty: 10, price: 2.5 } ], status: "A" }, { _id: 8, cust_id: "Don Quis", ord_date: new Date("2020-03-20"), price: 75, items: [ { sku: "chocolates", qty: 5, price: 10 }, { sku: "apples", qty: 10, price: 2.5 } ], status: "A" }, { _id: 9, cust_id: "Don Quis", ord_date: new Date("2020-03-20"), price: 55, items: [ { sku: "carrots", qty: 5, price: 1.0 }, { sku: "apples", qty: 10, price: 2.5 }, { sku: "oranges", qty: 10, price: 2.5 } ], status: "A" }, { _id: 10, cust_id: "Don Quis", ord_date: new Date("2020-03-23"), price: 25, items: [ { sku: "oranges", qty: 10, price: 2.5 } ], status: "A" } ]);
var mapFunction1 = function() { emit(this.cust_id, this.price); };Here this refers to the document being processed, emit is the function to output a key-value pair.
var reduceFunction1 = function(keyCustId, valuesPrices) { return Array.sum(valuesPrices); };Prices with the same customer id will all be sent as an array to this reduce function, which merely computes their sum.
db.orders.mapReduce( mapFunction1, reduceFunction1, { out: "map_reduce_example" } ) Here map_reduce_example is the collection to which the resulting key value pairs are output.
db.map_reduce_example.find().sort( { _id: 1 } )
{"_id" : "Ant O. Knee", "value" : 95 } { "_id" : "Busby Bee", "value" : 125 } { "_id" : "Cam Elot", "value" : 60 } { "_id" : "Don Quis", "value" : 155 }
{ action: "reduce", out: "map_reduce_example" }
Id Genre Title Price "Audiobook price" 1 fantasy "My first book" 20 30 2 education "Beginners Guide" 10 null 3 education "SQL strikes back" 40 null 4 fantasy "The rise of SQL" 10 null
CREATE KEYSPACE mykeyspace WITH replication = {'class':'SimpleStrategy', 'replication_factor' : 3};There is also a 'Network Topology Strategy'.
CREATE TABLE EMPLOYEE ( id int PRIMARY KEY, name text, city text, salary varint, phone varint );A primary key must be specified, though, so that we can store things in a column-oriented fashion.
INSERT INTO EMPLOYEE (id, name, city, phone, salary) VALUES (1, 'bob', 'San Jose', 4089995555, 75000);
SELECT * FROM EMPLOYEE;
:help #gives list of shell commands :exit #leave shell :use db_name #set db to use to db_name :source file_name #read cypher commands from file file_name SHOW DATABASES; #give a list of the current neo4j databases CREATE DATABASE some_db IF NOT EXISTS; # creates the database some_db if it doesn't exist DROP DATABASE some_db; # delete database some_db
CREATE (Bart:Reader {name:'Bart Baesens', age:32}) CREATE (Seppe:Reader {name:'Seppe vanden Broucke', age:30})Notice both Bart and Seppe are JSON Reader objects.
CREATE (b01)-[:IS_GENRE]->(Education), (b02)-[:IS_GENRE]->(Thriller), (b03)-[:IS_GENRE]->(Education)
// return all Book nodes MATCH (b:Book) RETURN b; // return the first 20 Book nodes ordered by price MATCH (b:Book) RETURN b ORDER BY b.price DESC LIMIT 20; // return Book nodes with title Beginning Neo4j MATCH (b:Book) WHERE b.title = "Beginning Neo4j" RETURN b; //customer names of customers over 30 who purchase a book by Wilfried Lemahieu MATCH (c:Customer)-[p:PURCHASED]->(b:Book)<- [:WRITTEN_BY]-(a:Author) WHERE a.name = "Wilfried Lemahieu" AND c.age > 30 AND p.type = "cash" RETURN DISTINCT c.name; // Return titles of books that are in a sub-genre of Programming //*0.. below means 0 or more PARENT traversals MATCH (b:Book)-[:IN_GENRE]->(:Genre) -[:PARENT*0..]-(:Genre {name:"Programming"}) RETURN b.title;
import org.neo4j.driver.v1.*; import static org.neo4j.driver.v1.Values.parameters; import java.util.List; import static java.util.Arrays.asList; import static java.util.Collections.singletonMap; public class Social { public static void main(String...args) { Config noSSL = Config.build().withEncryptionLevel( Config.EncryptionLevel.NONE).toConfig(); //Connect to a neo4j database as user neo4j with password test Driver driver = GraphDatabase.driver("bolt://localhost", AuthTokens.basic("neo4j","test"),noSSL); // <password> try (Session session = driver.session()) { //Create a list of pairs to insert into database List data = asList(asList("Jim","Mike"),asList("Jim","Billy"), asList("Anna","Jim"), asList("Anna","Mike"), asList("Sally","Anna"),asList("Joe","Sally"), asList("Joe","Bob"),asList("Bob","Sally")); String insertQuery = "UNWIND {pairs} as pair " + "MERGE (p1:Person {name:pair[0]}) " + "MERGE (p2:Person {name:pair[1]}) " + "MERGE (p1)-[:KNOWS]-(p2);"; session.run(insertQuery, singletonMap("pairs",data)).consume(); StatementResult result; String foafQuery = " MATCH (person:Person)-[:KNOWS]-(friend)-[:KNOWS]-(foaf) "+ " WHERE person.name = {name} " + " AND NOT (person)-[:KNOWS]-(foaf) " + " RETURN foaf.name AS name "; result = session.run(foafQuery, parameters("name","Joe")); while (result.hasNext()) { System.out.println(result.next().get("name")); } String commonFriendsQuery = "MATCH (user:Person)-[:KNOWS]-(friend)-[:KNOWS]-(foaf:Person) " + " WHERE user.name = {from} AND foaf.name = {to} " + " RETURN friend.name AS friend"; result = session.run(commonFriendsQuery, parameters("from","Joe","to","Sally")); while (result.hasNext()) { System.out.println(result.next().get("friend")); } String connectingPathsQuery = "MATCH path = shortestPath(" + " (p1:Person)-[:KNOWS*..6]-(p2:Person)) " + " WHERE p1.name = {from} AND p2.name = {to} " + " RETURN [n IN nodes(path) | n.name] as names"; result = session.run(connectingPathsQuery, parameters("from","Joe","to","Billy")); while (result.hasNext()) { System.out.println(result.next().get("names")); } } } }