zoukankan      html  css  js  c++  java
  • Hadoop 中文编码相关问题 -- mapreduce程序处理GBK编码数据并输出GBK编码数据

    Hadoop 中文编码相关问题 -- mapreduce程序处理GBK编码数据并输出GBK编码数据

    输入是GBK文件, 输出也是 GBK 文件的示例代码:

    Hadoop处理GBK文本时,发现输出出现了乱码,原来HADOOP在涉及编码时都是写死的UTF-8,如果文件编码格式是其它类型(如GBK),则会出现乱码。

    此时只需在mapper或reducer程序中读取Text时,使用transformTextToUTF8(text, "GBK");进行一下转码,以确保都是以UTF-8的编码方式在运行。

      
    
    
    1. public static Text transformTextToUTF8(Text text, String encoding) {
    2. String value = null;
    3. try {
    4. value = new String(text.getBytes(), 0, text.getLength(), encoding);
    5. } catch (UnsupportedEncodingException e) {
    6. e.printStackTrace();
    7. }
    8. return new Text(value);
    9. }
     

    这里核心代码是: String line=new String(text.getBytes(),0,text.getLength(),"GBK"); //这里的value是Text类型

    若直接使用 String line=value.toString(); 会输出乱码, 这是由Text这个Writable类型造成的。初学时,一直认为和LongWritable对long的封装一样,Text类型是String的Writable封装。但其实Text和String还是有些区别,它是一种UTF-8格式的Writable,而Java中的String是Unicode字符。所以直接使用value.toString()方法,会默认其中的字符都是UTF-8编码过的,因而原本GBK编码的数据使用Text读入后直接使用该方法就会变成乱码。

    正确的方法是将输入的Text类型的value转换为字节数组(value.getBytes()),使用String的构造器String(byte[] bytes, int offset, int length, Charset charset),通过使用指定的charset解码指定的byte子数组,构造一个新的String。

    如果需要map/reduce输出其它编码格式的数据,需要自己实现OutputFormat,在其中指定编码方式,而不能使用默认的TextOutputFormat。

    具体的范例可以见淘宝数据平台与产品部官方博客上的博文 http://www.tbdata.org/archives/244 。 

    来自:  Hadoop的map/reduce作业输入非UTF-8编码数据的处理原理

    以下摘自 淘宝数据平台与产品部官方博客:

    1 中文问题
        从url中解析出中文,但hadoop中打印出来仍是乱码?我们曾经以为hadoop是不支持中文的,后来经过查看源代码,发现hadoop仅仅是不支持以gbk格式输出中文而己。

        这是TextOutputFormat.class中的代码,hadoop默认的输出都是继承自FileOutputFormat来的,FileOutputFormat的两个子类一个是基于二进制流的输出,一个就是基于文本的输出TextOutputFormat。

        public static class TextOutputFormat<K, V> extends FileOutputFormat<K, V> {
      protected static class LineRecordWriter<K, V>
        implements RecordWriter<K, V> {
        private static final String utf8 = “UTF-8″;//这里被写死成了utf-8
        private static final byte[] newline;
        static {
          try {
            newline = “ ”.getBytes(utf8);
          } catch (UnsupportedEncodingException uee) {
            throw new IllegalArgumentException(“can’t find ” + utf8 + ” encoding”);
          }
        }

        public LineRecordWriter(DataOutputStream out, String keyValueSeparator) {
          this.out = out;
          try {
            this.keyValueSeparator = keyValueSeparator.getBytes(utf8);
          } catch (UnsupportedEncodingException uee) {
            throw new IllegalArgumentException(“can’t find ” + utf8 + ” encoding”);
          }
        }

        private void writeObject(Object o) throws IOException {
          if (o instanceof Text) {
            Text to = (Text) o;
            out.write(to.getBytes(), 0, to.getLength());//这里也需要修改
          } else {
            out.write(o.toString().getBytes(utf8));
          }
        }
     …
    }
        可以看出hadoop默认的输出写死为utf-8,因此如果decode中文正确,那么将Linux客户端的character设为utf-8是可以看到中文的。因为hadoop用utf-8的格式输出了中文。
        因为大多数数据库是用gbk来定义字段的,如果想让hadoop用gbk格式输出中文以兼容数据库怎么办?
        我们可以定义一个新的类:
        public class GbkOutputFormat<K, V> extends FileOutputFormat<K, V> {
      protected static class LineRecordWriter<K, V>
        implements RecordWriter<K, V> {
        //写成gbk即可
        private static final String gbk = “gbk”;
        private static final byte[] newline;
        static {
          try {
            newline = “ ”.getBytes(gbk);
          } catch (UnsupportedEncodingException uee) {
            throw new IllegalArgumentException(“can’t find ” + gbk + ” encoding”);
          }
        }

        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”);
          }
        }

        private void writeObject(Object o) throws IOException {
          if (o instanceof Text) {
                 //  Text to = (Text) o;
                 //  out.write(to.getBytes(), 0, to.getLength());
                //  } else {

            out.write(o.toString().getBytes(gbk));
          }
        }
     …
    }
        然后在mapreduce代码中加入conf1.setOutputFormat(GbkOutputFormat.class)
        即可以gbk格式输出中文。

  • 相关阅读:
    使用 yo 命令行向导给 SAP UI5 应用添加一个新的视图
    SAP Fiori Elements 应用的 manifest.json 文件运行时如何被解析的
    SAP UI5 标准应用的多语言支持
    微软 Excel 365 里如何设置下拉菜单和自动高亮成指定颜色
    SAP Fiori Elements 应用里的 Title 显示的内容是从哪里来的
    本地开发好的 SAP Fiori Elements 应用,如何部署到 ABAP 服务器上?
    如何在 Cypress 测试代码中屏蔽(Suppress)来自应用代码报出的错误消息
    教你一招:让集群慢节点无处可藏
    应用架构步入“无服务器”时代 Serverless技术迎来新发展
    MySQL数据库事务隔离性的实现
  • 原文地址:https://www.cnblogs.com/beiyi888/p/9558503.html
Copyright © 2011-2022 走看看