0

Fullstack JavaScript – Neo4j Script Procedures

Posted by Michael Hunger on Apr 1, 2017 in cypher, neo4j

Imagine, being a fullstack JavaScript developer and not just using the language in the frontend, middleware or backend but also to create your user-defined procedures and functions in the database.

Several other databases support a similar approach for views and user defined extensions, and now you can do it with Neo4j too.

Already early last year, Neo4j’s user defined procedures were still in their infancy.
I had just written an article about the Javas JavaScript engine “Nashorn”.

So naturally I experimented with using procedures to dynamically create and run JavaScript functions.

The function mapping is stored in Neo4j’s graph properties.

You could create JavaScript functions with a name and body and then later call them by name and passing parameters along.

CALL scripts.function('users', '
function users(name) {
  return collection(db.findNodes(label("User"),'lastname',name));
}')

CALL scripts.run('users','Anderson') YIELD value as user;

// or call as function, returns a list
RETURN scripts.run('users','Smith');

That worked all quite well, but I didn’t find the time to turn that into a proper project.

Later in the year I got some feature and pull requests on the APOC procedure library to add such functions.

As there are some concerns esp. from corporate users about scripting support, I pulled my work into a separate project: Neo4j Script Procedures

So, when I came across this tweet, it reminded me of wanting to update the project.

I thought it was a good opportunity to upgrade and release the project.

So, now you can try to run JavaScript functions from Neo4j’s Cypher by grabbing the jar-file from the latest release.

Just put it into $NEO4J_HOME/plugins and restart your server.

Note: In Neo4j Community Desktop, there is a directory chooser on the “Options” for the plugins directory)

The 3.1.0.1 of neo4j-script-procedures release does not support Neo4j 3.1.2 as there are some incompatibilities with procedures creating new property-names.
It should work with 3.1.0, 3.1.1 or 3.1.3 though.

Let me know what you think and how we can improve this little useful library, please raise issues on the repository for feedback and problems.

Tags: , ,

 
0

Creating a Neo4j Example Graph with the Arrows Tool

Posted by Michael Hunger on Mar 21, 2017 in cypher, import

Some years ago my colleague Alistair Jones created a neat little tool in JavaScript to edit and render example graphs in a consistent way.

It is aptly named Arrows and you can find it here: http://www.apcjones.com/arrows

We mostly use it for presentations, but also to show data models for Neo4j GraphGists and Neo4j Browser Guides.
Because it also stores the positions of nodes, it’s always true to the same layout and doesn’t wiggle around.

arrows.jpg

Read more…

 
0

Academy Awards (Oscars) from Kaggle to Neo4j

Posted by Michael Hunger on Mar 9, 2017 in cypher, import, neo4j
This is part 1, in the next part, we’ll look at using import.io to scrape IMDB and the Academy Awards Database.
You can query the imported data in this instance (user/pass:oscars) of the brand new Neo4j Sandbox.

I came across the tweet from @LynnLangit about first step with mxnet, which I really liked.

lynn langit mxnet.jpg

So I wanted to do the same for Neo4j and was looking for a good dataset.

Then I realized that the 89th Academy Awards (Oscars) ceremony was the next day.
I was really looking forward to it, hoping it would come with some strong statements towards the current administration.
And then him rage tweeting about it on Monday morning.

But instead we got a fun Jimmy Kimmel performance and the well know Moonlight and La-La-Land mess-up by the (ex)-PWC people.

So I found the data and imported it and had this post ready to go.

But then got distracted trying to scrape IMDB with import.io and missed the date.

But as it is a nice dataset interestingly not as widely available as you’d think, I feel it’s still worth publishing.

So enjoy my struggles with data (quality).

Read more…

 
0

5 Tips & Tricks for Fast Batched Updates of Graph Structures with Neo4j and Cypher

Posted by Michael Hunger on Mar 2, 2017 in Uncategorized, cypher, import, neo4j

Michael Hunger, @mesirii

When you’re writing a lot of data to the graph from your application or library, you want to be efficent.

Inefficient Solutions

These approaches are not very efficient:

  • hard coding values instead of using parameters

  • sending a single query / tx per individual update

  • sending many single queries within a single tx with individual updates

  • generating large, complex statements (hundreds of lines) and sending one of them per tx and update

  • sending in HUGE (millions) of updates in a single tx, will cause out-of-memory issues

Read more…

 
0

The Reddit Meme Graph with Neo4j

Posted by Michael Hunger on Feb 25, 2017 in cypher, import

Saturday night after not enough drinks, I came across these tweets by @LeFloatingGhost.

memegraph tweet.jpg

This definitely looks like a meme graph.

We can do that too

memegraph meme.jpg

Read more…

Tags:

 
0

User Defined Functions in Neo4j 3.1.0-M10

Posted by Michael Hunger on Oct 6, 2016 in apoc, cypher

Neo4j 3.1 brings some really neat improvements in Cypher alongside other cool features

I already demonstrated the – GraphQL inspired – map projections and pattern comprehensions in my last blog post.

User Defined Procedures

In the 3.0 release my personal favorite was user defined procedures which can be implemented using Neo4j’s Java API and called directly from Cypher.
You can tell, because I wrote about half of the 270 procedures for the APOC procedure collection “, with the remainder provided by other contributors.

Remember the syntax: …​ CALL namespace.procedure(arg1, arg2) YIELD col1, col2 AS alias …​

MATCH (from:Place {coords:{from}}), (to:Place {coords:{to}})

CALL apoc.algo.dijkstra(from, to, "ROAD", "cost") YIELD path, weight

RETURN nodes(path)
ORDER BY weight LIMIT 10;

Read more…

 
0

Neo4j 3.0 Stored Procedures

Posted by Michael Hunger on Feb 29, 2016 in cypher, java

One of the many exciting features of Neo4j 3.0 are “Stored Procedures” that, unlike the existing Neo4j-Server extensions are directly callable from Cypher.

At the time of this writing it is only possible to call them in a stand-alone statement with CALL package.procedure(params)
but the plan is to make them a fully integrated part of Cypher statements.
Either by making CALL a clause or by turning procedures into function-expressions (which would be my personal favorite).

Currently procedures can only be written in Java (or other JVM languages).
You might say, “WTF …​ Java”, but it is less tedious than it sounds.

First of all, the effort of setting up a procedure project, writing and building it is minimal.

To get up and running you first need a recent copy of Neo4j 3.0,
either the 3.0.0-M04 milestone or the latest build from the Alpha Site.

To get you started you also need a JDK and a build tool like Gradle or Maven.

You can effectively copy the procedure template example that Jake Hansson provided in neo4j-examples as a starting point.

But let me quickly walk you through an even simpler example (GitHub Repository).

You need to declare the org.neo4j:neo4j:3.0.0[-M04] dependency in the provided scope, to get the necessary annotations and the Neo4j API to talk to the database.

pom.xml
<dependency>
  <groupId>org.neo4j</groupId>
  <artifactId>neo4j</artifactId>
  <version>${neo4j.version}</version>
  <scope>provided</scope>
</dependency>
build.gradle
project.ext {
    neo4j_version = ""
}
dependencies {
	compile group: "org.neo4j", name:"neo4j", version:project.neo4j_version
	testCompile group: "org.neo4j", name:"neo4j-kernel", version:project.neo4j_version, classifier:"tests"
	testCompile group: "org.neo4j", name:"neo4j-io", version:project.neo4j_version, classifier:"tests"
	testCompile group: "junit", name:"junit", version:4.12
}

If you have a great idea on what kind of procedure you want to write, just open a file with a new class.

Please note that the only package and method names become the procedure name (but not the class name).

In our example we will create a very simple procedure that just computes the minimum and maximum degrees of a certain label.

The reference to Neo4j’s GraphDatabaseService instance is injected into your class into the field annotated with @Context.
As procedures are meant to be stateless, declaring non-injected non-static fields is not allowed.

In our case the procedure will be named stats.degree and called like CALL stats.degree('User').

package stats;

public class GraphStatistics {

    @Context private GraphDatabaseService db;

    // Result class
    public static class Degree {
        public String label;
        // note, that "int" values are not supported
        public long count, max, min = Long.MAX_VALUE;

        // method to consume a degree and compute min, max, count
        private void add(long degree) {
          if (degree < min) min = degree;
          if (degree > max) max = degree;
          count ++;
        }
    }

    @Procedure
    public Stream<Degree> degree(String label) {
        // create holder class for results
        Degree degree = new Degree(label);
        // iterate over all nodes with label
        try (ResourceIterator it = db.findNodes(Label.label(label))) {
            while (it.hasNext()) {
               // submit degree to holder for consumption (i.e. max, min, count)
               degree.add(it.next().getDegree());
            }
        }
        // we only return a "Stream" of a single element in this case.
        return Stream.of(degree);
    }
}

If you want to test the procedures quickly without spinning up an in-process server and connecting to it remotely (e.g. via the new binary bolt protocol as shown in the procedure-template), then you can use the test-facilities of Neo4j’s Java API.

Now we can test our new and shiny procedure by writing a small unit-test.

package stats;

class GraphStatisticsTest {
    @Test public void testDegree() {
        // given Alice knowing Bob and Charlie and Dan knowing no-one
        db.execute("CREATE (alice:User)-[:KNOWS]->(bob:User),(alice)-[:KNOWS]->(charlie:User),(dan:User)").close();

        // when retrieving the degree of the User label
        Result res = db.execute("CALL stats.degree('User')");

        // then we expect one result-row with min-degree 0 and max-degree 2
        assertTrue(res.hasNext());
        Map<String,Object> row = res.next();
        assertEquals("User", row.get("label"));
        // Dan has no friends
        assertEquals(0, row.get("min"));
        // Alice knows 2 people
        assertEquals(2, row.get("max"));
        // We have 4 nodes in our graph
        assertEquals(4, row.get("count"));
        // only one result record was produced
        assertFalse(res.hasNext());
    }
}

Of course you can use procedures to create procedures, e.g. in other languages that are supported natively on the JVM like JavaScript via Nashorn, or Clojure, Groovy, Scala, Frege (Haskell), (J)Ruby or (J/P)ython.
I wrote one for creating and running procedures implemented in JavaScript.

There are many other cool things that you can do with procedures, see the resources below.

If you have ideas for procedures or wrote some of your own, please let us know.

Join our public Slack channel and visit #neo4j-procedures.

Resources

 
0

Using XRebel 2 with Neo4j

Posted by Michael Hunger on May 5, 2015 in neo4j

At Spring.IO in Barcelona I met my pal Oleg from ZeroTurnaround and we looked at how the new XRebel 2
integrates with Neo4j, especially with the remote access using the transactional Cypher http-endpoint.

As you probably know, Neo4j currently offers a remoting API based on HTTP requests (a new binary protocol is in development).

Our JDBC driver utilizes that http-based protocol to connect to the database and execute parameterized statements while adhering to the JDBC APIs.

XRebel is a lightweight Java Application Profiler which is loaded as java-agent and instruments your application.
It traces runtime for web requests and records your backend-application CPU usage, database- (JDBC) and http-requests to other services.
For web-applications it integrates automatically with the http-processing and injects profiling information into the response.

Movies Webapp

For this quick demo, we use the example Movies application which is available for many programming languages from our developer resources.
The application is just a plain Java webapp that serves three JSON endpoints to a simple Javascript frontend page.
The backend connects to Neo4j via JDBC to retrieve the requested information via our Cypher query language.

To prepare for running our app, just download, unzip and start Neo4j, open it on http://localhost:7474/ and run the :play movies statement in the Neo4j browser.
Then we can get and build the application and run it.
To test that it works, open the app in your browser at http://localhost:8080

git clone http://github.com/neo4j-contrib/developer-resources
cd developer-resources/language-guides/java/jdbc

mvn compile exec:java -DmainClass="org.neo4j.example.movies.Movies"

Setup with XRebel

To use XRebel we just download it, get an eval license and attach the jar as a java-agent to our application.

MAVEN_OPTS="-javaagent:$HOME/Downloads/xrebel/xrebel.jar" mvn compile exec:java -DmainClass="org.neo4j.example.movies.Movies"

If we check our example application page again, we see a small green XRebel icon in the left corner.
It provides access to the XRebel UI which has tabs for application performance, database queries, exceptions and more.

For our initial query for the “Matrix” movie, it shows both the request time for the web-application, as well as the database calls to Neo4j.
Interestingly both the JDBC level as well as the underlying http calls to Neo4j are displayed.

If we uglify our app, that our queries are executed incorrectly, simulating a n+1 select, then that shows clearly up in XRebel as massive database interaction.

Runtime Exceptions due to a programming error are also made immediately accessible from the XRebel UI.

For non-visual REST-services you can access the same profiling information via a special endpoint that is added to your application, in our case: http://localhost:8080/xrebel

As you can see, XRebel can give you quick insights in the performance profile of your Neo4j backed application and highlights which queries / pages / secondary requests
need further optimization.

Ping Oleg or me if you have more questions.

If you’re in London this week and want to have a relaxing election day,
make sure to grab a seat for GraphConnect on May 7, the Neo4j conference.
Ping me via email (michael at neo4j.org) for a steep discount a an avid reader of this blog.

 
1

Neo4j Server Extension for Single Page Experiments

Posted by Michael Hunger on Apr 24, 2015 in neo4j, server

Sometimes you have a nice dataset in Neo4j and you’d want to provide a self-contained way of quickly exposing it to the outside world without a multi-tier setup.

So for experiments and proofs of concepts it would be helpful to be able to extend Neo4j Browser to accomodate new types of frames and commands.
Unfortunately we’re not there yet, there is still some work to be done until this will be possible.

Until then …. why not use what we already have.

I was discussing some helpful database and server extensions which would benefit from a tiny built-in UI with different people.
Then I had the idea to just use the JAX-RS mechanisms that Neo4j Server supports to not only serve JSON/text or XML but also deliver
HTML, JS, CSS and image files to the browser.
Those files would not live on the file-system but be packaged directly into the jar of the extension, e.g. residing in a resources/webapp folder.

How it works

This was actually much easier than expected.
This is a normal JAX-RS resource class that can then be mounted on an endpoint using the neo4j-server.properties configuration.

The HTTP-GET endpoint handles certain patterns declared by a regular expression.
There is one function that tries to find that file within the webapp folder within the JAR classpath, returning null if not found or the InputStream otherwise.
And one function for determining the content-type to be returned.

You can easily use this approach for your own Neo4j extension just by copying that StaticWebResource into your project and providing the html,js and css files in the webapp directory.

The Demo: Popoto.js

As my demo I used a setup that exposes popoto.js automatcially on top of the data you have in your graph.

The StaticWebResource provides the web-files of the visualization from the resources/webapp directory.
And PopotoResource adds a second endpoint to provide a config/config.js file which uses label, property and index information
to provide the necessary config for popoto’s visualization.

Note that you have to disable auth for this demo as I haven’t added means for it to configure a username/password.

You can use the demo by cloning and building (mvn clean install) this repository.
Copy the resulting jar in the server’s plugin directory.
Edit conf/neo4j-server.properties to register the package name with an endpoint.

cp target/neo4j-web-extension-2.2-SNAPSHOT.jar /path/to/neo/plugins/
echo 'org.neo4j.server.thirdparty_jaxrs_classes=extension.web=/popoto' >>  /path/to/neo/conf/neo4j-server.properties
/path/to/neo/bin/neo4j restart
open http://localhost:7474/popoto

popoto in neo demo

You can also download the JAR from here.

Enjoy exploring!

 
6

How To: Neo4j Data Import – Minimal Example

Posted by Michael Hunger on Apr 18, 2015 in import, neo4j

We want to import data into Neo4j, there are too many resources with a lot of information which makes it confusing.
Here is the minimal thing you need to know.

Imagine the data coming from the export of a relational or legacy system, just plain CSV files without headers (this time).

people.csv
1,"John"
10,"Jane"
234,"Fred"
4893,"Mark"
234943,"Anne"
friendships.csv
1,234
10,4893
234,1
4893,234943
234943,234
234943,1

Graph Model

Our graph Model would be very simple:

import data model.jpg
(p1:Person {userId:10, name:"Anne"})-[:KNOWS]->(p2:Person {userId:123,name:"John"})

Import with Neo4j Server & Cypher

  1. Download, install and start Neo4j Server.

  2. Open http://localhost:7474

  3. Run the following statements one by one:

I used http-urls here to run this as an interactive, live Graph Gist.

CREATE CONSTRAINT ON (p:Person) ASSERT p.userId IS UNIQUE;
LOAD CSV FROM "https://gist.githubusercontent.com/jexp/d8f251a948f5df83473a/raw/people.csv" AS row
CREATE (:Person {userId: toInt(row[0]), name:row[1]});
USING PERIODIC COMMIT
LOAD CSV FROM "https://gist.githubusercontent.com/jexp/d8f251a948f5df83473a/raw/friendships.csv" AS row
MATCH (p1:Person {userId: toInt(row[0])}), (p2:Person {userId: toInt(row[1])})
CREATE (p1)-[:KNOWS]->(p2);
You can also use file-urls.
Best with absolute paths like file:/path/to/data.csv, on Windows use: file:c:/path/to/data.csv

If you want to find your people not only by id but also by name quickly, also run:

CREATE INDEX ON :Person(name);

For instance all second degree friends of “Anne” and on how many ways they can be reached.

MATCH (:Person {name:"Anne"})-[:KNOWS*2..2]-(p2)
RETURN p2.name, count(*) as freq
ORDER BY freq DESC;

Bulk Data Import

For tens of millions up to billions of rows.

Shutdown the server first!!

Create two additional header files:

people_header.csv
userId:ID,name
friendships_header.csv
:START_ID,:END_ID

Execute from the terminal:

path/to/neo/bin/neo4j-import --into path/to/neo/data/graph.db  \
--nodes:Person people_header.csv,people.csv --relationships:KNOWS friendships_header.csv,friendships.csv

After starting your database again, run:

CREATE CONSTRAINT ON (p:Person) ASSERT p.userId IS UNIQUE;

Copyright © 2007-2017 Better Software Development All rights reserved.
Multi v1.4.5 a child of the Desk Mess Mirrored v1.4.6 theme from BuyNowShop.com.