코드 사본은 다음과 같습니다.
패키지 MyMaven;
import java.net.unknownhostexception;
java.util.set import;
import com.mongodb.basicdbobject;
com.mongodb.db import;
com.mongodb.dbcollection import;
com.mongodb.dbcursor import;
import com.mongodb.dbobject;
import com.mongodb.mongo;
com.mongodb.mongoException 가져 오기;
공개 수업 테스트 {
public static void main (String [] args)은 알 수없는 hostException, mongoException {
Mongo Mongo = New Mongo ( "172.27.9.104", 27017);
db db = mongo.getdb ( "mytestdb");
<string> cols = db.getCollectionNames (); // 데이터베이스에서 테이블을 얻는 것과 유사)
// 데이터베이스에서 컬렉션을 인쇄합니다. 여기서는 NULL이어야합니다.
for (문자열 s : cols) {
System.out.println (s);
}
dbcollection collection = db.getCollection ( "MyTestColl");
collection.drop (); // 컬렉션을 삭제하고 데이터를 삽입 할 때 자동으로 재 구축
BASICDBOBJECT OBJ = NEW BASICDBOBJECT (); // 데이터베이스에 삽입 된 DB 객체는 다음과 같습니다.
obj.put ( "from", "blog.ihomer.net");
obj.put ( "to", "forum.ithomer.net");
obj.put ( "주제", "ithomer.net");
Collection.Insert (OBJ);
dbobject dbobj = collection.findone ();
System.out.println (dbobj); // 방금 삽입 한 데이터를 인쇄합니다
// 삽입 10 {순위 : i} 데이터
for (int i = 0; i <10; i ++) {
collection.insert (new BasicdBobject (). Append ( "Ranking", i));
}
System.out.println ( "count :" + collection.getCount ());
dbcursor cursor = collection.find ();
while (cursor.hasnext ()) {
System.out.println (cursor.next ());
}
// 간단한 조건부 쿼리, 순위가 1 인 쿼리 레코드
BasicdBobject query = new BasicdBobject ();
query.put ( "순위", 1);
cursor = collection.find (query);
System.out.println ( "Collection Find ({/"Ranking/": 1}) :");
while (cursor.hasnext ()) {
System.out.println (cursor.next ());
}
// 복잡한 조건부 쿼리, 쿼리 레코드는 5보다 크거나 9 미만의 쿼리 레코드
query = 새 Basicdbobject ();
query.put ( "순위", New Basicdbobject ( "$ gte", 5) .append ( "$ lt", 9));
cursor = collection.find (query);
System.out.println ( "Collection Find ({/"Ranking/": [5-9)}) :");
while (cursor.hasnext ()) {
System.out.println (cursor.next ());
}
// mongo.dropdatabase ( "mytestdb");
}
}
실행 결과 :
코드 사본은 다음과 같습니다.
MyTestColl
System.indexes
{ "_id": { "$ oid": "52c62ed8e4b0f4de3dd107"}, "from": "blog.ihomer.net", "to": "forum.ihomer.net", "subject": "ihomer.net"}
수 : 11
{ "_id": { "$ oid": "52c62ed8e4b0f4de3dd107"}, "from": "blog.ihomer.net", "to": "forum.ihomer.net", "subject": "ihomer.net"}
{ "_id": { "$ oid": "52c62ed8e4b0f4de3dd10108"}, "순위": 0}
{ "_id": { "$ oid": "52c62ed8e4b0f4de3dd109"}, "순위": 1}
{ "_id": { "$ oid": "52c62ed8e4b0f4de3dd10a"}, "순위": 2}
{ "_id": { "$ oid": "52c62ed8e4b0f4de3dd1010b"}, "Ranking": 3}
{ "_id": { "$ oid": "52c62ed8e4b0f4de3dd1010c"}, "Ranking": 4}
{ "_id": { "$ oid": "52c62ed8e4b0f4de3dd1010d"}, "Ranking": 5}
{ "_id": { "$ OID": "52C62ED8E4B0F4DE3DD10E"}, "Ranking": 6}
{ "_id": { "$ oid": "52c62ed8e4b0f4de3dd1010f"}, "순위": 7}
{ "_id": { "$ oid": "52c62ed8e4b0f4de3dd10110"}, "순위": 8}
{ "_id": { "$ OID": "52C62ED8E4B0F4DE3DD10111"}, "Ranking": 9}
수집 찾기 ({ "순위": 1}) :
{ "_id": { "$ oid": "52c62ed8e4b0f4de3dd109"}, "순위": 1}
수집 찾기 ({ "순위": [5-9)}) :
{ "_id": { "$ oid": "52c62ed8e4b0f4de3dd1010d"}, "Ranking": 5}
{ "_id": { "$ OID": "52C62ED8E4B0F4DE3DD10E"}, "Ranking": 6}
{ "_id": { "$ oid": "52c62ed8e4b0f4de3dd1010f"}, "순위": 7}
{ "_id": { "$ oid": "52c62ed8e4b0f4de3dd10110"}, "순위": 8}
MongoDB 배열의 예 :
코드 사본은 다음과 같습니다.
@suppresswarnings ( "확인되지 않은")
public static void loadmediatags (list <mediaentity> mediaentitylist) {
mediaentityList.clear ();
노력하다 {
Mongo Mongo = New Mongo (cosinecluster.gmongo_host, cosinecluster.gmongo_port);
db db = mongo.getdb (cosinecluster.gmongo_db);
dbcollection collection = db.getCollection (cosinecluster.gmongo_coll_media);
dbcursor cursor = collection.find ();
int index = 0;
Long StartTime = System.CurrentTimeMillis ();
while (cursor.hasnext ()) {
BasicdBobject obj = (BasicdBobject) cursor.next ();
long id = obj.getLong ( "_ id");
ArrayList <string> taglist = (ArrayList <string>) obj.get ( "tag"); // tag
ArrayList <string> keywordList = (ArrayList <string>) obj.get ( "keyword"); // keyword
ArrayList <integer> CopyrightList = (ArrayList <integer>) obj.get ( "Copyright"); // Copyright
Mediaentity Mediaentity = New Mediaentity ();
mediaentity.setid (id);
// 태그
for (int j = 0; j <taglist.size (); j ++) {
mediaentity.addtag (taglist.get (j));
int tagid = getTagint (taglist.get (j));
mediaentity.addtag (tagid);
}
// 배우
ArrayList <dbobject> actorsobjlist = (arraylist <dbobject>) obj.get ( "Actors"); // Actors
for (int j = 0; j <actorsobjlist.size (); j ++) {
mediaentity.addactor ((string) actorsobjlist.get (j) .get ( "name"));
int actorid = getActorInt ((String) actorsObjlist.get (j) .get ( "name"));
Mediaentity.adcorid (Actorid);
}
// 감독
ArrayList <dbobject> DirectorObjlist = (ArrayList <dbobject>) obj.get ( "Director"); // Director
for (int j = 0;
Mediaentity.addirector ((String) Directionobjlist.get (j) .get ( "name"));
int directorId = getDirectorInt ((String) DirectorObjlist.get (j) .get ( "name"));
Mediaentity.addirector (DirectorId);
}
// 키워드
for (int j = 0; j <keywordList.size (); j ++) {
mediaentity.addkeyword (keywordList.get (j));
int keywordid = getKeywordint (keywordList.get (j));
mediaentity.addkeyword (키워드);
}
// 저작권
for (int j = 0; j <CopyrightList.size (); j ++) {
mediaentity.addcopyright (copyrightlist.get (j));
}
MediaentityList.add (Mediaentity);
색인 ++;
if (index> 100) {
부서지다;
}
System.out.println (index + "--- mediaentity :" + mediaentity.toString ());
}
Long Costtime = System.CurrentTimeMillis () - STARTTIME;
System.out.println ( "로드 데이터 원가 계약 시간 =" + index + "; costtime =" + costtime/1000f);
} catch (예외 e) {
e.printstacktrace ();
}
}
public static int gettagint (문자열 태그) {
int tagintid = -1;
노력하다 {
mongoclient mongo = 새로운 mongoclient (cosinecluster.gmongo_host, cosinecluster.gmongo_port);
db db = mongo.getdb (cosinecluster.gmongo_db);
dbcollection tagmapcollection = db.getCollection ( "prodse_tag_map");
dbcursor cursor = tagmapcollection.find (new Basicdbobject ( "name", tag));
if (cursor == null || cursor.toArray (). size () <= 0) {// 2 또는 n보다 작은 프로세스 매핑 키워드, 아래 동일
반환 tagintid;
}
dbobject obj = cursor.toArray (). get (0);
문자열 이름 = 태그;
tagintid = (정수) obj.get ( "id");
int num = (정수) obj.get ( "num");
mongo.close ();
} catch (unknoyHostException e) {
e.printstacktrace ();
}
반환 tagintid;
}
public static int getActorInt (String actor) {
int actorIntid = -1;
노력하다 {
mongoclient mongo = 새로운 mongoclient (cosinecluster.gmongo_host, cosinecluster.gmongo_port);
db db = mongo.getdb (cosinecluster.gmongo_db);
dbcollection tagmapcollection = db.getCollection ( "prodse_actor_map");
dbcursor cursor = tagmapcollection.find (new Basicdbobject ( "name", actor));
if (cursor == null || cursor.toArray (). size () <= 0) {
actorintid 리턴;
}
dbobject obj = cursor.toArray (). get (0);
문자열 이름 = 액터;
actorIntid = (정수) obj.get ( "id");
int num = (정수) obj.get ( "num");
mongo.close ();
} catch (unknoyHostException e) {
e.printstacktrace ();
}
actorintid 리턴;
}