메뉴 건너뛰기

Cloudera, BigData, Semantic IoT, Hadoop, NoSQL

Cloudera CDH/CDP 및 Hadoop EcoSystem, Semantic IoT등의 개발/운영 기술을 정리합니다. gooper@gooper.com로 문의 주세요.


* _uri및 ct에 like검색을 수행하여 리턴되는 결과값 중에서 con의 값을 string->interger로 casting한후 원래의 _id를 key로 하여

update하는 java소스 코드이다.(주의할점은 아래의 예제는 makeStringMap를 이용하여 모든 값을 String으로 변경되므로 숫자등의 속성이 유지 되어야 하는 값은 적절하게 변환하여 주어야 한다)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// MongoDB연결
        try {
            mongoClient = new MongoClient(new ServerAddress(db_server, Integer.parseInt(db_port)));
            db = mongoClient.getDB(db_name);
            table = db.getCollection(collection_name);
        } catch (Exception ex) {
            log.debug("MongoDB connection error : "+ex.getMessage());
            if(db != null) {
                db.cleanCursors(true);
                db = null;             
            }
            if(table != null) {table = null;}
            if(mongoClient != null ) {
                mongoClient.close();
            }
            throw ex;
        }
 
// con값에 대한 형변환(String -> Integer)
        // 형변환(shell 코드)
    /*      db.resource.find (
                    {"_uri": /TicketCount/status/CONTENT_INST/, "ct": /20161213/}
                )
                    .forEach(function(x) {
                    x.con = new NumberInt(x.con); 
                      db.resource.save(x)  })
    */     
       DBObject searchCastQuery = new BasicDBObject();  //"$match", new BasicDBObject("ct", new BasicDBObject("$gte", "20161213T160000")));
       searchCastQuery.put("_uri", new BasicDBObject("$regex", "TicketCount/status/CONTENT_INST"));
       //searchCastQuery.put("ct", new BasicDBObject("$regex", "20161213"));
       searchCastQuery.put("ct", new BasicDBObject("$regex", Utils.sysdateFormat.format(new Date())));
         
        DBCursor cursor = table.find(searchCastQuery);
        while (cursor.hasNext()) {
            DBObject oldObj = cursor.next();
             
            @SuppressWarnings("unchecked")
            Map<String, String> map = makeStringMap(oldObj.toMap());
            //map.put("_id", new ObjectId(map.get("_id")));
             
            ObjectId id = new ObjectId(map.get("_id"));
            BasicDBObject newObj = new BasicDBObject(map);
            newObj.append("_id", id);
                        newObj.append("con", Integer.parseInt(map.get("con")));
            newObj.append("ty", Integer.parseInt(map.get("ty")));
            newObj.append("st", Integer.parseInt(map.get("st")));
            newObj.append("cs", Integer.parseInt(map.get("cs")));
             
            String lbl_tmp = map.get("lbl");
            Gson gson = new Gson();
            String[] lbl_json = gson.fromJson(lbl_tmp ,String[].class);
             
            newObj.append("lbl", lbl_json);
 
            BasicDBObject updateObj = new BasicDBObject();
            updateObj.put("$set", newObj);
 
            table.update(oldObj, updateObj);
        }


makeStringMap함수

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public Map<String,String> makeStringMap(Map<String, String> map) {
        Map<String, String> newMap = new HashMap<String, String>();
         
        Set<String> entry = map.keySet();
        Iterator<String> itr = entry.iterator();
         
        while(itr.hasNext()) {
            String key = String.valueOf(itr.next());
            //System.out.println("key : "+key);
            String value = String.valueOf(map.get(key));
            //System.out.println("value : "+value);
             
            newMap.put(key, value);
        }
         
        return newMap;
    }



위로