Knowledge Graph part-3 :Building REST API over Knowledge Graph

In our previous post, we discussed about getting started with knowledge graph where we saw how to install neo4j in Docker and Modelling tabular data as graph where we saw how we can push the data to our graphical database.

Now that we have all the data in our graph database, we can analyse it directly in Neo4j. However a lot of time we need to access small part of the data via apps and we would need to build an API layer on the graph DB and expose only the relevant information.

In this post, we will discuss about exposing our knowledge graph via Flask API. All the data will be exposed in JSON format.

Identifying the queries

First, we will decide what we want to expose in our API. Let’s come up with few hypothetical requirements :

  • Get  a particular user’s details
  • Get user’s details and all connections for a given user
  • Size of our knowledge graph : count of all the nodes in our graph

Constructing Cypher queries

Now that we have decided what data we want to expose, we will write queries to fetch the data from Neo4j.

Get  a particular user’s details

Let’s find a user’s detail with user_id as 33 :

MATCH (n:Person) WHERE n.uid= 33
RETURN n as person

OR

MATCH (n:Person {uid: 33})
RETURN n as person

Get user’s details and all connections for a given user

Let’s find all the connection of user’s detail with user_id as 33 :

MATCH (n:Person {uid: 33})-[r]->(c)
RETURN n as person, type(r) as relationship, c as value

Size of our knowledge graph : count of all the nodes in our graph

Let’s count all the distinct number of nodes in our graph :

MATCH (n:Person)
  WITH count(n) as count
  RETURN 'Person' as NodeType, count
UNION ALL
MATCH (n:Country)
  WITH count(n) as count
  RETURN 'Country' as NodeType, count
UNION ALL
MATCH (n:WorkType)
  WITH count(n) as count
  RETURN 'WorkType' as NodeType, count
UNION ALL
MATCH (n:MajorStream)
  WITH count(n) as count
  RETURN 'MajorStream' as NodeType, count

Creating python wrapper functions

We have wrote all the queries, let’s create the python utility function for each queries. We have created three python functions for each of the queries above.  We then run these queries via py2neo on Neo4j.

Note : Use evaluate() if a single record is expected and use run()if multiple records are expected

Creating API endpoints

Let’s expose the Neo4j data over the REST API. We have created endpoints mapping for each of the functions.

I have used both GET and POST API just to show how its done.

Start the server

Let’s run the server to see our coding action :

$ python api/knowledge_graph_api.py

 

 .

 

 

All the codehttps://github.com/nikkisharma536/knowledge_graph/tree/master/api

Hope you like this post. Stay tuned for more blogs.

Checkout my portfolio here: https://confusedcoders.com/nikita-sharma-greenhorn-data-science-student

I am a greenhorn Data Science student with interest in finding patterns in data. My language of choice is Python and I am starting to get my hands dirty with R.

I blog on Medium.com [1] and ConfusedCoders.com [2]. I share my code on Github.com [3].

  1.  https://medium.com/@nikkisharma536
  2. https://confusedcoders.com/author/nikita
  3. https://github.com/nikkisharma536

Leave a Reply

Your email address will not be published. Required fields are marked *