|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.apache.mahout.cf.taste.impl.model.mongodb.MongoDBDataModel
public final class MongoDBDataModel
A DataModel
backed by a MongoDB database. This class expects a
collection in the database which contains a user ID (long
or
ObjectId
), item ID (long
or
ObjectId
), preference value (optional) and timestamps
("created_at", "deleted_at").
An example of a document in MongoDB:
}
Preference value is optional to accommodate applications that have no notion of a preference value (that is, the user simply expresses a preference for an item, but no degree of preference).
The preference value is assumed to be parseable as a double
.
The user IDs and item IDs are assumed to be parseable as long
s
or ObjectId
s. In case of ObjectId
s, the
model creates a Map<ObjectId>
, long
>
(collection "mongo_data_model_map") inside the MongoDB database. This
conversion is needed since Mahout uses the long datatype to feed the
recommender, and MongoDB uses 12 bytes to create its identifiers.
The timestamps ("created_at", "deleted_at"), if present, are assumed to be
parseable as a long
or Date
. To express
timestamps as Date
s, a DateFormat
must be provided in the class constructor. The default Date format is
"EE MMM dd yyyy HH:mm:ss 'GMT'Z (zzz)"
. If this parameter
is set to null, timestamps are assumed to be parseable as long
s.
It is also acceptable for the documents to contain additional fields. Those fields will be ignored.
This class will reload data from the MondoDB database when
refresh(Collection)
is called. MongoDBDataModel keeps the
timestamp of the last update. This variable and the fields "created_at"
and "deleted_at" help the model to determine if the triple
(user, item, preference) must be added or deleted.
Constructor Summary | |
---|---|
MongoDBDataModel()
Creates a new MongoDBDataModel |
|
MongoDBDataModel(String host,
int port,
String database,
String collection,
boolean manage,
boolean finalRemove,
DateFormat format)
Creates a new MongoDBDataModel with MongoDB basic configuration (without authentication) |
|
MongoDBDataModel(String host,
int port,
String database,
String collection,
boolean manage,
boolean finalRemove,
DateFormat format,
String user,
String password)
Creates a new MongoDBDataModel with MongoDB basic configuration (with authentication) |
|
MongoDBDataModel(String host,
int port,
String database,
String collection,
boolean manage,
boolean finalRemove,
DateFormat format,
String userIDField,
String itemIDField,
String preferenceField)
Creates a new MongoDBDataModel with MongoDB advanced configuration (without authentication) |
|
MongoDBDataModel(String host,
int port,
String database,
String collection,
boolean manage,
boolean finalRemove,
DateFormat format,
String user,
String password,
String userIDField,
String itemIDField,
String preferenceField)
Creates a new MongoDBDataModel with MongoDB advanced configuration (with authentication) |
Method Summary | |
---|---|
String |
fromIdToLong(String id,
boolean isUser)
Translates the MongoDB identifier to Mahout/MongoDBDataModel's internal identifier, if required. |
String |
fromLongToId(long id)
Translates the Mahout/MongoDBDataModel's internal identifier to MongoDB identifier, if required. |
org.apache.mahout.cf.taste.impl.common.LongPrimitiveIterator |
getItemIDs()
|
org.apache.mahout.cf.taste.impl.common.FastIDSet |
getItemIDsFromUser(long userID)
|
float |
getMaxPreference()
|
float |
getMinPreference()
|
int |
getNumItems()
|
int |
getNumUsers()
|
int |
getNumUsersWithPreferenceFor(long... itemIDs)
|
org.apache.mahout.cf.taste.model.PreferenceArray |
getPreferencesForItem(long itemID)
|
org.apache.mahout.cf.taste.model.PreferenceArray |
getPreferencesFromUser(long id)
|
Long |
getPreferenceTime(long userID,
long itemID)
|
Float |
getPreferenceValue(long userID,
long itemID)
|
org.apache.mahout.cf.taste.impl.common.LongPrimitiveIterator |
getUserIDs()
|
boolean |
hasPreferenceValues()
|
boolean |
isIDInModel(String ID)
Checks if an ID is currently in the model. |
Date |
mongoUpdateDate()
Date of the latest update of the model. |
void |
refresh(Collection<org.apache.mahout.cf.taste.common.Refreshable> alreadyRefreshed)
Triggers "refresh" -- whatever that means -- of the implementation. |
void |
refreshData(String userID,
Iterable<List<String>> items,
boolean add)
Adds/removes (user, item) pairs to/from the model. |
void |
removePreference(long userID,
long itemID)
|
void |
setPreference(long userID,
long itemID,
float value)
|
String |
toString()
|
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
public MongoDBDataModel() throws UnknownHostException, com.mongodb.MongoException
UnknownHostException
com.mongodb.MongoException
public MongoDBDataModel(String host, int port, String database, String collection, boolean manage, boolean finalRemove, DateFormat format) throws UnknownHostException, com.mongodb.MongoException
host
- MongoDB host.port
- MongoDB port. Default: 27017database
- MongoDB databasecollection
- MongoDB collection/tablemanage
- If true, the model adds and removes users and items
from MongoDB database when the model is refreshed.finalRemove
- If true, the model removes the user/item completely
from the MongoDB database. If false, the model adds the "deleted_at"
field with the current date to the "deleted" user/item.format
- MongoDB date format. If null, the model uses timestamps.
UnknownHostException
- if the database host cannot be resolved
com.mongodb.MongoException
public MongoDBDataModel(String host, int port, String database, String collection, boolean manage, boolean finalRemove, DateFormat format, String userIDField, String itemIDField, String preferenceField) throws UnknownHostException, com.mongodb.MongoException
userIDField
- Mongo user ID fielditemIDField
- Mongo item ID fieldpreferenceField
- Mongo preference value field
UnknownHostException
- if the database host cannot be resolved
com.mongodb.MongoException
MongoDBDataModel(String, int, String, String, boolean, boolean, DateFormat)
public MongoDBDataModel(String host, int port, String database, String collection, boolean manage, boolean finalRemove, DateFormat format, String user, String password) throws UnknownHostException, com.mongodb.MongoException
user
- Mongo username (authentication)password
- Mongo password (authentication)
UnknownHostException
- if the database host cannot be resolved
com.mongodb.MongoException
MongoDBDataModel(String, int, String, String, boolean, boolean, DateFormat)
public MongoDBDataModel(String host, int port, String database, String collection, boolean manage, boolean finalRemove, DateFormat format, String user, String password, String userIDField, String itemIDField, String preferenceField) throws UnknownHostException, com.mongodb.MongoException
UnknownHostException
- if the database host cannot be resolved
com.mongodb.MongoException
MongoDBDataModel(String, int, String, String, boolean, boolean, DateFormat, String, String, String)
,
MongoDBDataModel(String, int, String, String, boolean, boolean, DateFormat, String, String)
Method Detail |
---|
public void refreshData(String userID, Iterable<List<String>> items, boolean add) throws org.apache.mahout.cf.taste.common.NoSuchUserException, org.apache.mahout.cf.taste.common.NoSuchItemException
Adds/removes (user, item) pairs to/from the model.
userID
- MongoDB user identifieritems
- List of pairs (item, preference) which want to be added or
deletedadd
- If true, this flag indicates that the pairs (user, item)
must be added to the model. If false, it indicates deletion.
org.apache.mahout.cf.taste.common.NoSuchUserException
org.apache.mahout.cf.taste.common.NoSuchItemException
refresh(Collection)
public void refresh(Collection<org.apache.mahout.cf.taste.common.Refreshable> alreadyRefreshed)
Triggers "refresh" -- whatever that means -- of the implementation. The general contract is that any should always leave itself in a consistent, operational state, and that the refresh atomically updates internal state from old to new.
refresh
in interface org.apache.mahout.cf.taste.common.Refreshable
alreadyRefreshed
- s that are known to have already been refreshed as
a result of an initial call to a method on some object. This ensures
that objects in a refresh dependency graph aren't refreshed twice
needlessly.refreshData(String, Iterable, boolean)
public String fromIdToLong(String id, boolean isUser)
Translates the MongoDB identifier to Mahout/MongoDBDataModel's internal identifier, if required.
If MongoDB identifiers are long datatypes, it returns the id.
This conversion is needed since Mahout uses the long datatype to feed the recommender, and MongoDB uses 12 bytes to create its identifiers.
id
- MongoDB identifierisUser
-
fromLongToId(long)
,
Mongo Object IDspublic String fromLongToId(long id)
Translates the Mahout/MongoDBDataModel's internal identifier to MongoDB identifier, if required.
If MongoDB identifiers are long datatypes, it returns the id in String format.
This conversion is needed since Mahout uses the long datatype to feed the recommender, and MongoDB uses 12 bytes to create its identifiers.
id
- Mahout's internal identifier
fromIdToLong(String, boolean)
,
Mongo Object IDspublic boolean isIDInModel(String ID)
Checks if an ID is currently in the model.
ID
- user or item ID
public Date mongoUpdateDate()
Date of the latest update of the model.
public org.apache.mahout.cf.taste.impl.common.LongPrimitiveIterator getUserIDs() throws org.apache.mahout.cf.taste.common.TasteException
getUserIDs
in interface org.apache.mahout.cf.taste.model.DataModel
org.apache.mahout.cf.taste.common.TasteException
public org.apache.mahout.cf.taste.model.PreferenceArray getPreferencesFromUser(long id) throws org.apache.mahout.cf.taste.common.TasteException
getPreferencesFromUser
in interface org.apache.mahout.cf.taste.model.DataModel
org.apache.mahout.cf.taste.common.TasteException
public org.apache.mahout.cf.taste.impl.common.FastIDSet getItemIDsFromUser(long userID) throws org.apache.mahout.cf.taste.common.TasteException
getItemIDsFromUser
in interface org.apache.mahout.cf.taste.model.DataModel
org.apache.mahout.cf.taste.common.TasteException
public org.apache.mahout.cf.taste.impl.common.LongPrimitiveIterator getItemIDs() throws org.apache.mahout.cf.taste.common.TasteException
getItemIDs
in interface org.apache.mahout.cf.taste.model.DataModel
org.apache.mahout.cf.taste.common.TasteException
public org.apache.mahout.cf.taste.model.PreferenceArray getPreferencesForItem(long itemID) throws org.apache.mahout.cf.taste.common.TasteException
getPreferencesForItem
in interface org.apache.mahout.cf.taste.model.DataModel
org.apache.mahout.cf.taste.common.TasteException
public Float getPreferenceValue(long userID, long itemID) throws org.apache.mahout.cf.taste.common.TasteException
getPreferenceValue
in interface org.apache.mahout.cf.taste.model.DataModel
org.apache.mahout.cf.taste.common.TasteException
public Long getPreferenceTime(long userID, long itemID) throws org.apache.mahout.cf.taste.common.TasteException
getPreferenceTime
in interface org.apache.mahout.cf.taste.model.DataModel
org.apache.mahout.cf.taste.common.TasteException
public int getNumItems() throws org.apache.mahout.cf.taste.common.TasteException
getNumItems
in interface org.apache.mahout.cf.taste.model.DataModel
org.apache.mahout.cf.taste.common.TasteException
public int getNumUsers() throws org.apache.mahout.cf.taste.common.TasteException
getNumUsers
in interface org.apache.mahout.cf.taste.model.DataModel
org.apache.mahout.cf.taste.common.TasteException
public int getNumUsersWithPreferenceFor(long... itemIDs) throws org.apache.mahout.cf.taste.common.TasteException
getNumUsersWithPreferenceFor
in interface org.apache.mahout.cf.taste.model.DataModel
org.apache.mahout.cf.taste.common.TasteException
public void setPreference(long userID, long itemID, float value)
setPreference
in interface org.apache.mahout.cf.taste.model.DataModel
public void removePreference(long userID, long itemID)
removePreference
in interface org.apache.mahout.cf.taste.model.DataModel
public boolean hasPreferenceValues()
hasPreferenceValues
in interface org.apache.mahout.cf.taste.model.DataModel
public float getMaxPreference()
getMaxPreference
in interface org.apache.mahout.cf.taste.model.DataModel
public float getMinPreference()
getMinPreference
in interface org.apache.mahout.cf.taste.model.DataModel
public String toString()
toString
in class Object
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |