Skip to content

Commit

Permalink
new code examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Vandana-Rajamani committed Jan 30, 2024
1 parent e594ddf commit f5f22c9
Show file tree
Hide file tree
Showing 39 changed files with 13,115 additions and 5 deletions.
441 changes: 441 additions & 0 deletions examples-nosql-dotnet-sdk/sqlexamples/AddBagData.cs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples-nosql-dotnet-sdk/sqlexamples/CreateTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ private async static Task<NoSQLClient> getconnection_cloud()
var client = new NoSQLClient(new NoSQLConfig
{
Region = <your_region_identifier>,
Compartment = "<your_compartment_ocid"
Compartment = "<your_compartment_ocid>"
});
return client;
}
Expand Down
479 changes: 479 additions & 0 deletions examples-nosql-dotnet-sdk/sqlexamples/GroupSortData.cs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples-nosql-dotnet-sdk/sqlexamples/ModifyData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ private async static Task<NoSQLClient> getconnection_cloud()
var client = new NoSQLClient(new NoSQLConfig
{
Region = <your_region_identifier>,
Compartment = "<your_compartment_ocid"
Compartment = "<your_compartment_ocid>"
});
return client;
}
Expand Down
178 changes: 178 additions & 0 deletions examples-nosql-dotnet-sdk/sqlexamples/MultiDataOps.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
// Copyright (c) 2023, 2024 Oracle and/or its affiliates. All rights reserved.
// Licensed under the Universal Permissive License v 1.0 as shown at
// https://oss.oracle.com/licenses/upl/

namespace Oracle.NoSQL.SDK.Samples
{
using System;
using System.Threading.Tasks;
using Oracle.NoSQL.SDK;
// -----------------------------------------------------------------------
// Run the example as:
//
// dotnet run -f <target framework>
//
// where:
// - <target framework> is target framework moniker, supported values
// are netcoreapp5.1 and net7.0
// -----------------------------------------------------------------------
public class MultiDataOps
{
private const string Usage =
"Usage: dotnet run -f <target framework> [-- <config file>]";
private const string TableName = "examplesAddress";
private const string add1= @"{
""id"":1,
""address_line1"":""10 Red Street"",
""address_line2"":""Apt 3"",
""pin"":1234567
}";
private const string add2= @"{
""id"":2,
""address_line1"":""2 Green Street"",
""address_line2"":""Street 9"",
""pin"":1234567
}";
private const string add3= @"{
""id"":3,
""address_line1"":""5 Blue Ave"",
""address_line2"":""Floor 9"",
""pin"":1234567
}";
private const string add4= @"{
""id"":4,
""address_line1"":""9 Yellow Boulevard"",
""address_line2"":""Apt 3"",
""pin"":87654321
}";
private const string stmt1 ="select * from examplesAddress";

// Get a connection handle for Oracle NoSQL Database Cloud Service
private async static Task<NoSQLClient> getconnection_cloud()
{
// replace the region and compartment place holders with actual values
var client = new NoSQLClient(new NoSQLConfig
{
Region = <your_region_identifier>,
Compartment = "<your_compartment_ocid>"
});
return client;
}
// Get a connection handle for onPremise data store
private async static Task<NoSQLClient> getconnection_onPrem()
{
// replace the placeholder with your fullname of your host
var client = new NoSQLClient(new NoSQLConfig
{
ServiceType = ServiceType.KVStore,
Endpoint = "http://<hostname>:8080"
});
return client;
}

private static async Task crtTabAddData(NoSQLClient client)
{
// Create a table
var sql =
$@"CREATE TABLE IF NOT EXISTS {TableName}(id INTEGER,
address_line1 STRING,
address_line2 STRING,
pin INTEGER,
PRIMARY KEY(SHARD(pin), id))";

Console.WriteLine("\nCreate table {0}", TableName);
var tableResult = await client.ExecuteTableDDLAsync(sql,
new TableDDLOptions
{
TableLimits = new TableLimits(20, 20, 1)
});

Console.WriteLine(" Creating table {0}", TableName);
Console.WriteLine(" Table state: {0}", tableResult.TableState);
// Wait for the operation completion
await tableResult.WaitForCompletionAsync();
Console.WriteLine(" Table {0} is created",
tableResult.TableName);
Console.WriteLine(" Table state: {0}", tableResult.TableState);
// Write a record
Console.WriteLine("\nInsert records");
var putResult = await client.PutAsync(TableName, FieldValue.FromJsonString(add1).AsMapValue);
if (putResult.ConsumedCapacity != null)
{
Console.WriteLine(" Write used:");
Console.WriteLine(" " + putResult.ConsumedCapacity);
}
var putResult1 = await client.PutAsync(TableName, FieldValue.FromJsonString(add2).AsMapValue);
if (putResult1.ConsumedCapacity != null)
{
Console.WriteLine(" Write used:");
Console.WriteLine(" " + putResult1.ConsumedCapacity);
}
var putResult2 = await client.PutAsync(TableName, FieldValue.FromJsonString(add3).AsMapValue);
if (putResult2.ConsumedCapacity != null)
{
Console.WriteLine(" Write used:");
Console.WriteLine(" " + putResult2.ConsumedCapacity);
}
var putResult3 = await client.PutAsync(TableName, FieldValue.FromJsonString(add4).AsMapValue);
if (putResult3.ConsumedCapacity != null)
{
Console.WriteLine(" Write used:");
Console.WriteLine(" " + putResult3.ConsumedCapacity);
}
}
private static async Task fetchData(NoSQLClient client,String querystmt){
var queryEnumerable = client.GetQueryAsyncEnumerable(querystmt);
await DoQuery(queryEnumerable);
}

private static async Task mulDelRows(NoSQLClient client,int pinval){
var parKey = new MapValue {["pin"] = pinval};
var options = new DeleteRangeOptions();
do
{
var result = await client.DeleteRangeAsync(TableName,parKey,options);
Console.WriteLine($"Deleted {result.DeletedCount} row(s)");
options.ContinuationKey = result.ContinuationKey;
} while(options.ContinuationKey != null);
}
//replace the place holder for compartment with the OCID of your compartment
public static async Task Main(string[] args)
{
try {
//if using cloud service uncomment the code below
var client = await getconnection_cloud();
//if using onPremise uncomment the code below
//var client = await getconnection_onPrem();
Console.WriteLine("Created NoSQLClient instance");
await crtTabAddData(client);
await fetchData(client,stmt1);
await mulDelRows(client,1234567);
await fetchData(client,stmt1);

}
catch (Exception ex) {
Console.WriteLine("Exception has occurred:\n{0}: {1}",
ex.GetType().FullName, ex.Message);
Console.WriteLine("StackTrace is ");
Console.WriteLine( ex.StackTrace);
if (ex.InnerException != null)
{
Console.WriteLine("\nCaused by:\n{0}: {1}",
ex.InnerException.GetType().FullName,
ex.InnerException.Message);
}
}
}
private static async Task DoQuery(IAsyncEnumerable<QueryResult<RecordValue>> queryEnumerable){
Console.WriteLine(" Query results:");
await foreach (var result in queryEnumerable) {
foreach (var row in result.Rows)
{
Console.WriteLine();
Console.WriteLine(row.ToJsonString());
}
}
}
}
}
158 changes: 158 additions & 0 deletions examples-nosql-dotnet-sdk/sqlexamples/MultiWrite.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
// Copyright (c) 2023, 2024 Oracle and/or its affiliates. All rights reserved.
// Licensed under the Universal Permissive License v 1.0 as shown at
// https://oss.oracle.com/licenses/upl/

namespace Oracle.NoSQL.SDK.Samples
{
using System;
using System.Threading.Tasks;
using Oracle.NoSQL.SDK;
// -----------------------------------------------------------------------
// Run the example as:
//
// dotnet run -f <target framework>
//
// where:
// - <target framework> is target framework moniker, supported values
// are netcoreapp5.1 and net7.0
// -----------------------------------------------------------------------
public class MultiWrite
{
private const string Usage =
"Usage: dotnet run -f <target framework> [-- <config file>]";
private const string regTblName = "ticket";
private const string childTblName = "ticket.bagInfo";
private const string descTblName = "ticket.bagInfo.flightLegs";
private const string regtbl_ddl = $@"CREATE TABLE IF NOT EXISTS {regTblName}(ticketNo LONG,
confNo STRING,
primary key(ticketNo))";
private const string childtbl_ddl = $@"CREATE TABLE IF NOT EXISTS {childTblName}(id LONG,
tagNum LONG,
routing STRING,
lastActionCode STRING,
lastActionDesc STRING,
lastSeenStation STRING,
lastSeenTimeGmt TIMESTAMP(4),
bagArrivalDate TIMESTAMP(4),
primary key(id))";
private const string desctbl_ddl = $@"CREATE TABLE IF NOT EXISTS {descTblName}(flightNo STRING,
flightDate TIMESTAMP(4),
fltRouteSrc STRING,
fltRouteDest STRING,
estimatedArrival TIMESTAMP(4),
actions JSON,
primary key(flightNo))";
private const string data1=@"{""ticketNo"": ""1762344493810"",""confNo"" : ""LE6J4Z"" }";
private const string data2=@"{""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""}";
private const string data3=@"{""ticketNo"":""1762344493810"",
""id"":""79039899165297"",
""flightNo"":""BM604"",
""flightDate"":""2019-02-01T06:00:00Z"",
""fltRouteSrc"":""MIA"",
""fltRouteDest"":""LAX"",
""estimatedArrival"":""2019-02-01T11:00:00Z"",
""actions"":[ {
""actionAt"" : ""MIA"",
""actionCode"" : ""ONLOAD to LAX"",
""actionTime"" : ""2019-02-01T06:13:00Z""
}, {
""actionAt"" : ""MIA"",
""actionCode"" : ""BagTag Scan at MIA"",
""actionTime"" : ""2019-02-01T05:47:00Z""
}, {
""actionAt"" : ""MIA"",
""actionCode"" : ""Checkin at MIA"",
""actionTime"" : ""2019-02-01T04:38:00Z""} ]}";

//Get a connection handle for Oracle NoSQL Database Cloud Service
private async static Task<NoSQLClient> getconnection_cloud()
{
// replace the region and compartment place holders with actual values
var client = new NoSQLClient(new NoSQLConfig
{
Region = <your_region_identifier>,
Compartment = "<your_compartment_ocid>"
});
return client;
}
//Get a connection handle for onPremise data store
private async static Task<NoSQLClient> getconnection_onPrem()
{
// replace the placeholder with your fullname of your host
var client = new NoSQLClient(new NoSQLConfig
{
ServiceType = ServiceType.KVStore,
Endpoint = "http://<hostname>:8080"
});
return client;
}
// Create a table
private static async Task createTable(NoSQLClient client, string query_stmt, string reg_table)
{
if (reg_table == "true") {
var tableResult = await client.ExecuteTableDDLAsync(query_stmt,
new TableDDLOptions{
TableLimits = new TableLimits(20, 20, 1)
});
await tableResult.WaitForCompletionAsync();
Console.WriteLine(" Table {0} is created",tableResult.TableName);
}
else {
var tableResult= await client.ExecuteTableDDLAsync(query_stmt);
// Wait for the operation completion
await tableResult.WaitForCompletionAsync();
Console.WriteLine(" Table {0} is created",
tableResult.TableName);
}
}
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)
);
}

//replace the place holder for compartment with the OCID of your compartmetn
public static async Task Main(string[] args)
{
try {
//if using cloud service uncomment the code below,
// else if using onPremises comment it
var client = await getconnection_cloud();
// if using onPremise uncomment the code below,
// else if using cloud service comment it
// var client = await getconnection_onPrem();
Console.WriteLine("Created NoSQLClient instance");

await createTable(client,regtbl_ddl,"true");
await createTable(client,childtbl_ddl,"false");
await createTable(client,desctbl_ddl,"false");
await mul_write(client,regTblName,data1,childTblName,data2);
Console.WriteLine("Added a row to the {0} and {1} tables",regTblName,childTblName);
await client.PutAsync(descTblName, FieldValue.FromJsonString(data3).AsMapValue);
Console.WriteLine("Added a row to the {0} table",descTblName);
}
catch (Exception ex) {
Console.WriteLine("Exception has occurred:\n{0}: {1}",
ex.GetType().FullName, ex.Message);
Console.WriteLine("StackTrace is ");
Console.WriteLine( ex.StackTrace);
if (ex.InnerException != null)
{
Console.WriteLine("\nCaused by:\n{0}: {1}",
ex.InnerException.GetType().FullName,
ex.InnerException.Message);
}
}
}
}
}
2 changes: 1 addition & 1 deletion examples-nosql-dotnet-sdk/sqlexamples/QueryData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ private async static Task<NoSQLClient> getconnection_cloud()
var client = new NoSQLClient(new NoSQLConfig
{
Region = <your_region_identifier>,
Compartment = "<your_compartment_ocid"
Compartment = "<your_compartment_ocid>"
});
return client;
}
Expand Down
Loading

0 comments on commit f5f22c9

Please sign in to comment.