Table Store は、PutRow、GetRow、UpdateRow、DeleteRow などの単一行操作用のインターフェイスを提供します。
データ行を入れる (PutRow)
PutRow インターフェイスは、データ行を挿入するために使用されます。 行が存在しない場合は、この操作によって行が挿入されます。 行が存在する場合、この操作はそれを上書きします。 これは、元の行のすべてのバージョンのすべての列と列値が削除されることを意味します。
例
PutRowRequest req;
{
RowPutChange& chg = req.mutableRowChange();
chg.mutableTable() = "YourTable";
{
// 配置する行の主キーを設定します。
PrimaryKey& pkey = chg.mutablePrimaryKey();
pkey.append() = PrimaryKeyColumn(
"pkey",
PrimaryKeyValue::toStr("pkey-value"));
}
{
// 配置する行の属性を設定します。
IVector<Attribute>& attrs = chg.mutableAttributes();
attrs.append() = Attribute(
"attr",
AttributeValue::toInteger(123));
}
}
PutRowResponse resp;
Optional<OTSError> res = client.putRow(resp, req);
行のデータを取得する (GetRow)
GetRow インターフェイスは、単一行のデータを読み込むために使用されます。
行のテーブル名とプライマリキーを指定してください。 2 つの考えられる読み取り結果は次のとおりです。
-
この行が存在する場合、GetRowResponse オブジェクトはその行の各プラマリキー列と属性列を返します。
-
この行が存在しない場合、GetRowResponse オブジェクトに行は含まれず、エラーも報告されません。
例
GetRowRequest req;
{
PointQueryCriterion& query = req.mutableQueryCriterion();
query.mutableTable() = “YourTable”;
{
PrimaryKey& pkey = query.mutablePrimaryKey();
pkey.append() = PrimaryKeyColumn(
"pkey",
PrimaryKeyValue::toStr("some_key")); // テーブルには、文字列型のプライマリキーが 1 つしかないと仮定します。
}
query.mutableMaxVersions().reset(1);
}
GetRowResponse resp;
Optional<OTSError> res = client.getRow(resp, req);
行のデータを更新する (UpdateRow)
UpdateRow インターフェイスは、データ行を更新するために使用されます。 指定された行が存在しない場合は、新しい行が追加されます。
更新操作には、以下の 4 つのシナリオが考えられます。
-
バージョン番号を指定せずに列値を書き込みます。 Table Store サービスは自動的にバージョン番号を提示して、バージョン番号が確実に増分されるようにします。
-
指定されたバージョン番号で列値を書き込みます。 列にこのバージョンの列値がない場合は、データが挿入されます。 列にある場合は、元の値が上書きされます。
-
指定されたバージョンの列値を削除します。
-
列全体のすべてのバージョンの列値を削除します。
例
UpdateRowRequest req;
{
RowUpdateChange& chg = req.mutableRowChange();
chg.mutableTable() = "YourTable";
{
// 配置する行のプライマリキーを設定します。
PrimaryKey& pkey = chg.mutablePrimaryKey();
pkey.append() = PrimaryKeyColumn(
"pkey",
PrimaryKeyValue::toStr("pkey"));
}
{
// バージョンを指定せずに値を挿入します。
RowUpdateChange::Update& up = chg.mutableUpdates().append();
up.mutableType() = RowUpdateChange::Update::kPut;
up.mutableAttrName() = "attr0";
up.mutableAttrValue().reset(AttributeValue::toStr("new value without specifying version"));
}
{
// バージョンとともに値を挿入します。
RowUpdateChange::Update& up = chg.mutableUpdates().append();
up.mutableType() = RowUpdateChange::Update::kPut;
up.mutableAttrName() = "attr1";
up.mutableAttrValue().reset(AttributeValue::toStr("new value with version"));
up.mutableTimestamp().reset(UtcTime::now());
}
{
// 特定のバージョンの値を削除します。
RowUpdateChange::Update& up = chg.mutableUpdates().append();
up.mutableType() = RowUpdateChange::Update::kDelete;
up.mutableAttrName() = "attr2";
up.mutableTimestamp().reset(UtcTime::now());
}
{
// 属性列のすべての値を削除します。
RowUpdateChange::Update& up = chg.mutableUpdates().append();
up.mutableType() = RowUpdateChange::Update::kDeleteAll;
up.mutableAttrName() = "attr3";
}
}
UpdateRowResponse resp;
Optional<OTSError> res = client.updateRow(resp, req);
行のデータを削除する (DeleteRow)
DeleteRow インターフェイスは、行を削除するために使用されます。 この行が存在するかどうかにかかわらず、エラーは報告されません。
例
DeleteRowRequest req;
{
RowDeleteChange& chg = req.mutableRowChange();
chg.mutableTable() = "YourTable";
{
// 削除する行のプライマリキーを設定します。
PrimaryKey& pkey = chg.mutablePrimaryKey();
pkey.append() = PrimaryKeyColumn(
"pkey",
PrimaryKeyValue::toInteger(1));
}
}
DeleteRowResponse resp;
Optional<OTSError> res = client.deleteRow(resp, req);