The translation module provides facilities to:
- parse Cypher queries using the Cypher frontend,
- produce a translation to Gremlin,
- transform results to a format that is accepted by a Gremlin Server-based database, Amazon Neptune, or Azure Cosmos DB.
To add a dependency using Maven:
<dependency>
<groupId>org.opencypher.gremlin</groupId>
<artifactId>translation</artifactId>
<version>1.0.4</version>
</dependency>
To add a dependency using Gradle:
dependencies {
compile 'org.opencypher.gremlin:translation:1.0.4'
}
You can also build the snapshot from source.
To translate a Cypher query to a Gremlin query:
String cypher = "MATCH (p:Person) WHERE p.age > 25 RETURN p.name";
TranslationFacade cfog = new TranslationFacade();
String gremlin = cfog.toGremlinGroovy(cypher);
A bit more verbose version of the above, demonstrating several extension points:
String cypher = "MATCH (p:Person) WHERE p.age > 25 RETURN p.name";
CypherAst ast = CypherAst.parse(cypher);
Translator<String, GroovyPredicate> translator = Translator.builder().gremlinGroovy().build();
String gremlin = ast.buildTranslation(translator);
Note that Translator
instances are not reusable. A new one has to be created for each buildTranslation
call. TranslationFacade
handles this for you.
Translator
instances support other common translation targets out of the box, like Gremlin bytecode:
Translator<Bytecode, P> translator = Translator.builder()
.bytecode()
.build();
Note that by default, translation targets TinkerPop 3.4.0 and uses steps and predicates that are unavailable in earlier
Gremlin versions, for example With Step and startingWith.
To provide translation suitable for these environments (for example JanusGraph <0.4.0) use gremlinServer33x
flavor
Translator<String, GroovyPredicate> translator = Translator.builder()
.gremlinGroovy()
.build(TranslatorFlavor.gremlinServer33x());
A translator for Amazon Neptune can be configured like so:
Translator<String, GroovyPredicate> translator = Translator.builder()
.gremlinGroovy()
.inlineParameters()
.enableMultipleLabels()
.build(TranslatorFlavor.neptune());
A translator for Azure Cosmos DB can be configured like so:
Translator<String, GroovyPredicate> translator = Translator.builder()
.gremlinGroovy()
.build(TranslatorFlavor.cosmosDb());
Custom translation targets can be provided by implementing GremlinSteps
, GremlinPredicates
, and GremlinParameters
:
Translator.builder()
.custom(
new MyGremlinSteps(),
new MyGremlinPredicates(),
new MyGremlinBindings()
)
.build();
Consult the Javadoc for more information.
If you want to run queries, not just translate them, you might be interested in the Cypher client for Gremlin Server.