ストア内の表への行の書込み
データ・ストアの表への新しい行の書込みおよび既存の行の更新は同様の操作です。この項の後半では、行を更新する場合または行を作成する場合にのみ機能するメソッドについて説明します。表にデータを追加できるのは、表をストアに追加した後のみです。詳細は、「Oracle NoSQL Database表および索引の概要」を参照してください。
ストア内の表に行を書き込むには、次の手順を実行します。
-
データを書き込む表のハンドルを作成します。
KVStore.getTableAPI()
を使用して、TableAPI
インタフェース・インスタンスを取得することで、これを実行します。そのインスタンスを使用して、TableAPI.getTable()
によって表のハンドルを取得します。これにより、Table
インタフェース・インスタンスが返されます。ノート:
TableAPI.getTable()
メソッドは、サーバー側アクセスを必要とする消費が多いコールです。最高のパフォーマンスを得るには、表ハンドルが必要になるたびにこのメソッドをコールしないでください。可能であれば、コードの設定セクションで関連するすべての表に対してこのメソッドをコールします。その後、それらのハンドルをアプリケーションを通して再利用します。 -
前のステップで取得した
Table
インスタンスを使用して行インタフェース・インスタンスを作成するには、Table.createRow()
メソッドを使用します。 -
Row.put()
メソッドを使用して、行の各フィールドに書き込みます。NULL
値を書き込むには、Row.put()
ではなくRow.putNull()
を使用します。 -
TableAPI.put()
を使用して、新しい行をストアに書き込みます。ノート:
書き込む表にIDENTITY列が含まれている場合は、シーケンス・ジェネレータから生成された値が行で使用可能になります。
特別な目的のストリームを使用して、行をストアにロードすることもできます。詳細は、一括格納操作を参照してください。
次の例は、KVStore
ハンドルを作成済であることを前提として、行をストアに書き込む方法を示しています。
package kvstore.basicExample;
import oracle.kv.KVStore;
import oracle.kv.table.Row;
import oracle.kv.table.Table;
import oracle.kv.table.TableAPI;
...
// KVStore handle creation is omitted for brevity
...
TableAPI tableH = kvstore.getTableAPI();
// The name you give getTable() must be identical
// to the name of the table when you created it with
// the CREATE TABLE DDL statement (myTable in this example).
Table myTable = tableH.getTable("myTable");
// Get a Row instance
Row row = myTable.createRow();
// Use row.put to put all of the cells into the row.
// This does NOT actually write the data to the store.
row.put("item", "Bolts");
row.put("description", "Hex head, stainless");
row.put("count", 5);
row.put("percentage", 0.2173913);
// Now write the table to the store.
// "item" is the row's primary key. If we had not set that key and its value,
// this operation will result in an IllegalArgumentException.
tableH.put(row, null, null);
子表へのデータの書込み
子表に書き込むには、親表に対するタスクを完了します。ただし、parent-table.child-tableなどの2つのパートからなる表明を使用します。
たとえば、「子表の定義」では子表の作成方法を示しました。その表にデータを書き込むには、次の手順を実行します。
package kvstore.basicExample;
import oracle.kv.KVStore;
import oracle.kv.table.Row;
import oracle.kv.table.Table;
import oracle.kv.table.TableAPI;
...
// KVStore handle creation is omitted for brevity
...
TableAPI tableH = kvstore.getTableAPI();
// Get the corresponding child table
Table myChildTable = tableH.getTable("myInventory.itemDetails");
// Get a row instance
Row childRow = myChildTable.createRow();
// Populate the rows. Because the parent table's "itemCategory"
// field is a primary key, this must be populated in addition
// to all of the child table's rows
childRow.put("itemCategory", "Bolts");
childRow.put("itemSKU", "1392610");
childRow.put("itemDescription", "1/4-20 x 1/2 Grade 8 Hex");
childRow.put("price", new Float(11.99));
childRow.put("inventoryCount", 1457);
IDENTITY列への行の書込み
値をIDENTITY列にプログラムで挿入する場合は、特別な考慮事項が発生します。この項では、存在する問題とput()
およびその他のメソッドを使用してその問題を回避する方法を示します。
- GENERATED ALWAYS AS IDENTITY
- GENERATED BY DEFAULT AS IDENTITY
- GENERATED BY DEFAULT ON NULL AS IDENTITY
put
関数を使用した行の追加時に、そのバリアントの1つとともにアクティビティに影響します。
put
(無条件)put if absent
(行に値がない場合のみ)put if present
(行に値がある場合のみ)
CREATE Table foo(
idValue INTEGER GENERATED ALWAYS AS IDENTITY
(START WITH 1 INCREMENT BY 1 MAXVALUE 2 NO CYCLE),
name STRING,
PRIMARY KEY(idValue));
IDENTITY列への行の挿入
foo
表に行を挿入するには、アプリケーションで次を実行します。
L1: TableAPI api = store.getTableAPI(); // Gets the TableAPI for the store
L2: Table table = api.getTable("foo"); // Gets the Table foo instance
L3: Row row = table.createRow(); // constructs an empty Row row for Table foo.
L4: row.put("name", "joe"); // populates the values for the Row row
L5: api.put(row, null /* previous-row */, null /* write-options */);
// The client driver recognizes that the system must generate the id values and \
generates value 1 for the id field in the row before putting it in the DB.
L6: System.out.println("Value of idValue: " + row.get("idValue")); // 1
L7: row.put("name", "smith");
L8: api.put(row, null /* previous-row */, null /* write-options */);
// driver sets id field to 2
L9: System.out.println("Value of id: " + row.get("idValue")); // 2
ノート:
生成されたIDENTITY列の値を取得するには、L6およびL9
に示すとおり、IDENTITY列に対してget()
コールを使用します。
idValue
を返すには、次のようにRETURNING idValue
句を使用します。
StatementResult sr = store.executeSync("INSERT INTO foo " + "(name) VALUES ('foe')
RETURNING idValue");
int id = sr.iterator().next().get("idValue").asInteger().get();
GENERATED ALWAYS AS IDENTITY
として列を定義する場合、SGが常に次の値を指定する必要があるため、IDENTITY列に値を指定することはできません。
次の例は、IDENTITY列に値を指定しようとした場合を示しています。最初の追加であるjoe
およびjohn
は問題なく、SGは両方にidValue
を指定します。
CREATE TABLE foo(
idValue INTEGER GENERATED ALWAYS AS IDENTITY,
name STRING, PRIMARY KEY (idValue))
api.put(‘joe’)
api.put(‘john’)
get(idValue, name) or
select * from foo;
1, joe
2, john
GENERATED ALWAYS AS IDENTITY
として定義されている場合に次のエラーが発生します。
api.put(2,’dave’) // error –- cannot specify a value for \
a column defined as GENERATED ALWAYS AS IDENTITY
api.putIfPresent (2, ‘dave’) -- The following error occurs first in the code path,
even though idValue = 2 is present
// error – user cannot specify a value for \
IDENTITY column defined as GENERATED ALWAYS
api.putIfPresent (3,’cezar’) -- The following error occurs, first in the code path,
even though idValue = 3 is NOT present
// error - user cannot specify a value for \
IDENTITY column defined as GENERATED ALWAYS
api.putIfPresent (‘hema’)
//error – a primary key is not provided to look up the record.
putIfAbsent (10, joe) -– is an insert
// error - user cannot specify a value for \
IDENTITY column defined as GENERATED ALWAYS
GENERATED ALWAYS AS IDENTITY
として定義された列に対してUPDATEを使用するには、次のようにします。
Create table foo( idValue INTEGER GENERATED ALWAYS AS IDENTITY,
name STRING,
PRIMARY KEY (idValue))
UPDATE foo SET idValue = 10 WHERE name=joe
// error - user cannot set a value for an IDENTITY column defined as GENERATED ALWAYS
UPDATE foo SET name=hema WHERE idValue=2
// Success! By using the Primary Key value (idValue=2)to locate its name record,
// you can update the value and hema replaces john
select * from foo
1, joe
2, hema
put
、putIfPresent
およびputIfAbsent
を使用するには、次のようにします。
Create table Foo(idValue INTEGER GENERATED ALWAYS AS IDENTITY,
acctNumber INTEGER,
name STRING,
PRIMARY KEY (acctNumber) )
//Put two acctNumber and name values.
api.put(100, ‘joe’)
api.put (200, ‘john’)
//SG increments the IDENTITY values, 1 and 2:
api.get(idValue, acctNumber, name)
1, 100, joe
2, 200, john
//Attempt to put an idValue
api.put (2, 200, dave)
// error – Cannot specify a value for IDENTITY column defined as GENERATED ALWAYS
api.putIfPresent(3, 200, cezar)
//error – Cannot specify a value for IDENTITY column defined as GENERATED ALWAYS
api.putIfPresent (400, cezar) // not IDENTITY column value error
// error - Cannot specify a primary key (400) that is not present
api.putIFPresent (200, cezar)
1, 100, joe
2, 200, cezar
// Success! The IDENTITY value is updated.
The system generates a value on putIfPresent as the API semantics are to update
the entire record, and not update fields within the record selectively.
api.putIfAbsent (300, hema)
// Success! IDENTITY idValue was generated (3), and 300, hema were absent
get(idValue, acctNumber, name)
1, 100, joe
2, 200, cezar
3, 300, hema
api.putIfAbsent (20, 300, hema)
// error – user cannot specify a value for IDENTITY column defined as GENERATED ALWAYS
api.putIfAbsent (300, hema)
//error - no row with primary key = 300 is present
api.putIfAbsent (3,400, hema)
// error – user cannot specify a value for IDENTITY column defined as GENERATED ALWAYS
UPDATE
を使用するには、次のようにします。
Create table Foo(idValue INTEGER GENERATED ALWAYS AS IDENTITY,
acctNumber INTEGER,
name STRING,
PRIMARY KEY (acctNumber))
select * from foo
1, 100, joe
2, 200, cezar
3, 300, hema
UPDATE foo set name= dave, where PRIMARY KEY = 200
// replaces (2, 200, cezar) with (2, 200, dave)
select * from foo;
1, 100, joe
2, 200, dave
3, 300, hema
UPDATE foo set name=george, where acctNumber=100
// acctNumber is the PRIMARY KEY
// replaces (1, 100, joe) with (1, 100, george)
select * from foo;
1, 100, george
2, 200, dave
3, 300, hema
UPDATE foo set idValue=10, where acctNumber=100
// acctNumber is the PRIMARY KEY
// error - Cannot specify a value for IDENTITY column defined as GENERATED ALWAYS
PRIMARY KEYである、GENERATED BY DEFAULT AS IDENTITYとして定義されている列に対してput()
を使用するには、次の例を確認してください。この場合、IDENTITY列に値を指定しないと、SGによって値が生成されます。値を指定すると、指定した値が使用されます。
Create table foo( idValue INTEGER GENERATED BY DEFAULT AS IDENTITY,
name STRING,
PRIMARY KEY (idValue))
api.put(‘joe’)
api.put(‘john’)
//Since you supplied no idValue, SG supplies them:
get(idValue, name)
1, joe
2, john
//You supply 4 as the idValue, so system uses it
api.put (4, george)
get(idValue, name)
1, joe
2, john
4, george
api.put (2, sam) // replaces (2, john) with (2, sam)
get(idValue, name)
1, joe
2, sam
4, george
列に対してUPDATE()
を使用するには、次のようにします。
select * from foo;
1, joe
2, sam
4, george
UPDATE foo SET idValue=3 where name=sam
// Updates idValue 2 (2, sam) with 3, so becomes 3, sam
select * from foo
1, joe
3, sam
4, george
IDENTITY列を含む行の削除は、その製品の既存の削除ロジックに従います。変更はありません。
その他の格納操作
前述した非常に単純な使用方法であるTableAPI.put()
メソッド以外に、使用できる格納操作が3つあります。
-
TableAPI.putIfAbsent()
このメソッドでは、行の主キー値が表に存在しない場合にのみ行が格納されます。つまり、このメソッドは、作成操作になった場合にのみ成功します。
-
TableAPI.putIfPresent()
このメソッドでは、行の主キー値が表にすでに存在する場合にのみ行が格納されます。つまり、このメソッドは、更新操作になった場合にのみ成功します。
-
TableAPI.putIfVersion()
このメソッドでは、指定されたバージョン情報に値が一致する場合にのみ行が格納されます。詳細は、「行バージョンの使用」を参照してください。