Integrate SAS
with Java
Getting started with SAS Viya for Java
The SAS Java Client is an interface for SAS Cloud Analytic Services (CAS) which is the centerpiece of the SAS Viya framework.
- write Java programs that load data into memory
- invoke CAS actions to transform, summarize, model and score the data
- Java classes are provided that correspond to each CAS action
Sample code
The example below shows how to use the SUMMARY ACTION to get various descriptive statistics (minimum and maximum values, mean, standard Deviation, etc.) about a data table using SAS Cloud Analytics Services. This example requires that you have in-memory data to analyze.
Get Descriptive Statistics
Run the Summary Action
Save as examples/SummaryExample.java, compile, and run the example using your preferred Java tools. You need the cas-client JAR file, ANTLR, and the Google protobuf JAR files in your CLASSPATH. The example uses the CAS host name "mycas.example.com" and port 10738; adjust these values to match your deployment.
/*--------------------------------------------------------------------------
* Copyright (C), 2016
* SAS Institute Inc., Cary, N.C. 27513, U.S.A. All rights reserved.
*-------------------------------------------------------------------------*/
package examples;
import com.sas.cas.CASActionResults;
import com.sas.cas.CASClient;
import com.sas.cas.CASClientInterface;
import com.sas.cas.CASValue;
import com.sas.cas.actions.Casouttablebasic;
import com.sas.cas.actions.Castable;
import com.sas.cas.actions.Csvopts;
import com.sas.cas.actions.Csvopts.FILETYPE;
import com.sas.cas.actions.simple.SummaryOptions;
import static com.sas.cas.actions.simple.SummaryOptions.SUBSET.*;
import com.sas.cas.actions.table.UploadOptions;
/**
* Sample application showing how to create a new CAS session, upload data, and run analytics.
*
*/
public class SummaryExample {
public static void main(String[] args) throws Exception {
CASClientInterface client = null;
try {
// Create a new CAS session to the given host/port.
// User credentials are being read from .authinfo
client = new CASClient("mycas.example.com", 10738);
// Create the options to upload data
UploadOptions uploadOptions = new UploadOptions();
// Tell CAS we're going to upload a CSV file
Csvopts csvOptions = new Csvopts();
csvOptions.setFileType(FILETYPE.CSV);
uploadOptions.setImportOptions(csvOptions);
// Tell CAS the name of the table to create
Casouttablebasic casout = new Casouttablebasic();
casout.setName("orsales");
uploadOptions.setCasOut(casout);
// Invoke the action
CASActionResults<CASValue> results = client.invoke(uploadOptions);
// Now run summary statistics on our newly loaded table
SummaryOptions summaryOptions = new SummaryOptions();
summaryOptions.setSummarySubset(
new SummaryOptions.SUBSET[]{MIN, MAX, MEAN, N, NMISS, STD, STDERR}
); // 1
// Set our options
Castable tableParms = new Castable(); // 2
tableParms.setName("orsales");
summaryOptions.setTable(tableParms);
// Invoke the action
CASActionResults<CASValue> summaryResults = client.invoke(summaryOptions);
// Print out the results
for (int i = 0; i < results.getResultsCount(); i++) {
System.out.println(results.getResult(i));
}
}
finally {
if (client != null) {
client.close();
}
}
}
- A subset of the available descriptive statistics is used with the SummaryOptions class.
- The Castable class is used to represent in-memory tables. In the program, only the table name is set, but you can set several parameters, such as those used in BY-group processing, filtering with a where parameter, and so on.
The program prints the descriptive statistics on standard outout:
{
Summary=Summary Descriptive Statistics for ORSALES
Analysis Variable Min Max N Number Missing Mean Std Dev. Std Error
------------------ ----- ---------- --- -------------- -------------- --------------- -------------
Year 1999 2002 912 0 2000.5 1.11864745 0.03704212
Quantity 10 9026 912 0 1465.08552632 1621.72304437 53.70061616
Profit 209.8 552970.51 912 0 64786.23735197 84128.37618423 2785.76891008
Total_Retail_Price 422.3 1159837.26 912 0 122090.6840625 166576.07510888 5515.88503487
4 rows
}