Table Store提供了PutRow,GetRow,UpdateRow和DeleteRow等單行操作的介面。

说明 以下操作為同步介面的樣本,非同步介面的操作請參見非同步介面

插入一行資料(PutRow)

PutRow 介面用於插入一行資料。若該行不存在則插入,如果該行已經存在則覆蓋(即原行的所有列以及所有版本的列值都被刪除)。

樣本

PutRowRequest req;
{
    RowPutChange& chg = req.mutableRowChange();
    chg.mutableTable() = "YourTable";
    {
        // set primary key of the row to put
        PrimaryKey& pkey = chg.mutablePrimaryKey();
        pkey.append() = PrimaryKeyColumn(
            "pkey",
            PrimaryKeyValue::toStr("pkey-value"));
    }
    {
        // set attributes of the row to put
        IVector<Attribute>& attrs = chg.mutableAttributes();
        attrs.append() = Attribute(
            "attr",
            AttributeValue::toInteger(123));
    }
}
PutRowResponse resp;
Optional<OTSError> res = client.putRow(resp, req);

讀取一行資料(GetRow)

單行讀 GetRow 介面用於讀取一行資料。

指定表名和一行的主鍵,讀取的結果可能有兩種:

  • 若該行存在,則GetRowResponse對象返回該行的各主鍵列以及屬性列。

  • 若該行不存在,則GetRowResponse對象不含有行,並且不會報錯。

樣本

GetRowRequest req;
{
    PointQueryCriterion& query = req.mutableQueryCriterion();
    query.mutableTable() = “YourTable”;
    {
        PrimaryKey& pkey = query.mutablePrimaryKey();
        pkey.append() = PrimaryKeyColumn(
            "pkey",
            PrimaryKeyValue::toStr("some_key")); // 假設主鍵只有一列,並且類型為字串
    }
    query.mutableMaxVersions().reset(1);
}
GetRowResponse resp;
Optional<OTSError> res = client.getRow(resp, req);

更新一行資料(UpdateRow)

UpdateRow 介面用於更新一行資料,如果原行不存在,則新寫入一行。

更新操作有以下四種情況:

  • 不指定版本寫入一個列值,Table Store服務端會自動補上一個版本號碼,保證此種情況下版本號碼的遞增。

  • 指定版本寫入一個列值,若該列原先沒有該版本列值,則插入資料,否則覆蓋原值。

  • 刪除指定版本的列值。

  • 刪除整個列的所有版本列值。

樣本

UpdateRowRequest req;
{
    RowUpdateChange& chg = req.mutableRowChange();
    chg.mutableTable() = "YourTable";
    {
        // set primary key of the row to put
        PrimaryKey& pkey = chg.mutablePrimaryKey();
        pkey.append() = PrimaryKeyColumn(
            "pkey",
            PrimaryKeyValue::toStr("pkey"));
    }
    {
        // insert a value without specifying version
        RowUpdateChange::Update& up = chg.mutableUpdates().append();
        up.mutableType() = RowUpdateChange::Update::kPut;
        up.mutableAttrName() = "attr0";
        up.mutableAttrValue().reset(AttributeValue::toStr("new value without specifying version"));
    }
    {
        // insert a value with 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());
    }
    {
        // delete a value with specific version
        RowUpdateChange::Update& up = chg.mutableUpdates().append();
        up.mutableType() = RowUpdateChange::Update::kDelete;
        up.mutableAttrName() = "attr2";
        up.mutableTimestamp().reset(UtcTime::now());
    }
    {
        // delete all values of a attribute column
        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";
    {
        // set primary key of the row to delete
        PrimaryKey& pkey = chg.mutablePrimaryKey();
        pkey.append() = PrimaryKeyColumn(
            "pkey",
            PrimaryKeyValue::toInteger(1));
    }
}
DeleteRowResponse resp;
Optional<OTSError> res = client.deleteRow(resp, req);