mongoDBクライアント(Windows DOSプロンプト)を使ってみる
Windows DOSプロンプトからmongoDBクライアントを起動する
C:\Users\adminstrator>C:\mongodb\bin\mongo
MongoDB shell version: 2.4.1
connecting to: test
>
DB一覧の表示
> show dbs
exifdb 0.203125GB
local 0.078125GB
test (empty)
DBへのswitch
> use exifdb
switched to db exifdb
collection一覧の表示
> show collections
exifcollection
system.indexes
コレクション(レコード)数のカウント
> db.exifcollection.count()
14
データ参照( select * )
> db.exifcollection.find()
データ参照を見やすく整形する
> db.exifcollection.find().forEach(printjson)
デジカメ画像のExifデータをPowerShellで取得してmongoDBに入れる(jhead使用)その3
# # main処理 # # 外部プログラム名はフルパスで指定する $strCmd = "C:\mongodb\util\jhead.exe" # デジカメ画像ファイル格納パス $pathname = "C:\mongodb\imgs" # jpegファイル一覧 $files = Get-ChildItem $pathname -Recurse | Where-Object {$_.Extension -eq ".jpg"} # DB Driverパス指定 $driver_path = "C:\mongodb\driver\CSharpDriver-1.7.0.4714" Add-Type -Path "$driver_path\MongoDB.Bson.dll"; Add-Type -Path "$driver_path\MongoDB.Driver.dll"; # mongoDB接続 $dbName = "exifdb"; # DB名 $connectionString = "mongodb://localhost/{0}?safe=true" -f $dbName; $db = [MongoDB.Driver.MongoDatabase]::Create($connectionString); $collectionName = "exifcollection"; # collection名 $collection = $db[$collectionName]; foreach( $file in $files ){ # 画像ファイル情報 $filefullname = $file.fullname $dir_name = Split-Path $filefullname -Parent $file_name = Split-Path $filefullname -Leaf $file_LastWriteTime = $file.LastWriteTime $file_size = $file.Length # mongoDBに書き込む情報(画像ファイル情報) $doc = New-Object MongoDB.Bson.BsonDocument; $doc.Add("FileFname", [MongoDB.Bson.BsonValue]::Create($file_name)) $doc.Add("FileDir", [MongoDB.Bson.BsonValue]::Create($dir_name)) $doc.Add("FileDate", [MongoDB.Bson.BsonValue]::Create($file_LastWriteTime)) $doc.Add("FileSize", [MongoDB.Bson.BsonValue]::Create($file_size)) # jheadを実行して画像Exif情報を取得する $outputstr = ExecCmd $StrCmd $filefullname $outputstr -split "`\r`\n" | % { if ( $_ -ne "" ){ # 空行対策 $line = $_ -split ": ",2 $key = $line[0].trim().trim("`.")# 前後スペース、ドット除去 $value = $line[1].trim() # 前後スペース除去 } if(($key) -and ($value)){ # mongoDBに書き込む情報(画像Exif情報) $doc.Add($key, [MongoDB.Bson.BsonValue]::Create($value)) $key = "" $value = "" } } $collection.save($doc); }
デジカメ画像のExifデータをPowerShellで取得してmongoDBに入れる(jhead使用)その2
処理の流れ
- 画像ファイルフルパス名の取得
jheadの引数として必要。
- jheadの出力をPowerShellで受け取る
一旦ファイルに吐き出す等、やり方はいろいろあると思いますが、今回は、以下サイトの関数を使用し、jheadの標準出力をPowerShellで受け取ってみました。jheadは終了後exitするのでsleep、プロセスkill部分はコメントアウトしています。
---
"外部プログラムを実行して結果を受け取る [powershell]"
function ExecCmd([string]$strCmd,[string]$argument)
http://floridaorangebird.blog.so-net.ne.jp/2010-08-20-2
---
- jheadが出力したデータをKeyとValueに切り出してmongoDBに投入
デジカメ画像のExifデータをPowerShellで取得してmongoDBに入れる(jhead使用)その1
準備
- Exif Jpeg header manipulation tool
jhead(Widows版)のダウンロード
http://www.sentex.net/~mwandel/jhead/
- これまた適当なディレクトリに配置
(例)C:\mongodb\util\jhead.exe
DOSプロンプトから使ってみる
c:\mongodb\util>jhead C:\img\101_0521\*.jpg
以下のようなメタデータが表示される。
---
File name : C:\img\101_0521\IMGP0863.JPG
File size : 2341131 bytes
File date : 2012:05:21 19:17:26
Camera make : PENTAX
Camera model : PENTAX Optio WG-1 GPS
Date/Time : 2012:05:21 19:17:26
Resolution : 4288 x 3216
Flash used : Yes (manual)
Focal length : 5.0mm (35mm equivalent: 28mm)
Exposure time: 0.020 s (1/50)
Aperture : f/4.2
ISO equiv. : 400
Whitebalance : Auto
Metering Mode: pattern
Focus range : close
GPS Latitude : ? ?
GPS Longitude: ? ?
JPEG Quality : 98
PowerShellからmongoDBを使うには、CSharpDriverが必要
CSharpDriverのダウンロード
https://github.com/mongodb/mongo-csharp-driver/downloads
CSharpDriverの配置
どこでもいい
(例) C:\mongodb\driver\CSharpDriver-1.7.0.4714"
PowerShellスクリプトでの使用準備
# dllのパスを指定する
$driver_path = "C:\mongodb\driver\CSharpDriver-1.7.0.4714"
Add-Type -Path "$driver_path\MongoDB.Bson.dll";
Add-Type -Path "$driver_path\MongoDB.Driver.dll";
mongoDB Windows版インストールは簡単でいいね!
mongoDBダウンロード
- http://www.mongodb.org/downloads より32bit or 64bitのZipファイルをダウンロード。
- 展開後、適当なディレクトリに配置。特にレジストリとかもさわらないのでC:\mongodbとかの下にまとめて入れておけば管理も楽。
ファイル配置後にすること
どこでもいい(自分専用なので)
(例) C:\mongodb\data
-
サーバの起動
お手軽にフォアグランドで実行。
データの場所とジャーナルをとるかとらないかを指定。
(win32版はデフォルトジャーナルOFF)
バッチファイルを作っておくと便利かも?
start_mongodb.bat
---
C:\mongodb\bin\mongod --dbpath "C:\mongodb\data" --nojournal
---
最終行に以下メッセージが出ていればOK。
[initandlisten] waiting for connections on port 27017
[websvr] admin web console waiting for connections on port 28017
- クライアント起動
同じくバッチファイルを作っておくと便利かも?
client_mongodb.bat
---
C:\mongodb\bin\mongo
---
C:\mongodb\batch>C:\mongodb\bin\mongo
MongoDB shell version: 2.4.1
connecting to: test
>
簡単に使えるところがいいね!
NoSQL、スキーマレスって何?
最近良く耳にするmongoDB。NoSQL、スキーマレスって何?
良くわからないのでちょこっと使ってみることにしました。
まずは、お手軽にWindowsスタンドアローンな環境でPowerShellと連携してみます。