Two Languages Shockingly Easy to Use With MarkLogic

Two Languages Shockingly Easy to Use With MarkLogic

Posted on December 21, 2017 0 Comments
Developers looking at screen

As a MarkLogic consultant, my job is as often about managing MarkLogic or moving data in and out of MarkLogic as it is about the application with which the end-user interacts.  Here are two languages that don’t have large, formal MarkLogic toolkits like the DMSDK or Java Client API, but are easy to “plug in” to MarkLogic.  It’s possible to use these languages to build tools or scripts to simplify your MarkLogic work.

Why is it easy? MarkLogic is built to be standards compliant, interacting primarily over two REST APIs: a management API and a client API. Because these are standard REST APIs that can return JSON or XML responses, any language that can support REST will work with MarkLogic.  In 2017 that’s a pretty low bar, so almost every language is covered.

That said, some languages have particularly good support for XML and HTTP (including digest authentication for projects that don’t use basic authentication). Here are two of them, and my experience integrating each with MarkLogic in particular, and RESTful management and data access in general.

Using Go With MarkLogic

The Go language – sometimes called golang – is a language introduced by Google in 2007.  I enjoy it because Go is a language I can pretty much keep in my head all at once, kind of like when I first learned C.  It’s low level and about as fast as C.  Unlike C, Go has garbage collection, reflection, and built-in concurrency.  There is a rich tooling community around Go.  Where Go really shines is in the container space, where Go is to containers what C is to Unix.

To use Go with MarkLogic there’s only one dependency, and that’s only if you’re using the Digest authentication method.  I also created my own version to which I’ve added header support.  The code is fairly simple, for example, let’s get a list of all the hosts in a cluster:

package main

import (
    "io"
    "os"

    digest "github.com/paul-hoehne/go-http-digest-auth-client"
)

func main() {
    req := digest.NewRequest("admin", "kat", "GET", "http://192.168.56.201:8002/manage/v2/hosts?format=json", "")
    resp, _ := req.Execute()
    io.Copy(os.Stdout, resp.Body)
}

What makes Go attractive is that go programs can be run on the command line (using the “go run” command) or statically compiled into binaries with no runtime requirements. The binaries are easily cross-compiled to other architectures, supporting Windows, Linux x64, Linux ARM, and MacOS with little effort. Because Go has no runtime, is statically compiled, and relatively fast, it may become the language of choice for containers that include nothing but a minimal Linux userland.

Using Python With MarkLogic

Python is another language that integrates well with MarkLogic’s JSON API support.  The only dependency needed is the requests package.

Of course, MarkLogic plugs nicely into Python.  As an example, there is the MarkLogic Python management API.

import requests
from requests import auth

me = auth.HTTPDigestAuth("admin", "kat")
resp = requests.get("http://192.168.56.201:8002/manage/v2/hosts?format=json", auth=me)
print(resp.json())

Four lines of code!

Because the “json” method on the response from the requests package returns a Python dictionary, the data is immediately usable. Python is available for every computer architecture from a Raspberry Pi to a z/OS mainframe.  Because Python is everywhere, has an incredibly rich eco-system, is very approachable to even programming novices, is included with Linux, supported in Visual Studio for Windows, and is interpreted, it is a great choice for system administration and data movement tasks.

Another thing that makes Python so attractive is that it’s extremely popular in statistics and data science.  Libraries like SciPi, Pandas, Numpy, and Keras make machine learning with Python approachable to a wide number of people.  In the data science community, the Jupyter notebook tool based on Python is becoming ubiquitous.  In addition to data science and machine learning, Python has libraries for almost everything. As an interpreted language, there is no compile phase or management of executables separate from source code.

Other Languages That Work With MarkLogic

Java, Node, cURL, Ruby, C++ … The list of language and tools usable with MarkLogic is essentially limitless.  While Java and Node have specialized, idiomatic APIs into MarkLogic, those MarkLogic-supplied libraries actually call the same REST API that Go, Python, or other languages would use.  Plugging MarkLogic directly into your latest Python data science project or Rails application may be simpler, quicker, and easier than you think.

Paul Hoehne

View all posts from Paul Hoehne on the Progress blog. Connect with us about all things application development and deployment, data integration and digital business.

Comments

Comments are disabled in preview mode.
Topics

Sitefinity Training and Certification Now Available.

Let our experts teach you how to use Sitefinity's best-in-class features to deliver compelling digital experiences.

Learn More
Latest Stories
in Your Inbox

Subscribe to get all the news, info and tutorials you need to build better business apps and sites

Loading animation