💨

ひとりMongoDB University 12/19 - データセットのインポート

2020/12/19に公開

この記録は、アドベントカレンダー形式の、MongoDB Universityの学習コースの記録、19日目になります!
ただいまコース: M103[1]を進めています。目標は12/25までにChapter1の完走!

Chapter 1: Lab: Importing a Dataset (演習問題)

Problem:

Import a dataset into MongoDB using mongoimport:
mongoimportを使ってMongoDBにデータをインポートする

  • connect to a mongod process running on port 27000
    • port 27000で起動させること
  • import the data from /dataset/products.json
    • /dataset/products.json をインポートすること
  • import the data to applicationData.products
    • applicationData.productコレクションにデータをインポートする
  • use m103-application-user to authenticate to the database - this user has already - been created for you on the admin database with password m103-application-pass
    • m103-application-userを使うこと(認証用dbはadmin, パスワードはm103-application-user)

やってみる

  • 指定のポートでmongodを起動する
  • ユーザが作成されているはずなので、その認証情報で接続すること
    • 認証データベースはadmin
    • user: m103-application-user / m103-application-pass
  • mongoコマンドで接続し、applicationDataを確認してみる
  • コマンドラインでmongoimportを使ってデータを登録してみる

設定ファイルは以下の通り。

storage:
  dbPath: /var/mongodb/db
net:
  bindIp: localhost
  port: 27000
security:
  authorization: enabled
systemLog:
  destination: file
  path: /var/mongodb/logs/mongod.log
  logAppend: true
processManagement:
  fork: true

起動の確認

すでに起動しているー

bash-4.4# ps -elf | grep mongod
  159 root      0:01 mongod --port 27000 --dbpath /var/mongodb/db --auth --logpath /var/mongodb/logs/mongod.log --logappend --fork
  383 root      0:00 grep mongod

接続の確認

bash-4.4# mongo --port 27000 -u m103-application-user -p m103-application-pass
MongoDB shell version v4.0.5
connecting to: mongodb://127.0.0.1:27000/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("80d7435d-91f1-4725-a0d8-5fc4060a29a2") }
MongoDB server version: 4.0.5
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
        http://docs.mongodb.org/
Questions? Try the support group
        http://groups.google.com/group/mongodb-user
> show databases;
>
> use applicationData
switched to db applicationData

アカウントもすでにある模様。

インポートしてみる

データファイルの確認。jsonファイル(テキスト)になっている。

bash-4.4# head /dataset/products.json
{"_id":{"$oid":"573f7197f29313caab89b21b"},"sku":20000008,"name":"Come Into The World - CD","type":"Music","regularPrice":14.99,"salePrice":14.99,"shippingWeight":"0.25"}
{"_id":{"$oid":"573f7197f29313caab89b21c"},"sku":20000017,"name":"Paragon of Animals - CD","type":"Music","regularPrice":14.99,"salePrice":14.99,"shippingWeight":"0.25"}
{"_id":{"$oid":"573f7197f29313caab89b21d"},"sku":20000035,"name":"Hedgehog's Dilemma - CD","type":"Music","regularPrice":14.99,"salePrice":14.99,"shippingWeight":"0.25"}
{"_id":{"$oid":"573f7197f29313caab89b21e"},"sku":20000026,"name":"Very Best of the Foundations [Pony Canyon] - CD","type":"Music","regularPrice":13.99,"salePrice":13.99,"shippingWeight":"0.25"}
{"_id":{"$oid":"573f7197f29313caab89b21f"},"sku":20000053,"name":"Frontline of K-Pop - CD","type":"Music","regularPrice":22.99,"salePrice":22.99,"shippingWeight":"0.25"}
{"_id":{"$oid":"573f7197f29313caab89b220"},"sku":20000044,"name":"Prototpye A [EP] - CD","type":"Music","regularPrice":8.99,"salePrice":8.99,"shippingWeight":"0.25"}
{"_id":{"$oid":"573f7197f29313caab89b222"},"sku":20000062,"name":"New Year's Day [Single] - CD","type":"Music","regularPrice":20.99,"salePrice":20.99,"shippingWeight":"0.25"}
{"_id":{"$oid":"573f7197f29313caab89b223"},"sku":20000071,"name":"What a Circus - CD","type":"Music","regularPrice":17.99,"salePrice":17.99,"shippingWeight":"0.25"}
{"_id":{"$oid":"573f7197f29313caab89b224"},"sku":20000099,"name":"Summer Ends - CD","type":"Music","regularPrice":11.99,"salePrice":11.99,"shippingWeight":"0.25"}
{"_id":{"$oid":"573f7197f29313caab89b225"},"sku":20000105,"name":"Dance Dance Revolution - CD","type":"Music","regularPrice":11.99,"salePrice":11.99,"shippingWeight":"0.25"}

インポートオプションの確認

bash-4.4# mongoimport --help
Usage:
  mongoimport <options> <file>

Import CSV, TSV or JSON data into MongoDB. If no file is provided, mongoimport reads from stdin.

See http://docs.mongodb.org/manual/reference/program/mongoimport/ for more information.
mongoimport --port 27000 -u m103-application-user -p m103-application-pass \
   --authenticationDatabase admin \
   --db applicationData --collection products \
   --file /dataset/products.json

2020-12-19T12:33:31.575+0000    connected to: mongodb://localhost:27000/
2020-12-19T12:33:31.970+0000    9966 document(s) imported successfully. 0 document(s) failed to import.

どうやらうまく入った模様。

テスト実行で確認!

今日の進捗

Chapter1を完了しました!
つぎはいよいよレプリケーションです。WebのIDE上で、果たしてどうやって演習していくのかしら??
あと、つぎは30stepある.....

目標は10日後、29日あたりかな。

きょうのzenn

ひきつづき同じ方法で進めています。

脚注
  1. M103: Basic Cluster Administration のコースになります。コースを開始すると、完了までの期限は2ヶ月以内です。 ↩︎

Discussion