Loading

try {
  http:get("/blog/60028721581/zorba-28--the-nosql-edition")
} catch http:not-found {
  <p>
    404 NOT FOUND.
    Take me home 
  </p>
}
~
~
-- INSERT -- All 6, 17

Zorba 2.8 - The (No)SQL Edition

Posted 12 months ago

New Database Drivers

Last summer, we released the first JSONiq implementationJSONiq extends the XQuery data model with JSON objects and arrays. It enables developers to leverage the power and expressiveness of XQuery in both JSON stores and relational databases. Moreover, JSONiq is a unified query language that you can run accross multiple NoSQL stores. It also brings advanced query processing features in this space: joins, aggregation, windows, full-text search, collation support, powerful date/time arithmetic, and more. Hermes contains four new databases drivers in order to help you process your data:

Couchbase

Couchbase is a key-value database management system optimized for storing unstructured data. In the code snippet below, we connect to a couchbase instance at localhost:8091 and create a view on the bucket where the key of the map is the name of the state and the value is the population. For each row in the document, we group them by state and compute the total population for the state. Finally, we return the list of states with their total population.

import module namespace couchbase = "http://www.zorba-xquery.com/modules/couchbase";

variable $con := couchbase:connect({
"host": "localhost:8091",
"username" : null,
"password" : null,
"bucket" : "default"
});

variable $view-name := couchbase:create-view($con, "zip", "zip", {"key" : "doc.state", "values" : "doc.pop"});
variable $data := couchbase:view($con, $view-name);

for $d in $data("rows") ! jn:members(.)
group by $state := $d("key")
let $population := sum($d("value"))
return { "state" : $state, "population" : $population }

Oracle NoSQL

Oracle NoSQL Database is a distributed key-value database. It is built upon the proven Oracle Berkeley DB Java Edition high-availability storage engine. In the code snippet below, we connect to an Oracle NoSQL instance and run the same aggregation query than above. The Oracle NoSQL module has advanced functions that allow to return multiple objects at once. For example, in order to retrieve all zip documents from the database as JSON, the multi-get-json function can be used as follows:

import module namespace nosql = "http://www.zorba-xquery.com/modules/oracle-nosqldb";

let $con := nosql:connect({ "store-name" : "kvstore", "helper-host-ports" : ["localhost:5000"] })
for $zip in nosql:multi-get-json($db, { "major" : "zips" }, {"prefix": ""},
$nosql:depth-PARENT_AND_DESCENDANTS,
$nosql:direction-FORWARD)("value")
group by $state := $zip("state")
let $pop := sum($zip("pop"))
return { "state": $state, "population" : $pop }

SQLite

SQLite is a self-contained, serverless, zero-configuration, transactional SQL database engine. In the code snippet below, we connect to a database located at small.db and get all fields from all rows in maintable.

import module namespace s = "http://www.zorba-xquery.com/modules/sqlite";

let $con := s:connect("small.db")
return s:execute-query($con, "select * from maintable");

JDBC

The JDBC modules enables to query any JDBC enabled database (there are quite a few ;-)). In the code snippet below, we connect to a MySQL database running at localhost:3307, and get all fields from all rows in maintable

import module namespace jdbc = "http://www.zorba-xquery.com/modules/jdbc";

variable $con := jdbc:connect({
"url": "jdbc:mysql://localhost:3307/",
"user" : "root",
"password" : ""
});

jdbc:execute-query($con, "select * from maintable")

Info Extraction

Hermes introduces a new info extraction module, providing data extraction functions that return the entities, relations, categories and concepts present in a given text, with basis on Yahoo’s Content Analysis webservice.

In the example below, we extract the concepts available from a random newpaper headline:

import module namespace ex = "http://www.zorba-xquery.com/modules/info-extraction";

ex:entities("President Obama called Wednesday on Congress to extend a tax break for students.")

This query will return (you can also try it live):

<?xml version="1.0" encoding="UTF-8"?>
<ex:entity xmlns:ex="http://www.zorba-xquery.com/modules/info-extraction" start="0" end="14">
President Obama
</ex:entity>
<ex:entity xmlns:ex="http://www.zorba-xquery.com/modules/info-extraction" start="36" end="43">
<ex:type>organization</ex:type>Congress
</ex:entity>

See you at XMLPrague?

It’s that time of the year. XMLPrague is one of the best conferences in the XML circuit and this year’s edition will be no exception.

Three talks will be given by the Zorba team. Luis Rodriguez will get the show started with XQuery meets SQL and tell us everything about querying relational databases using JSONiq. Then William Candillon, will talk about XQuery Development in the Cloud(9) and introduce a new browser-based toolkit that relies on static code analysis. Finally, Juan Zacarias will bring NoSQL Datastores into an XQuery Playground and showcase how developers can leverage the power of JSONiq in the NoSQL space.

That’s all for now

We hope that you will take Zorba 2.8 for a ride and we are looking forward to your feedback.