ETL第二篇 调用webservice

前言

这里使用ETL [Java代码] 实现

代码中使用axis调用webservice

在ETL提供了 Processor类中直接写业务代码, 而且是每次读取一行数据

1084504-20180914070627636-1869124690.png

jar包准备

将需要用到的jar包提前放到data-integration/libdata-integration/libswt/对应的目录下
我这里为了方便, 将需要的包发到了data-integration/libswt/win64/
1084504-20180914071123328-983742900.png

用到的相关包: 链接: https://pan.baidu.com/s/1vpUppn-57sc9z3tFGux3uA 密码: 3phi

代码示例

这里提供一个简单的示例供参考

import java.net.URL;

import javax.xml.namespace.QName;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;



public boolean processRow(StepMetaInterface smi,StepDataInterface sdi) throws KettleException{

    Object[] r = getRow(); // 获取输入, 这里获取流中的一行数据
    
    if (r == null) {
        setOutputDone(); // 结束输出, 说明流中没有数据了
        return false; 
    }

    // 获取字段, 从r中获取需要的字段
    String pk_corp = get(Fields.In, "pk_corp").getString(r);

    
    // 因为我这里需要给WS接口发送的数据是[{"pk_corp":pk_corp},{}...]
    JSONArray syncDatas = new JSONArray();
    JSONObject syncData = new JSONObject();
        
    syncData.put("pk_corp",pk_corp);

    syncDatas.add(syncData);

    // 输出数组, 会输出到输出流
    Object[] outputRow = createOutputRow(r, data.outputRowMeta.size());
    JSONObject result = null;

try {
    // 输出日志,也是ETL提供的方法
    logBasic("【请求数据】" + syncDatas.toString());
    
    // 调用ws接口
    result = syncToNC(syncDatas.toString());
    
    logBasic("【结果】" + result.toString());

} catch (Exception e) {
    logError("【捕获异常】" + e.getMessage());

    // 这里获取流中的字段并赋值, syncSign和errorLog是我在ETL[字段]中自定义的
    get(Fields.Out, "syncSign").setValue( outputRow, '0' );
    get(Fields.Out, "errorLog").setValue( outputRow, e.getMessage());   
    
    // 输出数据传输到下一步, 下一步接收的数据就会有我在[字段中]添加的字段
    putRow(data.outputRowMeta, outputRow);
    
    return true;
}

    if( result.containsKey("code") &&  result.getInt("code") == 0) {
        JSONArray resultDatas = result.getJSONArray("data");
        JSONObject resultData = resultDatas.getJSONObject(0);

        // 输出字段
        get(Fields.Out, "syncSign").setValue( outputRow, '1' );
        get(Fields.Out, "newNCPK").setValue( outputRow, resultData.getString("ncPK"));
    } else {
        // 输出字段
        get(Fields.Out, "syncSign").setValue( outputRow, '2' );
        get(Fields.Out, "errorLog").setValue( outputRow, result.getString("msg"));      

    }
    
    putRow(data.outputRowMeta, outputRow);

    return true;
} 

// 调用webservice 
private static JSONObject syncToNC(String str) throws Exception {
    
        JSONObject jsonResult = null;
        // 接口地址  ( 这里后面不加"?wsdl"
        String endpoint = "http://localhost:8090/uapws/service/nc.ift.hs.ydrs.IAddStapplybService";     
        // targetNamespace
        String targetNamespace = "http://ydrs.hs.ift.nc/IAddStapplybService";
        // 调用接口中的方法
        String operationName = "addStapplyb";
        String result = null;
        String message = null;
        
        // 接口方法名
        Service service = new Service();
        Call call = (Call) service.createCall();
        // 设置webservice地址
        call.setTargetEndpointAddress(new URL(endpoint));
        // 发布的方法名
        call.setOperationName(new QName(targetNamespace, operationName));
        // 设置参数
        call.addParameter("string", org.apache.axis.encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
        // 返回类型
        call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);
        call.setEncodingStyle("utf-8");
        call.setUseSOAPAction(true);
        call.setSOAPActionURI(targetNamespace + operationName);
        // 设置参数组
        Object[] params = new Object[] { str };

        // 调用接口
        result = (String) call.invoke(params);
        // 处理结果
        if (result != null && ! "".equals(result)) {
            jsonResult = JSONObject.fromObject(result);
        }
    
        return jsonResult;
}

ETL中使用Java代码

1084504-20180914073847216-1101840651.png

自定义字段

1084504-20180914073642494-2126549837.png

转载于:https://www.cnblogs.com/52liming/p/9515526.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值