public final class CassandraDataModel extends Object implements DataModel, Closeable
A DataModel based on a Cassandra keyspace. By default it uses keyspace "recommender" but this
can be configured. Create the keyspace before using this class; this can be done on the Cassandra command
line with a command linke create keyspace recommender;.
Within the keyspace, this model uses four column families:
First, it uses a column family called "users". This is keyed by the user ID as an 8-byte long. It contains a column for every preference the user expresses. The column name is item ID, again as an 8-byte long, and value is a floating point value represnted as an IEEE 32-bit floating poitn value.
It uses an analogous column family called "items" for the same data, but keyed by item ID rather than user ID. In this column family, column names are user IDs instead.
It uses a column family called "userIDs" as well, with an identical schema. It has one row under key 0. IT contains a column for every user ID in th emodel. It has no values.
Finally it also uses an analogous column family "itemIDs" containing item IDs.
Each of these four column families needs to be created ahead of time. Again the
Cassandra CLI can be used to do so, with commands like create column family users;.
Note that this thread uses a long-lived Cassandra client which will run until terminated. You
must close() this implementation when done or the JVM will not terminate.
This implementation still relies heavily on reading data into memory and caching,
as it remains too data-intensive to be effective even against Cassandra. It will take some time to
"warm up" as the first few requests will block loading user and item data into caches. This is still going
to send a great deal of query traffic to Cassandra. It would be advisable to employ caching wrapper
classes in your implementation, like CachingRecommender
or CachingItemSimilarity.
| Constructor and Description |
|---|
CassandraDataModel()
Uses the standard Cassandra host and port (localhost:9160), and keyspace name ("recommender").
|
CassandraDataModel(String host,
int port,
String keyspaceName) |
| Modifier and Type | Method and Description |
|---|---|
void |
close() |
LongPrimitiveIterator |
getItemIDs() |
FastIDSet |
getItemIDsFromUser(long userID) |
float |
getMaxPreference() |
float |
getMinPreference() |
int |
getNumItems() |
int |
getNumUsers() |
int |
getNumUsersWithPreferenceFor(long itemID) |
int |
getNumUsersWithPreferenceFor(long itemID1,
long itemID2) |
PreferenceArray |
getPreferencesForItem(long itemID) |
PreferenceArray |
getPreferencesFromUser(long userID) |
Long |
getPreferenceTime(long userID,
long itemID) |
Float |
getPreferenceValue(long userID,
long itemID) |
LongPrimitiveIterator |
getUserIDs() |
boolean |
hasPreferenceValues() |
void |
refresh(Collection<Refreshable> alreadyRefreshed) |
void |
removePreference(long userID,
long itemID) |
void |
setPreference(long userID,
long itemID,
float value) |
String |
toString() |
public CassandraDataModel()
public LongPrimitiveIterator getUserIDs()
getUserIDs in interface DataModelpublic PreferenceArray getPreferencesFromUser(long userID) throws TasteException
getPreferencesFromUser in interface DataModelTasteExceptionpublic FastIDSet getItemIDsFromUser(long userID) throws TasteException
getItemIDsFromUser in interface DataModelTasteExceptionpublic LongPrimitiveIterator getItemIDs()
getItemIDs in interface DataModelpublic PreferenceArray getPreferencesForItem(long itemID) throws TasteException
getPreferencesForItem in interface DataModelTasteExceptionpublic Float getPreferenceValue(long userID, long itemID)
getPreferenceValue in interface DataModelpublic Long getPreferenceTime(long userID, long itemID)
getPreferenceTime in interface DataModelpublic int getNumItems()
getNumItems in interface DataModelpublic int getNumUsers()
getNumUsers in interface DataModelpublic int getNumUsersWithPreferenceFor(long itemID)
throws TasteException
getNumUsersWithPreferenceFor in interface DataModelTasteExceptionpublic int getNumUsersWithPreferenceFor(long itemID1,
long itemID2)
throws TasteException
getNumUsersWithPreferenceFor in interface DataModelTasteExceptionpublic void setPreference(long userID,
long itemID,
float value)
setPreference in interface DataModelpublic void removePreference(long userID,
long itemID)
removePreference in interface DataModelpublic boolean hasPreferenceValues()
hasPreferenceValues in interface DataModelpublic float getMaxPreference()
getMaxPreference in interface DataModelpublic float getMinPreference()
getMinPreference in interface DataModelpublic void refresh(Collection<Refreshable> alreadyRefreshed)
refresh in interface Refreshablepublic void close()
close in interface Closeableclose in interface AutoCloseableCopyright © 2008–2013 The Apache Software Foundation. All rights reserved.