MapReduce基础开发之十读写ORC File

1、ORC File

Orc是Hive特有的一种列式存储的文件格式,它有着非常高的压缩比和读取效率,因此很快取代了之前的RCFile,成为Hive中非常常用的一种文件格式。

2、编译ORC Jar包

    http://orc.apache.org/ 下载源代码orc-1.2.1.tar.gz编译jar包
   用ubuntu14编译,安装jdk1.8、cmake3.2.2、Maven3.0.5。
  解压orc-1.2.1.tar.gz,进入目录orc-1.2.1/java下,执行mvn package编译生成jar文件。
  编译完成后进入
orc-1.2.1/java/mapreduce/target获取orc-mapreduce-1.2.1.jar
orc-1.2.1/java/core/target获取orc-core-1.2.1.jar
orc-1.2.1/java/tools/target获取orc-tools-1.2.1.jar
orc-1.2.1/java/tools/target获取orc-tools-1.2.1-uber.jar
orc-1.2.1/java/ storage-api /target获取hive-storage-api-2.1.1-pre-orc.jar
四个jar包导入到MapReduce工程lib目录下。
注意提交到hadoop集群时,第三方独立jar要一并打包。

 

3、MR读写ORC File基础代码

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
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 org.apache.orc.mapred.OrcStruct;
import org.apache.orc.mapreduce.OrcInputFormat;
import org.apache.orc.mapreduce.OrcOutputFormat;
import org.apache.orc.TypeDescription;


public class ORCSample {

	public static class ORCMapper extends Mapper<NullWritable, OrcStruct, Text, Text> {
		private Text oKey=new Text();
		private Text oValue=new Text();
		public void map(NullWritable key, OrcStruct value, Context context) throws IOException, InterruptedException {
			//要知道OrcStruct存储的结构
			StringBuffer bf = new StringBuffer();			
			if(value.getNumFields()==3){
				Text valAcount=(Text)value.getFieldValue(0);
				Text valDomain=(Text)value.getFieldValue(1);
				Text valPost=(Text)value.getFieldValue(2);
				bf.append(valAcount.toString()).append("|").append(valDomain.toString()).append("|").append(valPost.toString());
			}
	  	    if (bf.length() > 0) oValue.set(bf.toString());
	  	    else oValue.set("");
	  	  oKey.set("");
	  	  context.write(oKey,oValue);
		}
	}
	/*
	public static class ORCReducer extends Reducer<Text, Text, Text, Text> {
		public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
			for(Text value:values){
				String strVal=value.toString();
				Text okey=new Text();
				okey.set(strVal);
				context.write(okey,null);
			}
		}
	}*/
	
	public static class ORCReducer extends Reducer<Text, Text, NullWritable, OrcStruct> {
		//具体OrcStruct字段对应hadoop的定义参考https://orc.apache.org/docs/mapreduce.html
		private TypeDescription schema = TypeDescription.fromString("struct<account:string,domain:string,post:string>");
		private OrcStruct orcs = (OrcStruct) OrcStruct.createValue(schema);
		private final NullWritable nw = NullWritable.get();
		public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
			for (Text val : values) {
				if(val.toString() ==null) continue;
                String[] strVals=val.toString().split("\\|");
				if(strVals.length==3){
					Text txtAccount=new Text();
					txtAccount.set(strVals[0]);
					orcs.setFieldValue(0, txtAccount);
					Text txtDomain=new Text();
					txtAccount.set(strVals[1]);
					orcs.setFieldValue(1, txtDomain);
					Text txtPost=new Text();
					txtAccount.set(strVals[2]);
					orcs.setFieldValue(2, txtPost);
					context.write(nw, orcs);
				}				
			}
		}
	}
    
	public static void main(String args[]) throws Exception {
		Configuration conf = new Configuration();
		//要设置结构,否则reduce会提示输入空值
		conf.set("orc.mapred.output.schema","struct<account:string,domain:string,post:string>");
		Job job = new Job(conf, "ORCSample");	
		job.setJarByClass(ORCSample.class);
		job.setMapperClass(ORCMapper.class);
		job.setReducerClass(ORCReducer.class);
		//map类型设置
		job.setInputFormatClass(OrcInputFormat.class);
		job.setMapOutputKeyClass(Text.class);//优先于setOutputKeyClass生效于map
		job.setMapOutputValueClass(Text.class);
		//reduce类型设置	
		job.setNumReduceTasks(1);
		job.setOutputFormatClass(OrcOutputFormat.class);
		job.setOutputKeyClass(NullWritable.class);
		job.setOutputValueClass(OrcStruct.class);
		//job.setOutputKeyClass(Text.class);//对map和reduce输出都生效
		//job.setOutputValueClass(Text.class);
		//输入输出路径
		FileInputFormat.addInputPath(job, new Path(args[0]));
		FileOutputFormat.setOutputPath(job, new Path(args[1]));
		System.exit(job.waitForCompletion(true) ? 0 : 1);
	}
}


    

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MapReduce是一种分布式计算框架,它可以并行处理大数据集。MapReduce框架由两个主要的阶段组成:Map阶段和Reduce阶段。在Map阶段中,数据被划分成一个个的小块,并且每个小块都被传递到映射函数中进行处理。在Reduce阶段中,Map阶段的结果被收集并且组合成最终的输出。以下是MapReduce基础编程的步骤: 1. 输入数据的划分:将输入数据分成若干个小块,每个小块可以被独立处理。 2. Map函数:定义一个Map函数,在Map阶段中对输入数据进行转换,将输入数据处理成键值对的形式。 3. Shuffle阶段:在Map阶段中,每个Map函数都会产生键值对,Shuffle阶段的目的是将相同键的值聚合在一起,并将它们传递给Reduce函数。 4. Reduce函数:定义一个Reduce函数,它接收来自Shuffle阶段的键值对,并将它们组合成最终的输出。 5. 输出数据:将最终的输出数据存储到一个文件中。 在MapReduce编程中,我们需要注意以下几点: 1. Map函数和Reduce函数应该是幂等的,在相同的输入数据下,它们应该产生相同的输出数据。 2. 在Map函数中,我们应该尽量避免使用全局变量,因为它们可能会被多个Map函数共享。 3. 在Reduce函数中,我们应该尽量避免使用数据库或者其他外部存储系统,因为它们可能会成为瓶颈。 4. 在编写Map函数和Reduce函数时,我们应该考虑到数据的分布情况,尽量避免数据倾斜问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值