secondarysort

来源:互联网 发布:淘宝主图厂家直销 编辑:程序博客网 时间:2024/05/22 01:39
package com.test;/** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */import java.io.DataInput;import java.io.DataOutput;import java.io.IOException;import java.text.ParseException;import java.text.SimpleDateFormat;/*import java.util.Date;import java.util.StringTokenizer;*/import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.Path;import org.apache.hadoop.io.LongWritable;import org.apache.hadoop.io.RawComparator;import org.apache.hadoop.io.Text;import org.apache.hadoop.io.WritableComparable;import org.apache.hadoop.io.WritableComparator;import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;import org.apache.hadoop.mapreduce.Job;import org.apache.hadoop.mapreduce.Mapper;import org.apache.hadoop.mapreduce.Partitioner;import org.apache.hadoop.mapreduce.Reducer;import org.apache.hadoop.util.GenericOptionsParser;/** * This is an example Hadoop Map/Reduce application. * It reads the text input files that must contain two integers per a line. * The output is sorted by the first and second number and grouped on the * first number. * * To run: bin/hadoop jar build/hadoop-examples.jar secondarysort * in-dir out-dir */public class SecondarySort { /** * Define a pair of integers that are writable. * They are serialized in a byte comparable format. */ public static class NewPair implements WritableComparable { private Text first; private Text second = new Text(); /** * Set the left and right values. */ public void set(Text left, Text right) { first = left; second = right; } public Text getFirst() { return first; } public Text getSecond() { return second; } /** * Read the two integers. * Encoded as: MIN_VALUE -> 0, 0 -> -MIN_VALUE, MAX_VALUE-> -1 */ @Override public void readFields(DataInput in) throws IOException { first=new Text(); first.readFields(in); second=new Text(); second.readFields(in); /*first=new Text(in.readUTF()); second=new Text(in.readUTF());*/ } @Override public void write(DataOutput out) throws IOException { first.write(out); /*out.writeChars(first.toString()); out.writeChars("\n");*/ second.write(out); //out.writeChars(second.toString()); } @Override public String toString() { return first + ";" + second+";"; } @Override public int hashCode() { return first.hashCode()*163+ second.hashCode(); } @Override public boolean equals(Object right) { if (right instanceof NewPair) { NewPair r = (NewPair) right; return r.first.equals(first) && r.second.equals(second); } else { return false; } } /** A Comparator that compares serialized NewPair. */ public static class Comparator extends WritableComparator { public Comparator() { super(NewPair.class); } public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) { return compareBytes(b1, s1, l1, b2, s2, l2); } } static { // register this comparator WritableComparator.define(NewPair.class, new Comparator()); } public boolean comparedate(String datestr0,String datestr1){ SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try {if (sdf.parse(datestr0).before(sdf.parse(datestr1))) return true;} catch (ParseException e) {// TODO Auto-generated catch blocke.printStackTrace();} return false; } @Override public int compareTo(NewPair o) { String temp0=new String(); int temp1; String osecond0=new String(); int osecond1; if (first != o.first) { return first.compareTo(o.first); } else if (second != o.second) { temp0=second.toString().split(".")[0]; osecond0=o.second.toString().split(".")[0]; osecond1=Integer.parseInt(o.second.toString().split(".")[1]); temp1=Integer.parseInt(second.toString().split(".")[1]); if (temp0!=osecond0){ if (comparedate(temp0,osecond0)) return 1; else return -1;} else { if(temp1<osecond1) return 1; else return -1; } } else { return 0; } } } /** * Partition based on the first part of the pair. */ public static class FirstPartitioner extends Partitioner{ @Override public int getPartition(NewPair key, Text value, int numPartitions) { return Math.abs(key.getFirst().hashCode() * 127) % numPartitions; } } /** * Compare only the first part of the pair, so that reduce is called once * for each value of the first part. */ public static class FirstGroupingComparator implements RawComparator { @Override public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) { return WritableComparator.compareBytes(b1, s1, Integer.SIZE/8, b2, s2, Integer.SIZE/8); } @Override public int compare(NewPair o1, NewPair o2) { /*int l = o1.getFirst(); int r = o2.getFirst(); return l == r ? 0 : (l < r ? -1 : 1);*/ return o1.getFirst().compareTo(o2.getFirst()); //return o1.compareTo(o2); } } /** * Read two integers from each line and generate a key, value pair * as ((left, right), right). */ public static class Map extends Mapper { private NewPair key = new NewPair(); private Text value = new Text(); private String[] strarr; private Text left=new Text(); private Text right=new Text(); @Override public void map(LongWritable inKey, Text inValue, Context context) throws IOException, InterruptedException { //StringTokenizer itr = new StringTokenizer(inValue.toString()); strarr=inValue.toString().split("\t"); left.set(strarr[0]); //int right = 0; right.set(strarr[3]); key.set(left, right); value.set(strarr[1]+";"+strarr[2]+";"+strarr[4]+";"+strarr[5]+";"+strarr[6]+";"+strarr[7]); //value.set(key.getFirst().toString()+key.getSecond().toString()); context.write(key, value); } } /** * A reducer class that just emits the sum of the input values. */ public static class Reduce extends Reducer { //private static final Text SEPARATOR = //new Text("------------------------------------------------"); private Text newKey = new Text(); @Override public void reduce(NewPair key, Iterable values, Context context ) throws IOException, InterruptedException { // context.write(SEPARATOR, null); //newKey.set(key.toString());//key.getFirst()+";"+key.getSecond()+";"); for(Text value: values) { newKey.set(key.toString()); context.write(newKey, value); } } } public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs(); if (otherArgs.length != 2) { System.err.println("Usage: secondarysrot "); System.exit(2); } Job job = new Job(conf, "secondary sort"); job.setJarByClass(SecondarySort.class); job.setMapperClass(Map.class); job.setReducerClass(Reduce.class); // group and partition by the first int in the pair job.setPartitionerClass(FirstPartitioner.class); job.setGroupingComparatorClass(FirstGroupingComparator.class); // the map output is NewPair, IntWritable job.setMapOutputKeyClass(NewPair.class); job.setMapOutputValueClass(Text.class); // the reduce output is Text, IntWritable job.setOutputKeyClass(Text.class); job.setOutputValueClass(Text.class); FileInputFormat.addInputPath(job, new Path(otherArgs[0])); FileOutputFormat.setOutputPath(job, new Path(otherArgs[1])); System.exit(job.waitForCompletion(true) ? 0 : 1); }}
原创粉丝点击