Using APIs to add table data
Add rows to your table. When you store data in table rows, your application can easily retrieve, add to, or delete information from a table.
PutRequest
class / put
method to perform unconditional and conditional puts to:
- Overwrite any existing row. Overwrite is the default functionality.
- Succeed only if the row does not exist. Use the
IfAbsent
method in this case. - Succeed only if the row exists. Use the
IfPresent
method in this case. - Succeed only if the row exists and the version matches a specific
version. Use
IfVersion
method for this case and thesetMatchVersion
method to specify the version to match.
The PutRequest
class provides the
setValueFromJson
method which takes a JSON string and uses
that to populate a row to insert into the table. The JSON string should specify
field names that correspond to the table field names.
private static void writeRows(NoSQLHandle handle, MapValue value)
throws Exception {
PutRequest putRequest =
new PutRequest().setValue(value).setTableName(tableName);
PutResult putResult = handle.put(putRequest);
if (putResult.getVersion() != null) {
System.out.println("Added a row to the stream_acct table");
} else {
System.out.println("Put failed");
}
}
The borneo.PutRequest
class represents input to the
borneo.NoSQLHandle.put()
method which is used to insert
single rows.
You can also add JSON data to your table. In the case of a fixed-schema table the JSON is converted to the target schema. JSON data can be directly inserted into a column of type JSON. The use of the JSON data type allows you to create table data without a fixed schema, allowing more flexible use of the data.
def insert_record(handle,table_name,acct_data):
request = PutRequest().set_table_name(table_name)
.set_value_from_json(acct_data)
handle.put(request)
print('Added a row to the stream_acct table')
The nosqldb.PutRequest
represents an input to the
nosqldb.Put()
function and is used to insert single rows.
The data value provided for a row (in PutRequest
) is
a *types.MapValue
. The key
portion of each
entry in the MapValue
must match the column name of target
table, and the value
portion must be a valid value for the
column. JSON data can also be directly inserted into a column of type
JSON
. The use of the JSON data type allows you to create
table data without a fixed schema, allowing more flexible use of the data.
func insertData(client *nosqldb.Client, err error,
tableName string,value1 *types.MapValue )(){
putReq := &nosqldb.PutRequest{
TableName: tableName,
Value: value1,
}
putRes, err := client.Put(putReq)
if err != nil {
fmt.Printf("failed to put single row: %v\n", err)
return
}
fmt.Printf("Added a row to the stream_acct table\n")
}
You use the put
method to insert a single row into
the table. For method details, see NoSQLClient class.
/* Adding 3 records in acct_stream table */
let putResult = await handle.put(TABLE_NAME, JSON.parse(acct1));
let putResult1 = await handle.put(TABLE_NAME, JSON.parse(acct2));
let putResult2 = await handle.put(TABLE_NAME, JSON.parse(acct3));
console.log("Added rows to the stream_acct table");
interface StreamInt {
acct_Id: Integer;
profile_name: String;
account_expiry: TIMESTAMP;
acct_data: JSON;
}
/* Adding 3 records in acct_stream table */
let putResult = await handle.put<StreamInt>(TABLE_NAME, JSON.parse(acct1));
let putResult1 = await handle.put<StreamInt>(TABLE_NAME, JSON.parse(acct2));
let putResult2 = await handle.put<StreamInt>(TABLE_NAME, JSON.parse(acct3));
console.log("Added rows to the stream_acct table");
The method PutAsync
and related methods
PutIfAbsentAsync
, PutIfPresentAsync
and
PutIfVersionAsync
are used to insert a single row into the
table or update a single row.
Each of the Put
methods above returns
Task<PutResult<RecordValue>>
.
PutResult
instance contains info about a completed
Put
operation, such as success status (conditional put
operations may fail if the corresponding condition was not met) and the
resulting RowVersion. Note that Success property of the result only indicates
successful completion as related to conditional Put operations and is always
true for unconditional Puts. If the Put operation fails for any other reason, an
exception will be thrown. Using fields of data type JSON allows more flexibility
in the use of data as the data in JSON field does not have a predefined schema.
To put value into a JSON field, supply a MapValue
instance as
its field value as part of the row value. You may also create its value from a
JSON string via FieldValue.FromJsonString
.
private static async Task insertData(NoSQLClient client, String acctdet){
var putResult = await client.PutAsync(TableName,
FieldValue.FromJsonString(acctdet).AsMapValue);
if (putResult.ConsumedCapacity != null)
{
Console.WriteLine(" Added a row to the stream_acct table");
}
}
You can add more than a row of data in a single database operation using MultiWrite API.
You can perform a sequence of PutRequest
operations associated
with a table that share the same shard key portion of their primary keys as a
single atomic write operation using the WriteMultipleRequest
class. You can also simultaneously add data to a parent and child table using
the WriteMultipleRequest
class. This is an efficient way to
atomically modify multiple related rows. If the operation is successful, the
WriteMultipleResult.getSuccess()
method returns true.
See Oracle NoSQL Java SDK API Reference for more details on the various classes and methods.
private static void writeMul(NoSQLHandle handle,String parent_tblname,
String parent_data, String child_tblname, String child_data){
WriteMultipleRequest umRequest = new WriteMultipleRequest();
PutRequest putRequest =
new PutRequest().setValueFromJson(parent_data,null).setTableName(parent_tblname);
umRequest.add(putRequest, false);
putRequest =
new PutRequest().setValueFromJson(child_data,null).setTableName(child_tblname);
umRequest.add(putRequest, false);
WriteMultipleResult umResult = handle.writeMultiple(umRequest);
}
You can perform a sequence of PutRequest
operations associated
with a table that share the same shard key portion of their primary keys as a
single atomic write operation using the
borneo.WriteMultipleRequest
class. You can also
simultaneously add data to a parent and child table using the
borneo.WriteMultipleRequest
class. This is an efficient way
to atomically modify multiple related rows.
See Oracle NoSQL Python SDK API Reference for more details on the various classes and methods.
def mul_write(handle,parent_tblname,parent_data,
child_tblname, child_data):
request = PutRequest()
request.set_value_from_json(parent_data)
request.set_table_name('ticket')
wm_req.add(request, True)
request1 = PutRequest()
request1.set_table_name(child_tblname)
request1.set_value_from_json(child_data)
wm_req.add(request1, True)
result = handle.write_multiple(wm_req)
You can perform a sequence of PutRequest
operations associated
with a table that share the same shard key portion of their primary keys as a
single atomic write operation using the WriteMultipleRequest
class. You can also simultaneously add data to a parent and child table using
the WriteMultipleRequest
class. This is an efficient way to
atomically modify multiple related rows.
See Oracle NoSQL Go SDK API Reference for more details on the various classes and methods.
//multiple write from the table
func mul_write(client *nosqldb.Client, err error, parenttbl_name string,
parent_data string, childtbl_name string, child_data string)(){
value, err := types.NewMapValueFromJSON(parent_data)
putReq := &nosqldb.PutRequest{
TableName: parenttbl_name,
Value: value,
}
wmReq := &nosqldb.WriteMultipleRequest{
TableName: "",
Timeout: 10 * time.Second,
}
wmReq.AddPutRequest(putReq, true)
value1, err := types.NewMapValueFromJSON(child_data)
putReq1 := &nosqldb.PutRequest{
TableName: childtbl_name,
Value: value1,
}
wmReq.AddPutRequest(putReq1, true)
wmRes, err := client.WriteMultiple(wmReq)
if err != nil {
fmt.Printf("WriteMultiple() failed: %v\n", err)
return
}
if wmRes.IsSuccess() {
fmt.Printf("WriteMultiple() succeeded\n")
} else {
fmt.Printf("WriteMultiple() failed\n")
}
}
You can perform a sequence of put operations associated with a table
that share the same shard key portion of their primary keys as a single atomic
write operation using the writeMany
method. You can also
simultaneously add data to a parent and child table using the
writeMany
method. This is an efficient way to atomically
modify multiple related rows.
For method details, see NoSQLClient class.
const ops = [
{
tableName: 'ticket',
put: {
"ticketNo": "1762344493810",
"confNo" : "LE6J4Z"
},
abortOnFail: true
},
{
tableName: 'ticket.bagInfo',
put: {
"ticketNo":"1762344493810",
"id":"79039899165297",
"tagNum":"17657806255240",
"routing":"MIA/LAX/MEL",
"lastActionCode":"OFFLOAD",
"lastActionDesc":"OFFLOAD",
"lastSeenStation":"MEL",
"lastSeenTimeGmt":"2019-02-01T16:13:00Z",
"bagArrivalDate":"2019-02-01T16:13:00Z"
},
abortOnFail: true
}
];
const res = await handle.writeMany(ops, null);
const ops = [
{
tableName: 'ticket',
put: {
"ticketNo": "1762344493810",
"confNo" : "LE6J4Z"
},
abortOnFail: true
},
{
tableName: 'ticket.bagInfo',
put: {
"ticketNo":"1762344493810",
"id":"79039899165297",
"tagNum":"17657806255240",
"routing":"MIA/LAX/MEL",
"lastActionCode":"OFFLOAD",
"lastActionDesc":"OFFLOAD",
"lastSeenStation":"MEL",
"lastSeenTimeGmt":"2019-02-01T16:13:00Z",
"bagArrivalDate":"2019-02-01T16:13:00Z"
},
abortOnFail: true
}
];
const res = await handle.writeMany(ops, null);
You can perform a sequence of put operations associated with a table that share
the same shard key portion of their primary keys as a single atomic write
operation using the PutManyAsync
method. You can also
simultaneously add data to a parent and child table using the
PutManyAsync
method. This is an efficient way to atomically
modify multiple related rows.
See Oracle NoSQL Dotnet SDK API Reference for more details of all classes and methods.
private static async Task mul_write(NoSQLClient client,string parentbl_name,
string data1, string childtbl_name, string data2){
var result = await client.WriteManyAsync(
new WriteOperationCollection()
.AddPut(parentbl_name, FieldValue.FromJsonString(data1).AsMapValue)
.AddPut(childtbl_name, FieldValue.FromJsonString(data2).AsMapValue)
);
}
For information about Oracle's commitment to accessibility, visit the Oracle Accessibility Program website at http://www.oracle.com/pls/topic/lookup?ctx=acc&id=docacc.
Access to Oracle Support
Oracle customers that have purchased support have access to electronic support through My Oracle Support. For information, visit http://www.oracle.com/pls/topic/lookup?ctx=acc&id=info or visit http://www.oracle.com/pls/topic/lookup?ctx=acc&id=trs if you are hearing impaired.