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:
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 pandas as pd
pp = pprint.PrettyPrinter(indent=4)
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.
ret = requests.get(baseurl+myaction,headers=head,auth=(u,p))
print("The request is: ",baseurl+myaction)
print("Request status is: ", ret.status_code)
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.
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"] )
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:
The possibilities for custom application development, and integration of SAS and CAS with other customer systems and processes seems endless.