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());
        }
    }
}
相关推荐
lekami_兰16 小时前
Java 并发工具类详解:4 大核心工具 + 实战场景,告别 synchronized
java·并发工具
有位神秘人16 小时前
Android中Notification的使用详解
android·java·javascript
云小逸16 小时前
【nmap源码学习】 Nmap网络扫描工具深度解析:从基础参数到核心扫描逻辑
网络·数据库·学习
肉包_51116 小时前
两个数据库互锁,用全局变量互锁会偶发软件卡死
开发语言·数据库·c++
大空大地202616 小时前
流程控制语句--if语句
开发语言
霖霖总总17 小时前
[小技巧64]深入解析 MySQL InnoDB 的 Checkpoint 机制:原理、类型与调优
数据库·mysql
tb_first17 小时前
LangChain4j简单入门
java·spring boot·langchain4j
毕设源码-邱学长17 小时前
【开题答辩全过程】以 基于PHP的发热病人管理平台的设计与实现为例,包含答辩的问题和答案
开发语言·php
HellowAmy17 小时前
我的C++规范 - 线程池
开发语言·c++·代码规范
独自破碎E17 小时前
【BISHI9】田忌赛马
android·java·开发语言