为什么我二次排序排不了呢?代码如下

我想飞走 2013-05-07 03:19:53
public class WritableSample  extends Configured implements Tool{

public static void main(String[] args) throws Exception {
ToolRunner.run(new WritableSample(), args);

}

@Override
public int run(String[] args) throws Exception {
Configuration conf = getConf();
Job job = new Job(conf);
job.setJarByClass(getClass());

FileSystem fs = FileSystem.get(conf);
fs.delete(new Path("out"), true);

FileInputFormat.addInputPath(job, new Path("sample.txt"));
FileOutputFormat.setOutputPath(job, new Path("out"));

job.setMapperClass(MyWritableMap.class);

job.setOutputKeyClass(MyPairWritable.class);
job.setOutputValueClass(NullWritable.class);



job.setSortComparatorClass(PairKeyComparator.class);


job.waitForCompletion(true);
return 0;
}




}




class MyWritableMap extends Mapper<LongWritable, Text, MyPairWritable, NullWritable>{
MyPairWritable pair= new MyPairWritable();
protected void map(LongWritable key, Text value, Context context) throws java.io.IOException ,InterruptedException {
String[] strs = value.toString().split(" ");
Text keyy = new Text(strs[0]);
IntWritable valuee = new IntWritable(Integer.parseInt(strs[1]));
pair.set(keyy, valuee);
context.write(pair, NullWritable.get());

};
}

class MyWritableReduce extends Reducer<MyPairWritable, NullWritable,MyPairWritable, NullWritable>{
protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws java.io.IOException ,InterruptedException {

};
}

class PairKeyComparator extends WritableComparator{

public PairKeyComparator() {
super(MyPairWritable.class,true);
}
@Override
public int compare(WritableComparable a, WritableComparable b) {

MyPairWritable p1 = (MyPairWritable)a;
MyPairWritable p2 = (MyPairWritable)b;
System.out.println(p1 + "___" + p2);
if(p1.getFirst() != p2.getFirst()){
return p1.first.toString().compareTo(p2.toString());
}else if(p1.second != p2.getSecond()){
return p1.second.get() - p2.getSecond().get();
}
else return 0;
}

}

class MyPairPartitioner extends Partitioner<MyPairWritable, NullWritable>{
public int getPartition(MyPairWritable key, NullWritable value, int numPartitions) {
if(key.getFirst().toString().startsWith("aaa"))
return 1;
if(key.getFirst().toString().startsWith("bbb"))
return 2;
if(key.getFirst().toString().startsWith("ccc"))
return 3;
else return 0;
};
}





class MyPairWritable implements WritableComparable<MyPairWritable>{
Text first;
IntWritable second;

public void set(Text first, IntWritable second)
{
this.first = first;
this.second = second;
}
public Text getFirst()
{
return first;
}
public IntWritable getSecond()
{
return second;
}

@Override
public void readFields(DataInput in) throws IOException {
// TODO Auto-generated method stub
first = new Text(in.readUTF());
second = new IntWritable(in.readInt());
}

@Override
public void write(DataOutput out) throws IOException {
out.writeUTF(first.toString());
out.writeInt(second.get());
}

@Override
public int compareTo(MyPairWritable o) {
if(this.first != o.getFirst()){
return this.first.toString().compareTo(o.toString());
}else if(this.second != o.getSecond()){
return this.second.get() - o.getSecond().get();
}
else return 0;
}

@Override
public String toString() {
// TODO Auto-generated method stub
return first.toString() + " " + second.get();
}
}
...全文
4492 33 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
33 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
wn520008 2013-05-16
  • 打赏
  • 举报
回复
楼主代码不加注释下次别发上来了。
naruto2000 2013-05-16
  • 打赏
  • 举报
回复
来学习一下
u010693711 2013-05-14
  • 打赏
  • 举报
回复
等我试一下~~~~~~
mbugaifc 2013-05-13
  • 打赏
  • 举报
回复
ftjavayp 2013-05-13
  • 打赏
  • 举报
回复
wewonder 2013-05-13
  • 打赏
  • 举报
回复
楼主有功率分配和中继位置相关的代码吗?小弟毕业设计急用!
a903669695 2013-05-13
  • 打赏
  • 举报
回复
唉!没知识没文化连看都看不懂呀!
u010676832 2013-05-13
  • 打赏
  • 举报
回复
尼玛什么更什么啊
薛123 2013-05-12
  • 打赏
  • 举报
回复
大卫219 2013-05-12
  • 打赏
  • 举报
回复
来 学习下!
u010669486 2013-05-12
  • 打赏
  • 举报
回复
来学下个
zq_lwl008 2013-05-12
  • 打赏
  • 举报
回复
顶,很不错。
line_us 2013-05-11
  • 打赏
  • 举报
回复
观摩高手意见。
qyxqyxqyx 2013-05-11
  • 打赏
  • 举报
回复
有c++的开源库吗?并行计算的,hadoop的
Logo525 2013-05-11
  • 打赏
  • 举报
回复
留学
mapleleaf1314 2013-05-11
  • 打赏
  • 举报
回复
来学习的!!!!!
fangyinlong 2013-05-11
  • 打赏
  • 举报
回复
来学习的!!!
u010453530 2013-05-11
  • 打赏
  • 举报
回复
来瞅瞅。。。
u010655270 2013-05-11
  • 打赏
  • 举报
回复
是来学习的 经常来看
加载更多回复(9)

20,848

社区成员

发帖
与我相关
我的任务
社区描述
Hadoop生态大数据交流社区,致力于有Hadoop,hive,Spark,Hbase,Flink,ClickHouse,Kafka,数据仓库,大数据集群运维技术分享和交流等。致力于收集优质的博客
社区管理员
  • 分布式计算/Hadoop社区
  • 涤生大数据
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧