有借鉴,借鉴来源同上一条博客,侵删。
map
public class StepFourMapper extends Mapper{ DecimalFormat df = new DecimalFormat("0.00"); List usersList = new ArrayList (); List
reduce
@Override
protected void reduce(Text key, Iterable values, Context context) throws IOException, InterruptedException {
StringBuffer item_of_sup = new StringBuffer();
for(Text val:values){
item_of_sup.append(val).append(",");
}
// 去掉最后一个逗号
String line = item_of_sup.substring(0,item_of_sup.length()-1);
String tablename = new String("myprojectRepeat");
if(isTableExist(tablename)){
//实例:put student,95001,Sname,LiYing
putDate(tablename,key.toString(),"recommendlist","items",line);
}
else{
createTable(tablename,"recommendlist");
putDate(tablename,key.toString(),"recommendlist","items",line);
}
outKey.set(key);
outValue.set(line);
context.write(outKey,outValue);
}
}
driver
public class StepFourDriver {
//转置评分矩阵路径
static String cache = new String("/llk/myproject/output/step3/part-r-00000");
//相似矩阵路径
static String inputPath = new String(/output/step2/part-r-00000");
static String outputPath = new String("/output/step4/");
public static void main(String[] args) throws InterruptedException, IOException, ClassNotFoundException, URISyntaxException {
// 1 获取配置信息以及获取 job 对象
Configuration conf = new Configuration();
Job job = Job.getInstance(conf);
// 2 关联本 Driver 程序的 jar
job.setJarByClass(StepFourDriver.class);
// 3 关联 Mapper 和 Reducer 的 jar
job.setMapperClass(StepFourMapper.class);
job.setReducerClass(StepFourReducer.class);
if(args.length > 2){
inputPath = args[0];
outputPath = args[1];
cache = args[2];
}
// 将文件路径添加为URI
job.addCacheArchive(new URI(cache + "#itemUserScore2"));
// 4 设置 Mapper 输出的 kv 类型
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class);
// 5 设置最终输出 kv 类型
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
// 6 设置输入和输出路径
FileInputFormat.setInputPaths(job,new Path(inputPath));
FileOutputFormat.setOutputPath(job, new Path(outputPath));
// 7 提交 job
boolean result = job.waitForCompletion(true);
System.exit(result ? 0: 1);
}
}



