Navigating through a sea of data assets can be a daunting task. SAS Information Catalog is your navigator in this journey, allowing you to discover, search, and manage your SAS Viya assets efficiently. When a data asset is discovered, hundreds of metrics are calculated. Imagine having the ability to upload these metrics to a CAS table, using a Catalog API. This opens up the possibility of using these rich metrics in custom reports or flows.
When utilizing the SAS Information Catalog REST API to upload metadata and metrics for data assets, tables, or files, you have several options to choose from. The metadata can be of various types, such as:
The middle level, dataDictionaryAndProfile, strikes an excellent balance between richness and complexity, making it the ideal choice if you need to identify private data, semantic type or classification at a column level.
For information on obtaining a SAS Viya access token, refer to the previous post Discover Your Data with SAS Information Catalog APIs from Python – Access.
The Python program upload_metadata.py retrieves metrics and metadata from SAS Information Catalog. It then uploads the metrics and metadata to a CAS table, using the Catalog REST API. The program:
Here’s the complete code:
# 1 Packages
import sys
import requests
import json
import os
# 2. Arguments
print ("Number of arguments:", len(sys.argv), "arguments")
print ("Argument List:", str(sys.argv) + '\n')
baseURL=str(sys.argv[1])
search_query=str(sys.argv[2])
pem_path=str(sys.argv[3])
# 3. Construct Variables
print ("REST API Inputs:\n")
print('\nYour SAS VIYA host is ', baseURL)
url = f'{baseURL}/catalog/instances' + search_query
print('\nCatalog API URL: ', url)
# 4. Get the Saved Access Token
print('\nRetrieving the saved token from api/access_token.txt\n')
with open("api/access_token.txt", "r", encoding="UTF-8") as f:
token = f.read()
#print(token)
print('\nUpload metadata specified in the URL\n')
"""
# Select one of the following - passed as a parameter
# Upload profile Metrics filtered by name with prefix
url = f"{baseURL}/catalog/instances/?filter=startsWith(name,'WATER')&?filter=contains(type,cas)&level=dataDictionaryAndProfile&limit=10"
# Upload detailedMetrics filtered by name with prefix
# url = f"{baseURL}/catalog/instances/?filter=contains(type,cas)&level=detailedMetrics&limit=10"
# Upload profile Metrics filtered by name with prefix
# url = f"{baseURL}/catalog/instances/?filter=contains(type,cas)&level=dataDictionary&prefix=simpleUpload&limit=100"
"""
headers = { 'Authorization': 'Bearer ' + token,'Content-Type': 'application/vnd.sas.metadata.instance.upload.request+json', 'Accept': 'application/vnd.sas.metadata.instance.upload.request+json'}
# Replace placeholders in the body with actual values
data = '''{
"level": "dataDictionaryAndProfile",
"prefix": "Catalog",
"dateTimeStampSuffix": false,
"serverName": "cas-shared-default",
"caslibName": "Public"
}'''
response = requests.post(url, headers=headers, data=data, verify=pem_path)
print('Response code: ', response.status_code)
print('\nThe Catalog Metadata was uploaded for you in CAS in Public.Catalog_DMDictionaryPlusMetrics \n')
print(response.text)
This program demonstrates how to use the SAS Viya Catalog REST API to perform a metadata upload.
The program expects the following command-line arguments:
In a Bash terminal on a Windows machine, you can run the program with command-line arguments:
# Certificate on a Windows machine and executable is Python
python upload_metadata.py https://sas_viya_url "?filter=startsWith(name,'WATER')&?filter=contains(type,cas)&level=dataDictionaryAndProfile&limit=10" "C:\\Users\\myuser\Downloads\\gelenv_trustedcerts.pem"
The filter uploads only metadata for data assets:
Running the program with the provided parameters will upload the metadata in an in-memory CAS table. In the request body, you can specify the CAS server, the caslib name, 'Public', and a CAS table prefix for the in-memory table, 'Catalog', in this example.
data = '''{
"level": "dataDictionaryAndProfile",
"prefix": "Catalog",
"dateTimeStampSuffix": false,
"serverName": "cas-shared-default",
"caslibName": "Public"
}'''
When running the program, a SAS Viya certificate is used, in the form of a PEM file. The PEM file was copied in the 'C:\Users\...\' folder.
In a Bash terminal on a Linux machine, the run statement would be:
# Certificate on a Linux machine and executable is python3
python3 download_metadata.py https://sas_viya_url "?filter=startsWith(name,'WATER')&?filter=contains(type,cas)&level=dataDictionaryAndProfile&limit=10" /home/cloud-user/.certs/gelenv_trustedcerts.pem
A SAS Viya certificate is used here, in the form of a PEM file. The PEM file is assumed to be present in the '/home/myuser/' folder.
You will see many metrics both at a table and at a column level, such as:
Select any image to see a larger version.
Mobile users: To view the images, select the "Full" version at the bottom of the page.
The table metadata, such as keywords, tags, most important columns, privacy and so on, is repeated for each column.
This program demonstrated how to use the SAS Viya REST API to upload in CAS a table, containing SAS Information Catalog metrics.
You might find the following resources helpful.
Read:
Watch:
Thank you for your time reading this post. If you liked the post, give it a thumbs up! Please comment and tell us what you think about having conversations with your data. If you wish to get more information, please write me an email.
Find more articles from SAS Global Enablement and Learning here.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
Data Literacy is for all, even absolute beginners. Jump on board with this free e-learning and boost your career prospects.