Neo4j is a high performance, open-source graph database written in Java and Scala.
But of course, you can use and extend it from other JVM Languages, like Kotlin.
I've been using Kotlin on and off since 2012, that's when I wrote my first article about this pragmatic and clean programming language.
Today I want to demonstrate in 4 examples how you can use Neo4j with Kotlin.
In preparation for KotlinConf 2017 we started to gather community activity of the Kotlin community in a Neo4j graph.
You can see tweets, GitHub projects, StackOverflow questions and answers and meetup events in this database of developer activity.
After showing some examples we will explore how to query that database using a Kotlin driver for Neo4j.
Then we'll look at the Kotlin + Spring (Data) demo app, that shows community members who get a lot of praise on a Twitter wall.
My main Kotlin project over the last year has been the GraphQL extension for Neo4j.
So we'll have a look at how we can extend Neo4j with a custom HTTP API and how we integrate the GraphQL-Java library using Kotlin.
This extension manages a GraphQL schema to translate GraphQL queries to Neo4js native query language Cypher.
Another way to extend Neo4j is with user-defined procedures and functions which (with a small trick) are really easy to implement with Kotlin too.
We'll take a look at the procedures and functions we expose as part of the neo4j-graphql extension.
14. The Whiteboard Model Is the Physical Model
Eliminates Graph-to-
Relational Mapping
In your data model
Bridge the gap
between business
and IT models
In your application
Greatly reduce need
for application code
15. CAR
name: “Dan”
born: May 29, 1970
twitter: “@dan”
name: “Ann”
born: Dec 5, 1975
since:
Jan 10, 2011
brand: “Volvo”
model: “V70”
Property Graph Model Components
Nodes
• The objects in the graph
• Can have name-value properties
• Can be labeled
Relationships
• Relate nodes by type and
direction
• Can have name-value properties
LOVES
LOVES
LIVES WITH
PERSON PERSON
16. Relational Versus Graph Models
Relational Model Graph Model
KNOWS
ANDREAS
TOBIAS
MICA
DELIA
Person FriendPerson-Friend
ANDREAS
DELIA
TOBIAS
MICA
17. Cypher: Powerful and Expressive Query Language
MATCH (:Person { name:“Dan”} ) -[:LOVES]-> (:Person { name:“Ann”} )
LOVES
Dan Ann
LABEL PROPERTY
NODE NODE
LABEL PROPERTY
18. Official Language Drivers
• Foundational drivers for
most programming languages
• Bolt: streaming
binary wire protocol
• Authoritative mapping to
native type system,
uniform across drivers
• Pluggable into richer frameworks
JavaScript Java .NET Python
Community
Drivers
Drivers
Bolt
19. Kotlin Wrapper
• Wraps Neo4j Java Driver
• Idiomatic API
• Use Data Classes as
– Parameters
– Results
– Destruct data class to Map
• Serializer (DateTime, Enum)
Bolt
https://github.com/erictsangx/kotlin-neo4j
23. Object Graph Mapping
• Neo4j OGM
• Spring Data Neo4j
– Dedicated support for Kotlin classes in Spring / Spring
Data
– Persistence Constructors (PR 405)
– Example Project:
– github.com/neo4j-examples/movies-kotlin-spring-data-neo4j
29. Approach
• Use Jupyter Notebooks
– Read Data from APIs
– Import into Neo4j Graph
• Evolve into AWS script
– spin instance up for any community
– based on search tags
• Query for analytics, newsletter, active people
35. Community Radar
• Kotlin Backend
– finds people that have been mentioned a lot or
tagged with a hashtag
– and recent tweets
• Angular Front-End
• Statistics Service with React UI (WIP)
38. GraphQL
• GraphQL is an API query language
• Based on a typesafe schema
• Contract between front & backend
• Flexible queries, nested „shape of data“
• Updates via Mutations
39. GraphQL
query {
Movie(title:"The Matrix") {
title
released
tagline
actors {
name
born
}
}
}
type Movie {
title: String!
released: Int
tagline: String
actors: [Person]
}
type Person {
name: String!
born: Int
movies: [Movie]
}
40. Neo4j-GraphQL
• Add support to Neo4j via extension
• Translate GraphQL query to Cypher based on
schema
• Execute & transform results
• Integrate graphql-java
• Add custom directives & generated mutations
44. Lessons Learned
• Started in Java switched to Kotlin = < 3
• Convince other devs
• Interoperability is impressive
• Language & Query transformations work well
• Extension functions are great for glue code
• Updates were painless
• I like: ?: / ?. / let / use
45. Lessons Learned
• Watch out for Nullability in Java libraries/APIs
• Too deep list comprehensions
• Too many „it“
• „?“ operator would be great
• Local return from closures
• Watch out for fun foo(): Any = {...}
47. Any JVM Language will do
• Nullable Inputs / Outputs
• Some Annotations needed (@JvmField)
• Example Project
– github.com/mfalcier/neo4j-kotlin-procedure-example
50. Other Projects
• Open Data (Healthcare) Import, Queries,
Object-Mapping
– slideshare.net/neo4j/open-data-with-neo4j-and-kotlin
• Neo4j Cloudfoundry Service Broker
• Extension Functions for Neo4j‘s Java API
– github.com/pisolutions/neo4j-kotlin