完美解决Kettle导数据库产生的中文乱码

    如果公司内一开始没有好好规划数据库建设,那么后期可能存在多种字符集的数据库实例。在做数据仓库或者来回导数据的时候,因字符集导致中文乱码问题困扰着不少人。网上有很多前辈们总结的解决中文乱码的方案,关于使用kettle如何解决也有一两篇谈到在建数据库连接时加characterEncoding来解决。我昨晚找到另外一种方式来跟大家分享:

 

经过对源码搜索”encoding“,找一句注释,发现其实解决方法很简单,

Java代码   收藏代码
  1. /** 
  2.  * Build the row using ResultSetMetaData rsmd 
  3.     * @param rm The resultset metadata to inquire 
  4.     * @param ignoreLength true if you want to ignore the length (workaround for MySQL bug/problem) 
  5.     * @param lazyConversion true if lazy conversion needs to be enabled where possible 
  6.  */  
  7. private RowMetaInterface getRowInfo(ResultSetMetaData rm, boolean ignoreLength, boolean lazyConversion) throws KettleDatabaseException  
  8. {  
  9.        if (rm==nullreturn null;  
  10.       
  11.     rowMeta = new RowMeta();  
  12.       
  13.     try  
  14.     {  
  15.         // TODO If we do lazy conversion, we need to find out about the encoding  
  16.         //  
  17.            int fieldNr = 1;  
  18.         int nrcols=rm.getColumnCount();   
  19.         for (int i=1;i<=nrcols;i++)  
  20.         {  
  21.             String name=new String(rm.getColumnName(i));  
  22.                  
  23.                // Check the name, sometimes it's empty.  
  24.                //  
  25.                if (Const.isEmpty(name) || Const.onlySpaces(name))  
  26.                {  
  27.                    name = "Field"+fieldNr;  
  28.                    fieldNr++;  
  29.                }  
  30.                  
  31.             ValueMetaInterface v = getValueFromSQLType(name, rm, i, ignoreLength, lazyConversion);  
  32.             rowMeta.addValueMeta(v);              
  33.         }  
  34.         return rowMeta;  
  35.     }  
  36.     catch(SQLException ex)  
  37.     {  
  38.         throw new KettleDatabaseException("Error getting row information from database: ", ex);  
  39.     }  
  40. }  

 就是这样”If we do lazy conversion, we need to find out about the encoding“,直接勾选”允许延迟转换“即可(旧版):

新版(6.0以上)如下(一般不要勾选简易转换,否则容易乱码):



这样在从数据库读取的数据就能保持原有字符集,不因默认强制使用utf8导致乱码,在输出时指定文件字符集,就会解决导出到文件中的乱码问题。

如果导入到目标表的字符集与源表不同,需要在入库前用select values做字符转换(纯属废话,相同就不会有乱码了):

 

整个流程如下

这样,无论到文件还是目标表,都不会再有乱码了。

 

 

如果以上还无法解决,可以在Table Input 和Table Output的数据库连接高级选项中设置当前session的字符集;以下除了可以设置session 的字符集,还可以设置日期格式等。

 

通过以上设置还无法解决,只能归结为RP不好了O(∩_∩)O~


转载自:

    如果公司内一开始没有好好规划数据库建设,那么后期可能存在多种字符集的数据库实例。在做数据仓库或者来回导数据的时候,因字符集导致中文乱码问题困扰着不少人。网上有很多前辈们总结的解决中文乱码的方案,关于使用kettle如何解决也有一两篇谈到在建数据库连接时加characterEncoding来解决。我昨晚找到另外一种方式来跟大家分享:

 

经过对源码搜索”encoding“,找一句注释,发现其实解决方法很简单,

Java代码   收藏代码
  1. /** 
  2.  * Build the row using ResultSetMetaData rsmd 
  3.     * @param rm The resultset metadata to inquire 
  4.     * @param ignoreLength true if you want to ignore the length (workaround for MySQL bug/problem) 
  5.     * @param lazyConversion true if lazy conversion needs to be enabled where possible 
  6.  */  
  7. private RowMetaInterface getRowInfo(ResultSetMetaData rm, boolean ignoreLength, boolean lazyConversion) throws KettleDatabaseException  
  8. {  
  9.        if (rm==nullreturn null;  
  10.       
  11.     rowMeta = new RowMeta();  
  12.       
  13.     try  
  14.     {  
  15.         // TODO If we do lazy conversion, we need to find out about the encoding  
  16.         //  
  17.            int fieldNr = 1;  
  18.         int nrcols=rm.getColumnCount();   
  19.         for (int i=1;i<=nrcols;i++)  
  20.         {  
  21.             String name=new String(rm.getColumnName(i));  
  22.                  
  23.                // Check the name, sometimes it's empty.  
  24.                //  
  25.                if (Const.isEmpty(name) || Const.onlySpaces(name))  
  26.                {  
  27.                    name = "Field"+fieldNr;  
  28.                    fieldNr++;  
  29.                }  
  30.                  
  31.             ValueMetaInterface v = getValueFromSQLType(name, rm, i, ignoreLength, lazyConversion);  
  32.             rowMeta.addValueMeta(v);              
  33.         }  
  34.         return rowMeta;  
  35.     }  
  36.     catch(SQLException ex)  
  37.     {  
  38.         throw new KettleDatabaseException("Error getting row information from database: ", ex);  
  39.     }  
  40. }  

 就是这样”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~


转载自:http://vase.iteye.com/blog/1525852

    如果公司内一开始没有好好规划数据库建设,那么后期可能存在多种字符集的数据库实例。在做数据仓库或者来回导数据的时候,因字符集导致中文乱码问题困扰着不少人。网上有很多前辈们总结的解决中文乱码的方案,关于使用kettle如何解决也有一两篇谈到在建数据库连接时加characterEncoding来解决。我昨晚找到另外一种方式来跟大家分享:

 

经过对源码搜索”encoding“,找一句注释,发现其实解决方法很简单,

Java代码   收藏代码
  1. /** 
  2.  * Build the row using ResultSetMetaData rsmd 
  3.     * @param rm The resultset metadata to inquire 
  4.     * @param ignoreLength true if you want to ignore the length (workaround for MySQL bug/problem) 
  5.     * @param lazyConversion true if lazy conversion needs to be enabled where possible 
  6.  */  
  7. private RowMetaInterface getRowInfo(ResultSetMetaData rm, boolean ignoreLength, boolean lazyConversion) throws KettleDatabaseException  
  8. {  
  9.        if (rm==nullreturn null;  
  10.       
  11.     rowMeta = new RowMeta();  
  12.       
  13.     try  
  14.     {  
  15.         // TODO If we do lazy conversion, we need to find out about the encoding  
  16.         //  
  17.            int fieldNr = 1;  
  18.         int nrcols=rm.getColumnCount();   
  19.         for (int i=1;i<=nrcols;i++)  
  20.         {  
  21.             String name=new String(rm.getColumnName(i));  
  22.                  
  23.                // Check the name, sometimes it's empty.  
  24.                //  
  25.                if (Const.isEmpty(name) || Const.onlySpaces(name))  
  26.                {  
  27.                    name = "Field"+fieldNr;  
  28.                    fieldNr++;  
  29.                }  
  30.                  
  31.             ValueMetaInterface v = getValueFromSQLType(name, rm, i, ignoreLength, lazyConversion);  
  32.             rowMeta.addValueMeta(v);              
  33.         }  
  34.         return rowMeta;  
  35.     }  
  36.     catch(SQLException ex)  
  37.     {  
  38.         throw new KettleDatabaseException("Error getting row information from database: ", ex);  
  39.     }  
  40. }  

 就是这样”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~

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值