IT/BigData와 MongoDB

INSERT / UPDATE / DELETE / FIND/ MapReduce

바바옄 2015. 7. 8. 16:39
반응형

정규표현식 참고 사이트

www.nextree.co.kr/p4327

 


INSERT

 

 

1. 이름이 김멍충 , 직업이 회사원인 사람 추가 하기

 

MongoDAO.java

 

 

결과화면

 

 

 

 

 

 2.  대량의 데이터 추가하기

 

MongoDAO.java

 

InsertThread 클래스 파일 추가

 

결과화면

 

 

UPDATE

 

MongoDAO.java

 

 

결과화면

 

 

 

DELETE

 

 

// 해당하는 이름만 삭제하는 로직

// MongoDAO.java

 

// Main.java

 

결과 화면

 

 

 

// MongoDAO.java

 

 

결과화면

 

 

 

FIND

 

 

// 여러 조건을 정의(Aggregation 사용)해 검색하기

// MongoDAO.java

 

 

결과화면

 

// Group 결과 집계하기

// MongoDAO.java

 

 

결과화면

 

MapReduce

 

// MongoDAO.java

 

 

 

결과화면

 

 

 

 

소스코드

 

 

Main.java

 

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
package com.ktds.dao;
 
import org.apache.catalina.core.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
 
public class Main {
    public static void main(String[] args){
        
        String url = "mongoContext.xml";
        
        GenericXmlApplicationContext ctx = new GenericXmlApplicationContext(url);
        MongoDAO dao = ctx.getBean("mongoDAO", MongoDAO.class);
        if(dao == null){
            System.out.println("NULL!!");
        }
        else{
            System.out.println("Not NULL!!");            
            //dao.findPersonByName("고길동454544");
            //dao.findPersonLimit10();
            dao.mapReduce();
            //dao.findPersonGroup();
            //dao.insertTestVO();
            //dao.massiveInsertPerson();
            //dao.updatePerson();
            //dao.deletePerson();
            //dao.deletePersonByName("김멍충");
            
        }    
        
        
    }
}
 
cs

 

 

MongoDAO.java

 

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package com.ktds.dao;
 
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
 
import org.springframework.data.annotation.Id;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.LimitOperation;
import org.springframework.data.mongodb.core.aggregation.MatchOperation;
import org.springframework.data.mongodb.core.aggregation.SkipOperation;
import org.springframework.data.mongodb.core.aggregation.SortOperation;
import org.springframework.data.mongodb.core.mapreduce.GroupBy;
import org.springframework.data.mongodb.core.mapreduce.GroupByResults;
import org.springframework.data.mongodb.core.mapreduce.MapReduceResults;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
 
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
 
public class MongoDAO {
    
    private MongoTemplate mongoTemplate;
 
    public void setMongoTemplate(MongoTemplate mongoTemplate) {
        this.mongoTemplate = mongoTemplate;
    }
    
    public void insertTestVO(){
        
        TestVO testVO = new TestVO();
        testVO.setName("김바보");
        testVO.setJob("회사원");
        
        // person : collection 명
        mongoTemplate.insert(testVO, "person2");
        
    }
    
    
    public void updatePerson(){
        
        //criteria.is("김바보") == db.person.update({"name" : "김바보"});
        // 조건 정의
        Criteria criteria = new Criteria("name");
        criteria.is("김멍충");
        
        // import org.springframework.data.mongodb.core.query.Query;
        // criteria를 사용하기 위해 query를 만들어 줌 
        Query query = new Query(criteria);
        
        // import org.springframework.data.mongodb.core.query.Update;
        // 업데이트 할 항목 정의(job이라는 key를 백수로 바꾸겠다,.)
        Update update = new Update();
        update.set("job""백수");
 
        // updateFirst : 이름이 김바보 중의 제일 위에 있는 것 하나만 update할때
        mongoTemplate.updateFirst(query, update, "person2");
        
        /*// updateMulti : 이름이 김바보인 모든 데이터 update
        mongoTemplate.updateMulti(query, update, "person");
        
        // new Query() : 조건 없이 person collection에 있는 모든 데이터를 update
        mongoTemplate.updateMulti(new Query(), update, "person");*/
 
    }
    
    // 모든 데이터 삭제
    public void deletePerson(){
        
        mongoTemplate.remove(new Query(),"person2");        
        
    }
    
    // 해당 이름 삭제
    public void deletePersonByName(String name){
        
        Criteria criteria = new Criteria("name");
        criteria.is(name);
        
        Query query = new Query(criteria);
        
        mongoTemplate.remove(query,"person2");
    }
    
    
    public void massiveInsertPerson(){
        
        InsertThread threads[] = new InsertThread[16];
        
        for(int i=0; i< 16; i++){
            threads[i] = new InsertThread(mongoTemplate, i*200000);
            new Thread(threads[i]).start();
        }
    }
    
    public void findPersonByName(String name){
        
        Criteria criteria = new Criteria("name");
        criteria.is(name);
        
        Query query = new Query(criteria);
        
        // TestVO.class : 결과는 이걸로 받겠다,  
        TestVO testVO = mongoTemplate.findOne(query, TestVO.class"person2");
        
        System.out.println(testVO.getName());
        
    }
    //p.119
    public void findPersonLimit10(){
        
        Criteria criteria = new Criteria("name");
        // 정규 표현식 : 고길동3으로 시작하는 것들
        criteria.regex("^고길동3");
        //criteria.is("고길동");
        
        /**
         * criteria = criteria.and("job");
         * criteria.in("백수", "부처");
         */
        criteria.and("job").in("백수","회사원");
        
        // Limit 조건 정의 함 : Limit 옵션 정의 검색 조건 중 100개만 가져옴
        LimitOperation limit = Aggregation.limit(100);
        // Sort 조건 정의 함
        SortOperation sort = Aggregation.sort(Sort.Direction.ASC, "name");
        // Skip 조건 정의 함(90개 건너 뛰어라)
        SkipOperation skip = Aggregation.skip(90);
        // 검색 조건 정의 함.
        MatchOperation match = Aggregation.match(criteria);
        
        // 여러 조건을 정의함. (순서 영향 있음..)
        // 검색 - match : 조건 정의 -> sort : 정렬 -> limit : 제한된 갯수만 가져와 -> skip : 건너 뛰어 
        Aggregation aggregation = Aggregation.newAggregation( match, sort, limit, skip);
        
        // person2 : collection(어디서 가져 올건지)
        AggregationResults<TestVO> result = mongoTemplate.aggregate(aggregation, "person2", TestVO.class);
        
        // 결과 값 list로 빼오기
        List<TestVO> personList = result.getMappedResults();
        for(TestVO testVO : personList){
            System.out.println("name: " + testVO.getName() +" job: "+ testVO.getJob());
        }
    }
    
    
    public void mapReduce(){
        Criteria criteria = new Criteria("name");
        criteria.regex("^고길동34");
        
        Query query = new Query(criteria);
        
        // key : job , value : name - 회사원의 job을 가진 갯수를 1로 설정한다.
        StringBuffer map = new StringBuffer();
        map.append(" function ( ) { ");
        map.append("  emit(this.job, 1); } ");
        
        //  Array.sum(value) : 회사원의 job을 가진 1의 갯수를 더한다.
        StringBuffer reduce = new StringBuffer();
        reduce.append(" function ( key, value ) { ");
        reduce.append("  return Array.sum(value) ");
        reduce.append(" } " );
        
        MapReduceResults<TestVO> result = mongoTemplate.mapReduce( query ,"person2" ,map.toString()
                                                                ,reduce.toString() ,TestVO.class);
        Iterator<TestVO> resultIter = result.iterator();
        List<TestVO> resultList = new ArrayList<TestVO>();
        while(resultIter.hasNext()){
            resultList.add(resultIter.next());
        }
        
        for(TestVO testVO : resultList){
            System.out.println("id:"+ testVO.getId());
            System.out.println("name:"+ testVO.getName());
            System.out.println("job:"+ testVO.getJob());
            System.out.println("count:"+ testVO.getCount());
            System.out.println("value:"+ testVO.getValue());
            System.out.println("============================");
        }
        
    }
    
    
    public void findPersonGroup(){
        // Group 결과의 집계항목을 위한 초기화 
        DBObject initial = new BasicDBObject();
        initial.put("count"0);
        
        // string을 갖다 붙이기 편해서 사용
        StringBuffer reduce = new StringBuffer();
        reduce.append(" function ( curr, result ) { ");
        reduce.append("   if( curr.job == result.job) { ");
        reduce.append("        result.count++ ");
        reduce.append("   } ");
        reduce.append(" } " );
        
        GroupBy group = new GroupBy("job");
        group.initialDocument(initial);
        group.reduceFunction(reduce.toString());
        
        Criteria criteria = new Criteria("name");
        // 고길동3으로 시작하는 것들
        criteria.regex("^고길동33232");
        
        GroupByResults<TestVO> result = mongoTemplate.group(criteria, "person2", group, TestVO.class);
        
        Iterator<TestVO> resultIter = result.iterator();
        List<TestVO> resultList = new ArrayList<TestVO>();
        while(resultIter.hasNext()){
            resultList.add(resultIter.next());
        }
        
        for(TestVO testVO : resultList){
            System.out.println("job:"+ testVO.getJob());
            System.out.println(testVO.getCount());
        }
    }
}
 
class TestVO{
 
    // @id : _id가 있다면 id에 넣어라.
    @Id
    private String id;
    private String value;
    private String job;
    private String name;
    private String count;
    
    
    public String getValue() {
        return value;
    }
    public void setValue(String value) {
        this.value = value;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getCount() {
        return count;
    }
    public void setCount(String count) {
        this.count = count;
    }
    public String getJob() {
        return job;
    }
    public void setJob(String job) {
        this.job = job;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}
 
cs

 

 

InsertThread.java

 

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
package com.ktds.dao;
 
import org.springframework.data.mongodb.core.MongoTemplate;
 
public class InsertThread implements Runnable{
    
    private MongoTemplate mongoTemplate;
    private long l;
    
    private final int DEST_NUMBER = 200000;
    
    public InsertThread(MongoTemplate mongoTemplate, long startNumber){
        this.mongoTemplate = mongoTemplate;
        this.l = startNumber;
    }
    
    @Override
    public void run() {
        
        TestVO testVO = null;
 
        for(long start = l; start< (l+DEST_NUMBER); start++){
            
            testVO = new TestVO(); 
            testVO.setName("고길동" + start);
            testVO.setJob("회사원");
            
            // person : collection 명
            mongoTemplate.insert(testVO, "person2");
            
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
            }
        }
    }
 
}
 
cs

 

 

mongoContext.xml

 

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
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mongo="http://www.springframework.org/schema/data/mongo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.7.xsd">
 
    <mongo:mongo-client
        host="localhost"
        port="27017"
        credentials="admin:admin@admin"><!-- id:pwd @ admin -->
        <mongo:client-options
            connections-per-host="8"
            threads-allowed-to-block-for-connection-multiplier="4"
            connect-timeout="100000"
            max-wait-time="150000"
            socket-keep-alive="true"
            socket-timeout="150000"
        />    
    </mongo:mongo-client>
 
    <bean    id="mongoTemplate"
            class="org.springframework.data.mongodb.core.MongoTemplate">
            <constructor-arg ref="mongo"/>
            <constructor-arg name="databaseName" value="test"/>    
    </bean>
    
    <bean    id="mongoDAO"
            class="com.ktds.dao.MongoDAO">
            <property name="mongoTemplate" ref="mongoTemplate"/>    
    </bean>
</beans>
 
cs

 

 

 

 

반응형

'IT > BigData와 MongoDB' 카테고리의 다른 글

Spring-Data-MongoDB 연동  (0) 2015.07.07
MapReduce  (0) 2015.07.07
Count, Distinct, Group, Map Reduce  (0) 2015.07.07
MongoDB 설치  (0) 2015.07.07