当前位置: 首页 > news >正文

Kettle时间戳转换为日期格式处理方式

概述

在Kettle(Pentaho Data Integration)中,时间戳转换为日期格式是常见的数据处理需求。本文档介绍使用Java和JavaScript两种脚本方式来处理时间戳转换。

1. Java脚本处理方式

1.1 使用Java脚本步骤

在Kettle中使用"Java代码"步骤来处理时间戳转换:

// 获取输入字段值
String timestampStr = get(Fields.In, "timestamp_field").getString(r);// 方法1:处理Unix时间戳(秒)
if (timestampStr != null && !timestampStr.isEmpty()) {try {// 判断是秒级还是毫秒级时间戳long timestamp = Long.parseLong(timestampStr);if (timestampStr.length() <= 10) {// 秒级时间戳,转换为毫秒timestamp = timestamp * 1000;}// 毫秒级时间戳直接使用// 创建Date对象Date date = new Date(timestamp);// 格式化日期SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");String formattedDate = sdf.format(date);// 输出到目标字段get(Fields.Out, "formatted_date").setValue(r, formattedDate);} catch (NumberFormatException e) {// 处理异常get(Fields.Out, "formatted_date").setValue(r, null);get(Fields.Out, "error_message").setValue(r, "Invalid timestamp format");}
}// 方法2:处理字符串格式的时间戳
String dateStr = get(Fields.In, "date_string").getString(r);
if (dateStr != null && !dateStr.isEmpty()) {try {// 解析各种日期格式SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");Date date = inputFormat.parse(dateStr);// 输出格式SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");String result = outputFormat.format(date);get(Fields.Out, "converted_date").setValue(r, result);} catch (ParseException e) {get(Fields.Out, "converted_date").setValue(r, null);get(Fields.Out, "error_message").setValue(r, "Date parsing error: " + e.getMessage());}
}

1.2 完整的Java脚本示例

import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.util.Date;// 主处理逻辑
public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException {Object[] r = getRow();if (r == null) {setOutputDone();return false;}// 创建输出行Object[] outputRow = createOutputRow(r, data.outputRowMeta.size());// 获取时间戳字段String timestampField = get(Fields.In, "timestamp").getString(r);if (timestampField != null && !timestampField.trim().isEmpty()) {try {// 处理数字时间戳if (timestampField.matches("\\d+")) {long timestamp = Long.parseLong(timestampField);// 判断是秒级还是毫秒级时间戳if (timestampField.length() <= 10) {// 秒级时间戳,转换为毫秒timestamp = timestamp * 1000;}// 毫秒级时间戳直接使用Date date = new Date(timestamp);// 多种输出格式SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");get(Fields.Out, "date_only").setValue(outputRow, sdf1.format(date));get(Fields.Out, "datetime").setValue(outputRow, sdf2.format(date));get(Fields.Out, "chinese_format").setValue(outputRow, sdf3.format(date));} else {// 处理字符串格式的时间戳SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");Date date = inputFormat.parse(timestampField);SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");get(Fields.Out, "converted_date").setValue(outputRow, outputFormat.format(date));}} catch (Exception e) {// 错误处理get(Fields.Out, "error_flag").setValue(outputRow, "Y");get(Fields.Out, "error_message").setValue(outputRow, e.getMessage());}}// 输出行putRow(data.outputRowMeta, outputRow);return true;
}

2. JavaScript脚本处理方式

2.1 使用JavaScript步骤

在Kettle中使用"JavaScript"步骤来处理时间戳转换:

// 获取输入字段
var timestampValue = timestamp_field;// 方法1:处理Unix时间戳
if (timestampValue != null && timestampValue != "") {try {// 转换为数字var timestamp = parseInt(timestampValue);// 判断时间戳长度,确定是秒级还是毫秒级if (timestampValue.toString().length <= 10) {// 秒级时间戳,转换为毫秒timestamp = timestamp * 1000;}// 毫秒级时间戳直接使用// 创建Date对象var date = new Date(timestamp);// 格式化日期var year = date.getFullYear();var month = String(date.getMonth() + 1).padStart(2, '0');var day = String(date.getDate()).padStart(2, '0');var hours = String(date.getHours()).padStart(2, '0');var minutes = String(date.getMinutes()).padStart(2, '0');var seconds = String(date.getSeconds()).padStart(2, '0');// 输出不同格式formatted_date = year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;date_only = year + "-" + month + "-" + day;chinese_format = year + "年" + month + "月" + day + "日 " + hours + ":" + minutes + ":" + seconds;} catch (e) {formatted_date = null;error_message = "Invalid timestamp: " + e.message;}
}// 方法2:处理ISO格式时间字符串
var isoString = iso_timestamp_field;
if (isoString != null && isoString != "") {try {var date = new Date(isoString);// 检查日期是否有效if (isNaN(date.getTime())) {throw new Error("Invalid date format");}// 格式化输出var year = date.getFullYear();var month = String(date.getMonth() + 1).padStart(2, '0');var day = String(date.getDate()).padStart(2, '0');var hours = String(date.getHours()).padStart(2, '0');var minutes = String(date.getMinutes()).padStart(2, '0');var seconds = String(date.getSeconds()).padStart(2, '0');converted_date = year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;} catch (e) {converted_date = null;error_message = "Date conversion error: " + e.message;}
}

2.2 完整的JavaScript脚本示例

// 时间戳转换函数
function convertTimestamp(timestampValue, format) {try {if (timestampValue == null || timestampValue == "") {return null;}var timestamp = parseInt(timestampValue);// 判断时间戳类型if (timestampValue.toString().length <= 10) {// 秒级时间戳,转换为毫秒timestamp = timestamp * 1000;}// 毫秒级时间戳直接使用var date = new Date(timestamp);// 验证日期有效性if (isNaN(date.getTime())) {throw new Error("Invalid timestamp");}var year = date.getFullYear();var month = String(date.getMonth() + 1).padStart(2, '0');var day = String(date.getDate()).padStart(2, '0');var hours = String(date.getHours()).padStart(2, '0');var minutes = String(date.getMinutes()).padStart(2, '0');var seconds = String(date.getSeconds()).padStart(2, '0');switch(format) {case 'date':return year + "-" + month + "-" + day;case 'datetime':return year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;case 'chinese':return year + "年" + month + "月" + day + "日 " + hours + ":" + minutes + ":" + seconds;case 'time':return hours + ":" + minutes + ":" + seconds;default:return year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;}} catch (e) {return null;}
}// 处理多个时间戳字段
var timestamp1 = timestamp_field_1;
var timestamp2 = timestamp_field_2;
var timestamp3 = timestamp_field_3;// 转换第一个时间戳
if (timestamp1 != null) {formatted_date_1 = convertTimestamp(timestamp1, 'datetime');date_only_1 = convertTimestamp(timestamp1, 'date');chinese_format_1 = convertTimestamp(timestamp1, 'chinese');
}// 转换第二个时间戳
if (timestamp2 != null) {formatted_date_2 = convertTimestamp(timestamp2, 'datetime');date_only_2 = convertTimestamp(timestamp2, 'date');
}// 转换第三个时间戳
if (timestamp3 != null) {formatted_date_3 = convertTimestamp(timestamp3, 'datetime');time_only_3 = convertTimestamp(timestamp3, 'time');
}// 错误处理
if (formatted_date_1 == null && timestamp1 != null) {error_flag = "Y";error_message = "Failed to convert timestamp1: " + timestamp1;
}

3. 常用时间格式

3.1 输入格式

  • Unix时间戳(秒):1640995200
  • Unix时间戳(毫秒):1640995200000
  • ISO 8601格式:2022-01-01T00:00:00.000Z
  • 标准格式:2022-01-01 00:00:00

3.2 输出格式

  • 日期:2022-01-01
  • 日期时间:2022-01-01 00:00:00
  • 中文格式:2022年01月01日 00:00:00
  • 时间:00:00:00

4. 性能优化建议

4.1 Java脚本优化

// 预创建SimpleDateFormat对象,避免重复创建
private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private static final SimpleDateFormat DATE_ONLY_FORMAT = new SimpleDateFormat("yyyy-MM-dd");// 在processRow方法中使用
String formattedDate = DATE_FORMAT.format(date);

4.2 JavaScript脚本优化

// 预定义格式化函数
var formatDate = function(date) {var year = date.getFullYear();var month = String(date.getMonth() + 1).padStart(2, '0');var day = String(date.getDate()).padStart(2, '0');var hours = String(date.getHours()).padStart(2, '0');var minutes = String(date.getMinutes()).padStart(2, '0');var seconds = String(date.getSeconds()).padStart(2, '0');return year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
};

5. 错误处理

5.1 常见错误类型

  • 无效的时间戳格式
  • 时间戳超出有效范围
  • 空值或null值
  • 数据类型不匹配

5.2 错误处理策略

// JavaScript错误处理示例
try {var result = convertTimestamp(timestampValue);if (result == null) {error_flag = "Y";error_message = "Conversion failed";}
} catch (e) {error_flag = "Y";error_message = "Error: " + e.message;formatted_date = null;
}

6. 测试用例

6.1 测试数据

输入时间戳输入类型期望输出
1640995200Unix秒级2022-01-01 00:00:00
1640995200000Unix毫秒级2022-01-01 00:00:00
2022-01-01T00:00:00.000ZISO格式2022-01-01 00:00:00
null空值null
abc无效格式null

6.2 验证脚本

// 验证转换结果
function validateConversion(input, output, expected) {if (output == expected) {validation_result = "PASS";} else {validation_result = "FAIL";validation_message = "Expected: " + expected + ", Got: " + output;}
}

7. 总结

本文档提供了Kettle中时间戳转换为日期格式的完整解决方案,包括:

  1. Java脚本方式:适合复杂的数据处理逻辑,性能较好
  2. JavaScript脚本方式:语法简单,易于理解和维护
  3. 错误处理:完善的异常处理机制
  4. 性能优化:提供优化建议和最佳实践
  5. 测试验证:包含测试用例和验证方法

选择哪种方式取决于具体的业务需求和性能要求。对于简单的转换,推荐使用JavaScript;对于复杂的业务逻辑,推荐使用Java脚本。


文章转载自:

http://AVQ4HbUi.ftznb.cn
http://kED3A3HC.ftznb.cn
http://wbzM3gQ2.ftznb.cn
http://uoPH9Y6G.ftznb.cn
http://XPHAB5wD.ftznb.cn
http://BNWt2FeY.ftznb.cn
http://lhFv6gkz.ftznb.cn
http://LXhY1clS.ftznb.cn
http://3klViQSe.ftznb.cn
http://gYdhJ2LJ.ftznb.cn
http://DM5KH2B4.ftznb.cn
http://Etvinl7A.ftznb.cn
http://pImjtdno.ftznb.cn
http://P3JSiTGb.ftznb.cn
http://kbocS7Wl.ftznb.cn
http://3afX5KvV.ftznb.cn
http://4cfqMGqR.ftznb.cn
http://BNwBvQEZ.ftznb.cn
http://5GJrVNVw.ftznb.cn
http://45xdLJUo.ftznb.cn
http://nDfB2jCy.ftznb.cn
http://GbNsva6v.ftznb.cn
http://j1u4yLHU.ftznb.cn
http://oqwPV5NN.ftznb.cn
http://4H7j5gf4.ftznb.cn
http://Smlgq0yn.ftznb.cn
http://XMRuznvI.ftznb.cn
http://1REmLFsD.ftznb.cn
http://yrZmeJIo.ftznb.cn
http://JbMnpc5R.ftznb.cn
http://www.dtcms.com/a/388539.html

相关文章:

  • go.js Panel中文API
  • 加密货币中的MEV是什么
  • 【Linux学习笔记】线程概念与控制(一)
  • Linux笔记---非阻塞IO与多路复用
  • 生物信息学中的 AI Agent: Codex 初探
  • 贪心算法应用:埃及分数问题详解
  • 力扣hot100刷题day1
  • 什么是跨站脚本攻击
  • 团队对 DevOps 理解不统一会带来哪些问题
  • I²C 总线通信原理与时序
  • C#关键字record介绍
  • 试验台铁地板的设计与应用
  • 原子操作:多线程编程
  • 项目:寻虫记日志系统(三)
  • 在Arduino上模拟和电子I/O工作
  • Windows 命令行:相对路径
  • 线程、进程、协程
  • Java/注解Annotation/反射/元数据
  • C++学习:哈希表的底层思路及其实现
  • 机器学习python库-Gradio
  • 创作一个简单的编程语言,首先生成custom_arc_lexer.g4文件
  • 湖北燃气瓶装送气工证考哪些科目?
  • MySQL死锁回滚导致数据丢失,如何用备份完美恢复?
  • Zustand入门及使用教程(二--更新状态)
  • Matplotlib统计图:绘制精美的直方图、条形图与箱线图
  • 在el-table-column上过滤数据,进行格式化处理
  • 记一次golang结合前端的axios、uniapp进行预签名分片上传遇到403签名错误踩坑
  • 十一章 无界面压测
  • 多色印刷机的高精度同步控制:EtherCAT与EtherNet/IP的集成应用
  • 【随笔】【蓝屏】DMA错误