Skip to content

Instantly share code, notes, and snippets.

@rvanbruggen
rvanbruggen / IKEA graphgist
Last active December 23, 2015 04:09
IKEA GraphGist
= IKEA GraphGist =
This gist is to complement the http://blog.bruggen.com/2013/09/ikea-wardrobes-and-graphs-perfect-fit.html[more elaborate blogpost] that I wrote about using http://neo4j.org[neo4j] to model the partlist and the assembly process of two IKEA wardrobes.
First, we will create a part of the graph using this model:
image::https://lh4.googleusercontent.com/7XxUuCjnFtDhO-JAI5Ia6tcYZkQoMfcv_1pNE0mTA2cx76vIySrBU0z0tnykAPvqsMrZZD-cca3Q7ca-ERI0f5sDsGAUCEJTIx7wt15mxKhFEXeYFrZD_vEGJQ[]
[source,cypher]
----
@rvanbruggen
rvanbruggen / 1-importscript
Created November 4, 2013 18:54
Browser History - Clickstream Analysis with Neo4j
// this script uses the neo4j-shell-tools
// create personal nodes
import-cypher -d ; -i ./IMPORT/INPUT/PersonalNodes.csv -o ./IMPORT/OUTPUT/personalnodeout.csv create (n:url {id:{id}+1000,name:{name},type:{type}}) return n.name as name
// create indices
CREATE index on :url(name);
CREATE index on :url(id);
CREATE index on :url(type);
@rvanbruggen
rvanbruggen / presenting-neo4j.txt
Last active December 28, 2015 03:39
Presenting Neo4j
= Presenting Neo4j GraphGist =
This gist is to complement the http://blog.bruggen.com/[more elaborate blogpost] that I wrote how the http://www.prezi.com[prezi] presentation style actually massively resembles a graph. And how you could actually create a graph about http://neo4j.org[neo4j] to explain what graph databases are all about.
[source,cypher]
----
// create the nodes
create (n1{id:'1', name:'Neo4j', type:'Product', comments:''}),
@rvanbruggen
rvanbruggen / importingthecsv.txt
Last active December 29, 2015 03:09
London Underground
// create indices and constraints
CREATE CONSTRAINT ON (n:Station) ASSERT n.name IS UNIQUE;
// create nodes
// create Stations
import-cypher -d ; -i ./IMPORT/INPUT/isdb.csv -o ./IMPORT/OUTPUT/nodeoutput.csv merge (n:Station {name:{StationA}}) return n
import-cypher -d ; -i ./IMPORT/INPUT/isdb.csv -o ./IMPORT/OUTPUT/nodeoutput.csv merge (n:Station {name:{StationB}}) return n
//create rels
import-cypher -d ; -i ./IMPORT/INPUT/isdb.csv -o ./IMPORT/OUTPUT/rel.csv MATCH (stationa:Station {name:{StationA}}), (stationb:Station {name:{StationB}}) CREATE stationa-[r:#{Line} {direction:{Direction}, distance:{Distance}, time:{Time}}]->stationb RETURN r
@rvanbruggen
rvanbruggen / bcpqueries.cql
Last active December 30, 2015 07:29
Business Continuity Planning
//Run these queries in the neo4j-shell or the neo4j-browser to see the database at work.
//
//Show the datamodel: what is related to what, and how?
MATCH (a)-[r]->(b)
RETURN DISTINCT head(labels(a)) AS This, type(r) AS To, head(labels(b)) AS That
LIMIT 100
//Which labels are there and how many nodes have that label?
MATCH n RETURN labels(n), count(n);
//Which relationship types are there and how many rels are there of that type?
@rvanbruggen
rvanbruggen / importknot.cql
Last active December 30, 2015 14:09
Untying the Graph Database Import Knot
= Untying the Graph Database Import Knot =
This Graphgist accompanies http://blog.bruggen.com/2013/12/untying-graph-database-import-knot.html[my blogpost of December 6th, 2013] which tries to explain the different types of questions that people should be asking themselves when thinking about importing data into http://www.neo4j.org[neo4j], and the tools that can contribute to finding the most optimal import strategy for your specific use case.
//setup
//hide
[source,cypher]
----
create (n1:Tool {id:'1',name:'Spreadsheets'}),
(n2:Tool {id:'2',name:'Cypher Statements'}),
@rvanbruggen
rvanbruggen / foodimport.cql
Last active May 1, 2021 08:37
Food network import instructions
// create nodes
// create BaseNodes
import-cypher -d ; -i ./IMPORT/INPUT/1-basenodes.csv -o ./IMPORT/OUTPUT/nodeoutput.csv create (n:#{type} {id:{id},name:{name}}) return n
// create indexes
create index on :INGREDIENT_CATEGORY(name);
create index on :INGREDIENT(name);
create index on :INGREDIENT(id);
create index on :COMPOUND(name);
create index on :COMPOUND(id);
@rvanbruggen
rvanbruggen / Open Source Licensing Gist
Last active January 4, 2016 00:19
Open Source Licensing Graph
= Open Source Licensing Graph =
This gist is explain the Open Source Licensing Graph, based on the wonderful data from http://choosealicense.com/[ChoosALicense.com], a http://www.github.com[github] service. I wrote a small blogpost around it too on http://blog.bruggen.com/2014/01/the-open-source-licensing-graph.html[my blog].
First we have to load the data into the graph. This was a bit of work - but not difficult at all:
//hide
//setup
//output
[source,cypher]
@rvanbruggen
rvanbruggen / talkshowqueries.cql
Created March 23, 2014 19:22
TalkshowQueries
//Total number of visits by guests to shows
match (g:GUEST)-[v:VISITED]->(sh:SHOW)
return count(v);
//Number of visits by show
match (g:GUEST)-[v:VISITED]->(sh:SHOW)
return sh.id as Show, count(v) as NrOfVisits
order by NrOfVisits desc;
//Number of Politicians by show
@rvanbruggen
rvanbruggen / hierarchy.cql for 2.0
Last active June 26, 2022 08:53
A gist to demonstrate the power of Neo4j for dealing with hierarchical data.
//Let's create the top of the tree, the PRODUCT
create (n1:PRODUCT {id:1});
//Let's create 100 children of the PRODUCT, COST_GROUPS connected to the top of the tree
match (n1:PRODUCT {id:1})
with range(1,100) as RANGE, n1
foreach (r in RANGE | create (n2:COST_GROUP {id:r})-[:PART_OF {quantity:round(rand()*100)}]->(n1) );
//for every COST_GROUP, let's connect 100 children at a 3rd level, the COST_TYPEs
match (n2:COST_GROUP)