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());
        }
    }
}
相关推荐
xiaoye-duck1 分钟前
C++ STL set 系列深度解析:从底层原理、核心接口到实战场景
开发语言·c++·stl
小涛不学习2 分钟前
Java高频面试题(带答案版)
java·开发语言
big_rabbit05022 分钟前
JVM堆内存查看命令
java·linux·算法
Liu628884 分钟前
Web开发与API
jvm·数据库·python
m0_662577974 分钟前
C++中的RAII技术深入
开发语言·c++·算法
m0_743470377 分钟前
Python字典与集合:高效数据管理的艺术
jvm·数据库·python
2501_945423547 分钟前
使用Scrapy框架构建分布式爬虫
jvm·数据库·python
yhole9 分钟前
MySQL无法连接到本地localhost的解决办法2024.11.8
数据库·mysql·adb
学习要积极11 分钟前
Springboot图片验证码-EasyCaptcha
java·spring boot·后端
2401_8512729911 分钟前
使用Python进行量化交易入门
jvm·数据库·python