홈 > 고객지원 > 기술자료 폴더

기술자료 폴더

통계분석툴 R & MongoDB 연동 방법

M 관리자 0 2780 0

안녕하세요? NoSQL 전문가 주종면입니다.

이번 차수에서는 MongoDB를 통계분석 툴인 R을 통해 분석하고 결과를 출력하기위한 연동 방법에 대해 알아보도록 하겠습니다. 

 

먼저, MongoDB를 시작한 후 yield_historical.out  컬렉션에 저장된 데이터를 확인 하겠습니다.

 

> use mongo_hadoop

switched to dbmongo_hadoop 

> 

>db.yield_historical.out.find().pretty() 

{ "_id" : 1990,"value" : 8.552400000000002 } 

{ "_id" : 1991,"value" : 7.8623600000000025 } 

{ "_id" : 1992,"value" : 7.008844621513946 } 

{ "_id" : 1993,"value" : 5.866279999999999 } 

{ "_id" : 1994,"value" : 7.085180722891565 } 

{ "_id" : 1995,"value" : 6.573920000000002 } 

{ "_id" : 1996,"value" : 6.443531746031742 } 

{ "_id" : 1997,"value" : 6.353959999999992 } 

{ "_id" : 1998,"value" : 5.262879999999994 } 

{ "_id" : 1999,"value" : 5.646135458167332 } 

 

다음은 R을 실행한 후 MongoDB 관련 패키지를 설치한 후 yield_historical.out 컬렉션을 검색해보겠습니다.

 

 

$ R 

R version 3.0.1(2013-05-16) -- "Good Sport" 

Copyright (C) 2013 The RFoundation for Statistical Computing 

Platform:x86_64-w64-mingw32/x64 (64-bit) 

> 

>library("RMongo")                                   ß MongoDB 접속하기 위한 패키지 로더 

> mongo <-mongoDbConnect("mongo_hadoop") ß mongo_hadoop 데이터베이스 접속 

> result <-dbGetQuery(mongo, "yield_historical.out", "", 0, 10) 

 

                         ßyield_historical.out 컬렉션에서 10개의Document 검색

 

 

> result                    ß 결과 검색

 

    X_id    value

1  1990 8.552400

2  1991 7.862360 

3  1992 7.008845 

4  1993 5.866280 

5  1994 7.085181 

6  1995 6.573920 

7  1996 6.443532 

8  1997 6.353960 

9  1998 5.262880 

10 1999 5.646135 

 

// 다음은BARPLOT 함수를 이용하여 막대 그레프 형태의 분석 결과를 확인해 보겠습니다.

 

> barplot(result$value, main='Analysis Status By year',col=c("darkblue", "red"),names.arg=c("1990","1991", "1992","1993", "1994", "1995", "1996","1997", "1998", "1999"), beside=TRUE,xlab="PLAN Information Technology co.") 

 

R_CHART.png 

 

 

다음 문법은 통계분석 툴 R을 통해 MongoDB 내의 컬렉션에 데이터를 조작하는 방법입니다.

 

// TEST DB에 접속하여 EMPLOYEES컬렉션에 하나의 Document를 입력합니다.

// MongoDB에 접속하여 먼저EMPLOYEES 컬렉션의 상태를 확인한 후 다음 문장을 실행합니다. 

 

test.dbInsertDocument <- function(){

  mongo <-mongoDbConnect("test") 

  output <-dbInsertDocument(mongo, "employees", '{"empno" : 1101,"ename": "Joo JongMyun"}') 

 dbDisconnect(mongo) 

} 

 

test.dbInsertDocument()

mongo <- mongoDbConnect("test") 

output <- dbGetQuery(mongo, 'employees', '', 0 , 20) 

output 

  

// TEST DB에 접속하여 EMPLOYEES컬렉션에 하나의 Document를 삭제합니다.

 

test.dbRemoveQuery <- function(){ 

  mongo <-mongoDbConnect("test") 

  output <-dbRemoveQuery(mongo, "employees", '{"ename": "JooJongMyun"}') 

 dbDisconnect(mongo) 

} 

 

test.dbRemoveQuery() 

mongo <- mongoDbConnect("test") 

output <- dbGetQuery(mongo, 'employees', '', 0 ,20) 

output 

  

// TEST DB에 접속하여 EMPLOYEES컬렉션에 2개의 Document를 입력한 후

  "Ju"로 시작되는 이름을 가진 사원을 검색합니다. 

  

test.dbGetQuery <- function(){

  mongo <-mongoDbConnect("test")

  output <- dbInsertDocument(mongo,"employees", '{"ename": "Ju YoungHyun"}')

  output <-dbInsertDocument(mongo, "employees", '{"ename": "JooJongMyun"}')

  dbDisconnect(mongo)

}

  

test.dbGetQuery()

 mongo <- mongoDbConnect("test")

 output <- dbGetQuery(mongo, 'employees','{"ename": { "$regex": "Joo","$options": "m"} }')

 output

 

// TEST DB에 접속하여 EMPLOYEES컬렉션에서 ename 필드를 기준으로 올림차순으로 분류하여 출력합니다.

 

mongo <- mongoDbConnect("test")

output <- dbGetQuery(mongo, "employees",'{ "$query": {}, "$orderby": { "ename": 1 } }}') 

dbDisconnect(mongo) 

output 

 

 

0 Comments
Category
State
  • 현재 접속자 2 명
  • 오늘 방문자 54 명
  • 어제 방문자 340 명
  • 최대 방문자 3,131 명
  • 전체 방문자 223,436 명
  • 전체 게시물 211 개
  • 전체 댓글수 41 개
  • 전체 회원수 1,230 명
Facebook Twitter GooglePlus KakaoStory NaverBand