Neo4j with Azure Functions
Recently, I’ve had a couple of people ask me how to use Neo4j with Azure functions, and well – I’d not done it myself, but now I have – let’s get it done!
- Login to your Azure Portal
-
Create a new Resource, and search for ‘function app’:
- Select ‘Function App’ from the Market Place:
- Press ‘Create’ to actually make one:
- Fill in your details as you want them
I’m assuming you’re reasonably au fait with the setting here, in essence if you have a Resource Group you want to put it into (maybe something with a VNet) then go for it, in my case, I’ve just created a new instance of everything.
- Create the function, and wait for it to be ready. Mayhaps make a tea or coffee, have a break from the computer for a couple of mins – it’s all good!
- When it’s ready, click on it and go into the Function App itself (if it doesn’t take you there!)
-
Create a new function:
- We want to create an
HttpTrigger
function in C# for this instance:
- This gives us a ‘
run.csx
’ file, which will have a load of default code, you can run it if you want,
and you’ll see an output window appear which will say:
Well – good – Azure Functions work, so let’s get a connection to a Neo4j instance – now – for this I’m assuming you have an IP to connect to – you can always use the free tier on GrapheneDB if you want to play around with this.
- Add references to a driver
We need to add a reference to a Neo4j client, in this case, I’ll show the official driver, but it will work as well with the community driver. First off, we need to add a ‘project.json’ file, so press ‘View Files’ on the left hand side –
Then add a file:
Then call it project.json
– and yes it has to be that name:
With our new empty file, we need to paste in the nuget reference we need:
{ "frameworks": { "net46":{ "dependencies": { "neo4j.driver": "1.5.2" } } } }
Annoyingly if you copy/paste this into the webpage, the function will add extra ‘closing’ curly braces, so just delete those.
If you press ‘Save and Run’ you should get the same response as before – which is good as it means that the Neo4j.Driver package has been installed, if we look at files, we’ll see the ‘project.json.lock’ file which we want to.
- Code
We want to add our connection information now, we’re going to go basic, and just return the COUNT of the nodes in our DB. First we need to add a ‘using’ statement to our code:
So add,
using Neo4j.Driver.V1;
Then replace the code in the function with:
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log) { using (var driver = GraphDatabase.Driver("bolt://YOURIP:7687", AuthTokens.Basic("user", "pass"))) { using (var session = driver.Session()) { IRecord record = session.Run("MATCH (n) RETURN COUNT(n)").Single(); int count = record["COUNT(n)"].As<int>(); return req.CreateResponse(HttpStatusCode.OK, "Count: " + count); } } }
Basically, we’ll create a Driver, open a session and then return a 200 with the count!
- Run
You can now ‘Save and Run’ and your output window should now tell you the count:
- Done
Your first function using Neo4j, Yay!
This is very interesting.
Neo4.driver no longer officially supported,
What about Neo4jClient?
Neo4j.Driver is officially supported, there is no intention for it to not be supported – where did you see that?