mapreduce输出乱码的处理

mapreduce输出乱码的处理,主要在于数据的编码,默认是以utf-8编码。

若数据源不是以utf-8编码,如以gbk编码的。若在mapreduce中要以gbk进行解码。

核心语句:

复制代码
String str = new String(value.getBytes(), 0, value.getLength(), "GBK");

解释:value.getBytes()是获取字节码数据,转换成字符码是需要加相应编码。

程序实验:

复制代码
package org.two;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class CleanOneDriver {
    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
        Configuration conn = new Configuration();

        Job job = Job.getInstance(conn);

        job.setJarByClass(CleanOneDriver.class);
        job.setMapperClass(CleanOneMapper.class);
        job.setReducerClass(CleanOneReduce.class);

        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(NullWritable.class);

        FileInputFormat.addInputPath(job,new Path(args[0]));
        FileOutputFormat.setOutputPath(job,new Path(args[1]));

        System.exit(job.waitForCompletion(true)?0:1);



    }

    private static class CleanOneMapper extends Mapper<LongWritable,Text,Text,NullWritable> {
        @Override
        protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, NullWritable>.Context context) throws IOException, InterruptedException {
            String str = new String(value.getBytes(), 0, value.getLength(), "GBK");
            context.write(new Text(str),NullWritable.get());
        }
    }

    private static class CleanOneReduce extends Reducer<Text,NullWritable,Text,NullWritable> {
        @Override
        protected void reduce(Text key, Iterable<NullWritable> values, Reducer<Text, NullWritable, Text, NullWritable>.Context context) throws IOException, InterruptedException {
            context.write(key,NullWritable.get());
        }
    }
}
相关推荐
名字还没想好☜7 小时前
Python f-string 进阶:数字格式化、对齐填充、调试 = 号与嵌套表达式
开发语言·数据库·python·字符串格式化·f-string
ltl7 小时前
Serverless 数据库弹性理论:Neon 与 Aurora Serverless v2
数据库
·薯条大王7 小时前
经济实惠玩云服务器|一台云服务器多人共用,子账号配置教程
java·linux·运维·服务器·汇编·c++·python
一壶浊酒..10 小时前
Scrape Webpack练习
开发语言·javascript·webpack
ZJH__GO10 小时前
网络编程v4pro--实现聊天室文件传输功能
java·服务器·网络·计算机网络
程序员黑豆10 小时前
Windows 系统 Java 环境变量配置全攻略:解决“不是内部或外部命令”
java·前端·ai编程
命运之光11 小时前
【C语言完整代码】就诊信息管理系统
java·c语言·开发语言
凤山老林11 小时前
Spring Boot @Async 线上实战:从默认配置到生产级线程池治理
java·spring boot·后端
命运之光11 小时前
【C语言完整代码】图书管理系统:图书馆场景下的增删改查实战
c语言·开发语言
猿长大人11 小时前
C# | Serilog 新手入门
开发语言·c#·.net·log