3

I'm new to Java, Gremlin, Nodejs, Tickerpop, Maven and just about everything else. What does this code do? In particular what is 'java.import' doing? Is it a Java class? What has this got to do with Titan?

var Titan = require('titan-node');
var gremlin = new Titan.Gremlin({ loglevel: 'OFF' });

var TinkerGraphFactory = gremlin.java.import('com.tinkerpop.blueprints.impls.tg.TinkerGraphFactory');
var graph = TinkerGraphFactory.createTinkerGraphSync();
var g = gremlin.wrap(graph);

g.V('name', 'marko').next(function (err, v) {
    v.getProperty('name', function (err, value) {
        console.log(value);
    });
});

Why when I use the Rexster can I not see the database being queried here?

Ian Warburton
  • 15,170
  • 23
  • 107
  • 189
  • Aha... so its a class. http://grepcode.com/file/repo1.maven.org/maven2/com.tinkerpop.blueprints/blueprints-core/2.0.0/com/tinkerpop/blueprints/impls/tg/TinkerGraphFactory.java#TinkerGraphFactory.createTinkerGraph%28%29 – Ian Warburton Aug 28 '14 at 16:57
  • So I've got a javascript wrapper around an API that uses Java. – Ian Warburton Aug 28 '14 at 17:01

2 Answers2

6

To add to @mscdex valid answer.

This is JavaScript-flavored Gremlin code in Node.js using direct Java bindings via node-java.

Gremlin is not a language per se but a DSL. It is most of the time written in Groovy (because of its shortened syntax over Java), but it also exists in any JVM-compliant languages (ie. Java, Groovy, Scala, JavaScript via rhino and now nashorn with Java 8, to name a few). The full Groovy/Java API is accessible when typing Gremlin queries/scripts, which makes it a turing-complete "language".

I recommend reading http://gremlindocs.com/ and http://sql2gremlin.com for interesting beginner resources on Gremlin. http://www.tinkerpop.com/docs/3.0.0.M1/ will give you detailed information on TinkerPop and Gremlin (note: link will break as official v3.0 doc is released).

Because of the way node-java works and exposes Java methods (sync/async), you're required to use callbacks here in order to not block the event loop. This is a JavaScript concern and has nothing to do with Gremlin strictly speaking.

There a couple other clients which do not bind to the JVM directly but uses HTTP for TinkerPop 2.x (https://github.com/gulthor/grex for Node.js) or WebSocket for TinkerPop 3.0+ (https://github.com/gulthor/gremlin-client, for Node.JS/browsers, which will become the official TP3 JavaScript driver). Note: TinkerPop member / lib author here.

jbmusso
  • 3,436
  • 1
  • 25
  • 36
1

gremlin (a dependency of titan-node) uses node-java, which is a module that provides a bridge between node and Java. node-java allows you to import Java classes, instantiate Java data types, etc.

So what you're seeing is node-java importing a particular Java class because Gremlin is a Java/JVM thing.

mscdex
  • 104,356
  • 15
  • 192
  • 153