• Milvusについて
  • スタート
  • コンセプト
  • ユーザーガイド
  • データインポート
  • AIツール
  • 管理ガイド
  • ツール
  • 統合
  • チュートリアル
  • よくあるご質問
  • API Reference

Milvusサーバへの接続

このトピックでは、Milvusサーバへのクライアント接続を確立し、一般的な接続オプションを設定する方法について説明します。

前提条件

URIによる接続 (認証無効)

Milvusサーバーのアドレス(例:http://localhost:19530 )を使って接続を確立する。

from pymilvus import MilvusClient

client = MilvusClient("http://localhost:19530")
import io.milvus.v2.client.ConnectConfig;
import io.milvus.v2.client.MilvusClientV2;

ConnectConfig config = ConnectConfig.builder()
        .uri("http://localhost:19530")
        .build();
client = new MilvusClientV2(config);
import { MilvusClient } from '@zilliz/milvus2-sdk-node';

const client = new MilvusClient({
   address: 'http://localhost:19530'
});
import "github.com/milvus-io/milvus/client/v2/milvusclient"

c, err := milvusclient.New(ctx, &milvusclient.ClientConfig{
    Address: "localhost:19530",
})
# The RESTful API is stateless, so there is no persistent connection.
# Each request hits the server directly; the /collections/list endpoint
# doubles as a health check when you need to verify reachability.
export HOST="localhost:19530"

curl -X POST "http://${HOST}/v2/vectordb/collections/list" \
    -H "Content-Type: application/json" \
    -d '{}'

認証情報で接続(認証有効)

"username:password" 形式のトークンかuserpassword のどちらかを指定してください。デフォルトの組み込み管理者はroot:Milvus です(本番環境では変更してください)。

from pymilvus import MilvusClient

# Token form
client = MilvusClient(
    uri="http://localhost:19530",
    token="root:Milvus",
)

# Or explicit user/password
client = MilvusClient(
    uri="http://localhost:19530",
    user="root",
    password="Milvus",
)
import io.milvus.v2.client.ConnectConfig;
import io.milvus.v2.client.MilvusClientV2;

ConnectConfig config = ConnectConfig.builder()
        .uri("http://localhost:19530")
        .username("root")
        .password("Milvus")
        .build();
client = new MilvusClientV2(config);
import { MilvusClient } from '@zilliz/milvus2-sdk-node';

const client = new MilvusClient({
   address: 'http://localhost:19530',
   username: 'root',
   password: 'Milvus'
});
import "github.com/milvus-io/milvus/client/v2/milvusclient"

c, err := milvusclient.New(ctx, &milvusclient.ClientConfig{
    Address: "localhost:19530",
    Username: "root",
    Password: "Milvus",
})
export HOST="localhost:19530"
export TOKEN="root:Milvus"

curl -X POST "http://${HOST}/v2/vectordb/collections/list" \
    -H "Authorization: Bearer ${TOKEN}" \
    -H "Content-Type: application/json" \
    -d '{}'

トークンの形式は"<username>:<password>" です。ドキュメントには、デフォルトのクレデンシャルとしてroot:Milvus が明記されています。また、Create Users & Rolesガイドでは、ユーザーの管理について説明しています。

タイムアウトの設定

クライアント接続のデフォルトのタイムアウトを設定します:

from pymilvus import MilvusClient

client = MilvusClient(uri="http://localhost:19530", timeout=1000) # If not set, the timeout defaults to 10s
import io.milvus.v2.client.ConnectConfig;
import io.milvus.v2.client.MilvusClientV2;

ConnectConfig config = ConnectConfig.builder()
        .uri("http://localhost:19530")
        .rpcDeadlineMs(1000)
        .build();
client = new MilvusClientV2(config);

import { MilvusClient } from '@zilliz/milvus2-sdk-node';

const client = new MilvusClient({
   address: 'http://localhost:19530',
   username: 'root',
   password: 'Milvus',
   timeout: 1000 // ms
});
// await client.listCollections({ timeout: 2000})
import "github.com/milvus-io/milvus/client/v2/milvusclient"

ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
c, err := milvusclient.New(ctx, &milvusclient.ClientConfig{
    Address: "localhost:19530",
})
export HOST="localhost:19530"
export TOKEN="root:Milvus"

# Request-Timeout is applied per-request (unit: seconds). --max-time
# caps the total curl wall-clock so the client gives up even if the
# server never responds.
curl -X POST "http://${HOST}/v2/vectordb/collections/list" \
    -H "Authorization: Bearer ${TOKEN}" \
    -H "Content-Type: application/json" \
    -H "Request-Timeout: 5" \
    --max-time 7 \
    -d '{}'
  • 上記のSDKでは、このタイムアウトは接続を確立するときにのみ使用され、他のAPI操作のデフォルトのタイムアウトとしては機能しません。

  • RESTful APIでは、Request-Timeout は秒単位のリクエストごとの期限である(JavaのrpcDeadlineMs やNode.jsのtimeout とは異なり、ミリ秒単位である)ので、期限が必要なすべての呼び出しにこれを含めること。

特定のデータベースに接続する

db_name を使用して、構築中にターゲット・データベースを選択します。using_database() を使用して、後で切り替えることもできます。

from pymilvus import MilvusClient

# Set the database when creating the client
client = MilvusClient(
    uri="http://localhost:19530",
    token="root:Milvus",
    db_name="analytics",
)

# (Optional) Switch the active database later
# client.using_database("reports")
import io.milvus.v2.client.ConnectConfig;
import io.milvus.v2.client.MilvusClientV2;

ConnectConfig config = ConnectConfig.builder()
        .uri("http://localhost:19530")
        .username("root")
        .password("Milvus")
        .dbName("analytics")
        .build();
client = new MilvusClientV2(config);
import { MilvusClient } from '@zilliz/milvus2-sdk-node';

const client = new MilvusClient({
   address: 'http://localhost:19530',
   username: 'root',
   password: 'Milvus',
   database: 'analytics'
});
// (Optional) Switch the active database later
// await milvusClient.useDatabase({
//   db_name: 'reports',
//});
import "github.com/milvus-io/milvus/client/v2/milvusclient"

c, err := milvusclient.New(ctx, &milvusclient.ClientConfig{
    Address: "localhost:19530",
    DBName:  "analytics",
    APIKey:  "root:Milvus",
})
// (Optional) switch the active database later with:
err = c.UseDatabase(ctx, milvusclient.NewUseDatabaseOption("reports"))
export HOST="localhost:19530"
export TOKEN="root:Milvus"

# Target a specific database by setting "dbName" in the request body.
# The RESTful API is stateless, so include "dbName" on every request
# that should run against a non-default database.
curl -X POST "http://${HOST}/v2/vectordb/collections/list" \
    -H "Authorization: Bearer ${TOKEN}" \
    -H "Content-Type: application/json" \
    -d '{
      "dbName": "analytics"
    }'

データベースの作成、一覧表示、説明、および広範なデータベース管理タスクについては、データベース・ガイドを参照してください。

次の作業

Try Managed Milvus for Free

Zilliz Cloud is hassle-free, powered by Milvus and 10x faster.

Get Started
フィードバック

このページは役に立ちましたか ?