@Override protected void setup(Context context) throws IOException { Configuration jobConf = context.getConfiguration(); maxPrefsPerUserConsidered = jobConf.getInt(MAX_PREFS_PER_USER_CONSIDERED, DEFAULT_MAX_PREFS_PER_USER_CONSIDERED); IDReader idReader = new IDReader (jobConf); idReader.readIDs(); usersToRecommendFor = idReader.getUserIds(); }
private void readUserIds() throws IOException, IllegalStateException { if (isUsersFileSpecified() && !isUserItemFileSpecified()) { userIds = readIDList(usersFile); } else if (isUserItemFileSpecified() && !isUsersFileSpecified()) { readUserItemFilterIfNeeded(); userIds = extractAllUserIdsFromUserItemFilter(userItemFilter); } else if (!isUsersFileSpecified() && !isUserItemFileSpecified()) { throw new IllegalStateException("Neither usersFile nor userItemFile options are specified"); } else { throw new IllegalStateException("usersFile and userItemFile options cannot be used simultaneously"); } }
private void readItemIds() throws IOException, IllegalStateException { if (isItemsFileSpecified() && !isUserItemFileSpecified()) { itemIds = readIDList(itemsFile); } else if (isUserItemFileSpecified() && !isItemsFileSpecified()) { readUserItemFilterIfNeeded(); itemIds = extractAllItemIdsFromUserItemFilter(userItemFilter); } else if (!isItemsFileSpecified() && !isUserItemFileSpecified()) { throw new IllegalStateException("Neither itemsFile nor userItemFile options are specified"); } else { throw new IllegalStateException("itemsFile and userItemFile options cannot be specified simultaneously"); } }
private void readUserItemFilterIfNeeded() throws IOException { if (!isUserItemFilterSpecified() && isUserItemFileSpecified()) { userItemFilter = readUserItemFilter(userItemFile); } }
/** * Reads user ids and item ids from files specified in a job configuration * * @throws IOException if an error occurs during file read operation * * @throws IllegalStateException if userItemFile option is specified together with usersFile or itemsFile */ public void readIDs() throws IOException, IllegalStateException { if (isUserItemFileSpecified()) { readUserItemFilterIfNeeded(); } if (isUsersFileSpecified() || isUserItemFilterSpecified()) { readUserIds(); } if (isItemsFileSpecified() || isUserItemFilterSpecified()) { readItemIds(); } }
@Test public void testUserItemFilter() throws Exception { Configuration conf = getConfiguration(); IDReader idReader = new IDReader(conf); Map<Long, FastIDSet> userItemFilter = Maps.newHashMap(); long user1 = 1; long user2 = 2; idReader.addUserAndItemIdToUserItemFilter(userItemFilter, user1, 100L); idReader.addUserAndItemIdToUserItemFilter(userItemFilter, user1, 200L); idReader.addUserAndItemIdToUserItemFilter(userItemFilter, user2, 300L); FastIDSet userIds = IDReader.extractAllUserIdsFromUserItemFilter(userItemFilter); assertEquals(2, userIds.size()); assertTrue(userIds.contains(user1)); assertTrue(userIds.contains(user1)); setField(idReader, USER_ITEM_FILTER_FIELD, userItemFilter); FastIDSet itemsForUser1 = idReader.getItemsToRecommendForUser(user1); assertEquals(2, itemsForUser1.size()); assertTrue(itemsForUser1.contains(100L)); assertTrue(itemsForUser1.contains(200L)); FastIDSet itemsForUser2 = idReader.getItemsToRecommendForUser(user2); assertEquals(1, itemsForUser2.size()); assertTrue(itemsForUser2.contains(300L)); FastIDSet itemsForNonExistingUser = idReader.getItemsToRecommendForUser(3L); assertTrue(itemsForNonExistingUser.isEmpty()); }
@Override protected void setup(Context context) throws IOException { Configuration conf = context.getConfiguration(); recommendationsPerUser = conf.getInt(NUM_RECOMMENDATIONS, DEFAULT_NUM_RECOMMENDATIONS); booleanData = conf.getBoolean(RecommenderJob.BOOLEAN_DATA, false); indexItemIDMap = TasteHadoopUtils.readIDIndexMap(conf.get(ITEMID_INDEX_PATH), conf); idReader = new IDReader(conf); idReader.readIDs(); itemsToRecommendFor = idReader.getItemIds(); }
private Map<Long, FastIDSet> readUserItemFilter(String pathString) throws IOException { Map<Long, FastIDSet> result = new HashMap<>(); try (InputStream in = openFile(pathString)) { for (String line : new FileLineIterable(in)) { try { String[] tokens = SEPARATOR.split(line); Long userId = Long.parseLong(tokens[0]); Long itemId = Long.parseLong(tokens[1]); addUserAndItemIdToUserItemFilter(result, userId, itemId); } catch (NumberFormatException nfe) { log.warn("userItemFile line ignored: {}", line); } } } return result; }
FastIDSet itemsForUser = null; if (idReader != null && idReader.isUserItemFilterSpecified()) { itemsForUser = idReader.getItemsToRecommendForUser(userID.get());
/** * Gets a collection of items which should be recommended for a user * * @param userId ID of a user we are interested in * @return if a userItemFile option is specified, and that file contains at least one item ID for the user, * then this method returns a {@link FastIDSet} object populated with item IDs. Otherwise, this * method returns an empty set. */ public FastIDSet getItemsToRecommendForUser(Long userId) { if (isUserItemFilterSpecified() && userItemFilter.containsKey(userId)) { return userItemFilter.get(userId); } else { return emptySet; } }
/** * Reads user ids and item ids from files specified in a job configuration * * @throws IOException if an error occurs during file read operation * * @throws IllegalStateException if userItemFile option is specified together with usersFile or itemsFile */ public void readIDs() throws IOException, IllegalStateException { if (isUserItemFileSpecified()) { readUserItemFilterIfNeeded(); } if (isUsersFileSpecified() || isUserItemFilterSpecified()) { readUserIds(); } if (isItemsFileSpecified() || isUserItemFilterSpecified()) { readItemIds(); } }
@Override protected void setup(Context context) throws IOException { Configuration conf = context.getConfiguration(); recommendationsPerUser = conf.getInt(NUM_RECOMMENDATIONS, DEFAULT_NUM_RECOMMENDATIONS); booleanData = conf.getBoolean(RecommenderJob.BOOLEAN_DATA, false); indexItemIDMap = TasteHadoopUtils.readIDIndexMap(conf.get(ITEMID_INDEX_PATH), conf); idReader = new IDReader(conf); idReader.readIDs(); itemsToRecommendFor = idReader.getItemIds(); }
private void readUserItemFilterIfNeeded() throws IOException { if (!isUserItemFilterSpecified() && isUserItemFileSpecified()) { userItemFilter = readUserItemFilter(userItemFile); } }
private Map<Long, FastIDSet> readUserItemFilter(String pathString) throws IOException { Map<Long, FastIDSet> result = Maps.newHashMap(); InputStream in = openFile(pathString); try { for (String line : new FileLineIterable(in)) { try { String[] tokens = SEPARATOR.split(line.toString()); Long userId = Long.parseLong(tokens[0]); Long itemId = Long.parseLong(tokens[1]); addUserAndItemIdToUserItemFilter(result, userId, itemId); } catch (NumberFormatException nfe) { log.warn("userItemFile line ignored: {}", line); } } } finally { Closeables.close(in, true); } return result; }
FastIDSet itemsForUser = null; if (idReader != null && idReader.isUserItemFilterSpecified()) { itemsForUser = idReader.getItemsToRecommendForUser(userID.get());
/** * Gets a collection of items which should be recommended for a user * * @param userId ID of a user we are interested in * @return if a userItemFile option is specified, and that file contains at least one item ID for the user, * then this method returns a {@link FastIDSet} object populated with item IDs. Otherwise, this * method returns an empty set. */ public FastIDSet getItemsToRecommendForUser(Long userId) { if (isUserItemFilterSpecified() && userItemFilter.containsKey(userId)) { return userItemFilter.get(userId); } else { return emptySet; } }
private void readUserIds() throws IOException, IllegalStateException { if (isUsersFileSpecified() && !isUserItemFileSpecified()) { userIds = readIDList(usersFile); } else if (isUserItemFileSpecified() && !isUsersFileSpecified()) { readUserItemFilterIfNeeded(); userIds = extractAllUserIdsFromUserItemFilter(userItemFilter); } else if (!isUsersFileSpecified()) { throw new IllegalStateException("Neither usersFile nor userItemFile options are specified"); } else { throw new IllegalStateException("usersFile and userItemFile options cannot be used simultaneously"); } }
private void readItemIds() throws IOException, IllegalStateException { if (isItemsFileSpecified() && !isUserItemFileSpecified()) { itemIds = readIDList(itemsFile); } else if (isUserItemFileSpecified() && !isItemsFileSpecified()) { readUserItemFilterIfNeeded(); itemIds = extractAllItemIdsFromUserItemFilter(userItemFilter); } else if (!isItemsFileSpecified()) { throw new IllegalStateException("Neither itemsFile nor userItemFile options are specified"); } else { throw new IllegalStateException("itemsFile and userItemFile options cannot be specified simultaneously"); } }
@Override protected void setup(Context context) throws IOException { Configuration jobConf = context.getConfiguration(); maxPrefsPerUserConsidered = jobConf.getInt(MAX_PREFS_PER_USER_CONSIDERED, DEFAULT_MAX_PREFS_PER_USER_CONSIDERED); IDReader idReader = new IDReader (jobConf); idReader.readIDs(); usersToRecommendFor = idReader.getUserIds(); }