David Allen Profile picture
Director, Developer Advocacy for @grafana | Prev: @neo4j, fintech startups, @MITREcorp | community builder, people matter | he/him

Sep 14, 2021, 9 tweets

Example thread on how to load #JSON into #Neo4j Aura -- working up from simple to more complex. Let's use the .@TheHackersNews public API to load a mini-feed of stories.

First: head endpoint with best stories, and simplest JSON load:

the apoc.load.json call always returns "value" with whatever came back. HackerNews is sending results, an array of post IDs.

We can extract out just the post IDs with a bit of extra cypher like this. Nice clean array of long values.

One step further; now we will UNWIND the array, turning the nested array into each individual item, and then build the URL we'll ask of HackerNews to get the detail of each story. This is how we build URLs one by one; we just take the story ID and concat it into a string URL

Now, let's just add an apoc.load.json call on to each of those URLs we constructed, and see what the resulting JSON of each story is. This gives us a result set of the JSON payload of every story on the site.

#BuildingStepByStep #neo4j

Simplest graph format we can do here is just a user submitted a story. Something like:

(:User)-[:SUBMITTED]->(:Story)

So let's do that.

Here's the load code with the MERGE statements that put our Users and Stories into the graph. And it works!

resulting snapshot of the graph in #Neo4jAura

some slightly fancy things in that merge statement explained:

(1) hackernews reports times in UNIX time (seconds since the epoch) so

datetime({ epochSeconds: value.time })

turns that into a neo4j datetime

it's possible for the URL to be missing from a story! So:

coalesce(value.url, 'REMOVED')

this returns the first non-null value; so if value.url is missing, you get a url of 'removed'

Share this Scrolly Tale with your friends.

A Scrolly Tale is a new way to read Twitter threads with a more visually immersive experience.
Discover more beautiful Scrolly Tales like this.

Keep scrolling