メインコンテンツまでスキップ
バージョン: 3.13

ScalarDB Cluster .NET Client SDK で C# クラスとしてテーブルをはじめよう

注記

このページは英語版のページが機械翻訳されたものです。英語版との間に矛盾または不一致がある場合は、英語版を正としてください。

ScalarDB Cluster .NET Client SDK は、ScalarDB テーブルを C# オブジェクトとして抽象化することで、クラスターにアクセスするコードの作成に役立ちます。クラスター内のテーブルを表すクラスを定義すると、クラスターをクエリするときに列名やその型が混同されないようにすることができます。さらに、テーブルの構造が変更された場合は、IDE のリファクタリング機能を使用してコードに変更を適用できます。

注記

次の例のように非同期メソッドを使用することをお勧めしますが、代わりに同期メソッドを使用することもできます。

SDK をインストールする

ScalarDB Cluster と同じメジャーバージョンとマイナーバージョンの SDK を .NET プロジェクトにインストールします。組み込みの NuGet パッケージマネージャーを使用してこれを行うことができます。<MAJOR>.<MINOR> を、使用しているバージョンに置き換えます。

dotnet add package ScalarDB.Client --version '<MAJOR>.<MINOR>.*'

すべての ScalarDB テーブルのクラスを作成する

ScalarDB テーブルを C# オブジェクトとして操作するには、使用するテーブルごとにクラスを作成する必要があります。例:

using System.ComponentModel.DataAnnotations.Schema;
using ScalarDB.Client.DataAnnotations;

// ...

[Table("ns.statements")]
public class Statement
{
[PartitionKey]
[Column("order_id", Order = 0)]
public string OrderId { get; set; } = String.Empty;

[ClusteringKey]
[Column("item_id", Order = 1)]
public int ItemId { get; set; }

[Column("count", Order = 2)]
public int Count { get; set; }
}

プロパティに使用する型の詳細については、ScalarDB 列型と .NET 型間の変換方法を参照してください。

CRUD 操作を実行する

各テーブルのクラスを作成した後、ITransactionCrudOperable の汎用 GetAsyncScanAsyncInsertAsyncUpdateAsyncDeleteAsyncUpsertAsync、または MutateAsync メソッドを使用して、クラスをオブジェクトとして使用できます。

これらの汎用メソッドを使用するには、using セクションに次の名前空間を追加します。

using ScalarDB.Client.Extensions;

GetAsync メソッドを使用して1つのオブジェクトを取得します

var keys = new Dictionary<string, object>
{
{ nameof(Statement.OrderId), "1" }
};
var statement = await transaction.GetAsync<Statement>(keys);

Console.WriteLine($"ItemId: {statement.ItemId}, Count: {statement.Count}");

ScanAsync メソッドを使用して複数のオブジェクトを取得する

var startKeys = new Dictionary<string, object>
{
{ nameof(Statement.OrderId), "1" },
{ nameof(Statement.ItemId), 3 }
};
var endKeys = new Dictionary<string, object>
{
{ nameof(Statement.ItemId), 6}
};

await foreach (var s in transaction.ScanAsync<Statement>(startKeys, endKeys))
Console.WriteLine($"ItemId: {s.ItemId}, Count: {s.Count}");
注記

IAsyncEnumerable<T> で LINQ メソッドを使用するには、System.Linq.Async パッケージをインストールします。

InsertAsync メソッドを使用して新しいオブジェクトを挿入します

var statement = new Statement
{
OrderId = "2",
ItemId = 4,
Count = 8
};
await transaction.InsertAsync(statement);

UpdateAsync メソッドを使用してオブジェクトを更新する

// ...
statement.ItemId = 4;
statement.Count = 8;

await transaction.UpdateAsync(statement);

DeleteAsync メソッドを使用してオブジェクトを削除する

// ...
await transaction.DeleteAsync(statement);

UpsertAsync メソッドを使用してオブジェクトをUPSERTする

var statement = new Statement
{
OrderId = "2",
ItemId = 4,
Count = 8
};
await transaction.UpsertAsync(statement);

MutateAsync メソッドを使用して複数のオブジェクトを一度にUPSERTおよび削除する

var statement = new Statement
{
OrderId = "2",
ItemId = 4,
Count = 16
};

// ...

await transaction.MutateAsync(objectsToUpsert: new[] { statement },
objectsToDelete: new[] { statement2 });
注記

UpdateAsyncDeleteAsyncUpsertAsync、または MutateAsync メソッドを使用してオブジェクトを変更するには、まず GetAsync または ScanAsync メソッドを使用してオブジェクトを取得する必要があります。

Administrative API を使用する

C# オブジェクトも Administrative API で使用できます。汎用 Administrative API メソッドを使用するには、次の名前空間を using セクションに追加します。

using ScalarDB.Client.Extensions;

新しい名前空間を作成する

await admin.CreateNamespaceAsync<Statement>();

既存の名前空間を削除する

await admin.DropNamespaceAsync<Statement>();

名前空間が存在するかどうかを確認する

var namespaceExists = await admin.IsNamespacePresentAsync<Statement>();

新しいテーブルを作成する

await admin.CreateTableAsync<Statement>();

既存のテーブルを削除する

await admin.DropTableAsync<Statement>();

テーブルが存在するかどうかを確認する

var tableExists = await admin.IsTablePresentAsync<Statement>();
This website uses cookies to enhance the visitor experience. By continuing to use this website, you acknowledge that you have read and understood our privacy policy and consent to the use of cookies to help improve your browsing experience and provide you with personalized content.