大家好呀,我是阿瞒,感谢大家收看我的博客,今天给大家带来的是一个众所周知的推荐系统的小demo,废话不多说,上才艺!!!
首先简单的看一下项目结构,很简单。
你得会创建SpringBoot项目
详细教程走这个链接,写得非常详细了
IDEA 如何快速创建 Springboot 项目https://blog.csdn.net/sunnyzyq/article/details/108666480
package com.study;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SparkApplication {
public static void main(String[] args) {
SpringApplication.run(SparkApplication.class, args);
}
}
其中有一段这么个代码,这是获取的本地文件的电影或电视剧的数据,这个txt文件我也会给大家放在下边分享的文件链接里!
JavaRDD lines = jsc.textFile("D:\\NirvanaRebirth\\study\\spark\\recommend.txt");
给大家解释一下这个数据的格式,看到第一行是1,1,5
1(代表用户编号),1(代表电视剧或电影、商品编号),5(代表编号为1的用户给编号为1的电视剧的评分)
package com.study;
import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.Function;
import org.apache.spark.api.java.function.PairFunction;
import org.apache.spark.api.java.function.VoidFunction;
import org.apache.spark.mllib.recommendation.ALS;
import org.apache.spark.mllib.recommendation.MatrixFactorizationModel;
import org.apache.spark.mllib.recommendation.Rating;
import org.apache.spark.rdd.RDD;
import scala.Tuple2;
import java.util.ArrayList;
import java.util.List;
public class As {
public static void main(String[] args) {
List list=new ArrayList();
list.add("1,6,0");
list.add("2,1,4.5");
list.add("2,2,9.9");
list.add("3,3,5.0");
list.add("3,4,2.0");
list.add("3,5,5.0");
list.add("3,6,9.9");
list.add("4,2,9.9");
list.add("4,5,0");
list.add("4,6,0");
list.add("5,2,9.9");
list.add("5,3,9.9");
list.add("5,4,9.9");
list.add("3,10,5.0");
list.add("3,11,2.0");
list.add("3,12,5.0");
list.add("3,12,9.9");
list.add("4,14,9.9");
list.add("4,15,0");
list.add("4,16,7.0");
list.add("5,17,9.9");
list.add("5,18,9.9");
list.add("5,19,6.9");
// JavaRDD temp=sc.parallelize(list);
//上述方式等价于
// JavaRDD temp2=sc.parallelize(Arrays.asList("a","b","c"));
System.out.println("牛逼牛逼");
SparkConf conf = new SparkConf().setAppName("als").setMaster("local[5]");
JavaSparkContext jsc = new JavaSparkContext(conf);
JavaRDD lines = jsc.textFile("D:\\NirvanaRebirth\\study\\spark\\recommend.txt");
// JavaRDD lines = jsc.parallelize(list);
// 映射
RDD ratingRDD = lines.map(new Function() {
public Rating call(String line) throws Exception {
String[] arr = line.split(",");
return new Rating(new Integer(arr[0]), new Integer(arr[1]), Double.parseDouble(arr[2]));
}
}).rdd();
MatrixFactorizationModel model = ALS.train(ratingRDD, 10, 10);
// 通过原始数据进行测试
JavaPairRDD testJPRDD = ratingRDD.toJavaRDD().mapToPair(new PairFunction() {
public Tuple2 call(Rating rating) throws Exception {
return new Tuple2(rating.user(), rating.product());
}
});
// 对原始数据进行推荐值预测
JavaRDD predict = model.predict(testJPRDD);
System.out.println("原始数据测试结果为:");
predict.foreach(new VoidFunction() {
public void call(Rating rating) throws Exception {
System.out.println("UID:" + rating.user() + ",PID:" + rating.product() + ",SCORE:" + rating.rating());
}
});
// 向指定id的用户推荐n件商品
Rating[] predictProducts = model.recommendProducts(2, 8);
System.out.println("\r\n向指定id的用户推荐n件商品");
for(Rating r1:predictProducts){
System.out.println("UID:" + r1.user() + ",PID:" + r1.product() + ",SCORE:" + r1.rating());
}
// 向指定id的商品推荐给n给用户
Rating[] predictUsers = model.recommendUsers(2, 4);
System.out.println("\r\n向指定id的商品推荐给n给用户");
for(Rating r1:predictProducts){
System.out.println("UID:" + r1.user() + ",PID:" + r1.product() + ",SCORE:" + r1.rating());
}
// 向所有用户推荐N个商品
RDD> predictProductsForUsers = model.recommendProductsForUsers(3);
System.out.println("\r\n******向所有用户推荐N个商品******");
predictProductsForUsers.toJavaRDD().foreach(new VoidFunction>() {
public void call(Tuple2
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.6.2
com.study
spark
0.0.1-SNAPSHOT
spark
Demo project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-test
test
org.apache.spark
spark-core_2.11
2.1.0
org.apache.spark
spark-sql_2.11
2.3.1
org.apache.spark
spark-mllib_2.11
2.1.0
compile
com.google.guava
guava
14.0.1
org.codehaus.janino
janino
3.0.8
org.codehaus.janino
commons-compiler
2.7.8
io.netty
netty-all
4.1.17.Final
org.apache.hadoop
hadoop-client
3.3.1
org.apache.hadoop
hadoop-common
3.3.1
org.apache.hadoop
hadoop-hdfs
3.3.1
org.springframework.boot
spring-boot-maven-plugin
向指定id的用户推荐n件商品
好了,到这里就结束咯,是不是很简单呢?有啥不懂的或者有啥可改进的可以看下边添加我微信一起交流哦!微信:NIKE2022888 需要毕业设计的小伙伴也可以联系,帝王般的服务你值得拥有