Skip to Content

/wp-content/uploads/2015/10/d_shop_blog_logo_818001.jpg

If you are an SAP Employee, please follow us on Jam.


Clojure is a dynamic programming language that targets the Java Virtual Machine. It is designed to be a general-purpose language, combining the approachability and interactive development of a scripting language with an efficient and robust infrastructure for multi-threaded programming.


Clojure_Logo.png


Also…Clojure is a dialect of Lisp, and shares with Lisp the code-as-data philosophy and a powerful macro system.

So, before we move on…I want to share an XKCD comic that I really like about Lisp 🙂 And you will realize why after you see my source code -;)

Lisp_XKCD.jpg

So…no example or demonstration would be complete if we didn’t hook it up with SAP HANA, right? So…let’s go and do it 😉

First, we need to create a Calculation View and call it “FLIGHTS_BY_CARRIER”. It will be composed of two tables, SCARR and SFLIGHT.

First, we need to create a Join object and link the table by MANDT and CARRID. From here select the following fields as output MANDT, CARRID, CARRNAME, PRICE and CURRENCY.

Then create an Aggregation object selecting the fields CARRNAME, PRICE (As Aggregated Column) and CURRENCY. Filter the CURRENCY field by ‘USD’.

Then create a Projection object and select only PRICE and CARRNAME.


On the Semantics object make sure to select “CROSS CLIENT” as the Default Client.

Calculation_View.jpg

Now, switch to the SAP HANA Development View and create a new repository. Call it “Flights”.

Create a new “XS Engine” project and call it “Flights” as well. Link it to the “Flights” repository.

Create an empty “.xsapp” file.

Create a file called “.xsaccess” with the following code.

.xsaccess

{

“exposed” : true,

“authentication” : [ { “method” : “Basic” } ]

}

Finally create a file called “flights.xsodata” with the following code

flights.xsodata

service {

          “Blag/FLIGHTS_BY_CARRIER.calculationview” as “FLIGHTS” keys generate local “Id”;

}


Activate your project and launch it on your browser, you should see something like this…


XSProject.jpg

The SAP HANA part is done…so we can move into the Clojure part…

The best and easiest way to use Clojure in to install Leiningen, you can find the installation process here


Once installed, simply create a project like this…

Create Project

lein new clojure_hana

cd clojure_hana

nano project.clj

And modify it to looks like this…

project.clj

(defproject clojure_hana “0.1.0-SNAPSHOT”

  :description “FIXME: write description”

  :url “http://example.com/FIXME

  :license {:name “Eclipse Public License”

            :url “http://www.eclipse.org/legal/epl-v10.html“}

  :dependencies [[org.clojure/clojure “1.6.0”]

[clj-http “1.1.0”]

[cheshire “5.4.0”]]

:main clojure-hana.core)


This will download and install the clj-http (To deal with web pages) and chesire (To deal with JSon) libraries into your project.

To code for Clojure, you can use any editor that you like…


Inside your clojure_hana folder, do the following…

Create source file

cd src

cd clojure_hana

nano core.clj

Copy and paste the following code…

core.clj

(ns clojure-hana.core

                (:require [clj-http.client :as client])

                (:require [cheshire.core :refer :all]))

(defn show_info [results len ctr]

                (cond

                                (or (= ctr len) (< ctr len))

                                                                (concat ((results ctr) :CARRNAME) ” : ” ((results ctr) :PRICE) “\n”

                                                                (show_info results len (inc ctr)))

                                (> ctr len)

                                                (concat “” “\n”)))

(defn -main

  “Reads information from SAP HANA OData”

  [& args]

                (let [json (:body (client/get “http://YourHANAServer:8000/Flights/flights.xsodata/FLIGHTS?$format=json” {:basic-auth [“YourUser” “YourPassword”]}))

                                  parsed_json (parse-string json true)

                                  results ((parsed_json :d) :results)]

                   (do(print(apply str(show_info results (dec (count results)) 0)) (symbol “”)))))

Now you see it…Clojure is all about parenthesis, just like good old Lisp 🙂 Anyway…to run the project simply go back to your main clojure_hana folder and run

lein run

Clojure_HANA.jpgI have been interfacing SAP HANA OData with a lot of languages…and so far…Racket was one of my fastest implementations…but I have to say…Clojure was even faster to implement and the code is even shorter 😛 …while it’s hard to get used to all those weird parenthesis…it’s a very nice and powerful language…and thing get done faster that one could imagine…

To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply