kettle中执行java代码

kettle中有时候需要从http接口的请求中获取数据,故会使用java代码组件来调用http接口;

新建的java代码组件的必须有上一步骤,否则不会执行。

1、固定的模版 

import java.io.UnsupportedEncodingException;
import java.io.IOException;

public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException,IOException  {
  
  //接收上一步骤
  Object[] r = getRow();
  if (r == null) {
    setOutputDone();
    return false;
  }
 
  String result = "";
  try {
	//输出日志
    logBasic(result);
	//返回值到下一步骤
	get(Fields.Out, "result_1").setValue(r, result);
   } catch (Exception e) {
	e.printStackTrace();
   } finally {
	getMethod.releaseConnection();
  }
  putRow(data.outputRowMeta, r);
  return true;
}

2、执行http的get请求

import java.io.UnsupportedEncodingException;
import java.io.IOException;
import java.net.URLEncoder;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;

public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException,IOException  {
  
  //接收上一步骤
  Object[] r = getRow();

  if (r == null) {
    setOutputDone();
    return false;
  }
  //接口地址
  String url = "http://xxxx/api/attAreaPerson/area/list?pageNo=1&pageSize=1000";
  
  HttpClient httpClient = new HttpClient();
  //PostMethod为post请求,GetMethod为get请求
  GetMethod getMethod = new GetMethod(url);
  //http请求返回值
  String result = "";
  try {
	int statusCode = httpClient.executeMethod(getMethod);
	if (statusCode == HttpStatus.SC_OK) {
		result = getMethod.getResponseBodyAsString();
	}
	//输出日志
    logBasic(result);
	//返回值到下一步骤
	get(Fields.Out, "result_1").setValue(r, result);
   } catch (Exception e) {
	e.printStackTrace();
   } finally {
	getMethod.releaseConnection();
  }
  putRow(data.outputRowMeta, r);
  return true;
}

3、执行http请求的post方法

import java.io.UnsupportedEncodingException;
import java.io.IOException;
import java.net.URLEncoder;
import net.sf.json.JSONObject;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;

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

  Object[] r = getRow();

  if (r == null) {
    setOutputDone();
    return false;
  }
  //接口地址
  String url = "http://xxxx/api/department/add";
  //请求参数(上一步骤传递过来的参数)
  String name = get(Fields.In, "name").getString(r);
  String code = get(Fields.In, "code").getString(r);
  String parentCode = get(Fields.In, "parentCode").getString(r);
  JSONObject p = new JSONObject();
  p.put("name", name);
  p.put("code", code);
  p.put("parentCode", parentCode); 
  StringRequestEntity requestEntity = new StringRequestEntity(p.toString(),
				"application/json", "UTF-8");
  HttpClient httpClient = new HttpClient();
  //PostMethod为post请求,GetMethod为get请求
  PostMethod postMethod = new PostMethod(url);
  //设置请求参数
  postMethod.setRequestEntity(requestEntity);
  
  String result = "";
  try {
	int statusCode = httpClient.executeMethod(postMethod);
	if (statusCode == HttpStatus.SC_OK) {
		result = (postMethod.getResponseBodyAsString());
	}
	JSONObject obj = JSONObject.fromObject(result);
	String message = obj.get("message").toString();
	if(message.equals("success")){
		//输出到下一步的参数,code_2为自定义的参数名字,code为具体的值
		get(Fields.Out, "code_2").setValue(r, code);		
	}
	logBasic(result);
   } catch (Exception e) {
	e.printStackTrace();
   } finally {
	postMethod.releaseConnection();
  }
  putRow(data.outputRowMeta, r);
  return true;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值