/**
* 将数据保存到 MongoDB userId -> 1, recs -> 22:4.5|45:3.8
*
* @param userId 用户ID
* @param streamRecs 流式的推荐结果
* @param mongoConfig MongoDB 的配置
*/
public static void saveRecListToMongoDB(int userId, Tuple2<Integer, Double>[] streamRecs, Properties mongoConfig) {
log.info("准备将实时推荐列表保存到 MongoDB,入参为:userId={} streamRecs={}", userId, streamRecs);
// 1.连接到 StreamRecList实时推荐列表
MongoCollection<Document> streamRecListCollection = null;
try (MongoClient mongoClient = MongoClients.create(mongoConfig.getProperty("mongo.uri"))) {
streamRecListCollection = mongoClient
.getDatabase(mongoConfig.getProperty("mongo.db"))
.getCollection(mongoConfig.getProperty("mongo.collection"));
}
// 2.删除 Mongo collection 中已存在的记录
log.info("删除 Mongo collection 中已存在的用户 userId={} 的实时推荐表", userId);
// fixme java.lang.IllegalStateException: state should be: open
streamRecListCollection.findOneAndDelete(Filters.eq("userId", userId));
// 3.插入文档(即将用户实时推荐列表写入到MongoDB)
List<Document> recList = Arrays.stream(streamRecs).map(rec ->
new Document().append("movieId", rec._1).append("rating", rec._2)
).collect(Collectors.toList());
Document doc = new Document("userId", userId).append("recs", recList);
streamRecListCollection.insertOne(doc);
}
# Mongo相关配置
mongo.uri=mongodb://192.168.188.128:27017,192.168.188.129:27017,192.168.188.130:27017/recommend
mongo.db=recommend
public static void saveRecListToMongoDB(int userId, Tuple2<Integer, Double>[] streamRecs, Properties mongoConfig) {
log.info("准备将实时推荐列表保存到 MongoDB,入参为:userId={} streamRecs={}", userId, streamRecs);
// 1.连接到 StreamRecList实时推荐列表
MongoCollection<Document> streamRecListCollection = null;
try (MongoClient mongoClient = MongoClients.create(mongoConfig.getProperty("mongo.uri"))) {
streamRecListCollection = mongoClient
.getDatabase(mongoConfig.getProperty("mongo.db"))
.getCollection(mongoConfig.getProperty("mongo.collection"));
// 2.删除 Mongo collection 中已存在的记录
log.info("删除 Mongo collection 中已存在的用户 userId={} 的实时推荐表", userId);
// fixme java.lang.IllegalStateException: state should be: open
streamRecListCollection.findOneAndDelete(Filters.eq("userId", userId));
// 3.插入文档(即将用户实时推荐列表写入到MongoDB)
List<Document> recList = Arrays.stream(streamRecs).map(rec ->
new Document().append("movieId", rec._1).append("rating", rec._2)
).collect(Collectors.toList());
Document doc = new Document("userId", userId).append("recs", recList);
streamRecListCollection.insertOne(doc);
}
}
至此,Bug成功解决了
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.model.Filters;
import org.bson.Document;
/**
* 演示 fixme java.lang.IllegalStateException: state should be: open
*/
public class BugTest {
public static void main(String[] args) {
// 1.连接到 StreamRecList实时推荐列表
MongoCollection<Document> collection = null;
try (MongoClient mongoClient = MongoClients.create("mongodb://192.168.188.150:27017/testdb")) {
collection = mongoClient
.getDatabase("testdb")
.getCollection("user");
}
// 2.删除 Mongo collection 中已存在的记录
// fixme java.lang.IllegalStateException: state should be: open
collection.findOneAndDelete(Filters.eq("userId", 1));
// 3.插入文档(即将用户实时推荐列表写入到MongoDB)
Document doc = new Document("userId", 2);
collection.insertOne(doc);
}
}
Exception in thread "main" java.lang.IllegalStateException: state should be: open
at com.mongodb.assertions.Assertions.isTrue(Assertions.java:72)
at com.mongodb.internal.connection.BaseCluster.getDescription(BaseCluster.java:167)
at com.mongodb.internal.connection.SingleServerCluster.getDescription(SingleServerCluster.java:41)
at com.mongodb.client.internal.MongoClientDelegate.getConnectedClusterDescription(MongoClientDelegate.java:155)
at com.mongodb.client.internal.MongoClientDelegate.createClientSession(MongoClientDelegate.java:105)
at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.getClientSession(MongoClientDelegate.java:287)
at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.execute(MongoClientDelegate.java:211)
at com.mongodb.client.internal.MongoCollectionImpl.executeFindOneAndDelete(MongoCollectionImpl.java:733)
at com.mongodb.client.internal.MongoCollectionImpl.findOneAndDelete(MongoCollectionImpl.java:714)
at com.mongodb.client.internal.MongoCollectionImpl.findOneAndDelete(MongoCollectionImpl.java:708)
at com.clear.bug.BugTest.main(BugTest.java:23)
public class BugTest {
public static void main(String[] args) {
// 1.连接到 StreamRecList实时推荐列表
MongoCollection<Document> collection = null;
try (MongoClient mongoClient = MongoClients.create("mongodb://192.168.188.150:27017/testdb")) {
collection = mongoClient
.getDatabase("testdb")
.getCollection("user");
// 2.删除 Mongo collection 中已存在的记录
collection.findOneAndDelete(Filters.eq("userId", 1));
// 3.插入文档(即将用户实时推荐列表写入到MongoDB)
Document doc = new Document("userId", 2);
collection.insertOne(doc);
}
}
}