Chinaunix首页 | 论坛 | 博客
  • 博客访问: 171302
  • 博文数量: 25
  • 博客积分: 3015
  • 博客等级: 中校
  • 技术积分: 545
  • 用 户 组: 普通用户
  • 注册时间: 2007-08-15 17:00
文章分类

全部博文(25)

文章存档

2010年(1)

2009年(9)

2008年(15)

我的朋友

分类: Java

2008-11-06 15:37:04

------------------------------------------------------------------------------
添加SAS JAR.RAR中的JAR包.
运行以下代码:
 
 
 
 
文件: SAS jar.rar
大小: 1581KB
下载: 下载

import java.rmi.RemoteException;
import java.sql.ResultSet;
import java.util.Properties;

import com.sas.iom.WorkspaceFactory;
import com.sas.iom.SAS.IDataService;
import com.sas.iom.SAS.ILanguageService;
import com.sas.iom.SAS.IWorkspace;
import com.sas.rio.MVAConnection;




public class CreditServiceImpl implements java.rmi.Remote
{
    
    private String HOST = "localhost";
    private String PORT = "8591";
    private String JDBS_DRIVER_NAME = "com.sas.rio.MVADriver";
    private String JDBS_DATEBASE_URL = "jdbc:sasiom://localhost:8591";
    private String SAS_USER = "sasadm";
    private String SAS_PWD = "sas123?";
    
    
   public ResultSet getCreditLimit(String libnamestr,String sql )throws java.rmi.RemoteException
   {

     java.sql.Connection connection = null;
     java.sql.Statement statement = null;
     java.sql.ResultSet rs = null;
     IWorkspace iWorkspace          = null;
     WorkspaceFactory wsf          = null;
    
      try
      {
         wsf = new WorkspaceFactory();
        
         Properties serverInfo = new Properties();
         
         serverInfo.put("host", HOST);
         serverInfo.put("port", PORT);
         serverInfo.put("userName", SAS_USER);
         serverInfo.put("password", SAS_PWD);
         
         iWorkspace = wsf.createWorkspaceByServer(serverInfo);

         ILanguageService iLang = iWorkspace.LanguageService();

         //String libnamecm = "libname libstg 'e:\\data';";


         
          ILanguageService sasLanguage = iWorkspace.LanguageService();
         
          sasLanguage.Submit(libnamestr);

         String log = iLang.FlushLog(50000);
         
         System.out.println(log);
                
         /* Read the result via an MVAConnection */
         IDataService iDataService = iWorkspace.DataService();

         connection = new MVAConnection(iDataService, new Properties());
         statement = connection.createStatement();
         //String sql = "Select * from libstg.userinfo";

         
         System.out.println("Curr Libname Command: " + libnamestr);
         System.out.println("");
         System.out.println("Curr Running SQL :" + sql);
         
         
         rs = statement.executeQuery(sql);

// if( rs.next() )

// {

// String UserName = rs.getString("userName");

// System.out.println(UserName);

// }

      }
      catch( Throwable t )
      {
         t.printStackTrace();
         java.rmi.RemoteException ex = new java.rmi.RemoteException("Error getting credit limit", t);
         throw ex;
      }
      finally {
         try{
            /* Close JDBC connection if open */
            if(connection != null)
            {
               if(!connection.isClosed())
                  connection.close();
            }

            /* Close iWorkspace */
            if(iWorkspace != null)
               iWorkspace.Close();

            /* Shutdown WorkspaceFactory */
            if(wsf != null)
               wsf.shutdown();
         } catch(Throwable t) {
            //t.printStackTrace();

         }
      }
      return rs;
 }

 


    /**
     * @param args
     */

    public static void main(String[] args) {
        // TODO 自动生成方法存根

        
        CreditServiceImpl csi = new CreditServiceImpl();
        String sql = "Select * from libstg.userinfo";
        String libnamecm = "libname libstg 'e:\\data';";
        try {
            csi.getCreditLimit(libnamecm,sql);
        } catch (RemoteException e) {
            // TODO 自动生成 catch 块

            e.printStackTrace();
        }

    }

}

 

 

 

 

通过IOM(“Integrated Object Model” )服务器,没有必要使SAS安装在本地系统,通过Management Console和EntERPrise Guide工具可以完美的连接到远端的SAS主机上
需要注意的是IOM服务器并不是代表一个具体的服务器,在SAS IT中,有下面4中类型的IOM服务器
 Metadata 服务器 – 存储和管理元数据库,包含哪些SAS服务器、库、和存储过程可用
 工作空间服务器 – 维护SAS程序环境;
 存储过程服务器 – 运行 “canned” SAS程序
 OLAP服务器 – 驱动CUBE数据到SAS Enterprise Guide或者其它OLAP客户端

工作空间服务器有下面3中类型
 Java客户端连接到IOM服务器使用JAVA-IOM桥
 WIN客户端连接到IOM服务器使用COM/DCOM技术,
 WIN客户端可以连接到运行在UNXI上的IOM服务器,通过使用COM-IOM桥

对于WIN应用,IOM支持ADO通过OLEDB访问SAS数据
对应JAVA,JDBC2.0访问是被支持的

工作空间服务器包含下面接口
 工作空间Workspace – SAS会话
 语言服务Language Service –提交SAS DATA步和过程步,接收LOG和LIST输出,运行存储过程
 数据服务Data Service – 管理SAS库
 文件服务File Service – 管理文件标识
 实用工具Utilities – formats, options, result packages, host system information

存储过程服务器
 用于存放存储过程,其结果可以被Web(Information Delivery Portal,BI WEB服务)、Office(OFFICE插件)或者Enterprise Guide 3.0使用

假设在SAS服务器上存在下面文件
 C:\temp>type IOMTest.sas
 %let cond=;
 *ProcessBody;
 proc print data=sashelp.class;
 title "Test Stored Process";
 where &cond;
 run;
 
VB例子:
 必须引用下面库
  SAS: Integrated Object Model (SAS System 9.1)
   SASWorkspaceManager 1.1 Type Library
    
Connecting with DCOM in Visual Basic
Option Explicit
' define a global workspace
Dim obSAS As SAS.Workspace
Dim obWSMgr As New SASWorkspaceManager.WorkspaceManager
Private Sub Form_Load()
Dim XMLInfo As String
'
create Workspace server
Dim obServer As New SASWorkspaceManager.ServerDef
obServer.MachineDNSName = "hunding"
Set obSAS = obWSMgr.Workspaces.CreateWorkspaceByServer _
("", VisibilityProcess, obServer, "", "", xmlInfo)
End Sub
Private Sub cmdTest1_Click()
' use LanguageService to submit code
obSAS.LanguageService.Submit _
"%include '
c:\temp\IOMTest.sas'; run;"
MsgBox obSAS.LanguageService.FlushLog(100000)
MsgBox obSAS.LanguageService.FlushList(100000)
End Sub
Private Sub cmdTest2_Click()
'
run the stored SAS program
Dim obStoredProcessService As SAS.StoredProcessService
Set obStoredProcessService = _
obSAS.LanguageService.StoredProcessService
obStoredProcessService.Repository = "file:c:\temp"
obStoredProcessService.Execute "IOMtest", "cond='sex eq ""M""'"
MsgBox obSAS.LanguageService.FlushLog(100000)
MsgBox obSAS.LanguageService.FlushList(1000000)
End Sub
Private Sub Form_Unload(Cancel As Integer)
obWSMgr.Workspaces.RemoveWorkspaceByUUID _
obSAS.UniqueIdentifier
obSAS.Close
End Sub
  
VC例子
#include <iostream>
#include <stdexcept>
#include <Windows.h>
using namespace std;
#import "C:\Program Files\SAS Institute\Shared Files\Integration
Technologies\sas.tlb"

#import "C:\Program Files\SAS Institute\Shared Files\Integration
Technologies\SASWMan.dll"

int main()
{
SASWorkspaceManager::IWorkspaceManager2Ptr pIWorkspaceManager;
SASWorkspaceManager::IServerDef2Ptr pIServerDef = NULL;
SAS::IWorkspacePtr pIWorkspace;
BSTR xmlInfo;
HRESULT hr = CoInitialize(NULL);
hr = pIWorkspaceManager.CreateInstance(
"SASWorkspaceManager.WorkspaceManager.1");
pIServerDef.CreateInstance("SASWorkspaceManager.ServerDef");
pIServerDef->PutMachineDNSName("hygelac");
pIServerDef->Protocol = SASWorkspaceManager::ProtocolBridge;
pIServerDef->put_Port(8591);
pIWorkspace = pIWorkspaceManager->Workspaces->CreateWorkspaceByServer(
_bstr_t(""), //workspace name


SASWorkspaceManager::VisibilityProcess,
pIServerDef, // server


_bstr_t("sassrv"), // login


_bstr_t("sasuser"), // password


&xmlInfo // connection log


);
pIWorkspace->LanguageService->Submit(
"%include '/home/sasadm/IOMTest.sas'; run;");
MessageBox(NULL,
pIWorkspace->LanguageService->FlushLog(10000),
"SAS Log",
MB_OK
);
MessageBox(NULL,
pIWorkspace->LanguageService->FlushList(10000),
"List Output",
MB_OK
);
pIWorkspace->Close();
return(0);
}
JAVA客户端

 编译时使用下面命令
 javac -classpath ".:C:/Program Files/SAS/SAS 9.1/core/sasmisc/sas.svc.connection.jar" IOMTest.java
 sas.svc.connection.jar文件位于 C:\Program Files\SAS\SAS 9.1\core\sasmisc 下




import com.sas.services.connection.Server;
import com.sas.services.connection.BridgeServer;
import com.sas.services.connection.ConnectionFactoryConfiguration;
import com.sas.services.connection.ConnectionFactoryManager;
import com.sas.services.connection.ConnectionFactoryInterface;
import com.sas.services.connection.ConnectionFactoryException;
import com.sas.services.connection.ConnectionInterface;
import com.sas.services.connection.ManualConnectionFactoryConfiguration;
import com.sas.iom.SAS.IWorkspace;
import com.sas.iom.SAS.IWorkspaceHelper;
import com.sas.iom.SAS.ILanguageService;
import com.sas.iom.SAS.ILanguageServicePackage.CarriageControlSeqHolder;
import com.sas.iom.SAS.ILanguageServicePackage.LineTypeSeqHolder;
import com.sas.iom.SASIOMDefs.GenericError;
import com.sas.iom.SASIOMDefs.StringSeqHolder;
public class IOMTest
{
  
 public IOMTest() throws ConnectionFactoryException, GenericError
 {
  String classID = Server.CLSID_SAS;
  String host = "localhost";
  int port = 8591;
  String userName = "sasadm";
  String password = "sas123?";
  Server server = new BridgeServer(classID, host, port);
  ConnectionFactoryConfiguration cxfConfig = new ManualConnectionFactoryConfiguration(
    server);
  ConnectionFactoryManager cxfManager = new ConnectionFactoryManager();
  ConnectionFactoryInterface cxf = cxfManager.getFactory(cxfConfig);
  ConnectionInterface cx = cxf.getConnection(userName, password);
  IWorkspace iWorkspace = IWorkspaceHelper.narrow(cx.getObject());
  ILanguageService sasLanguage = iWorkspace.LanguageService();
  sasLanguage.Submit("%include 'd:\\auto.sas'; run;");
  StringSeqHolder logHldr = new StringSeqHolder();
  sasLanguage.FlushLogLines(Integer.MAX_VALUE,
    new CarriageControlSeqHolder(), new LineTypeSeqHolder(),
    logHldr);
  String[] logLines = logHldr.value;
  StringSeqHolder listHldr = new StringSeqHolder();
  sasLanguage.FlushListLines(Integer.MAX_VALUE,
    new CarriageControlSeqHolder(), new LineTypeSeqHolder(),
    listHldr);
  String[] listLines = listHldr.value;
  iWorkspace.Close();
  cx.close();
 }
 public static void main(String args[]) {
 try {
   new IOMTest();
   System.exit(0);
  } catch (Exception ex) {
   ex.printStackTrace();
   System.exit(1);
  }
 }
}

阅读(1087) | 评论(3) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2011-01-07 16:30:53

怎么看SAS的端口,用户名呀?

chinaunix网友2011-01-07 16:14:55

期待回复,itibm@126.com

chinaunix网友2011-01-07 16:14:23

你好,我用你的CreditServiceImpl类,导入那2个jar类编译不过,这个几个类 import com.sas.iom.WorkspaceFactory; import com.sas.iom.SAS.IDataService; import com.sas.iom.SAS.ILanguageService; import com.sas.iom.SAS.IWorkspace; import com.sas.rio.MVAConnection; 通不过了? itibm@126.com