APIの使用
UPDATE SQLコマンドを使用して、表にあるJSONオブジェクト内のデータを追加および削除できます。
問合せを実行するには、NoSQLHandle.query()
APIを使用します。
こちらにあるサンプルの中からフル・コードModifyData.javaをダウンロードします。
//Update data
private static void updateRows(NoSQLHandle handle,String sqlstmt) throws Exception {
QueryRequest queryRequest = new QueryRequest().setStatement(sqlstmt);
handle.query(queryRequest);
System.out.println("Updated table " + tableName);
}
/* update JSON data and add a node*/
String upd_json_addnode="UPDATE stream_acct acct1 ADD acct1.acct_data.contentStreamed.seriesInfo[1].episodes
{\"date\" : \"2022-04-26\","+
"\"episodeID\" : 43,"+
"\"episodeName\" : \"Season 2 episode 2\","+
"\"lengthMin\" : 45,"+
"\"minWatched\" : 45} WHERE acct_Id=2 RETURNING *";
updateRows(handle,upd_json_addnode);
/* update JSON data and remove a node*/
String upd_json_delnode="UPDATE stream_acct acct1 REMOVE acct1.acct_data.contentStreamed.seriesInfo[1].episodes[1] WHERE acct_Id=2 RETURNING *";
updateRows(handle,upd_json_delnode);
問合せを実行するには、borneo.NoSQLHandle.query()
メソッドを使用します。
こちらにあるサンプルの中からフル・コードModifyData.pyをダウンロードします。
#update data
def update_data(handle,sqlstmt):
request = QueryRequest().set_statement(sqlstmt)
result = handle.query(request)
print('Data Updated in table: stream_acct')
# update JSON data and add a node
upd_json_addnode = '''UPDATE stream_acct acct1 ADD acct1.acct_data.contentStreamed.seriesInfo[1].episodes {
"date" : "2022-04-26",
"episodeID" : 43,
"episodeName" : "Season 2 episode 2",
"lengthMin" : 45,
"minWatched" : 45} WHERE acct_Id=2 RETURNING *'''
update_data(handle,upd_json_addnode)
# update JSON data and delete a node
upd_json_delnode = '''UPDATE stream_acct acct1 REMOVE acct1.acct_data.contentStreamed.seriesInfo[1].episodes[1] WHERE acct_Id=2 RETURNING *'''
update_data(handle,upd_json_delnode)
問合せを実行するには、Client.Query
関数を使用します。
こちらにあるサンプルの中からフル・コードModifyData.goをダウンロードします。
//update data in the table
func updateRows(client *nosqldb.Client, err error, tableName string, querystmt string)(){
prepReq := &nosqldb.PrepareRequest{
Statement: querystmt,
}
prepRes, err := client.Prepare(prepReq)
if err != nil {
fmt.Printf("Prepare failed: %v\n", err)
return
}
queryReq := &nosqldb.QueryRequest{
PreparedStatement: &prepRes.PreparedStatement, }
var results []*types.MapValue
for {
queryRes, err := client.Query(queryReq)
if err != nil {
fmt.Printf("Upsert failed: %v\n", err)
return
}
res, err := queryRes.GetResults()
if err != nil {
fmt.Printf("GetResults() failed: %v\n", err)
return
}
results = append(results, res...)
if queryReq.IsDone() {
break
}
}
for i, r := range results {
fmt.Printf("\t%d: %s\n", i+1, jsonutil.AsJSON(r.Map()))
}
fmt.Printf("Updated data in the table: \n")
}
upd_json_addnode := `UPDATE stream_acct acct1 ADD acct1.acct_data.contentStreamed.seriesInfo[1].episodes {
"date" : "2022-04-26",
"episodeID" : 43,
"episodeName" : "Season 2 episode 2",
"lengthMin" : 45,
"minWatched" : 45} WHERE acct_Id=2 RETURNING *`
updateRows(client, err,tableName,upd_json_addnode)
upd_json_delnode := `UPDATE stream_acct acct1 REMOVE acct1.acct_data.contentStreamed.seriesInfo[1].episodes[1]
WHERE acct_Id=2 RETURNING *`
updateRows(client, err,tableName,upd_json_delnode)
問合せを実行するには、query
メソッドを使用します。
JavaScript: こちらにあるサンプルの中からフル・コードModifyData.jsをダウンロードします。
/*updates data in the table*/
async function updateData(handle,querystmt) {
const opt = {};
try {
do {
const result = await handle.query(querystmt, opt);
opt.continuationKey = result.continuationKey;
} while(opt.continuationKey);
} catch(error) {
console.error(' Error: ' + error.message);
}
}
TypeScript: こちらにあるサンプルの中からフル・コードModifyData.tsをダウンロードします。
interface StreamInt {
acct_Id: Integer;
profile_name: String;
account_expiry: TIMESTAMP;
acct_data: JSON;
}
async function updateData(handle: NoSQLClient,querystmt: string) {
const opt = {};
try {
do {
const result = await handle.query<StreamInt>(querystmt, opt);
for(let row of result.rows) {
console.log(' %O', row);
}
opt.continuationKey = result.continuationKey;
} while(opt.continuationKey);
} catch(error) {
console.error(' Error: ' + error.message);
}
}
const upd_json_addnode =
`UPDATE stream_acct acct1 ADD acct1.acct_data.contentStreamed.seriesInfo[1].episodes {
"date" : "2022-04-26",
"episodeID" : 43,
"episodeName" : "Season 2 episode 2",
"lengthMin" : 45,
"minWatched" : 45} WHERE acct_Id=2 RETURNING *`
await updateData(handle,upd_json_addnode);
console.log("New data node added in the table");
const upd_json_delnode =
'UPDATE stream_acct acct1 REMOVE acct1.acct_data.contentStreamed.seriesInfo[1].episodes[1]
WHERE acct_Id=2 RETURNING *'
await updateData(handle,upd_json_delnode);
console.log("New Data node removed from the table");
UPDATE SQLコマンドを使用して、表にあるJSONオブジェクト内のデータを追加および削除できます。問合せを実行するには、QueryAsync
メソッドをコールするか、GetQueryAsyncEnumerable
メソッドをコールして、結果の非同期列挙可能性に対して反復処理します。
こちらにあるサンプルの中からフル・コードModifyData.csをダウンロードします。
private static async Task updateData(NoSQLClient client,String querystmt){
var queryEnumerable = client.GetQueryAsyncEnumerable(querystmt);
}
private const string upd_json_addnode =
@"UPDATE stream_acct acct1 ADD acct1.acct_data.contentStreamed.seriesInfo[1].episodes {
""date"" : ""2022-04-26"",
""episodeID"" : 43,
""episodeName"" : ""Season 2 episode 2"",
""lengthMin"" : 45,
""minWatched"" : 45} WHERE acct_Id=2 RETURNING *";
await updateData(client,upd_json_addnode);
Console.WriteLine("New data node added in the table");
private const string upd_json_delnode =
"UPDATE stream_acct acct1 REMOVE acct1.acct_data.contentStreamed.seriesInfo[1].episodes[1]
WHERE acct_Id=2 RETURNING *";
await updateData(client,upd_json_delnode);
Console.WriteLine("New Data node removed from the table");