思路:kettle 获取了一个 Object 的数组,把这些转成字符串直接输出就可以了
import java.io.*;
import java.nio.charset.*;
import java.nio.file.*;
public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException {
if (first) {
first = false;
/* TODO: Your code here. (Using info fields)
FieldHelper infoField = get(Fields.Info, "info_field_name");
RowSet infoStream = findInfoRowSet("info_stream_tag");
Object[] infoRow = null;
int infoRowCount = 0;
// Read all rows from info step before calling getRow() method, which returns first row from any
// input rowset. As rowMeta for info and input steps varies getRow() can lead to errors.
while((infoRow = getRowFrom(infoStream)) != null){
// do something with info data
infoRowCount++;
}
*/
}
// new org.pentaho.di.core.logging.LogChannel("自定义日志输出").logMinimal("start!");
String countName = getCurrentCountName();
// new org.pentaho.di.core.logging.LogChannel("自定义日志输出").logMinimal("countName : " + countName);
String countStr = getVariable(countName);
String path = getFilePath();
// new org.pentaho.di.core.logging.LogChannel("自定义日志输出").logMinimal("path : " + path);
// 获取当前已经缓存的数据条数
int count = Integer.parseInt(String.valueOf(countStr));
// new org.pentaho.di.core.logging.LogChannel("自定义日志输出").logMinimal("count : " + countStr);
String cacheName = getCurrentCache();
// 获取缓存的数据文件
String cache = getVariable(cacheName);
if (cache == null) {
cache = "";
}
// 判断是否达到预定的条数,达到了就写入
if (count++ == 1000) {
// new org.pentaho.di.core.logging.LogChannel("自定义日志输出").logMinimal("path : " + path);
writeFile(cache, path);
count = 1;
cache = "";
}
String ch01 = String.valueOf((char)1);
// 保存本次查询到的数据
StringBuilder sb = new StringBuilder();
Object[] r = getRow();
if (r == null) {
new org.pentaho.di.core.logging.LogChannel("自定义日志输出").logMinimal("r null");
// 如果没有输入,将已经缓存的全部输出
writeFile(cache, path);
setOutputDone();
return false;
}
// It is always safest to call createOutputRow() to ensure that your output row's Object[] is large
// enough to handle any new fields you are creating in this step.
r = createOutputRow(r, data.outputRowMeta.size());
/* TODO: Your code here. (See Sample)
// Get the value from an input field
String foobar = get(Fields.In, "a_fieldname").getString(r);
foobar += "bar";
// Set a value in a new output field
get(Fields.Out, "output_fieldname").setValue(r, foobar);
*/
// 用来保存 要输出的文件信息
for (int i = 1; i < r.length; i++) {
if (r[i] != null) {
sb.append(r[i].toString()).append(ch01);
}
}
// 删除最后一个多余的分隔符,并加上回车
sb.deleteCharAt(sb.length() -1);
sb.append("\n");
String content = cache;
content += sb.toString();
// 把本次处理后的 count 和 cache 重新赋值
setVariable(countName,String.valueOf(count));
setVariable(cacheName,content);
// Send the row on to the next step.
// putRow(data.outputRowMeta, r);
return true;
}
public void writeFile(String content, String path) {
File file = new File(path);
Path p = file.toPath();
byte[] bytes = content.getBytes(StandardCharsets.UTF_8);
StandardOpenOption[] option = {StandardOpenOption.APPEND};
try {
if (!file.exists()){
File dir = new File(file.getParent());
if (!dir.exists()){
dir.mkdirs();
}
file.createNewFile();
}
Files.write(p,bytes,option);
} catch (IOException e) {
new org.pentaho.di.core.logging.LogChannel("自定义日志输出").logMinimal("filePath:" + path);
e.printStackTrace();
}
}
public String getCurrentCountName() {
// new org.pentaho.di.core.logging.LogChannel("自定义日志输出").logMinimal("Internal.Step.Unique.Number : " + getVariable("Internal.Step.Unique.Number"));
String countName = "count" + String.valueOf(getVariable("Internal.Step.Unique.Number"));
return countName;
}
public String getCurrentCache() {
String cacheName = "cache" + String.valueOf(getVariable("Internal.Step.Unique.Number"));
return cacheName;
}
// ${avlDir}/${filenamehead}${SDATE}${filenamenum}0{x}
public String getFilePath() {
String path = getVariable("avlDir");
String filename = getFileName();
path = path + File.separator + filename;
// new org.pentaho.di.core.logging.LogChannel("自定义日志输出").logMinimal("filePath:" + path);
return path;
}
// 获取文件的名字
public String getFileName() {
String fh = getVariable("filenamehead");
// new org.pentaho.di.core.logging.LogChannel("自定义日志输出").logMinimal("fh:" + fh);
String date = getVariable("SDATE");
// new org.pentaho.di.core.logging.LogChannel("自定义日志输出").logMinimal("SDATE:" + date);
String fn = getVariable("filenamenum");
// new org.pentaho.di.core.logging.LogChannel("自定义日志输出").logMinimal("fn:" + fn);
String x = getVariable("x");
// new org.pentaho.di.core.logging.LogChannel("自定义日志输出").logMinimal("x:" + x);
String num = getNumBySetp();
// new org.pentaho.di.core.logging.LogChannel("自定义日志输出").logMinimal("num:" + num);
String name = fh + date + fn + "0" + x + num + ".AVL";
return name;
}
public String getNumBySetp() {
return getFourNumber(String.valueOf(getVariable("Internal.Step.Unique.Number")));
}
public String getNumByPageNum() {
return getFourNumber(String.valueOf(getVariable("PAGENUM")));
}
// 将序号变成四位数 , 并且数值 + 1 ,因为步骤数是从零开始的
public String getFourNumber(String name) {
int page = Integer.parseInt(name);
if (page < 9) {
name = "000" + String.valueOf(page + 1);
} else if (page < 99) {
name = "00" + String.valueOf(page + 1);
} else if (page < 999) {
name = "0" + String.valueOf(page + 1);
} else {
name = String.valueOf(page + 1);
}
return name;
}