英文:
method fit ALSModel not found
问题
以下是您提供的代码的翻译部分:
我对Spark还很陌生,我想从数据库中读取数据并向特定用户推荐产品。
我找到了以下代码
public class CollaborativeFiltering {
public static void main(String[] args) {
// 步骤 1:设置Spark环境
SparkSession spark = SparkSession.builder()
.appName("CollaborativeFiltering")
.master("local[*]")
.getOrCreate();
// 步骤 2:配置数据库连接并将评分数据加载到DataFrame中
String url = "jdbc:mysql://localhost:3306/your_database"; // 用您的数据库URL替换
String table = "ratings"; // 用您的表名替换
String user = "your_username"; // 用您的数据库用户名替换
String password = "your_password"; // 用您的数据库密码替换
DataFrameReader reader = spark.read().format("jdbc");
Dataset<Row> ratingsDF = reader.option("url", url)
.option("dbtable", table)
.option("user", user)
.option("password", password)
.load();
// 步骤 3:为协同过滤准备数据
Dataset<Row> preparedData = ratingsDF.withColumnRenamed("user_id", "userId")
.withColumnRenamed("product_id", "itemId");
// 步骤 4:构建协同过滤模型
ALS als = new ALS();
// 设置必需的参数
als.setUserCol("userId");
als.setItemCol("itemId");
als.setRatingCol("rating");
// 设置其他可选参数
als.setRank(10); // 设置潜在因子的数量
als.setMaxIter(10); // 设置最大迭代次数
als.setRegParam(0.01); // 设置正则化参数
ALSModel model = als.fit(preparedData);
// 步骤 5:为特定用户生成推荐
int userId = 123; // 用所需的用户ID替换
Dataset<Row> userRecommendations = model.recommendForUserSubset(spark.createDataset(Collections.singletonList(userId), Encoders.INT), 5); // 获取前5个推荐
// 打印推荐
userRecommendations.show(false);
// 停止Spark会话
spark.stop();
}}
希望这可以帮助您。如果您遇到setMaxIter
,setRegParam
和fit
方法找不到的问题,请确保您的依赖项和版本与Spark 3.3.0和Scala 2.13兼容。如果问题仍然存在,请提供更多关于您的环境和错误消息的信息,以便更进一步的帮助。
英文:
i'm new to spark, i want to read data from database and recommand product to a specific user.
i found the following code
public class CollaborativeFiltering {
public static void main(String[] args) {
// Step 1: Set up Spark environment
SparkSession spark = SparkSession.builder()
.appName("CollaborativeFiltering")
.master("local[*]")
.getOrCreate();
// Step 2: Configure database connection and load ratings data into DataFrame
String url = "jdbc:mysql://localhost:3306/your_database"; // Replace with your database URL
String table = "ratings"; // Replace with your table name
String user = "your_username"; // Replace with your database username
String password = "your_password"; // Replace with your database password
DataFrameReader reader = spark.read().format("jdbc");
Dataset<Row> ratingsDF = reader.option("url", url)
.option("dbtable", table)
.option("user", user)
.option("password", password)
.load();
// Step 3: Prepare data for collaborative filtering
Dataset<Row> preparedData = ratingsDF.withColumnRenamed("user_id", "userId")
.withColumnRenamed("product_id", "itemId");
// Step 4: Build collaborative filtering model
ALS als = new ALS();
// Set the required parameters
als.setUserCol("userId");
als.setItemCol("itemId");
als.setRatingCol("rating");
// Set additional optional parameters
als.setRank(10); // Set the number of latent factors
als.setMaxIter(10); // Set the maximum number of iterations
als.setRegParam(0.01); // Set the regularization parameter
ALSModel model = als.fit(preparedData);
// Step 5: Generate recommendations for a specific user
int userId = 123; // Replace with the desired user ID
Dataset<Row> userRecommendations = model.recommendForUserSubset(spark.createDataset(Collections.singletonList(userId), Encoders.INT), 5); // Get top 5 recommendations
// Print the recommendations
userRecommendations.show(false);
// Stop the Spark session
spark.stop();
}}
but the methods setMaxIter , setRegParam and fit are not founds.
any help please.
PS : i'm using spark version 3.3.0 and scala version 2.13, i've tried other versions but it's always the same problem.
答案1
得分: 1
问题已解决,通过将版本更改为以下内容:
<scala.version>2.12</scala.version>
<spark.version>3.2.0</spark.version>
英文:
problem solved by changing versions to those
<scala.version>2.12</scala.version>
<spark.version>3.2.0</spark.version>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论