BookmarkSubscribeRSS Feed

SAS Viya CAS REST API: How to fetch a table?

Started ‎10-31-2019 by
Modified ‎10-31-2019 by
Views 7,952

A SAS customer recently approached me with a data management question. They are using SAS Viya among many other popular commercial and open-source data science and reporting tools. The question was in the line of:

 

“We have spent a lot of time and effort preparing good quality data sets on SAS Viya. At the same time, we have a open source developers and reporting users who duplicate the effort of creating very similar data sets with their tools. Is there any common API to share this data between the platforms?”

 

Knowing they are open to the concept of APIs, I started thinking SAS Viya offers a rich set of functionality via it's REST APIs and that would easily fulfill the need for one common API. In SAS Viya there are dedicated APIs for R and Python available, but use their bespoke integration libraries called SWAT (SAS Scripting Wrapper for Analytics Transfer). However, using a REST API would answer their need for sharing their data in a common format, which typically is JSON.

 

If a REST API is not open to the whole wide world, it requires authentication before allowing user to perform any operations. In SAS Viya's case this involves a need to 1) register the client and 2) obtain an access token. Best way for me to learn is to follow a good example, so I turned to this excellent SAS blog on authentication.

 

The details are all there, so I'm not deep diving there in this blog. After mastering those steps, you are ready to make your SAS Viya Cloud Analytic Services (CAS) API calls. I used cURL for experimenting with the REST API. There are many other API testing tools as well, such as Postman that seems more user friendly and is likely to save some of the copy-paste work compared to working with cURL from the command line.

 

Getting back to the original question on how my customer was able to share their data, I investigated the long list of available CAS actions. As I needed one action that simply returns all the rows from a given table in JSON format, a SAS Viya CAS action called table.fetch was the a match. If you’re new to SAS Viya, a CAS action is the smallest unit of work for the CAS server. Actions can load data, transform data, compute statistics, perform analytics, create output and many other purposes. You configure each action by specifying a set of input parameters.  

 

Before I can call my CAS action, I need to create a CAS session and know the session ID. As it happens, there exists a superb SAS blog describing the needed steps.

 

To make things short, there are two steps involved:

 

  1. Create a CAS session, I use <mytoken> to refer to the access token acquired in the previous step:

 

curl -X POST http://yourserver:8777/cas/sessions \
         -H 'Authorization: Bearer <mytoken>'

 

       This will return the session ID that I'm calling <mysession> in the next step. It should look like:

 

      {
        "session": "16dd9ee7-3189-1e40-8ba7-934a4a257fd7"
      }

 

 

  1. Fetch the table:

      

curl --data-binary "@body.json" -X POST http://yourserver:8777/cas/<mysession>/actions/table.fetch/ \
          -H "Authorization: Bearer <mytoken>" \
          -H "Accept: application/json" \
          -H "Content-Type: application/json"

 

 

I used an external JSON file I named body.json to describe the payload i.e. the parameters to provide for the table.fetch action.

 

My body.json contains only the parameters "table", "to" and "maxRows":

    

     {
       "table":"<tablename>",
       "to":"<last row to return>",
       "maxRows":"<maximum number of returned rows>"
     }

 

 

The REST APIs and CAS REST APIs in SAS Viya are very powerful for extending analytics services and data to external applications. In my opinion, re-usability is the key: create once and call from whatever application you need. In my example, the customer already encountered the trouble of perfecting their data once, so it makes good sense to extend this data for use with other applications where needed.

 

I recommend checking out what REST APIs on SAS Viya can offer you. You might be surprised how easy it is once you past the somewhat complex looking syntax of REST calls.

Version history
Last update:
‎10-31-2019 08:13 AM
Updated by:
Contributors

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!

Free course: Data Literacy Essentials

Data Literacy is for all, even absolute beginners. Jump on board with this free e-learning  and boost your career prospects.

Get Started

Article Tags