Greetings from the Edge Using javaobj in DATA

Greetings from the Edge: Using javaobj in DATA Step Richard A. De. Venezia Independent Consultant SAS is a registered trademark or trademark of SAS Institute Inc. in the USA and other countries. ® indicates USA registration. Other brand product names are registered trademarks or Trademarks of their respective companies. Slide imagery copyright © 2004, SAS Institute Inc. Copyright © 2004 , Richard A. De. Venezia. All rights reserved.

Overview n SAS Version 9 DATA Step has experimental syntax for interfacing with objects • Java, Hash. Iter n Demonstration by way of examples. Copyright © 2004 , Richard A. De. Venezia. All rights reserved.

Java is. . . n 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” n Widespread n Networked n Applications • Analysis and management Copyright © 2004 , Richard A. De. Venezia. All rights reserved.

Component Interface is. . . n support. sas. com/rnd/. . . “DATA Step Component Interface provides a mechanism for accessing predefined component objects from within a DATA Step program. ” n Instantiate • Create an object • declare Type var, var = _new_ n Access • var. method() Copyright © 2004 , Richard A. De. Venezia. All rights reserved.

javaobj is. . . n DATA Step n Experimental n Part of broader initiative • Component Interface • Object Dot Syntax Copyright © 2004 , Richard A. De. Venezia. All rights reserved.

Java development n Free kit • java. sun. com/j 2 se • compiler, packager and loader javac, jar, java • Runtime Environment • Online training n Tutorials • numerous web sites and publications Copyright © 2004 , Richard A. De. Venezia. All rights reserved.

The edge n New or experimental n Where two things come together Copyright © 2004 , Richard A. De. Venezia. All rights reserved.

SAS session n Version 9 • CLASSPATH environment variable -set CLASSPATH my. Path; %CLASSPATH% n Version 9. 1 • jreoptions SAS option -jreoptions ( -Djava. class. path=my. Path; %CLASSPATH% -Dsas. jre. version=jre. Path ) Default JRE is installed at sharedJRE1. 4. 1 Copyright © 2004 , Richard A. De. Venezia. All rights reserved.

A little birdie n Version 9. 1 Proc JAVAINFO n --log-java. version = 1. 4. 1 java. vm. version = 1. 4. 1 -b 21 java. home = c: optsassharedJRE1. 4. 1 java. class. path = <no value>. . . Copyright © 2004 , Richard A. De. Venezia. All rights reserved.

Java coding pattern - variables public class Class { private double variable; public double get. Variable() { return this. variable; } public void set. Variable(double variable) { this. variable = variable; } } n java. sun. com/docs/codeconv /html/Code. Conv. TOC. doc. html Copyright © 2004 , Richard A. De. Venezia. All rights reserved.

Java coding pattern - methods public class Class { public <class> method. Name. As. Verb(args. . . ) {. . . } } n java. sun. com/docs/codeconv /html/Code. Conv. TOC. doc. html Copyright © 2004 , Richard A. De. Venezia. All rights reserved.
![Declare statement n declare javaobj var ('Class' [, arg-1[, …[, arg-N]]) ; - or Declare statement n declare javaobj var ('Class' [, arg-1[, …[, arg-N]]) ; - or](http://slidetodoc.com/presentation_image/e6b049f5ad17474275bd4896714782aa/image-12.jpg)
Declare statement n declare javaobj var ('Class' [, arg-1[, …[, arg-N]]) ; - or n n n declare javaobj var; var = _NEW_ javaobj ('Class' [, arg-1[, …[, arg-N]]) ; declare javaobj j (‘java/lang/String’); declare javaobj j (‘java/awt/geom/Ellipse 2 D/Double’); Copyright © 2004 , Richard A. De. Venezia. All rights reserved.

Accessing methods and fields n public Type method (args…) {…} args n obj. call. Type. Method ( ‘method’, return ); Type method args…, args n public Type field; field n obj. [get|set]Type. Field ( ‘field’, value ); Type Copyright © 2004 , Richard A. De. Venezia. All rights reserved.

Hello. SAS. java public class Hello. SAS { public String get. Message () { return "Hello SAS"; } } Copyright © 2004 , Richard A. De. Venezia. All rights reserved.

Hello. SAS. sas data _null_; length message $200; declare javaobj j (’Hello. SAS'); j. call. String. Method ('get. Message', message); put message=; run; --- log -message=Hello SAS Copyright © 2004 , Richard A. De. Venezia. All rights reserved.

Gotchas n Classes are cached per SAS session n Did you recompile a Java class? • Restart SAS n Signature Types • pass double, String, javaobj • return double, String Copyright © 2004 , Richard A. De. Venezia. All rights reserved.

System. Properties. class import java. util. Enumeration; public class System. Properties { private Enumeration e; public System. Properties () { e = System. get. Properties(). property. Names(); } public String get. Property () { if (e. has. More. Elements()) { String p = (String) e. next. Element(); return p + "=" + System. get. Property(p); } else { return null; } } } Copyright © 2004 , Richard A. De. Venezia. All rights reserved.

System. Properties. sas data _null_; dcl javaobj j (’System. Properties'); length s $200; j. call. String. Method ('get. Property', s); do while (s ne ''); put s; j. call. String. Method ('get. Property', s); end; run; --- log --. . . java. vm. version=1. 4. 2_03 -b 02 java. vm. vendor=Sun Microsystems Inc. sas. jre. home=C: j 2 sdk 1. 4. 2_03jre … Copyright © 2004 , Richard A. De. Venezia. All rights reserved.

Exceptions n n Always check, handle and clear exception. Describe(1) • write exceptions to stdout or console n exception. Check(rc) • did an exception occur ? n exception. Clear() • clear the exception flag Copyright © 2004 , Richard A. De. Venezia. All rights reserved.

Object persistence n Intra DATA Step ? No • javaobj is gone when DATA Step ends • obj. delete() recommended n Birdie: “An instantiated javaobj creates a JNI reference which will not be garbage collected. The reference needs to be explicitly deleted. ” Copyright © 2004 , Richard A. De. Venezia. All rights reserved.

Case for persistence n Creating SAS Table from Q, a DBMS Query n Q java. sql. Result. Set • Two SAS Passes • 1. Result. Set. Meta. Data, columns and types • 2. Results. Set, rows n Without persistence, Q must be issued for each pass • not wanted Copyright © 2004 , Richard A. De. Venezia. All rights reserved.

Persistence categories n Intra SAS session • Object persists between DATA Steps in one SAS session • static fields and thread n Inter SAS session • Object persists between DATA Steps and SAS sessions • Remote method invocation - RMI n Require wrapper classes Copyright © 2004 , Richard A. De. Venezia. All rights reserved.

Persistence via Static n Static fields n java n sas public class Persist {static double x, y, z; } data _null_; declare javaobj j ('Persist'); j. set. Static. Double. Field('x', 100); j. delete(); run; data _null_; declare javaobj j ('Persist'); j. get. Static. Double. Field('x', x); j. delete(); put x=; run; x=100 Copyright © 2004 , Richard A. De. Venezia. All rights reserved.

Persistence via Thread n Runnable (R) • monitors its state • persists in Thread started by A n Adapter (A) • new Thread ( new R() ). start () • methods grant access to R’s state • static fields persist across DATA Step - reference to R Copyright © 2004 , Richard A. De. Venezia. All rights reserved.

Persistence via RMI n Server process outside of SAS n DATA Step is client of server • via wrapper n Server allocates resources • Returns handles • Methods accessed by handle Copyright © 2004 , Richard A. De. Venezia. All rights reserved.

DBMS servers n Commercial n Open source • Postgresql • my. SQL n JDBC • not an acronym (just like ess-a-ess) • trademark • 210 drivers listed at http: //servlet. java. sun. com/products/jdbc/drivers Copyright © 2004 , Richard A. De. Venezia. All rights reserved.

JDBC connection pattern Class. for. Name(jdbc. Driver); Connection con = Driver. Manager. get. Connection ( URL, username, password ); n jdbc. Driver = “org. postgresql. Driver” n URL = “jdbc: postgresql: //www. devenezia. com: 5434/sesug 03 demo” n username = “sesug 03 demo” n password = “D 3 m 0 oeoe” Copyright © 2004 , Richard A. De. Venezia. All rights reserved.

Gateway - an RMI scheme n Three classes n An Interface • Gateway. Interface n An Implementation • Gateway. Manager n A Server • Gateway. Server Copyright © 2004 , Richard A. De. Venezia. All rights reserved.

Gateway. Interface n Declares methods public int get. Connection. Handle ( String driver. Class, String database. URL, String username, String password ) throws Remote. Exception; public int get. Statement. Handle (int c. Handle) throws Remote. Exception; public int execute. Query (int handle, String sql) throws Remote. Exception; Copyright © 2004 , Richard A. De. Venezia. All rights reserved.

Gateway. Manager n Implements the Interface public int get. Connection. Handle ( String driver. Class, String database. URL, String username, String password) throws Remote. Exception { … try { System. out. println ("loading "+driver. Class); Class. for. Name(driver. Class); System. out. println ("connecting to "+database. URL); con = Driver. Manager. get. Connection (database. URL, username, password); System. out. println ("connected"); } … Copyright © 2004 , Richard A. De. Venezia. All rights reserved.

Gateway. Server n Hosts a Gateway. Manager protected static final String RMI_NAME = "JDBC-GATEWAY-MANAGER"; public static void main(String args[]){ … try { Locate. Registry. create. Registry(1099); Gateway. Manager manager = new Gateway. Manager (5); Naming. rebind (RMI_NAME, manager); } … System. out. println ( manager. get. Class(). get. Name() + " ready to manage " + 5 + " connections. "); Copyright © 2004 , Richard A. De. Venezia. All rights reserved.

Data. Step. Gateway. Adapter n Reimplements Gateway. Interface • creates and uses delegate • performs typecasting where needed public class Data. Step. Gateway. Adapter { private Gateway. Interface dbi; public Data. Step. Gateway. Adapter() throws Exception { dbi=Gateway. Server. get. Reference. To. Persistent. Manager (); } public int get. Statement. Handle (double c. Handle) throws Exception { return dbi. get. Statement. Handle ( (int) c. Handle); } Copyright © 2004 , Richard A. De. Venezia. All rights reserved.

SAS macros n Facilitate use of Gateway n start. Server n get. Connection. Handle n get. Statement. Handle n jdbc. Load. Table n jdbc. Query Copyright © 2004 , Richard A. De. Venezia. All rights reserved.

Using the macros n Reimplements Gateway. Interface %let jdbc_server_policy = gateway. policy; %let jdbc_driver = org. postgresql. Driver; %let db_url = jdbc: postgresql: //www. devenezia. com: 5434/sesug 03 demo; %let username = sesug 03 demo ; %let password = D 3 m 0 oeoe; %let c. Handle =; %get. Connection. Handle ( driver = &jdbc_driver , url = &db_url , user = &username , pass = &password , c. Handle_mv = c. Handle ); Copyright © 2004 , Richard A. De. Venezia. All rights reserved.

Using the macros %jdbc. Load. Table ( c. Handle=&c. Handle, data=sashelp. zipcode, obs=20); %jdbc. Query ( c. Handle=&c. Handle, sql=SELECT * FROM ZIPCODE, out=WORK. ZIPCODE); Copyright © 2004 , Richard A. De. Venezia. All rights reserved.

Other tasks handled well by Java n Directory listings n Archived file sets n Database connectivity n 2 D and 3 D graphics n Open Office n XML n HTML parsing n and so much more Copyright © 2004 , Richard A. De. Venezia. All rights reserved.

Conclusion n Javaobj opens new horizons • Utilize problem solvers who work in Java n Hybrid solutions • Combine features of different technologies n Paper, slides and examples www. devenezia. com/papers Copyright © 2004 , Richard A. De. Venezia. All rights reserved.
- Slides: 37