完美解决Kettle导数据库产生的中文乱码
来源:互联网 发布:淘宝v4是什么意思 编辑:程序博客网 时间:2024/06/06 15:36
如果公司内一开始没有好好规划数据库建设,那么后期可能存在多种字符集的数据库实例。在做数据仓库或者来回导数据的时候,因字符集导致中文乱码问题困扰着不少人。网上有很多前辈们总结的解决中文乱码的方案,关于使用kettle如何解决也有一两篇谈到在建数据库连接时加characterEncoding来解决。我昨晚找到另外一种方式来跟大家分享:
经过对源码搜索”encoding“,找一句注释,发现其实解决方法很简单,
- /**
- * Build the row using ResultSetMetaData rsmd
- * @param rm The resultset metadata to inquire
- * @param ignoreLength true if you want to ignore the length (workaround for MySQL bug/problem)
- * @param lazyConversion true if lazy conversion needs to be enabled where possible
- */
- private RowMetaInterface getRowInfo(ResultSetMetaData rm, boolean ignoreLength, boolean lazyConversion) throws KettleDatabaseException
- {
- if (rm==null) return null;
- rowMeta = new RowMeta();
- try
- {
- // TODO If we do lazy conversion, we need to find out about the encoding
- //
- int fieldNr = 1;
- int nrcols=rm.getColumnCount();
- for (int i=1;i<=nrcols;i++)
- {
- String name=new String(rm.getColumnName(i));
- // Check the name, sometimes it's empty.
- //
- if (Const.isEmpty(name) || Const.onlySpaces(name))
- {
- name = "Field"+fieldNr;
- fieldNr++;
- }
- ValueMetaInterface v = getValueFromSQLType(name, rm, i, ignoreLength, lazyConversion);
- rowMeta.addValueMeta(v);
- }
- return rowMeta;
- }
- catch(SQLException ex)
- {
- throw new KettleDatabaseException("Error getting row information from database: ", ex);
- }
- }
/** * Build the row using ResultSetMetaData rsmd * @param rm The resultset metadata to inquire * @param ignoreLength true if you want to ignore the length (workaround for MySQL bug/problem) * @param lazyConversion true if lazy conversion needs to be enabled where possible */private RowMetaInterface getRowInfo(ResultSetMetaData rm, boolean ignoreLength, boolean lazyConversion) throws KettleDatabaseException{ if (rm==null) return null;rowMeta = new RowMeta();try{// TODO If we do lazy conversion, we need to find out about the encoding// int fieldNr = 1;int nrcols=rm.getColumnCount();for (int i=1;i<=nrcols;i++){String name=new String(rm.getColumnName(i)); // Check the name, sometimes it's empty. // if (Const.isEmpty(name) || Const.onlySpaces(name)) { name = "Field"+fieldNr; fieldNr++; } ValueMetaInterface v = getValueFromSQLType(name, rm, i, ignoreLength, lazyConversion);rowMeta.addValueMeta(v);}return rowMeta;}catch(SQLException ex){throw new KettleDatabaseException("Error getting row information from database: ", ex);}}
就是这样”If we do lazy conversion, we need to find out about the encoding“,直接勾选”允许延迟转换“即可:
这样在从数据库读取的数据就能保持原有字符集,不因默认强制使用utf8导致乱码,在输出时指定文件字符集,就会解决导出到文件中的乱码问题。
如果导入到目标表的字符集与源表不同,需要在入库前用select values做字符转换(纯属废话,相同就不会有乱码了):
整个流程如下
这样,无论到文件还是目标表,都不会再有乱码了。
如果以上还无法解决,可以在Table Input 和Table Output的数据库连接高级选项中设置当前session的字符集;以下除了可以设置session 的字符集,还可以设置日期格式等。
通过以上设置还无法解决,只能归结为RP不好了O(∩_∩)O~
0 0
- 完美解决Kettle导数据库产生的中文乱码
- firefox对ajax的URL中文编码产生乱码完美解决
- firefox对ajax的URL中文编码产生乱码完美解决
- 解决mysqldump导数据的乱码问题
- 中文产生乱码的解决方式
- impdp导数据中文乱码
- Kettle 7.1 share.xml中文乱码解决
- 完美解决PHP中文乱码
- 完美解决PHP中文乱码
- 完美解决PHP中文乱码
- 完美解决PHP中文乱码
- 完美解决PHP中文乱码
- mysql中文乱码完美解决
- 完美解决PHP中文乱码
- 完美解决PHP中文乱码
- 完美解决PHP中文乱码
- 完美解决PHP中文乱码
- 如何解决cut off中文字符(unicode)产生的乱码
- 敏捷宣言
- iOS开发者有价值的工具集
- Java第九课
- 纯CSS制作的图形效果
- gtk treeview MVC
- 完美解决Kettle导数据库产生的中文乱码
- 双方都
- 【python系列】python给函数传值的奇葩方法
- FTP下载附件,然后成功三次以后提示需要输入用户名和密码
- Reveal通过视图层次帮你理解复杂的app 并提供相关Debug功能
- 导入eclips工程出现红色叉号:Unable to resolve target 'android-15'
- 和规范化
- 【算法-排序之一】冒泡排序
- 演练:创建和使用静态库 (C++)