我是 Grails 的新手。我正在使用Mahout Recommender Plugin来创建推荐引擎。我正在关注本教程。这对我来说可以。
现在我想做的是,我只想使用后台作业来做这个 Mahout 推荐。这样用户就可以根据自己的喜好自动推荐。我将Quartz 2.x 调度程序用于后台作业目的。如何使用 Mahout 作为后台作业?
在本教程中,您可以看到我创建了一个 PrefrenceController.groovy,我在其中添加了 userID、itemID 和 prefrence number。
我的 Prefrence.groovy 域类文件——
package com.rbramley.mahout
import org.apache.commons.lang.builder.HashCodeBuilder
class Preference implements Serializable {
long userId
long itemId
float prefValue
static constraints = {
userId()
itemId()
prefValue range: 0.0f..5.0f
}
boolean equals(other) {
if(!(other instanceof Preference)) {
return false
}
other.userId == userId && other.itemId == itemId
}
int hashCode() {
def builder = new HashCodeBuilder()
builder.append userId
builder.append itemId
builder.toHashCode()
}
static mapping = {
id composite: ['userId', 'itemId']
version false
}
}
和我的 PrefrenceController.groovy
package com.rbramley.mahout
import org.springframework.dao.DataIntegrityViolationException
class PreferenceController {
static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
def index() {
redirect(action: "list", params: params)
}
def list(Integer max) {
params.max = Math.min(max ?: 10, 100)
[preferenceInstanceList: Preference.list(params), preferenceInstanceTotal: Preference.count()]
}
def create() {
[preferenceInstance: new Preference(params)]
}
def save() {
def preferenceInstance = new Preference(params)
if (!preferenceInstance.save(flush: true)) {
render(view: "create", model: [preferenceInstance: preferenceInstance])
return
}
flash.message = message(code: 'default.created.message', args: [message(code: 'preference.label', default: 'Preference'), preferenceInstance.id])
redirect(action: "show", id: preferenceInstance.id)
}
def show(Long id) {
def preferenceInstance = Preference.get(id)
if (!preferenceInstance) {
flash.message = message(code: 'default.not.found.message', args: [message(code: 'preference.label', default: 'Preference'), id])
redirect(action: "list")
return
}
[preferenceInstance: preferenceInstance]
}
def edit(Long id) {
def preferenceInstance = Preference.get(id)
if (!preferenceInstance) {
flash.message = message(code: 'default.not.found.message', args: [message(code: 'preference.label', default: 'Preference'), id])
redirect(action: "list")
return
}
[preferenceInstance: preferenceInstance]
}
def update(Long id, Long version) {
def preferenceInstance = Preference.get(id)
if (!preferenceInstance) {
flash.message = message(code: 'default.not.found.message', args: [message(code: 'preference.label', default: 'Preference'), id])
redirect(action: "list")
return
}
if (version != null) {
if (preferenceInstance.version > version) {
preferenceInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
[message(code: 'preference.label', default: 'Preference')] as Object[],
"Another user has updated this Preference while you were editing")
render(view: "edit", model: [preferenceInstance: preferenceInstance])
return
}
}
preferenceInstance.properties = params
if (!preferenceInstance.save(flush: true)) {
render(view: "edit", model: [preferenceInstance: preferenceInstance])
return
}
flash.message = message(code: 'default.updated.message', args: [message(code: 'preference.label', default: 'Preference'), preferenceInstance.id])
redirect(action: "show", id: preferenceInstance.id)
}
def delete(Long id) {
def preferenceInstance = Preference.get(id)
if (!preferenceInstance) {
flash.message = message(code: 'default.not.found.message', args: [message(code: 'preference.label', default: 'Preference'), id])
redirect(action: "list")
return
}
try {
preferenceInstance.delete(flush: true)
flash.message = message(code: 'default.deleted.message', args: [message(code: 'preference.label', default: 'Preference'), id])
redirect(action: "list")
}
catch (DataIntegrityViolationException e) {
flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'preference.label', default: 'Preference'), id])
redirect(action: "show", id: id)
}
}
}
假设我在我的数据库中手动添加了一些数据。现在,当用户单击 RecommendController 并输入特定的用户 ID 时,它将显示推荐。但我想将其作为背景工作。建议应自动向所有用户建议,无需任何人工干预。