We’re smarter together. Learn from this collection of community knowledge and add your expertise.

This snake is having a REST

by SAS Employee GerryNelson on ‎06-28-2017 06:44 PM (702 Views)

Open interfaces are one of the great new features in SAS VIYA.  The open interfaces provided in 16w38 are Python, Java and Lua.  Python and Lua require a wrapper package, SWAT(SAS Scripting Wrapper Analytic Transfer), in order to talk to CAS. Java requires jars that provide the CAS client and CAS actions classes to interact with CAS. The CAS REST API provides additional open access to a CAS Server. In this blog I will use native python and the CAS REST API to run a process in CAS. 

I have been writing SAS code for a long time (I am not saying how long). About a year ago, just for fun, I took an online Python class. I have to say I am a convert. I am sure my Python code is still pretty noobish, however, I really do find it quicker to achieve a programming task in python than the same task would have been in datastep or SCL. One of the great benefits of python is that there are many packages which provide great functionality.

In the example I will:

  • call the CAS REST API
  • view and process the results of the API call
  • print a report.


Python has three packages that make it easy call the REST API and display the results:

requests is used to make the REST API call which is just an http request.  Requests is described as "an elegant and simple HTTP library for Python, built for human beings" (You should read the doc, it is quite amusing).

json to convert the json API call results to a python dictionary

pandas to do some data processing. Pandas "is an open source, BSD-licensed library providing high-performance, easy-to-use data structures and data analysis tools for the Python programming language".

Let's look at a simple example where we will retrieve the memory metrics for each machine in our CAS cluster, calculate the percentage memory used, and write an alert message based on the calculation.

Step 1) Import the packages we need and set some variables like the baseurl for the CAS REST API.



import json
import requests

import pandas as pd

import pprint
pp = pprint.PrettyPrinter(indent=4)
baseurl="http://gatekrbhdp01.gatehadoop.com:8777"
u='cas'
p='lnxsas'
head= {'Content-type':'application/json',
'Accept':'application/json'
}
 


Step 2)
Make a REST call to return the memory metrics on all machines in the CAS cluster. Read the JSON results into a python dictionary and print the CAS REST API request, status and the results.

myaction="/cas/nodes/memoryMetrics"
ret = requests.get(baseurl+myaction,headers=head,auth=(u,p))
print("The request is: ",baseurl+myaction)
print("Request status is: ", ret.status_code)
result=ret.json()
pp.pprint(result)

 

gn_pythonrest1.png


Step 3) Load the result dictionary into a pandas dataframe. Process the data frame to calculate a new column from the result. The new column is the percent of free memory on each node. Create a table with only two columns and fetch the rows to check the result.

dtable=pd.DataFrame(result)
dtable['pctfree']=dtable["MemFree_kB"]/dtable["MemTotal_kB"]*100
newtable=dtable[['name','pctfree']].head()
newtable

gn_pythonrest2.png


Step 4)
Process the rows of the table and write a message when the percentage free memory reaches a threshold. Instead of writing a message this could obviously send an email, text etc. to alert an administrator.  

for index, row in newtable.iterrows():
    if row["pctfree"] < 65 :
       print("WARNING: Using too much memory on ", row["name"] )

 

gn_pythonrest3.png


This is obviously a simple example. We can take this a step further and access the full power of CAS from the REST API. Rather than make the article much longer I have attached a documented Jupyter Notebook which contains a more complete example that:

  1. Starts a CAS session.
  2. Creates a function in Python that can call a CAS action and display the result in a pandas dataframe
  3. Uses the function to make multiple CAS REST API calls and view the results.

 

The possibilities for custom application development, and integration of SAS and CAS with other customer systems and processes seems endless.

Contributors
Your turn
Sign In!

Want to write an article? Sign in with your profile.