background5-test.jpg
java-logo-test

Getting Started with SAS® Viya for Java 

The SAS Java Client interface for Viya provides a Java interface to SAS Cloud Analytic Services (CAS) which is the centerpiece of the SAS Viya framework. With this software, you can write Java programs that load data into memory and invoke CAS actions to transform, summarize, model and score the data. Classes are provided that correspond to each CAS action and you can use Java to post process CAS result tables.

 

Documentation

Getting Started with SAS® Viya for Java
Learn the concepts and capabilities of the Java API for SAS Viya.

Java API reference
View the API reference (Javadoc) for the Java API for SAS Viya.

 


Examples

Select a sample below and copy and paste into your application.

Get Descriptive Statistics

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.

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();
    }
  }
  
}

  1. A subset of the available descriptive statistics is used with the SummaryOptions class.
  2. 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

}
SAS Viya logo

One underlying code base, programmable in the language of your choice.

Learn more about SAS Viya

SAS Viya logo

Engage in the discussions happening around the power of SAS® Viyaand discover what it can do for you.

Join our Coding on SAS Viya Community

Back to Top