Created
November 8, 2016 20:02
-
-
Save jexp/943520f6bc631e93902f6eebfe0488c5 to your computer and use it in GitHub Desktop.
Using [Neo4j's JDBC Driver](https://github.com/neo4j-contrib/neo4j-jdbc) with [JDBI](http://jdbi.org) also for [DropWizard](dropwizard.io)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
install and start Neo4j from http://neo4j.com/download | |
start the Neo4j Desktop or $NEO4J_HOME/bin/neo4j start | |
$GROOVY_HOME/bin/groovy neo4j-jdbi.groovy | |
The Matrix | |
The Matrix | |
Neo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Code from the JDBI Getting Started Page: http://www.jdbi.org/ | |
// JDBI API Docs: http://www.jdbi.org/apidocs/ | |
// neo4j-jdbc-driver: https://github.com/neo4j-contrib/neo4j-jdbc | |
@Grapes( | |
[@Grab(group='org.jdbi', module='jdbi', version='2.77'), | |
@Grab(group='org.neo4j', module='neo4j-jdbc-driver', version='3.0.1')] | |
) | |
import org.skife.jdbi.v2.* | |
import org.skife.jdbi.v2.sqlobject.* | |
import org.skife.jdbi.v2.util.* | |
DBI dbi = new DBI("jdbc:neo4j:bolt://localhost","neo4j","test"); | |
// dbi.setStatementRewriter(new NoOpStatementRewriter()); | |
dbi.setStatementRewriter(new HashPrefixStatementRewriter()); | |
Handle h = dbi.open(); | |
h.execute("create index on :Movie(title)"); | |
h.execute("create (m:Movie) SET m.id = #id, m.name = #name", 1, "The Matrix"); | |
String name = h.createQuery("MATCH (m:Movie) WHERE m.id = #id RETURN m.name as name") | |
.bind("id", 1) | |
.map(StringMapper.FIRST) | |
.first(); | |
println(name) | |
assert name == "The Matrix"; | |
String name3 = h.createQuery("MATCH (m:Movie) WHERE m.id = {1} RETURN m.name as name") | |
.bind(0, 1) | |
.map(StringMapper.FIRST) | |
.first(); | |
println(name3) | |
h.close(); | |
public interface PersonDAO | |
{ | |
@SqlUpdate("create index on :Person(name)") | |
void createIndex(); | |
@SqlUpdate("merge (p:Person {id: #id}) ON CREATE SET p.name=#name") | |
void insert(@Bind("id") int id, @Bind("name") String name); | |
@SqlQuery("match (p:Person) where p.id = #id return p.name as name") | |
String findNameById(@Bind("id") int id); | |
/** | |
* close with no args is used to close the connection | |
*/ | |
void close(); | |
} | |
PersonDAO dao = dbi.open(PersonDAO.class); | |
dao.createIndex(); | |
dao.insert(2, "Neo"); | |
String name2 = dao.findNameById(2); | |
println(name2) | |
assert name2 == "Neo"; | |
dao.close(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment