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());
        }
    }
}
相关推荐
孟陬1 小时前
国外技术周刊 #1:Paul Graham 重新分享最受欢迎的文章《创作者的品味》、本周被划线最多 YouTube《如何在 19 分钟内学会 AI》、为何我不
java·前端·后端
想用offer打牌1 小时前
一站式了解四种限流算法
java·后端·go
华仔啊1 小时前
Java 开发千万别给布尔变量加 is 前缀!很容易背锅
java
也些宝2 小时前
Java单例模式:饿汉、懒汉、DCL三种实现及最佳实践
java
Nyarlathotep01133 小时前
SpringBoot Starter的用法以及原理
java·spring boot
wuwen53 小时前
WebFlux + Lettuce Reactive 中 SkyWalking 链路上下文丢失的修复实践
java
SimonKing3 小时前
GitHub 10万星的OpenCode,正在悄悄改变我们的工作流
java·后端·程序员
Seven974 小时前
虚拟线程深度解析:轻量并发编程的未来趋势
java
雨中飘荡的记忆14 小时前
ElasticJob分布式调度从入门到实战
java·后端