Cloudera CDH/CDP 및 Hadoop EcoSystem, Semantic IoT등의 개발/운영 기술을 정리합니다. gooper@gooper.com로 문의 주세요.
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();
}
}