Remote Gremlin Script Web Console


 An additional feature has been added to the Remote OrientDB Graph Viewer application that is currently running on CloudFoundry.

Two exiting technologies are used to create the web console – dojox.gfx on the client side and Gremlin Groovy Script Engine on the server side that is completely written in JAVA.

The Gremlin-Groovy ScriptEngine is an implementation of the JAVA interface ScriptEngine. The principle behind the new ScriptEngine is Groovy sits on top of JAVA while Gremlin sits on top of Groovy. This way the Gremlin script can be executed in JAVA and parameters can be passed from JAVA to Gremlin and vice versa.

Here a simple code snipped:

OrientGraph graph = new OrientGraph(OGraphDatabase );
OGremlinHelper.global().create();
ScriptEngine engine = new GremlinGroovyScriptEngineFactory().getScriptEngine();
engine.getBindings(ScriptContext.ENGINE_SCOPE).put(“g“, graph);
List<Object> results = new ArrayList<Object>();
engine.eval(<any Gremlin script something like x=g.V>);
String obj = engine.get(“x“) + “”; // for single result
engine.getBindings(ScriptContext.ENGINE_SCOPE).put(“result”, results); // for list result
 

As highlighted in orange the JAVA object OrientGraph gets passed to Gremlin via ENGINE_SCOPE and gets bound to the variable g. Hence, the variable for the graph object in Gremlin hast to be g as well. The examples are shown below.

The same binding applies to the JAVA List object results, highlighted in green. If the script returns a list, for example g.V.fill(result), then the Gremlin list of the vertexes result will get passed to the JAVA list results.

Single return values of the Gremlin script, for example x= g.v(‘7:0′), can be directly read from the ScriptEngine via get() as highlighted in red. For convenience these pre and suffixes “x=” and “.fill(result)” respectively will be added automatically on the server side so that the user doesn’t have to type them for every Gremlin command. Unfortunately the following script for a single line command does not work:

Vertex v1 = g.addVertex(null);
v1.setProperty(“name”,”Katherine”);
v1.setProperty(“age”,12);
Vertex v2 = g.addVertex(null);
v2.setProperty(“name”,”Tom”);
v2.setProperty(“age”,9);
Edge e1 = g.addEdge(v1, v2, ‘knows’);
 

Instead the syntax will be like that:

Gremlin>g.addVertex(null); 
Gremlin>v[#6:-2]
Gremlin>g.addVertex(null); 
Gremlin>v[#6:-3]
Gremlin>g.addEdge(g.v(‘6:2′),g.v(‘6:3′),’knows’)
Gremlin>e[#7:0][#6:2-knows->#6:3]
 

Gremlin Script Web Console

As mentioned before the web console is basically a dojox.gfx canvas from the dojox.gfx API. All text elements are drawn on the canvas. This way the developer has complete control of all elements including the caret (cursor). Also the dojox library works beautifully across all browsers. HTML5 browsers will render the canvas as a HTML5 canvas.

NOTE: The Web Console is still in BETA. Many functions like moving caret to the left don’t work yet. Another blog will be posted on the web console only when a GA version is released.

Visit & like my FaceBook page

The first remote OrientDB Graph Viewer


When I first played around with OrientDB I was not convinced whether I would find a use for this kind of database. After all I got used to MongoDB, which was my preferred NOSQL database.

To that day I didn’t know the huge potential of  the data graph model. The Vertex and Edge model is a type of a database schema where relationships are made with connections between the records.

In this blog I don’t want to go deeper what a graph database is. For more information have a look at the TINKERPOP wiki page.

The OrientDB install comes with the database application, a web server and a OrientDB Studio application that can be accessed via IP of the running OrientDB server and the port 2480 (or otherwise specified in the orientdb-server-config XML file in the config folder). One should also define a OrientDB user other than ROOT, which can also be set in the same file under the <users> tag.

For more information see HERE.

The studio application is a powerful tool were one can create, alter and delete databases, vertexes, edges and relationships using normal SQL or Gremlin language.

OrienDB Studio is written entirely in Javascript using the JQuery library. This is the only downfall of this application as it MUST run on the same server as the database.

Cross-site scripting (XSS) is not allowed!

Using another OrienDB instance on a different server and trying to remotely log into the target server leads to no success. This is called Cross-site scripting and requires a hell of a hack of coding to bypass the browser security. It is not recommended to do so as your and the remote computer would be vulnerable to other attackers.

Hence, the only solution is to re-use part of the Studio code and create a viewer from scratch.

Try out the app running on CloudFoundry

Orient Remote Viewer

 

Also please like my FaceBook page:

MongoDB, GridFS & Spring – File Upload & Download (Pure JAVA example)


GridFS

The best feature of MongoDB is the MongoDB file system GridFS. GridFS goes with the same key-value concept of MongoDB but has the ability to store files. How that all works please read here:

GridFS Specification

What I want to demonstrate with this post, is how easy it is to actually store (upload) files to GridFS and stream them back (download) to the client. As usual I’m using an ordinary Dynamic Web Project from the SpringSource Tool Suite (STS) without using JAVA beans. To instantiate MongoOperations see my last post:

MongoDB, GridFS & Spring – File Upload & Download (Pure JAVA example)

Here the code snippets for file upload to MongoDB GridFS:

 
File anyFile = new File();
String fileName = anyFile.getName();
GridFS gfsFileType = new GridFS(, );
GridFSInputFile gfsFile = gfsFileType.createFile(anyFile);
gfsFile.setFilename(fileName);
gfsFile.save();
 
and the code snippet for downloading a GridFSDBFile back to the client:
 
GridFS gfsFileType = new GridFS(, );
GridFSDBFile fileOut = gfsFile.findOne(new ObjectId());
response.setContentType();
InputStream is = fileOut.getInputStream();
int read = 0;
byte[] bytes = new byte[1024];
OutputStream os = response.getOutputStream();
while((read = is.read(bytes)) != -1) {
os.write(bytes, 0, read);
}
os.flush();
os.close();

To see how this upload/download application works in the cloud click here dittmarconsulting-mongodb.cloudfoundry.com

MongoDB, Spring data, & Cloud Foundry – The pure JAVA approach (No JAVA Beans)


Cloud Foundry Environment

Although NoSQL databases (Not only SQL) won’t replace traditional relational database management systems (RDBMS), in some instances normal RDBMS would not ab able to cope with huge data volumes. NoSQL databases rose alongside traditional DB’s and got used by major Internet companies like Google, Amazon, Facebook and Twitter.

To test Cloud Foundry (CL) I have chosen the WaveMaker (WM) framework, which is also a VMware®  product.

Although I have good experience in the JAVA framework, the Spring Bean approach is still a bit foreign to me. Hence, to develop a MongoDB (MDB) test application that can be deployed on CL, I have decided to develop an application that uses purely JAVA packages. Here the list of all packages:

  • cloudfoundry-runtime-0.8.0.jar
  • mongo-2.7.2.jar
  • mongo-java-driver-2.5.3.jar
  • spring-data-commons-core-1.2.0.M1.jar
  • spring-data-mongodb-1.0.0.M4.jar
  • spring-aop-3.0.6.RELEASE.jar
  • spring-asm-3.0.5.RELEASE.jar
  • spring-context-3I.0.5.RELEASE.jar
  • spring-core-3.0.5.RELEASE.jar

To run the app locally, MongoDB need to be installed on the local machine first. Once running MDB can be started with the command from the command line “mongod“.

To bind the local MDB service with the class “MongoOperations” following line of code is sufficient:

MongoOperations mongoOperation = new MongoTemplate(
  new SimpleMongoDbFactory(new Mongo(), “<yourDB>”));
 

Obviously this line would not work in the Cloud Foundry environment. Why? Because the services on CL are all provisioned and can’t be created. It is also NOT possible to SSH into the cloud. The only possibility is to mimic the local command line database access via “vmc tunnel“.

Coming back to our problem to create “MongoOperations” for the cloud and not using BEANS. The following line does the trick:

MongoOperations mongoOperation = new MongoTemplate(
  new MongoServiceCreator(
    new CloudEnvironment()).createSingletonService().service);
 

Instead of creating a new DB instance, this line just creates a new Mongo service. Later this service can be bind to the provisioned service of CL.

To see how this simple CRUD application works in the cloud click here dittmarconsulting-mongodb.cloudfoundry.com

If you like this blog to to the application and click on the Facebook “Like” button.

Tom

Dittmar Consulting


Follow

Get every new post delivered to your Inbox.