我有用户ID,如a6347324,b7432408,f54789922(非数字)和数字项ID。我希望能够在Myrrix中使用REST API来插入此类用户项对。但是/pref/a6347324/1234
会返回错误。考虑到第74行中的parseLong
,我们可以预期
https://github.com/myrrix/myrrix-recommender/blob/master/web-common/src/net/myrrix/web/servlets/PreferenceServlet.java
我使用的是版本0.11。我不想使用“标记”功能,因为它不会返回字符串标记,如同等数字ID,我认为它不适用于because
API(因为标签不会返回!) 。很快会有解决方法吗?是否可以立即使用或快速实施?
答案 0 :(得分:2)
REST API本身在数字ID方面起作用。客户端可能会对其他ID(数字或非数字)进行哈希处理以生成数字ID。您希望使用TranslatingClientRecommender
,这有助于将翻译管理回原始ID。
额外的好处是潜在的一些安全性;您无需实际向服务发送真实的用户和项目ID,如果要将其托管在Amazon EC2等第三方云上,这可能很重要。
答案 1 :(得分:1)
我决定在这个答案中添加一些关于我如何最终解决问题的细节。请注意,我稍后进行了重大修改,使代码更加面向对象。这只是一个易于理解的简单解释。
这是一个WebClientRecommender
类,它基本上是TranslatingClientRecommender
周围的小包装。
package suggester.client.recommender;
import java.io.File;
import java.io.Reader;
import java.net.URI;
import java.util.Arrays;
import java.util.List;
import net.myrrix.client.MyrrixClientConfiguration;
import net.myrrix.client.translating.TranslatedRecommendedItem;
import org.apache.mahout.cf.taste.common.TasteException;
/**
*
* @author Nilesh Chakraborty
*/
public class WebClientRecommender extends AbstractClientRecommender {
public WebClientRecommender(MyrrixClientConfiguration myrrixClientConfiguration) {
super(myrrixClientConfiguration);
}
@Override
public List<TranslatedRecommendedItem> recommend(String idListFile, String recommendTo, String recommendType, int howMany) throws TasteException {
clientRecommender.addItemIDs(new File(idListFile));
List<TranslatedRecommendedItem> recommendations = clientRecommender.recommend(recommendTo, howMany, false, new String[]{recommendType});
return recommendations;
}
@Override
public List<TranslatedRecommendedItem> recommendAnonymous(String idListFile, String recommendType, int howMany, String[] list) throws TasteException {
clientRecommender.addItemIDs(new File(idListFile));
float[] values = new float[list.length];
Arrays.fill(values, 30);
List<TranslatedRecommendedItem> recommendations = clientRecommender.recommendToAnonymous(list, values, howMany, new String[]{recommendType}, "testID");
return recommendations;
}
public List<TranslatedRecommendedItem> recommendAnonymous(URI idListFile, String recommendType, int howMany, String[] list) throws TasteException {
clientRecommender.addItemIDs(new File(idListFile));
float[] values = new float[list.length];
Arrays.fill(values, 30);
List<TranslatedRecommendedItem> recommendations = clientRecommender.recommendToAnonymous(list, values, howMany, new String[]{recommendType}, "testID");
return recommendations;
}
@Override
public void ingest(Reader csvReader) throws TasteException {
clientRecommender.ingest(csvReader);
}
}
以下是AbstractClientRecommender
:
package suggester.client.recommender;
import java.io.IOException;
import java.io.Reader;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.myrrix.client.ClientRecommender;
import net.myrrix.client.MyrrixClientConfiguration;
import net.myrrix.client.translating.TranslatedRecommendedItem;
import net.myrrix.client.translating.TranslatingClientRecommender;
import org.apache.mahout.cf.taste.common.TasteException;
/**
*
* @author nilesh
*/
abstract class AbstractClientRecommender {
protected TranslatingClientRecommender clientRecommender;
public AbstractClientRecommender(MyrrixClientConfiguration myrrixClientConfiguration) {
try {
clientRecommender = new TranslatingClientRecommender(new ClientRecommender(myrrixClientConfiguration));
} catch (IOException ex) {
Logger.getLogger(CLIClientRecommender.class.getName()).log(Level.SEVERE, null, ex);
}
}
public abstract List<TranslatedRecommendedItem> recommend(String idListFile, String recommendTo, String recommendType, int howMany) throws TasteException;
public abstract List<TranslatedRecommendedItem> recommendAnonymous(String idListFile, String recommendType, int howMany, String[] list) throws TasteException;
public void ingest(String csvFile) throws TasteException {
}
public void ingest(Reader csvReader) throws TasteException {
}
}
现在,我在自定义Servlet中使用WebClientRecommender
类。这是SuggesterServlet
:
package suggester.client.servlets;
import com.google.common.base.Splitter;
import suggester.client.recommender.WebClientRecommender;
import java.io.IOException;
import java.net.URL;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.myrrix.client.MyrrixClientConfiguration;
import net.myrrix.web.servlets.AbstractMyrrixServlet;
/**
*
* @author nilesh
*/
public abstract class AbstractSuggesterServlet extends AbstractMyrrixServlet {
static final Splitter SLASH = Splitter.on('/').omitEmptyStrings();
private WebClientRecommender recommender;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
MyrrixClientConfiguration config = new MyrrixClientConfiguration();
config.setHost(request.getServerName());
config.setPort(request.getServerPort());
recommender = new WebClientRecommender(config);
}
protected final WebClientRecommender getClientRecommender() {
return recommender;
}
}
现在,我们使用SuggesterServlet
:
package suggester.client.servlets;
import com.google.common.collect.Iterables;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URISyntaxException;
import java.util.List;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.myrrix.client.translating.TranslatedRecommendedItem;
import org.apache.mahout.cf.taste.common.TasteException;
/**
*
* @author nilesh
*/
public class SuggesterServlet extends AbstractSuggesterServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
super.doGet(request, response);
String pathInfo = request.getPathInfo();
String[] pathComponents = Iterables.toArray(SLASH.split(pathInfo), String.class);
if (pathComponents.length == 0) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
try {
List<TranslatedRecommendedItem> recommended = getClientRecommender().recommendAnonymous(getPropFilePath("proplist").toURI(), request.getParameter("type"), getHowMany(request), pathComponents);
output(request, response, recommended);
} catch (URISyntaxException use) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST, use.toString());
} catch (NamingException ne) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST, ne.toString());
} catch (TasteException te) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST, te.toString());
}
}
protected final void output(HttpServletRequest request,
ServletResponse response,
List<TranslatedRecommendedItem> items) throws IOException {
PrintWriter writer = response.getWriter();
// Always print JSON
writer.write('[');
boolean first = true;
for (TranslatedRecommendedItem item : items) {
if (first) {
first = false;
} else {
writer.write(',');
}
writer.write("[\"");
writer.write(item.getItemID());
writer.write("\",");
writer.write(Float.toString(item.getValue()));
writer.write(']');
}
writer.write(']');
}
}
读取prop文件并将字符串ID添加到TranslatingClientRecommender
(连接到Myrrix REST Servlet),使用自定义Servlet中的TranslatingClientRecommender
(HAS-A关系) - 这基本上就是我做的。我只需要修改Myrrix的web.xml以将我的自定义servlet添加到列表中,所有内容都可以在Tomcat上的单个应用程序中运行。