メインコンテンツまでスキップ
メインコンテンツまでスキップ

mongodb テーブル関数

リモートの MongoDB サーバーに保存されているデータに対して SELECT クエリを実行できるようにします。

構文

mongodb(host:port, database, collection, user, password, structure[, options[, oid_columns]])

引数

引数説明
host:portMongoDB サーバーのアドレス。
databaseリモートデータベース名。
collectionリモートコレクション名。
userMongoDB ユーザー。
passwordユーザーのパスワード。
structureこの関数から返される ClickHouse テーブルのスキーマ。
optionsMongoDB 接続文字列のオプション(任意のパラメータ)。
oid_columnsWHERE 句で oid として扱うべきカラムのカンマ区切りリスト。デフォルトは _id
ヒント

MongoDB Atlas クラウドサービスを使用している場合は、これらのオプションを追加してください:

'connectTimeoutMS=10000&ssl=true&authSource=admin'

URI で接続することも可能です:

mongodb(uri, collection, structure[, oid_columns])
引数説明
uri接続文字列。
collectionリモートコレクション名。
structureこの関数から返される ClickHouse テーブルのスキーマ。
oid_columnsWHERE 句で oid として扱うべきカラムのカンマ区切りリスト。デフォルトは _id

戻り値

元の MongoDB テーブルと同じカラムを持つテーブルオブジェクト。

MongoDB データベース test に定義されたコレクション my_collection があり、いくつかのドキュメントを挿入したとします:

db.createUser({user:"test_user",pwd:"password",roles:[{role:"readWrite",db:"test"}]})

db.createCollection("my_collection")

db.my_collection.insertOne(
    { log_type: "event", host: "120.5.33.9", command: "check-cpu-usage -w 75 -c 90" }
)

db.my_collection.insertOne(
    { log_type: "event", host: "120.5.33.4", command: "system-check"}
)

mongodb テーブル関数を使用してコレクションをクエリします:

SELECT * FROM mongodb(
    '127.0.0.1:27017',
    'test',
    'my_collection',
    'test_user',
    'password',
    'log_type String, host String, command String',
    'connectTimeoutMS=10000'
)

または:

SELECT * FROM mongodb(
    'mongodb://test_user:[email protected]:27017/test?connectionTimeoutMS=10000',
    'my_collection',
    'log_type String, host String, command String'
)