前書き
OrientDBは、グラフおよびドキュメントデータベースをサポートするマルチモデルのNoSQLデータベースです。 これはJavaアプリケーションであり、任意のオペレーティングシステムで実行できます。 また、マルチマスターレプリケーションをサポートするACIDに完全に対応しています。 同名の企業によって開発され、Enterprise EditionとCommunity Editionがあります。
この記事では、GratefulDeadConcertsデータベースを使用して、OrientDBデータベースをエクスポートおよびインポートする方法を示します。 このデータベースは、OrientDBのすべてのインストールに付属しているため、新しいデータベースを作成する必要はありません。
前提条件
チュートリアルを完了するには、次のものが必要です。
-
Ubuntu 14.04ドロップレット(initial setup guideを参照)
-
How To Install and Configure OrientDB on Ubuntu 14.04を使用してインストールされたOrientDBの最新版
準備が整ったら、始めましょう。
[[step-1 -—- export-an-existing-orientdb-database]] ==ステップ1—既存のOrientDBデータベースをエクスポートします
OrientDBデータベースをインポートするには、まずインポートするDBをエクスポートする必要があります。 このステップでは、インポートする必要があるデータベースをエクスポートします。
OrientDBが実行されていない場合、起動します:
sudo service orientdb start
実行中かどうかわからない場合は、いつでもステータスを確認できます。
sudo service orientdb status
次に、OrientDBコンソールを使用してサーバーに接続します。
sudo -u orientdb /opt/orientdb/bin/console.sh
出力は次のようになります。
OutputOrientDB console v.2.1.3 (build UNKNOWN@r; 2015-10-04 10:56:30+0000) www.orientdb.com
Type 'help' to display all the supported commands.
Installing extensions for GREMLIN language v.2.6.0
orientdb>
エクスポートするデータベースに接続します。 ここでは、データベースのデフォルトユーザーadminとそのパスワードadminを使用してGratefulDeadConcertsデータベースに接続しています。
connect plocal:/opt/orientdb/databases/GratefulDeadConcerts admin admin
次のような出力が表示されるはずです。
OutputConnecting to database [plocal:/opt/orientdb/databases/GratefulDeadConcerts] with user 'admin'...OK
orientdb {db=GratefulDeadConcerts}>
または、リモートモードを使用してデータベースに接続することもできます。これにより、複数のユーザーが同じデータベースにアクセスできます。
connect remote:127.0.0.1/GratefulDeadConcerts admin admin
接続出力は次のようになります。
OutputDisconnecting from the database [null]...OK
Connecting to database [remote:127.0.0.1/GratefulDeadConcerts] with user 'admin'...OK
orientdb {db=GratefulDeadConcerts}>
次に、データベースをエクスポートします。 export
コマンドは、現在のデータベースをgzipで圧縮されたJSONファイルにエクスポートします。 この例では、OrientDBのデータベースディレクトリ/opt/orientdb/databases
にエクスポートしています。
export database /opt/orientdb/databases/GratefulDeadConcerts.export
ターゲットデータベースの完全なエクスポートコマンド出力は次のとおりです。
OutputExporting current database to: database /opt/orientdb/databases/GratefulDeadConcerts.export in GZipped JSON format ...
Started export of database 'GratefulDeadConcerts' to /opt/orientdb/databases/GratefulDeadConcerts.export.gz...
Exporting database info...OK
Exporting clusters...OK (15 clusters)
Exporting schema...OK (14 classes)
Exporting records...
- Cluster 'internal' (id=0)...OK (records=3/3)
- Cluster 'index' (id=1)...OK (records=5/5)
- Cluster 'manindex' (id=2)...OK (records=1/1)
- Cluster 'default' (id=3)...OK (records=0/0)
- Cluster 'orole' (id=4)...OK (records=3/3)
- Cluster 'ouser' (id=5)...OK (records=3/3)
- Cluster 'ofunction' (id=6)...OK (records=0/0)
- Cluster 'oschedule' (id=7)...OK (records=0/0)
- Cluster 'orids' (id=8)...OK (records=0/0)
- Cluster 'v' (id=9).............OK (records=809/809)
- Cluster 'e' (id=10)...OK (records=0/0)
- Cluster 'followed_by' (id=11).............OK (records=7047/7047)
- Cluster 'written_by' (id=12).............OK (records=501/501)
- Cluster 'sung_by' (id=13).............OK (records=501/501)
- Cluster '_studio' (id=14)...OK (records=0/0)
Done. Exported 8873 of total 8873 records
Exporting index info...
- Index OUser.name...OK
- Index dictionary...OK
- Index ORole.name...OK
OK (3 indexes)
Exporting manual indexes content...
- Exporting index dictionary ...OK (entries=0)
OK (1 manual indexes)
Database export completed in 60498ms
これでエクスポート手順は完了です。
ドロップレットに別のターミナルを開き、データベースディレクトリの内容を一覧表示します。
ls -lh /opt/orientdb/databases
元のデータベースに加えて、データベースエクスポート用の圧縮ファイルが表示されます。
Outputtotal 164K
drwxr-xr-x 2 orientdb orientdb 4.0K Nov 27 02:36 GratefulDeadConcerts
-rw-r--r-- 1 orientdb orientdb 158K Nov 27 14:19 GratefulDeadConcerts.export.gz
OrientDBコンソールを使用して端末に戻ったら、次のように入力して現在のデータベースから切断できます。
disconnect
正常に切断された場合、次のような出力が表示されます。
OutputDisconnecting from the database [GratefulDeadConcerts]...OK
orientdb>
次の手順で使用するため、コンソールへの接続は開いたままにしてください。
[[step-2 -—- import-database]] ==ステップ2—データベースをインポートする
この手順では、手順1でエクスポートしたデータベースをインポートします。 デフォルトでは、データベースをインポートすると、インポート先の既存データが上書きされます。 そのため、まずターゲットデータベースに接続します。 この例では、ステップ1で使用したデフォルトのデータベースに接続します。
connect plocal:/opt/orientdb/databases/GratefulDeadConcerts admin admin
次を使用して接続することもできます。
connect remote:127.0.0.1/GratefulDeadConcerts admin admin
どちらの出力も次のようになります。
OutputConnecting to database [remote:127.0.0.1/GratefulDeadConcerts] with user 'admin'...OK
orientdb {db=GratefulDeadConcerts}>
接続が確立されたら、エクスポートされたファイルをインポートしましょう。
import database /opt/orientdb/databases/GratefulDeadConcerts.export.gz
インポートするレコードの数によっては、数分以上かかる場合があります。 だから座ってリラックスしたり、お気に入りの液体を手に取ってください。
インポート出力は次のようになります(出力は切り捨てられます):
OutputImporting database database /opt/orientdb/databases/GratefulDeadConcerts.export.gz...
Started import of database 'remote:127.0.0.1/GratefulDeadConcerts' from /opt/orientdb/databases/GratefulDeadConcerts.export.gz...
Non merge mode (-merge=false): removing all default non security classes
...
Done. Imported 8,865 records in 915.51 secs
Importing indexes ...
- Index 'OUser.name'...OK
- Index 'dictionary'...OK
- Index 'ORole.name'...OK
Done. Created 3 indexes.
Importing manual index entries...
- Index 'dictionary'...OK (0 entries)
Done. Imported 1 indexes.
Rebuild of stale indexes...
Stale indexes were rebuilt...
Deleting RID Mapping table...OK
Database import completed in 1325943 ms
これで、データベースから切断できます。
disconnect
OrientDBコンソールを終了し、通常のシェルプロンプトに戻り、exit
と入力します。
exit
結論
OrientDBデータベースをエクスポートおよびインポートする方法を見てきました。 インポート/エクスポート機能は、プロセス全体を通してデータベースをロックしないため、プロセスの実行中に書き込みを受信する可能性があることに注意してください。 このトピックの詳細については、official OrientDB export/import guideを参照してください。