hadoop实现购物商城推荐系统
1,商城:是单商家,多买家的商城系统。数据库是mysql,语言java。
2,sqoop1.9.33:在mysql和hadoop中交换数据。
3,hadoop2.2.0:这里用于练习的是伪分布模式。
4,完毕内容:喜欢该商品的人还喜欢,同样购物喜好的好友推荐。
步骤:
1,通过sqoop从mysql中将 “用户收藏商品” (这里用的是用户收藏商品信息表作为推荐系统业务上的根据,业务根据能够非常复杂。这里主要介绍推荐系统的基本原理,所以推荐根据非常easy)的表数据导入到hdfs中。
2,用MapReduce实现推荐算法。
3,通过sqoop将推荐系统的结果写回mysql。
4,java商城通过推荐系统的数据实现<喜欢该商品的人还喜欢,同样购物喜好的好友推荐。>两个功能。
实现:
1,
推荐系统的数据来源:
左边是用户,右边是商品。用户每收藏一个商品都会生成一条这种信息,<喜欢该商品的人还喜欢,同样购物喜好的好友推荐。>的数据来源都是这张表。
sqoop导入数据,这里用的sqoop1.9.33。sqoop1.9.33的资料非常少,会出现一些错误,搜索不到的能够发到我的邮箱keepmovingzx@163.com。
创建链接信息
这个比較简单
创建job
信息填对就能够了
导入数据运行 start job --jid 上面创建成功后返回的ID
导入成功后的数据
2,eclipse开发MapReduce程序
ShopxxProductRecommend<喜欢该商品的人还喜欢>
整个项目分两部,一,以用户对商品进行分组,二,求出商品的同现矩阵。
一
第1大步的数据为输入參数对商品进行分组
输出參数:
二,以第一步的输出数据为输入求商品的同现矩阵
输出数据
第一列数据为当前商品,第二列为与它相似的商品,第三列为相似率(越高越相似)。
整个过程就完了,以下
package xian.zhang.common;
import java.util.regex.Pattern;
public class Util {
public static final Pattern DELIMITER = Pattern.compile("[\t,]");
}
package xian.zhang.core; import java.io.IOException;
import java.util.Iterator; import org.apache.hadoop.conf.Configuration;
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.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; /**
* 将输入数据 userid1,product1 userid1,product2 userid1,product3
* 合并成 userid1 product1,product2,product3输出
* @author zx
*
*/
public class CombinProductInUser { public static class CombinProductMapper extends Mapper<LongWritable, Text, IntWritable, Text>{
@Override
protected void map(LongWritable key, Text value,Context context)
throws IOException, InterruptedException {
String[] items = value.toString().split(",");
context.write(new IntWritable(Integer.parseInt(items[0])), new Text(items[1]));
}
} public static class CombinProductReducer extends Reducer<IntWritable, Text, IntWritable, Text>{ @Override
protected void reduce(IntWritable key, Iterable<Text> values,Context context)
throws IOException, InterruptedException {
StringBuffer sb = new StringBuffer();
Iterator<Text> it = values.iterator();
sb.append(it.next().toString());
while(it.hasNext()){
sb.append(",").append(it.next().toString());
}
context.write(key, new Text(sb.toString()));
} } @SuppressWarnings("deprecation")
public static boolean run(Path inPath,Path outPath) throws IOException, ClassNotFoundException, InterruptedException{ Configuration conf = new Configuration();
Job job = new Job(conf,"CombinProductInUser"); job.setJarByClass(CombinProductInUser.class);
job.setMapperClass(CombinProductMapper.class);
job.setReducerClass(CombinProductReducer.class); job.setOutputKeyClass(IntWritable.class);
job.setOutputValueClass(Text.class); FileInputFormat.addInputPath(job, inPath);
FileOutputFormat.setOutputPath(job, outPath); return job.waitForCompletion(true); } }
package xian.zhang.core; import java.io.IOException;
import java.util.Iterator; import org.apache.hadoop.conf.Configuration;
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.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; /**
* 将输入数据 userid1,product1 userid1,product2 userid1,product3
* 合并成 userid1 product1,product2,product3输出
* @author zx
*
*/
public class CombinProductInUser { public static class CombinProductMapper extends Mapper<LongWritable, Text, IntWritable, Text>{
@Override
protected void map(LongWritable key, Text value,Context context)
throws IOException, InterruptedException {
String[] items = value.toString().split(",");
context.write(new IntWritable(Integer.parseInt(items[0])), new Text(items[1]));
}
} public static class CombinProductReducer extends Reducer<IntWritable, Text, IntWritable, Text>{ @Override
protected void reduce(IntWritable key, Iterable<Text> values,Context context)
throws IOException, InterruptedException {
StringBuffer sb = new StringBuffer();
Iterator<Text> it = values.iterator();
sb.append(it.next().toString());
while(it.hasNext()){
sb.append(",").append(it.next().toString());
}
context.write(key, new Text(sb.toString()));
} } @SuppressWarnings("deprecation")
public static boolean run(Path inPath,Path outPath) throws IOException, ClassNotFoundException, InterruptedException{ Configuration conf = new Configuration();
Job job = new Job(conf,"CombinProductInUser"); job.setJarByClass(CombinProductInUser.class);
job.setMapperClass(CombinProductMapper.class);
job.setReducerClass(CombinProductReducer.class); job.setOutputKeyClass(IntWritable.class);
job.setOutputValueClass(Text.class); FileInputFormat.addInputPath(job, inPath);
FileOutputFormat.setOutputPath(job, outPath); return job.waitForCompletion(true); } }
package xian.zhang.core;
import java.io.IOException;
import org.apache.hadoop.fs.Path;
public class Main {
public static void main(String[] args) throws ClassNotFoundException, IOException, InterruptedException {
if(args.length < 2){
throw new IllegalArgumentException("要有两个參数,数据输入的路径和输出路径");
}
Path inPath1 = new Path(args[0]);
Path outPath1 = new Path(inPath1.getParent()+"/CombinProduct");
Path inPath2 = outPath1;
Path outPath2 = new Path(args[1]);
if(CombinProductInUser.run(inPath1, outPath1)){
System.exit(ProductCo_occurrenceMatrix.run(inPath2, outPath2)?0:1);
}
}
}
ShopxxUserRecommend<同样购物喜好的好友推荐>
整个项目分两部,一,以商品对用户进行分组,二,求出用户的同现矩阵。
原理和ShopxxProductRecommend一样
以下附上代码
package xian.zhang.common;
import java.util.regex.Pattern;
public class Util {
public static final Pattern DELIMITER = Pattern.compile("[\t,]");
}
package xian.zhang.core; import java.io.IOException;
import java.util.Iterator; import org.apache.hadoop.conf.Configuration;
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.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; /**
* 将输入数据 userid1,product1 userid1,product2 userid1,product3
* 合并成 productid1 user1,user2,user3输出
* @author zx
*
*/
public class CombinUserInProduct { public static class CombinUserMapper extends Mapper<LongWritable, Text, IntWritable, Text>{
@Override
protected void map(LongWritable key, Text value,Context context)
throws IOException, InterruptedException {
String[] items = value.toString().split(",");
context.write(new IntWritable(Integer.parseInt(items[1])), new Text(items[0]));
}
} public static class CombinUserReducer extends Reducer<IntWritable, Text, IntWritable, Text>{ @Override
protected void reduce(IntWritable key, Iterable<Text> values,Context context)
throws IOException, InterruptedException {
StringBuffer sb = new StringBuffer();
Iterator<Text> it = values.iterator();
sb.append(it.next().toString());
while(it.hasNext()){
sb.append(",").append(it.next().toString());
}
context.write(key, new Text(sb.toString()));
} } @SuppressWarnings("deprecation")
public static boolean run(Path inPath,Path outPath) throws IOException, ClassNotFoundException, InterruptedException{
Configuration conf = new Configuration();
Job job = new Job(conf,"CombinUserInProduct"); job.setJarByClass(CombinUserInProduct.class);
job.setMapperClass(CombinUserMapper.class);
job.setReducerClass(CombinUserReducer.class); job.setOutputKeyClass(IntWritable.class);
job.setOutputValueClass(Text.class); FileInputFormat.addInputPath(job, inPath);
FileOutputFormat.setOutputPath(job, outPath); return job.waitForCompletion(true); } }
package xian.zhang.core; import java.io.IOException;
import java.util.Iterator; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; import xian.zhang.common.Util; /**
* 用户的同先矩阵
* @author zx
*
*/
public class UserCo_occurrenceMatrix { public static class Co_occurrenceMapper extends Mapper<LongWritable, Text, Text, IntWritable>{ IntWritable one = new IntWritable(1); @Override
protected void map(LongWritable key, Text value, Context context)throws IOException, InterruptedException { String[] products = Util.DELIMITER.split(value.toString());
for(int i=1;i<products.length;i++){
for(int j=1;j<products.length;j++){
if(i != j){
context.write(new Text(products[i] + ":" + products[j]), one);
}
}
} } } public static class Co_occurrenceReducer extends Reducer<Text, IntWritable, NullWritable, Text>{ NullWritable nullKey =NullWritable.get(); @Override
protected void reduce(Text key, Iterable<IntWritable> values,Context context)
throws IOException, InterruptedException {
int sum = 0;
Iterator<IntWritable> it = values.iterator();
while(it.hasNext()){
sum += it.next().get();
}
context.write(nullKey, new Text(key.toString().replace(":", ",") + "," + sum));
} } @SuppressWarnings("deprecation")
public static boolean run(Path inPath,Path outPath) throws IOException, ClassNotFoundException, InterruptedException{ Configuration conf = new Configuration();
Job job = new Job(conf,"UserCo_occurrenceMatrix"); job.setJarByClass(UserCo_occurrenceMatrix.class);
job.setMapperClass(Co_occurrenceMapper.class);
job.setReducerClass(Co_occurrenceReducer.class); job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class); job.setOutputKeyClass(NullWritable.class);
job.setOutputKeyClass(Text.class); FileInputFormat.addInputPath(job, inPath);
FileOutputFormat.setOutputPath(job, outPath); return job.waitForCompletion(true);
} }
package xian.zhang.core;
import java.io.IOException;
import org.apache.hadoop.fs.Path;
public class Main {
public static void main(String[] args) throws ClassNotFoundException, IOException, InterruptedException {
if(args.length < 2){
throw new IllegalArgumentException("要有两个參数,数据输入的路径和输出路径");
}
Path inPath1 = new Path(args[0]);
Path outPath1 = new Path(inPath1.getParent()+"/CombinUser");
Path inPath2 = outPath1;
Path outPath2 = new Path(args[1]);
if(CombinUserInProduct.run(inPath1, outPath1)){
System.exit(UserCo_occurrenceMatrix.run(inPath2, outPath2)?0:1);
}
}
}
代码在github上有
git@github.com:chaoku/ShopxxProductRecommend.git
hadoop实现购物商城推荐系统的更多相关文章
- 微信小程序购物商城系统开发系列-目录结构
上一篇我们简单介绍了一下微信小程序的IDE(微信小程序购物商城系统开发系列-工具篇),相信大家都已经蠢蠢欲试建立一个自己的小程序,去完成一个独立的商城网站. 先别着急我们一步步来,先尝试下写一个自己的 ...
- Python开发程序:ATM+购物商城
一.程序要求 模拟实现一个ATM + 购物商城程序 额度 15000或自定义 实现购物商城,买东西加入 购物车,调用信用卡接口结账 可以提现,手续费5% 每月22号出账单,每月10号为还款日,过期未还 ...
- ThinkPHP 购物商城网站(数据库中增删改查的功能实现)——————重点——————
控制器 ---------------------GoodsController.class.php------------------------------------------------- ...
- 微信小程序购物商城系统开发系列
微信小程序购物商城系统开发系列 微信小程序开放公测以来,一夜之间在各种技术社区中就火起来啦.对于它 估计大家都不陌生了,对于它未来的价值就不再赘述,简单一句话:可以把小程序简单理解为一个新的操作系统. ...
- python_day2_homework_1(简单购物商城)
'''简单购物商城(要求):1,商品展示,价格2,买,加入购物车3,付款,钱不够''' 1 #_*_ coding: utf-8 _*_ __author__ = 'A-rno' meu_list_1 ...
- python 信用卡系统+购物商城见解
通过完成信用卡系统+购物商城 使自己在利用 字典和列表方面有了较大的提升,感悟很深, 下面将我对此次作业所展示的重点列表如下: #!/usr/bin/env python3.5 # -*-coding ...
- 模拟实现一个ATM+购物商城程序
记得上次小编上传了一个购物车程序,这次呢稍微复杂一点,还是那句话,上传在这里不是为了炫耀什么,只是督促小编学习,如果大神有什么意见和建议,欢迎指导.(PS:本次主要参考学习为主,自己原创的很少) 要求 ...
- python_项目_ATM和购物商城的程序
1 需求 模拟实现一个ATM + 购物商城程序 额度15000或自定义 实现购物商城,买东西加入购物车,调用信用卡接口结账 可以提现,手续费5% 支持多账户登录 支持账户间转账 记录每月日常消费流水 ...
- 模拟实现ATM+购物商城程序
流程图: 需求: ATM:模拟实现一个ATM + 购物商城程序额度 15000或自定义实现购物商城,买东西加入 购物车,调用信用卡接口结账可以提现,手续费5%支持多账户登录支持账户间转账记录每月日常消 ...
随机推荐
- AsyncCallback BeginInvode endinvode 异步调用
下面是搜藏的代码: //首先准备好,要进行C#异步调用的方法(能C#异步调用的,最好不多线程) private string MethodName(int Num, out int Num2) { N ...
- 让你更值钱的方法:培养稀缺(追逐新技术,淬炼已有技能、做到出类拔萃,寻找自己所在的行业痛点,App开发者是市场动态平衡的典型)
一个开发者,如何才能更值钱? 答案非常简单:掌握稀缺资源. 那么,怎样才能持续不断地掌握稀缺资源,让自己更值钱呢? 请看接下来介绍的 2 种识别稀缺的方法和 2 种培养稀缺的策略. 稀缺资源的秘密 资 ...
- Project Euler 613 Pythagorean Ant(概率+积分)
题目链接:点击我打开题目链接 题目大意: 给你一只蚂蚁,它在一个 边长为 \(30-40-50\) 的直角三角形\((x,y)\)上,并且它在直角三角形中选择的位置和移动方向的概率都是相等的.问你这只 ...
- Java BigDecimal的基本使用方法
1.对于不需要任何准确计算精度的数字可以直接使用float或double,但是如果需要精确计算的结果,则必须使用BigDecimal类 2.运算速度比一般的+.-.*./要快 3.基本方 法描 述 ...
- 步步为营(十五)搜索(一)DFS 深度优先搜索
前方大坑预警! 先讲讲什么是搜索吧. 有一天你去一个果园摘梨子,果农告诉你.有一棵树上有一个金子做的梨子,找到就是你的,你该怎么找? 地图例如以下: S 0 0 0 0 0 0 0 0 0 0 0 0 ...
- spyder在虚拟机上运行内核一直崩溃
可能的原因是因为虚拟机设置的内存过小了
- 1 Spring Cloud Eureka服务治理(上)
注:此随笔为读书笔记.<Spring Cloud微服务实战>,想学习Spring Cloud的同伴们可以去看看此书,里面对源码有详细的解读. 什么是微服务? 微服务是将一个原本独立的系统拆 ...
- Linux上制作Window启动盘
Linux上制作Window启动盘 注意: U盘在Linux中的标签(依具体情况而定:执行df查看) U盘 ----- /dev/sdb4 格式化U盘 建立U盘的启动分区 安装关键工具 ms-sys ...
- css3-10 css3中的边框样式有哪几种
css3-10 css3中的边框样式有哪几种 一.总结 一句话总结:1.border-radius 2. box-shadow 3.border-image三种,box一种border两种 1.css ...
- keil编译后Program Size: Code=46284 RO-data=988 RW-data=580 ZI-data=1094588
Program Size: Code=46284 RO-data=988 RW-data=580 ZI-data=1094588 Code : 程序中代码所占字节大小 RO-data : ...