totalsalary

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;


import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;


import org.apache.hadoop.io.Text;




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.Reducer;
import org.apache.hadoop.util.GenericOptionsParser;


public class TotalSalary {

enum Counter{
LINESKIP;
}


public static class TSMapper extends Mapper<LongWritable,Text,Text,Text>{
private String str;
private String[] arr;
private Text dep=new Text();
private Text salary=new Text();
public void map(LongWritable key,Text value,Context context) throws IOException,InterruptedException{
str=value.toString();
arr=str.split(",");
try{
if(arr.length==8){
dep.set(arr[7].trim());
salary.set("emp:"+arr[5].trim());
}
else
{
dep.set(arr[0].trim());
salary.set("dep:"+arr[1].trim());
}    
context.write(dep, salary);
}catch(Exception e){
System.out.println(e.getStackTrace());
context.getCounter(Counter.LINESKIP).increment(1);
return;
}

}

}
public static class TSReducer extends Reducer<Text,Text,Text,IntWritable>{
private IntWritable result=new IntWritable();


public void reduce(Text key,Iterable<Text> values,Context context) throws IOException,InterruptedException{
String[] s;
String depname = ""; 
int sum=0;
for(Text val:values){
s=val.toString().split(":");
if("dep".equals(s[0]))
depname=s[1];
else if("emp".equals(s[0]))
    sum+=Integer.parseInt(s[1].toString());

}
key.set(key+","+depname);
result.set(sum);
   context.write(key, result);


}
}
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
conf.set("mapred.job.tracker", "192.168.1.23:9001");
   String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
   if (otherArgs.length != 2) {
     System.err.println("Usage: TotalSalary <in> <out>");
     System.exit(2);
   }
   Job job = new Job(conf, "Total salary");

   job.setJarByClass(TotalSalary.class);
   job.setMapperClass(TSMapper.class);
   //job.setCombinerClass(TSReducer.class);
   job.setReducerClass(TSReducer.class);   // 
   FileSystem fs=FileSystem.get(conf);
   Path out=new Path(args[1]);
   if(fs.exists(out))
    fs.delete(out);
   FileInputFormat.addInputPath(job, new Path(args[0]));
   FileOutputFormat.setOutputPath(job, out); 
   //job.setOutputFormatClass(TextOutputFormat.class);
   job.setOutputKeyClass(Text.class);
   job.setOutputValueClass(Text.class);
   boolean res=job.waitForCompletion(true);
// Print out Job finishing status
        System.out.println( "Job Name: " + job.getJobName() );
        System.out.println( "Job Successful: " + ( job.isSuccessful() ? "Yes" : "No" ) );
        System.out.println( "Lines of Mapper Input: " + job.getCounters().findCounter("org.apache.hadoop.mapred.Task$Counter", "MAP_INPUT_RECORDS").getValue() );
        System.out.println( "Lines of Reducer Output: " + job.getCounters().findCounter("org.apache.hadoop.mapred.Task$Counter", "REDUCE_OUTPUT_RECORDS").getValue() );
        System.out.println( "Lines skipped: " + job.getCounters().findCounter(Counter.LINESKIP).getValue() );
       
   if (res)
        System.exit(0);
   else System.exit(1);
}
}
解释并补充这段代码#include<iostream> using namespace std; #include<string.h> const int N=10000; #define NAMECHARS 10 class Worker{ public: Worker(); Worker(char* _name,int _level); // virtual ~Worker(); protected: // const float baseSalary; char name[NAMECHARS+1]; int level; unsigned int ID; // float salary; static unsigned int count; float reward; public: // static float total; void setName(char* _name); virtual void infoList()=0; void setLevel(int _level); void sum(); static float Average(); friend void setReward(Worker* pWorker,float reward); // friend void setReward(Worker& wk,float reward); void setReward(float _reward); virtual float getSalary()=0; };// 在此处补充你的代码int main() { Worker* pWorkerArr[N] = {NULL}; int n; cin >> n; for(int i=0; i<n; i++) { char name[NAMECHARS+1]; int level; int choice; cin >> name >> level >> choice; switch (choice ) { case 1: { float hour; cin >> hour; if (choice%2) pWorkerArr[i] = (Worker*) new HourWorker(name,level,hour); else { HourWorker *p = new HourWorker(name, level); p->setHour(hour); pWorkerArr[i] = (Worker*)p; } } break; case 2: { int piece; cin >> piece; if (choice % 2) pWorkerArr[i] = (Worker*) new PieceWorker(name,level,piece); else { PieceWorker *p = new PieceWorker(); p->setName(name); p->setLevel(level); p->setPiece(piece); pWorkerArr[i] = (Worker*)p; } } break; } } int index; float reward; cin >> index>>reward; if (index >= 0 && index < n) setReward(pWorkerArr[index], reward); float totalSalary = 0.0f; for( int i=0;i<n;i++) { if (pWorkerArr[i] != NULL) { totalSalary += pWorkerArr[i]->getSalary(); pWorkerArr[i]->infoList(); } } cout << totalSalary << endl; for (int i = 0; i<N; i++) { if (pWorkerArr[i] != NULL) delete pWorkerArr[i]; } return 0; }
04-21
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值