hadoop 中文乱码问题解决
2013-03-09 22:22
295 查看
当做分析的时候有时候会接触到中文。因为hadoop默认都是UTF8格式。。读了一下源码,又结合网上的说法重新了一下FileOutPutFormat
其实hadoop原本在操作的时候一直操作的是 字节组的载入。问题出在我们手写部分 Text.toString() 方法,他被默认转换成UTF8格式的了。
手写的mapper 方法中要注意转换成GBK格式,以确保从头到尾都是GBK操作。
String str=new String(value.getBytes[],"GBK") ;
这样中文问题就解决了
其实hadoop原本在操作的时候一直操作的是 字节组的载入。问题出在我们手写部分 Text.toString() 方法,他被默认转换成UTF8格式的了。
public class GbkOutputFormat<K, V> extends FileOutputFormat<K, V> { protected static class LineRecordWriter<K, V> extends RecordWriter<K, V> { private static final String gbk = "gbk"; private static final byte[] newline; static { try { newline = "\n".getBytes(gbk); } catch (UnsupportedEncodingException uee) { throw new IllegalArgumentException("can't find " + gbk + " encoding"); } } protected DataOutputStream out; private final byte[] keyValueSeparator; public LineRecordWriter(DataOutputStream out, String keyValueSeparator) { this.out = out; try { this.keyValueSeparator = keyValueSeparator.getBytes(gbk); } catch (UnsupportedEncodingException uee) { throw new IllegalArgumentException("can't find " + gbk + " encoding"); } } public LineRecordWriter(DataOutputStream out) { this(out, "\t"); } /** * Write the object to the byte stream, handling Text as a special * case. * @param o the object to print * @throws IOException if the write throws, we pass it on */ private void writeObject(Object o) throws IOException { if (o instanceof Text) { out.write(o.toString().getBytes(gbk)); } } public synchronized void write(K key, V value) throws IOException { boolean nullKey = key == null || key instanceof NullWritable; boolean nullValue = value == null || value instanceof NullWritable; if (nullKey && nullValue) { return; } if (!nullKey) { writeObject(key); } if (!(nullKey || nullValue)) { out.write(keyValueSeparator); } if (!nullValue) { writeObject(value); } out.write(newline); } public synchronized void close(TaskAttemptContext context) throws IOException { out.close(); } } public RecordWriter<K, V> getRecordWriter(TaskAttemptContext job ) throws IOException, InterruptedException { Configuration conf = job.getConfiguration(); boolean isCompressed = getCompressOutput(job); String keyValueSeparator= conf.get("mapred.textoutputformat.separator", "\t"); CompressionCodec codec = null; String extension = ""; if (isCompressed) { Class<? extends CompressionCodec> codecClass = getOutputCompressorClass(job, GzipCodec.class); codec = (CompressionCodec) ReflectionUtils.newInstance(codecClass, conf); extension = codec.getDefaultExtension(); } Path file = getDefaultWorkFile(job, extension); FileSystem fs = file.getFileSystem(conf); if (!isCompressed) { FSDataOutputStream fileOut = fs.create(file, false); return new LineRecordWriter<K, V>(fileOut, keyValueSeparator); } else { FSDataOutputStream fileOut = fs.create(file, false); return new LineRecordWriter<K, V>(new DataOutputStream (codec.createOutputStream(fileOut)), keyValueSeparator); } } }
手写的mapper 方法中要注意转换成GBK格式,以确保从头到尾都是GBK操作。
String str=new String(value.getBytes[],"GBK") ;
这样中文问题就解决了
相关文章推荐
- 中文乱码问题解决
- jquery向.ashx文件post中文乱码问题的解决方法
- 解决Ajax中文乱码问题
- 解决Url带中文参数乱码问题
- JSP中文乱码问题已经解决方法
- 解决response.setHeader()下载中文文件名乱码问题
- 解决AJAX传送中文会导致乱码的问题
- 配置Tomcat解决URL传参引出的中文乱码问题
- 解决SpringMVC中文乱码问题以及配置不生效问题
- MySQL 5.5版本解决中文乱码问题时my.ini内[mysqld]项中不能再写default-character-set=utf8
- JSP+Servlet学习经验1:解决form表单提交后相应页面中文乱码的问题
- 解决QT读Visual Fox Pro DBF中文乱码问题
- ssh 解决中文显示乱码问题
- ubuntu下MySQL数据库中文乱码问题解决
- 解决Xshell显示中文乱码的问题
- 解决Get和Post请求中中文乱码问题
- Struts 从零开始 二、解决中文乱码的问题实例
- 利用tomcat解决get提交中文乱码问题
- php解决中文截取乱码问题
- 解决中文字符乱码问题,如何将gbk格式转换utf-8格式