自定义ArrayWritable

*主要在 重写 readFields 时要定义values的长度,我在这卡了很久。

public static class MyMapper extends Mapper<LongWritable,Text,Text,IntArrayWritable>{
		@Override
		protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, IntArrayWritable>.Context context)
				throws IOException, InterruptedException {
			String[] splited = value.toString().split("\t");
			String msisdn = splited[1];
			Text k2 = new Text(msisdn);
			String[] array=new String[4];
			array[0]=splited[6];
			array[1]=splited[7];
			array[2]=splited[8];
			array[3]=splited[9];
			context.write(k2, new IntArrayWritable(array));
		}
		
		
	}

	public static class MyReduer extends Reducer<Text,IntArrayWritable,Text,NullWritable>{
		String k3=null;
		@Override
		protected void reduce(Text k2, Iterable<IntArrayWritable> v2s,
				Reducer<Text, IntArrayWritable, Text, NullWritable>.Context context) throws IOException, InterruptedException {
			long upPackNum = 0L;
			long downPackNum = 0L;
			long upPayLoad = 0L;
			long downPayLoad = 0L;
			
			for (IntArrayWritable kpiWritable : v2s) {
				String[] writable= kpiWritable.toStrings();
				upPackNum += Integer.parseInt(writable[0]);
				downPackNum += Integer.parseInt(writable[1]);
				upPayLoad += Integer.parseInt(writable[2]);
				downPayLoad += Integer.parseInt(writable[3]);
			}
			 k3=k2+"\t"+upPackNum+"\t"+downPackNum+"\t"+upPayLoad+"\t"+downPayLoad;
			       k2=new Text(k3);
			context.write(k2, NullWritable.get());
		}
	}

	public static void main(String[] args) throws Exception{
		Configuration conf=new Configuration();
		Job job=Job.getInstance(conf,TrafficStatistics2.class.getSimpleName());
		
		job.setJarByClass(TrafficStatistics2.class);
		
		job.setMapperClass(MyMapper.class);
		job.setReducerClass(MyReduer.class);
		
		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(IntArrayWritable.class);
		
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(NullWritable.class);
		
		FileInputFormat.setInputPaths(job,new Path("hdfs://193.124.1.120:9000/HTTP_20130313143750.dat"));
		FileOutputFormat.setOutputPath(job,new Path("hdfs://193.124.1.120:9000/TrafficStatistics_out1"));
		
		job.waitForCompletion(true);
		
	}

}


class IntArrayWritable extends ArrayWritable {
	
	private Long[] values=null;
	
	    public IntArrayWritable() { 
	    	 super(LongWritable.class); 
	    }
	    
	    public IntArrayWritable(String[] strings) {
	    	super(LongWritable.class);
	    	values=new Long[strings.length];
	        for (int i = 0; i < strings.length; i++) {
	        	values[i] =Long.parseLong(strings[i]);
	        }
	        
	      }
	    
	    @Override
	    public String[] toStrings() {
	        String[] strings = new String[values.length];
	        for (int i = 0; i < values.length; i++) {
	          strings[i] = values[i]+"";
	        }
	        return strings;
	      }
	    
	    
	    @Override
	    public void readFields(DataInput in) throws IOException {
	    	values = new Long[4];
	    	for (int i = 0; i < values.length; i++) {
	        values[i] = in.readLong();                          // store it in values
	      }
	    }

	    @Override
	    public void write(DataOutput out) throws IOException {
	      for (int i = 0; i < values.length; i++) {
	        out.writeLong(values[i]);
	      }
	    }
	    


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值