greetings from the edge: using javaobj in data step richard a. devenezia

31
Greetings from the Edge: Using javaobj in DATA Step Richard A. DeVenezia

Upload: dwight-hart

Post on 23-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Greetings from the Edge: Using javaobj in DATA Step Richard A. DeVenezia

Greetings from the Edge:Using javaobj in DATA Step

Richard A. DeVenezia

Page 2: Greetings from the Edge: Using javaobj in DATA Step Richard A. DeVenezia

Java is ...

• java.sun.com“The Java 2 Platform provides robust end-to-end solutions for networked applications as well as a trusted standard for embedded applications”

• Widespread

• Networked

• Applications– Analysis and management

Page 3: Greetings from the Edge: Using javaobj in DATA Step Richard A. DeVenezia

javaobj is ...

• DATA Step

• Experimental

• Part of broader initiative– Component Interface– Object Dot Syntax

Page 4: Greetings from the Edge: Using javaobj in DATA Step Richard A. DeVenezia

Component Interface is ...

• support.sas.com/rnd/... “DATA Step Component Interface provides a mechanism for accessing predefined component objects from within a DATA Step program.”

• Instantiate– Create an object– declare Type var, var = _new_

• Access– var.method()

Page 5: Greetings from the Edge: Using javaobj in DATA Step Richard A. DeVenezia

Java development

• Free kit– java.sun.com/j2se– compiler, packager and loader

javac, jar, java– Runtime Environment– Online training

• Tutorials– numerous web sites and publications

Page 6: Greetings from the Edge: Using javaobj in DATA Step Richard A. DeVenezia

SAS session

• CLASSPATH– Location of Java classes

• Configuration options– Host preset environment variable– Command line-set CLASSPATH myPath;%CLASSPATH%

– configuration file (sasv9.cfg)-set CLASSPATH “myPath;%CLASSPATH%”

Page 7: Greetings from the Edge: Using javaobj in DATA Step Richard A. DeVenezia

Java coding pattern

public class Class{private double variable;public double getVariable(){

return this.variable;}public void setVariable(double variable){

this.variable = variable;}

}

Page 8: Greetings from the Edge: Using javaobj in DATA Step Richard A. DeVenezia

Declare statement

declare javaobj var;

var = _NEW_ javaobj

('Class' [,arg-1[,…[,arg-N]]) ;

- or -

declare javaobjvar ('Class' [,arg-1[,…[,arg-N]]) ;

Page 9: Greetings from the Edge: Using javaobj in DATA Step Richard A. DeVenezia

Signature

public void setX (double X) { this.X = X }public void setX (String X) { try { this.X = Double.parseDouble(X); } catch (Exception e) { this.X = Double.Nan; }}

• Pattern of argument types

• Correspondence

• DATA Step ERROR: ?

Page 10: Greetings from the Edge: Using javaobj in DATA Step Richard A. DeVenezia

Accessing methods and fields

• public Type Method (args…) {…}

• obj.callTypeMethod ( ‘Method’, args…, return );

• fields:

obj.[get|set]TypeField ( ‘field’, value );

Page 11: Greetings from the Edge: Using javaobj in DATA Step Richard A. DeVenezia

HelloSAS.java

public class HelloSAS {public HelloSAS () {}

public String getMessage () { return "Hello SAS";}

}

Page 12: Greetings from the Edge: Using javaobj in DATA Step Richard A. DeVenezia

HelloSAS.sas

data _null_;

declare javaobj j (’HelloSAS');

length message $200;

j.callStringMethod ('getMessage', message);

put message=;

run;

--- log --

message=Hello SAS

Page 13: Greetings from the Edge: Using javaobj in DATA Step Richard A. DeVenezia

Gotchas

• Classes are cached per SAS session

• Java class recompiled ?– Restart SAS

• Signature Types– pass double, String, Object (new)– return double, String

Page 14: Greetings from the Edge: Using javaobj in DATA Step Richard A. DeVenezia

Example 8 - Enumeration

import java.util.Enumeration;

public class Example8 { private Enumeration e; public Example8 () { e = System.getProperties().propertyNames(); } public String getProperty () { if (e.hasMoreElements()) { String p = (String) e.nextElement(); return p + "=" + System.getProperty(p); } else { return null; } }}

Page 15: Greetings from the Edge: Using javaobj in DATA Step Richard A. DeVenezia

Example 8 - DATA Step

data _null_; dcl javaobj j ('Example8'); length s $200; j.callStringMethod ('getProperty', s); do while (s ne ''); put s; j.callStringMethod ('getProperty', s); end;run;--- log ---

java.vm.version=1.4.1_01-b01

java.vm.vendor=Sun Microsystems Inc.

Page 16: Greetings from the Edge: Using javaobj in DATA Step Richard A. DeVenezia

Object Persistence

• No– javaobj gone when DATA Step ends– obj.delete() recommended

• Birdie:“An instantiated javaobj creates a JNI reference

which will not be garbage collected. The reference needs to be explicitly deleted.”

Page 17: Greetings from the Edge: Using javaobj in DATA Step Richard A. DeVenezia

A Case for Persistence

• Creating SAS Table from Query

• Query -> ResultSet

• Requires Two SAS Passes– ResultSetMetaData– Read ResultsSet into Data Set

• Same Query for each pass– not wanted

Page 18: Greetings from the Edge: Using javaobj in DATA Step Richard A. DeVenezia

Databases

• Commercial

• Open source– Postgresql, mySQL– communities of devoted developers and users

• JDBC– 109 drivers– trademark– not an acronym (just like ess-a-ess)

Page 19: Greetings from the Edge: Using javaobj in DATA Step Richard A. DeVenezia

Persistence == RMI

• Objects can persist outside SAS– in RMI server process

• Obtaining reference and access – requires Java wrapper class

Page 20: Greetings from the Edge: Using javaobj in DATA Step Richard A. DeVenezia

Gateway - an RMI Scheme

• Server

• DATA Step is client of server– via a wrapper

• Server allocates resources– Returns handles– Methods

Page 21: Greetings from the Edge: Using javaobj in DATA Step Richard A. DeVenezia

Gateway Implementation

• Three Classes

• An Interface– GatewayInterface

• An Implementation– GatewayManager

• A Server– GatewayServer

Page 22: Greetings from the Edge: Using javaobj in DATA Step Richard A. DeVenezia

GatewayInterface

• Declares methods

public int getConnectionHandle (String driverClass, String databaseURL,String username, String password )

throws RemoteException;

public int getStatementHandle (int cHandle) throws RemoteException;

public int executeQuery (int handle, String sql) throws RemoteException;

Page 23: Greetings from the Edge: Using javaobj in DATA Step Richard A. DeVenezia

GatewayManager

• Implements the Interface

public int getConnectionHandle (String driverClass, String databaseURL,String username, String password)

throws RemoteException{… try { System.out.println ("loading "+driverClass); Class.forName(driverClass); System.out.println ("connecting to "+databaseURL); con = DriverManager.getConnection (databaseURL, username, password); System.out.println ("connected");}…

Page 24: Greetings from the Edge: Using javaobj in DATA Step Richard A. DeVenezia

GatewayServer

• Hosts a GatewayManagerprotected static final String RMI_NAME = "JDBC-GATEWAY-MANAGER";

public static void main(String args[]){…try { LocateRegistry.createRegistry(1099); GatewayManager manager = new GatewayManager (5);

Naming.rebind (RMI_NAME, manager);

System.out.println ( manager.getClass().getName() + " ready to manage " + 5 + " connections.");}…

Page 25: Greetings from the Edge: Using javaobj in DATA Step Richard A. DeVenezia

GatewayServer

• getReferenceToPersistentManager()– Convience method– Client starts immediately after server

for (i=0;i<4;i++) { try { remote = Naming.lookup(RMI_NAME); } catch (java.rmi.NotBoundException e) { Thread.currentThread().sleep(250/*ms*/); }}

Page 26: Greetings from the Edge: Using javaobj in DATA Step Richard A. DeVenezia

DataStepGatewayAdapter

• Reimplements GatewayInterface– delegates everything to – performs typecasting where needed

public class DataStepGatewayAdapter{ private GatewayInterface dbi; public DataStepGatewayAdapter() throws Exception { dbi = GatewayServer.getReferenceToPersistentManager ();}

public int getStatementHandle (double cHandle) throws Exception{ return dbi.getStatementHandle ( (int) cHandle); }

Page 27: Greetings from the Edge: Using javaobj in DATA Step Richard A. DeVenezia

SAS Macros

• Facilitate use of Gateway• startServer• getConnectionHandle• getStatementHandle• jdbcLoadTable• jdbcQuery

Page 28: Greetings from the Edge: Using javaobj in DATA Step Richard A. DeVenezia

Using the macros

%let jdbc_server_policy = gateway.policy;%let jdbc_driver = org.postgresql.Driver;%let db_url =

jdbc:postgresql://www.devenezia.com:5434/sesug03demo;

%let username = sesug03demo ; %let password = D3m0oeoe; %let cHandle =;

%getConnectionHandle ( driver = &jdbc_driver , url = &db_url , user = &username , pass = &password , cHandle_mv = cHandle );

Page 29: Greetings from the Edge: Using javaobj in DATA Step Richard A. DeVenezia

Using the macros

%jdbcLoadTable (

cHandle=&cHandle,

data=sashelp.zipcode,

obs=20);

%jdbcQuery (

cHandle=&cHandle,

sql=SELECT * FROM ZIPCODE, out=WORK.ZIPCODE);

Page 30: Greetings from the Edge: Using javaobj in DATA Step Richard A. DeVenezia

JDBC connection pattern

Class.forName(jdbcDriver);Connection con = DriverManager.getConnection

( URL, username, password );

• jdbcDriver = “org.postgresql.Driver”• URL =

“jdbc:postgresql://www.devenezia.com:5434/sesug03demo”;

• username = “sesug03demo” ;• password = “D3m0oeoe”;

Page 31: Greetings from the Edge: Using javaobj in DATA Step Richard A. DeVenezia

Conclusion

• Javaobj opens new horizons

• Hybrid solutions– Combine best features of different technologies

• Web

www.devenezia.com/papers/sesug-2003