블로그 이미지
윤영식
Full Stacker, Application Architecter, KnowHow Dispenser and Bike Rider

Publication

Category

Recent Post

'GridFS'에 해당되는 글 2

  1. 2013.02.23 [MongoDB] GridFS 사용하기
  2. 2013.02.23 [MongoDB] GridFS 개념잡기
2013. 2. 23. 16:53 MongoDB/MapReduce

GridFS에 대하여 샘플을 돌려보자


1) 예제 다운로드 

  - git 복제한다

[~/development/mongodb]git clone https://github.com/jamescarr/nodejs-mongodb-streaming.git gridfs_mongoose

Cloning into 'gridfs_mongoose'...

remote: Counting objects: 44, done.

remote: Compressing objects: 100% (30/30), done.

remote: Total 44 (delta 12), reused 40 (delta 8)

Unpacking objects: 100% (44/44), done.


  - 파일을 JetBrains WebStorm에서 열어보았다 

    + coffee-script 이용하여 app.js 를 app.coffee 로 작성

  


  - 실행하기전 express, mongoose, request, jade 모듈 설치

    + npm install express

    + npm install mongoose

    + npm install request


  - 실행 : http://localhost:3000 호출

    + 호출하기전 MongoDB를 start 해 놓아야 한다 

[~/development/mongodb/gridfs_mongoose]coffee app

Server running. Navigate to localhost:3000

  

    + 브라우져 호출하면 파일 업로드 화면이 나온다

  

 


2) GridFS 파일 업로드 하기 

  - 화면에서 Name을 입력하고 File 을 선택한 후 "제출" 버튼을 클릭한다 


  - MongoDB의 mongo에서 확인을 한다 : dowonFile 은 프로그램안에서 사용한 Collection 명칭이다

[~/mongodb]mongo

MongoDB shell version: 2.2.3

connecting to: test


///////////////

//  제출전 상태

> show dbs

local (empty)


///////////////

//  제출후 상태

> show dbs

dowonFile 0.203125GB

local (empty)

> use dowonFile

switched to db dowonFile

> show collections

applications

fs.chunks

fs.files

system.indexes

> db.applications.find();

{ "name" : "dowonGridFileSample", "_id" : ObjectId("51392f28dfa5f69404000001"), "files" : [ { "md5" : "6dda9a5cd37e113b246fd00604bf3638", "uploadDate" : ISODate("2013-03-08T00:22:01.077Z"), "chunkSize" : 262144, "length" : 7772701, "contentType" : "binary/octet-stream", "_id" : ObjectId("51392f28dfa5f69404000002") } ] }



3) 예제 분석하기 

  - *.coffee 를 컴파일하여 분석할 수도 있다 : coffee -c *.coffee 수행하면 *.js 파일 생성됨

  - 가급적 coffee-script에 익숙해 지도록 하자 (참조)

  - app.coffee 

    + 기본 환경 셋업

    + 스키마 생성

    + 호출을 위한 get/post 구성 : /new/:id 값을 RESTful 로 호출하면 파일을 다운로드 받을 수 있다 (id는 몽고디비에서 _id 참조)

  

  - gridfs.coffee

    + gridfs 전용 put, find 관련 메소드를 정의하여 모듈화 한다 


4) Mocha 테스트 

  - git clone 받은 디렉토리에서 Mocha 테스트를 수행한다 : current 디렉토리 밑으로 test 디렉토리의 테스트 파일을 자동 수행한다

  - 테스트 파일 형식

    + .js : mocha

    + .coffee : mocha --compilers coffee:coffee-script



<참조>

  - 원문 : nodejs-mongodb-streaming (Node.js mongoose+GridFS 예제)


'MongoDB > MapReduce' 카테고리의 다른 글

[MongoDB] Aggregation Framework 실습하기  (0) 2013.08.03
[MongoDB] Aggregation Framework 이해하기  (0) 2013.08.03
[MongoDB] GridFS 개념잡기  (0) 2013.02.23
posted by 윤영식
2013. 2. 23. 15:50 MongoDB/MapReduce

몽고디비가 샤딩으로 저장한 데이터들은 어떻게 조회되어 가져올 수 있을까? GridFS에 대해서 알아보자. 텐젠의 설명을 잠시 보자

GridFS is a specification for storing and retrieving files that exceed the BSON-document size limit of 16MB.

(GridFS는 16MB 넘는 사이즈의 데이터를 저장하고 조회하는 명세이다. 모든 파일을 도큐먼트로 다룬다는게 중요하겠다)


Instead of storing a file in an single document, GridFS divides a file into parts, or chunks, [1] and stores each of those chunks as a separate document. By default GridFS limits chunk size to 256k. GridFS uses two collections to store files. One collection stores the file chunks, and the other stores file metadata.

(한개의 파일로 저장하는 대신에 부분이나 청크로 나누어 분리된 도큐먼트를 청크로 저장한다. 청크사이즈는 기본 256k로 제한되어 있다. 파일 청크와 메타데이터를 저장하는 두개의 컬렉션을 사용한다)



1) 개념

  - 분산 파일 시스템 : Redis (Map-key:value- Data)

  - Redis와 같은 기능을 MongoDB에서는 GridFS에서 담당한다

  - mongoose ODM 드라이버를 통해서 GridFS를 사용할 수 있다


2) 사용하기 

  - javascript.pdf 50.3MBytes 자리 파일을 GridFS로 구성

  - 미디어 파일도 MongoDB에 컬렉션으로 관리하게 되는 것이다 

[~/mongodb]mongofiles -d dowon put javascript.pdf

connected to: 127.0.0.1

added file: { _id: ObjectId('5128625889a440df3f1359db'), filename: "javascript.pdf", chunkSize: 262144, uploadDate: new Date(1361601113683), md5: "57b836baabd09c486778e192fa6c350e", length: 53872431 }

done!


///////////////

// 결과 

[~/mongodb]mongo

MongoDB shell version: 2.2.3

connecting to: test

> show dbs

dowon 0.203125GB

dowonDB 0.203125GB

local (empty)

> use dowon

switched to db dowon

> show collections

fs.chunks

fs.files

system.indexes

> db.fs.files.find();

{ "_id" : ObjectId("5128625889a440df3f1359db"), "filename" : "javascript.pdf", "chunkSize" : 262144, "uploadDate" : ISODate("2013-02-23T06:31:53.683Z"), "md5" : "57b836baabd09c486778e192fa6c350e", "length" : 53872431 }


  - 대용량 데이터를 그림처럼 GridFS로 저장하면

    + 맵 : 분산된 데이터를 Key=Value (map) 기반으로 찾고

    + 리듀스 : 자바스크립트 펑션을 통하여 통합한다(비즈니스 로직 구현)

  



<참조>

  - 몽구스(mongoose)를 이용하여 gridfs 구현하기 

  - 텐젠 메뉴얼 : http://docs.mongodb.org/manual/applications/gridfs/


'MongoDB > MapReduce' 카테고리의 다른 글

[MongoDB] Aggregation Framework 실습하기  (0) 2013.08.03
[MongoDB] Aggregation Framework 이해하기  (0) 2013.08.03
[MongoDB] GridFS 사용하기  (0) 2013.02.23
posted by 윤영식
prev 1 next