메뉴 건너뛰기

Bigdata, Semantic IoT, Hadoop, NoSQL

Bigdata, Hadoop ecosystem, Semantic IoT등의 프로젝트를 진행중에 습득한 내용을 정리하는 곳입니다.
필요한 분을 위해서 공개하고 있습니다. 문의사항은 gooper@gooper.com로 메일을 보내주세요.


mongodb console명령과 API비교

총관리자 2015.12.21 15:11 조회 수 : 120

1. 날짜 범위와 문자열의 일부를 이용하여 data를 가져오는 경우

  가. console

    db.getCollection('resource').find({ct:{$gte:"20151221T135154", $lt:"20151221T135236"}}, {_uri:/status/Data/}).count()

  나. Java Api

String m = "status/Data";

MongoClient mongoClient = new MongoClient(new ServerAddress(ip, port));
DB db = mongoClient.getDB(dbname);

BasicDBObject searchQuery = new BasicDBObject("ct",
		new BasicDBObject("$gte", startDate).append("$lt", endDate));
searchQuery.put("_uri", java.util.regex.Pattern.compile(m));

DBCursor cursor = null;
int cnt = 0;

try {
	cursor = table.find(searchQuery);

	while (cursor.hasNext()) {
		DBObject doc = cursor.next();
		log.debug("value of [" + cnt++ + "]...." + doc.toString());

	}
} catch (MongoException e) {
	e.printStackTrace();
	if (db != null) {
		db.cleanCursors(true);
		table = null;
		db = null;
	}
	if (mongoClient != null) {
		mongoClient.close();
	}
	throw e;
} catch (Exception e) {
	e.printStackTrace();
	if (db != null) {
		db.cleanCursors(true);
		table = null;
		db = null;
	}
	if (mongoClient != null) {
		mongoClient.close();
	}
	throw e;
} finally {
	if (cursor != null) {
		cursor.close();
	}
}

  

A personal place to organize information learned during the development of such Hadoop, Hive, Hbase, Semantic IoT, etc.
We are open to the required minutes. Please send inquiries to gooper@gooper.com.

위로