Pentaho:如何将字段(= 列)动态添加到 OutputRow?
Posted
技术标签:
【中文标题】Pentaho:如何将字段(= 列)动态添加到 OutputRow?【英文标题】:Pentaho: How to dynamically add Field (= Column) to OutputRow? 【发布时间】:2013-02-27 14:31:12 【问题描述】:我想在 Kettle 的结果输出行中动态添加字段(或新列)。
在花了几个小时阅读来自他的帖子并且他的脚本文档做得不太好之后,我想知道 *** 是否会有所帮助。
【问题讨论】:
【参考方案1】:我们可以使用以下步骤来生成动态列生成:
-
计算器
添加常量。
在表格输入中选择必填字段并将这些值分配为设置变量,第二个转换级别使用 get variables hop
【讨论】:
我刚刚投票支持这个答案作为解决方案,因为它听起来是最有用的答案...。在给定时间和给定客户端评估给定用例的 pentaho 之后,我们继续评估其他“产品”,然后从我们的开发人员驱动的团队中抽离出来,让我们自己构建一些东西。【参考方案2】:您的输入值是如何传递给 SQL 查询的?如果它们是变量,那么只需将表输入步骤传递给“获取变量”步骤并以这种方式获取新列。
您也可以使用计算器或添加常量来添加列。
或者您甚至可以使用“获取系统信息”步骤来获取命令行参数和日期等。
【讨论】:
【参考方案3】:首先,让我给你一个我在用户定义的 Java 类步骤中的代码 sn-p:
private int fieldToHashGeoIndex;
private int fieldToHashHeadIndex;
public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException
Object[] r=getRow();
if (r==null)
setOutputDone();
return false;
if (first)
fieldToHashGeoIndex = getInputRowMeta().indexOfValue(getParameter("FIELD_TO_HASH_GEO"));
if (fieldToHashGeoIndex<0)
throw new KettleException("Field to hash not found in the input row, check parameter 'FIELD_TO_HASH_GEO'!");
fieldToHashHeadIndex = getInputRowMeta().indexOfValue(getParameter("FIELD_TO_HASH_HEAD"));
if (fieldToHashHeadIndex<0)
throw new KettleException("Field to hash not found in the input row, check parameter 'FIELD_TO_HASH_HEAD'!");
first=false;
Object[] outputRowData = RowDataUtil.resizeArray(r, data.outputRowMeta.size());
int outputIndex = getInputRowMeta().size();
String fieldToHashGeo = getInputRowMeta().getString(r, fieldToHashGeoIndex);
String fieldToHashHead = getInputRowMeta().getString(r, fieldToHashHeadIndex);
outputRowData[outputIndex++] = MurmurHash.hash64(fieldToHashGeo);
outputRowData[outputIndex++] = MurmurHash.hash64(fieldToHashHead);
putRow(data.outputRowMeta, outputRowData);
return true;
现在,通常你从步骤的配置中配置outputRowMeta
,但也许你可以在代码中修改它。这应该允许您在代码中指定其他字段。
作为替代方案,您可以通过在步骤上定义固定输出字段(如“field1”、“field2”等)并跟踪其他地方的字段名称来锁定可变字段。您可能必须创建所有类型为 String 的字段,然后再进行自己的类型调整。
现在我想起来了,可变的输出字段可能会导致麻烦:您必须非常小心您在后面的步骤中所做的事情,以避免由于类型不匹配或缺少字段而出现错误。
【讨论】:
以上是关于Pentaho:如何将字段(= 列)动态添加到 OutputRow?的主要内容,如果未能解决你的问题,请参考以下文章