如果公司内一开始没有好好规划数据库建设,那么后期可能存在多种字符集的数据库实例。在做数据仓库或者来回导数据的时候,因字符集导致中文乱码问题困扰着不少人。网上有很多前辈们总结的解决中文乱码的方案,关于使用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);
- }
- }
就是这样”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“,找一句注释,发现其实解决方法很简单,
- /**
- * 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~
转载自:http://vase.iteye.com/blog/1525852
如果公司内一开始没有好好规划数据库建设,那么后期可能存在多种字符集的数据库实例。在做数据仓库或者来回导数据的时候,因字符集导致中文乱码问题困扰着不少人。网上有很多前辈们总结的解决中文乱码的方案,关于使用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);
- }
- }
就是这样”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~