运行结果:
mongodb数组示例:
// director
ArrayList<DBObject> directorObjList = (ArrayList<DBObject>)obj.get("director"); // director
for(int j=0; j<directorObjList.size(); j++) {
mediaEntity.addDirector((String) directorObjList.get(j).get("name"));
int directorId = getDirectorInt((String) directorObjList.get(j).get("name"));
mediaEntity.addDirector(directorId);
}
// keyword
for(int j=0; j<keywordList.size(); j++) {
mediaEntity.addKeyword(keywordList.get(j));
int keywordId = getKeywordInt(keywordList.get(j));
mediaEntity.addKeyword(keywordId);
}
// copyright
for(int j=0; j<copyrightList.size(); j++) {
mediaEntity.addCopyright(copyrightList.get(j));
}
mediaEntityList.add(mediaEntity);
index++;
if(index > 100) {
break;
}
System.out.println(index + " --- mediaEntity : " + mediaEntity.toString());
}
long costTime = System.currentTimeMillis() - startTime;
System.out.println("load data costTime = " + index + "; costTime = " + costTime/1000f);
} catch (Exception e) {
e.printStackTrace();
}
}
public static int getTagInt(String tag) {
int tagIntId = -1;
try {
MongoClient mongo = new MongoClient(CosineCluster.gMongo_HOST, CosineCluster.gMongo_PORT);
DB db = mongo.getDB(CosineCluster.gMongo_DB);
DBCollection tagmapCollection = db.getCollection("recommend_tag_map");
DBCursor cursor = tagmapCollection.find(new BasicDBObject("name", tag));
if(cursor == null || cursor.toArray().size() <= 0) { // 处理小于2或n的映射关键字,下同
return tagIntId;
}
DBObject obj = cursor.toArray().get(0);
String name = tag;
tagIntId = (Integer) obj.get("id");
int num = (Integer) obj.get("num");
mongo.close();
} catch (UnknownHostException e) {
e.printStackTrace();
}
return tagIntId;
}
public static int getActorInt(String actor) {
int actorIntId = -1;
try {
MongoClient mongo = new MongoClient(CosineCluster.gMongo_HOST, CosineCluster.gMongo_PORT);
DB db = mongo.getDB(CosineCluster.gMongo_DB);
DBCollection tagmapCollection = db.getCollection("recommend_actor_map");
DBCursor cursor = tagmapCollection.find(new BasicDBObject("name", actor));
if(cursor == null || cursor.toArray().size() <= 0) {
return actorIntId;
}
DBObject obj = cursor.toArray().get(0);
String name = actor;
actorIntId = (Integer) obj.get("id");
int num = (Integer) obj.get("num");
mongo.close();
} catch (UnknownHostException e) {
e.printStackTrace();
}
return actorIntId;
}
新闻热点
疑难解答