diff --git a/examples-nosql-dotnet-sdk/sqlexamples/AddBagData.cs b/examples-nosql-dotnet-sdk/sqlexamples/AddBagData.cs new file mode 100644 index 0000000..4cafac3 --- /dev/null +++ b/examples-nosql-dotnet-sdk/sqlexamples/AddBagData.cs @@ -0,0 +1,441 @@ +// 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 + // + // where: + // - is target framework moniker, supported values + // are netcoreapp5.1 and net7.0 + // ----------------------------------------------------------------------- + public class AddBagData + { + private const string Usage = + "Usage: dotnet run -f [-- ]"; + private const string TableName = "BaggageInfo"; + private const string bag1= @"{ + ""ticketNo"":""1762376407826"", + ""fullName"":""Dierdre Amador"", + ""gender"":""M"", + ""contactPhone"":""165-742-5715"", + ""confNo"":""ZG8Z5N"", + ""bagInfo"":[ { + ""id"" : ""7903989918469"", + ""tagNum"" : ""17657806240229"", + ""routing"" : ""JFK/MAD"", + ""lastActionCode"" : ""OFFLOAD"", + ""lastActionDesc"" : ""OFFLOAD"", + ""lastSeenStation"" : ""MAD"", + ""flightLegs"" : [ { + ""flightNo"" : ""BM495"", + ""flightDate"" : ""2019-03-07T07:00:00Z"", + ""fltRouteSrc"" : ""JFK"", + ""fltRouteDest"" : ""MAD"", + ""estimatedArrival"" : ""2019-03-07T14:00:00Z"", + ""actions"" : [ { + ""actionAt"" : ""MAD"", + ""actionCode"" : ""Offload to Carousel at MAD"", + ""actionTime"" : ""2019-03-07T13:54:00Z"" + }, { + ""actionAt"" : ""JFK"", + ""actionCode"" : ""ONLOAD to MAD"", + ""actionTime"" : ""2019-03-07T07:00:00Z"" + }, { + ""actionAt"" : ""JFK"", + ""actionCode"" : ""BagTag Scan at JFK"", + ""actionTime"" : ""2019-03-07T06:53:00Z"" + }, { + ""actionAt"" : ""JFK"", + ""actionCode"" : ""Checkin at JFK"", + ""actionTime"" : ""2019-03-07T05:03:00Z"" + } ] + } ], + ""lastSeenTimeGmt"" : ""2019-03-07T13:51:00Z"", + ""bagArrivalDate"" : ""2019-03-07T13:51:00Z"" + } ] + }"; + private const string bag2= @"{ + ""ticketNo"":""1762344493810"", + ""fullName"":""Adam Phillips"", + ""gender"":""M"", + ""contactPhone"":""893-324-1064"", + ""confNo"":""LE6J4Z"", + ""bagInfo"":[ { + ""id"" : ""79039899165297"", + ""tagNum"" : ""17657806255240"", + ""routing"" : ""MIA/LAX/MEL"", + ""lastActionCode"" : ""OFFLOAD"", + ""lastActionDesc"" : ""OFFLOAD"", + ""lastSeenStation"" : ""MEL"", + ""flightLegs"" : [ { + ""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"" + } ] + }, { + ""flightNo"" : ""BM667"", + ""flightDate"" : ""2019-02-01T06:13:00Z"", + ""fltRouteSrc"" : ""LAX"", + ""fltRouteDest"" : ""MEL"", + ""estimatedArrival"" : ""2019-02-01T16:15:00Z"", + ""actions"" : [ { + ""actionAt"" : ""MEL"", + ""actionCode"" : ""Offload to Carousel at MEL"", + ""actionTime"" : ""2019-02-01T16:15:00Z"" + }, { + ""actionAt"" : ""LAX"", + ""actionCode"" : ""ONLOAD to MEL"", + ""actionTime"" : ""2019-02-01T15:35:00Z"" + }, { + ""actionAt"" : ""LAX"", + ""actionCode"" : ""OFFLOAD from LAX"", + ""actionTime"" : ""2019-02-01T15:18:00Z"" + } ] + } ], + ""lastSeenTimeGmt"" : ""2019-02-01T16:13:00Z"", + ""bagArrivalDate"" : ""2019-02-01T16:13:00Z"" + } ] + }"; + private const string bag3= @"{ + ""ticketNo"":""1762341772625"", + ""fullName"":""Gerard Greene"", + ""gender"":""M"", + ""contactPhone"":""395-837-3772"", + ""confNo"":""MC0E7R"", + ""bagInfo"":[ { + ""id"" : ""79039899152842"", + ""tagNum"" : ""1765780626568"", + ""routing"" : ""SFO/IST/ATH/JTR"", + ""lastActionCode"" : ""OFFLOAD"", + ""lastActionDesc"" : ""OFFLOAD"", + ""lastSeenStation"" : ""JTR"", + ""flightLegs"" : [ { + ""flightNo"" : ""BM318"", + ""flightDate"" : ""2019-03-07T04:00:00Z"", + ""fltRouteSrc"" : ""SFO"", + ""fltRouteDest"" : ""IST"", + ""estimatedArrival"" : ""2019-03-07T17:00:00Z"", + ""actions"" : [ { + ""actionAt"" : ""SFO"", + ""actionCode"" : ""ONLOAD to IST"", + ""actionTime"" : ""2019-03-07T04:08:00Z"" + }, { + ""actionAt"" : ""SFO"", + ""actionCode"" : ""BagTag Scan at SFO"", + ""actionTime"" : ""2019-03-07T03:53:00Z"" + }, { + ""actionAt"" : ""SFO"", + ""actionCode"" : ""Checkin at SFO"", + ""actionTime"" : ""2019-03-07T02:20:00Z"" + } ] + }, { + ""flightNo"" : ""BM696"", + ""flightDate"" : ""2019-03-07T05:08:00Z"", + ""fltRouteSrc"" : ""IST"", + ""fltRouteDest"" : ""ATH"", + ""estimatedArrival"" : ""2019-03-08T04:10:00Z"", + ""actions"" : [ { + ""actionAt"" : ""IST"", + ""actionCode"" : ""ONLOAD to ATH"", + ""actionTime"" : ""2019-03-08T04:55:00Z"" + }, { + ""actionAt"" : ""IST"", + ""actionCode"" : ""BagTag Scan at IST"", + ""actionTime"" : ""2019-03-08T04:34:00Z"" + }, { + ""actionAt"" : ""IST"", + ""actionCode"" : ""OFFLOAD from IST"", + ""actionTime"" : ""2019-03-08T04:47:00Z"" + } ] + }, { + ""flightNo"" : ""BM665"", + ""flightDate"" : ""2019-03-07T04:08:00Z"", + ""fltRouteSrc"" : ""ATH"", + ""fltRouteDest"" : ""JTR"", + ""estimatedArrival"" : ""2019-03-07T16:10:00Z"", + ""actions"" : [ { + ""actionAt"" : ""JTR"", + ""actionCode"" : ""Offload to Carousel at JTR"", + ""actionTime"" : ""2019-03-07T16:09:00Z"" + }, { + ""actionAt"" : ""ATH"", + ""actionCode"" : ""ONLOAD to JTR"", + ""actionTime"" : ""2019-03-07T15:51:00Z"" + }, { + ""actionAt"" : ""ATH"", + ""actionCode"" : ""OFFLOAD from ATH"", + ""actionTime"" : ""2019-03-07T15:43:00Z"" + } ] + } ], + ""lastSeenTimeGmt"" : ""2019-03-07T16:01:00Z"", + ""bagArrivalDate"" : ""2019-03-07T16:01:00Z"" + } ] + }"; + private const string bag4= @"{ + ""ticketNo"":""1762320369957"", + ""fullName"":""Lorenzo Phil"", + ""gender"":""M"", + ""contactPhone"":""364-610-4444"", + ""confNo"":""QI3V6Q"", + ""bagInfo"":[ { + ""id"" : ""79039899187755"", + ""tagNum"" : ""17657806240001"", + ""routing"" : ""SFO/IST/ATH/JTR"", + ""lastActionCode"" : ""OFFLOAD"", + ""lastActionDesc"" : ""OFFLOAD"", + ""lastSeenStation"" : ""JTR"", + ""flightLegs"" : [ { + ""flightNo"" : ""BM318"", + ""flightDate"" : ""2019-03-12T03:00:00Z"", + ""fltRouteSrc"" : ""SFO"", + ""fltRouteDest"" : ""IST"", + ""estimatedArrival"" : ""2019-03-12T16:00:00Z"", + ""actions"" : [ { + ""actionAt"" : ""SFO"", + ""actionCode"" : ""ONLOAD to IST"", + ""actionTime"" : ""2019-03-12T03:11:00Z"" + }, { + ""actionAt"" : ""SFO"", + ""actionCode"" : ""BagTag Scan at SFO"", + ""actionTime"" : ""2019-03-12T02:49:00Z"" + }, { + ""actionAt"" : ""SFO"", + ""actionCode"" : ""Checkin at SFO"", + ""actionTime"" : ""2019-03-12T01:50:00Z"" + } ] + }, { + ""flightNo"" : ""BM696"", + ""flightDate"" : ""2019-03-12T04:11:00Z"", + ""fltRouteSrc"" : ""IST"", + ""fltRouteDest"" : ""ATH"", + ""estimatedArrival"" : ""2019-03-13T03:14:00Z"", + ""actions"" : [ { + ""actionAt"" : ""IST"", + ""actionCode"" : ""ONLOAD to ATH"", + ""actionTime"" : ""2019-03-13T04:10:00Z"" + }, { + ""actionAt"" : ""IST"", + ""actionCode"" : ""BagTag Scan at IST"", + ""actionTime"" : ""2019-03-13T03:56:00Z"" + }, { + ""actionAt"" : ""IST"", + ""actionCode"" : ""OFFLOAD from IST"", + ""actionTime"" : ""2019-03-13T03:59:00Z"" + } ] + }, { + ""flightNo"" : ""BM665"", + ""flightDate"" : ""2019-03-12T03:11:00Z"", + ""fltRouteSrc"" : ""ATH"", + ""fltRouteDest"" : ""JTR"", + ""estimatedArrival"" : ""2019-03-12T15:12:00Z"", + ""actions"" : [ { + ""actionAt"" : ""JTR"", + ""actionCode"" : ""Offload to Carousel at JTR"", + ""actionTime"" : ""2019-03-12T15:06:00Z"" + }, { + ""actionAt"" : ""ATH"", + ""actionCode"" : ""ONLOAD to JTR"", + ""actionTime"" : ""2019-03-12T14:16:00Z"" + }, { + ""actionAt"" : ""ATH"", + ""actionCode"" : ""OFFLOAD from ATH"", + ""actionTime"" : ""2019-03-12T14:13:00Z"" + } ] + } ], + ""lastSeenTimeGmt"" : ""2019-03-12T15:05:00Z"", + ""bagArrivalDate"" : ""2019-03-12T15:05:00Z"" + }, + { + ""id"" : ""79039899197755"", + ""tagNum"" : ""17657806340001"", + ""routing"" : ""SFO/IST/ATH/JTR"", + ""lastActionCode"" : ""OFFLOAD"", + ""lastActionDesc"" : ""OFFLOAD"", + ""lastSeenStation"" : ""JTR"", + ""flightLegs"" : [ { + ""flightNo"" : ""BM318"", + ""flightDate"" : ""2019-03-12T03:00:00Z"", + ""fltRouteSrc"" : ""SFO"", + ""fltRouteDest"" : ""IST"", + ""estimatedArrival"" : ""2019-03-12T16:40:00Z"", + ""actions"" : [ { + ""actionAt"" : ""SFO"", + ""actionCode"" : ""ONLOAD to IST"", + ""actionTime"" : ""2019-03-12T03:14:00Z"" + }, { + ""actionAt"" : ""SFO"", + ""actionCode"" : ""BagTag Scan at SFO"", + ""actionTime"" : ""2019-03-12T02:50:00Z"" + }, { + ""actionAt"" : ""SFO"", + ""actionCode"" : ""Checkin at SFO"", + ""actionTime"" : ""2019-03-12T01:58:00Z"" + } ] + }, { + ""flightNo"" : ""BM696"", + ""flightDate"" : ""2019-03-12T04:11:00Z"", + ""fltRouteSrc"" : ""IST"", + ""fltRouteDest"" : ""ATH"", + ""estimatedArrival"" : ""2019-03-13T03:18:00Z"", + ""actions"" : [ { + ""actionAt"" : ""IST"", + ""actionCode"" : ""ONLOAD to ATH"", + ""actionTime"" : ""2019-03-13T04:17:00Z"" + }, { + ""actionAt"" : ""IST"", + ""actionCode"" : ""BagTag Scan at IST"", + ""actionTime"" : ""2019-03-13T03:59:00Z"" + }, { + ""actionAt"" : ""IST"", + ""actionCode"" : ""OFFLOAD from IST"", + ""actionTime"" : ""2019-03-13T03:48:00Z"" + } ] + }, { + ""flightNo"" : ""BM665"", + ""flightDate"" : ""2019-03-12T03:11:00Z"", + ""fltRouteSrc"" : ""ATH"", + ""fltRouteDest"" : ""JTR"", + ""estimatedArrival"" : ""2019-03-12T15:12:00Z"", + ""actions"" : [ { + ""actionAt"" : ""JTR"", + ""actionCode"" : ""Offload to Carousel at JTR"", + ""actionTime"" : ""2019-03-12T15:06:00Z"" + }, { + ""actionAt"" : ""ATH"", + ""actionCode"" : ""ONLOAD to JTR"", + ""actionTime"" : ""2019-03-12T14:16:00Z"" + }, { + ""actionAt"" : ""ATH"", + ""actionCode"" : ""OFFLOAD from ATH"", + ""actionTime"" : ""2019-03-12T14:23:00Z"" + } ] + } ], + ""lastSeenTimeGmt"" : ""2019-03-12T16:05:00Z"", + ""bagArrivalDate"" : ""2019-03-12T16:25:00Z"" + } ] + }"; + //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); + Console.WriteLine("\nSuccess!"); + } + 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); + } + } + } + // Get a connection handle for Oracle NoSQL Database Cloud Service + private async static Task getconnection_cloud() + { + // replace the region and compartment place holders with actual values + var client = new NoSQLClient(new NoSQLConfig + { + Region = , + Compartment = "" + }); + return client; + } + // Get a connection handle for onPremise data store + private async static Task getconnection_onPrem() + { + // replace the placeholder with your fullname of your host + var client = new NoSQLClient(new NoSQLConfig + { + ServiceType = ServiceType.KVStore, + Endpoint = "http://:8080" + }); + return client; + } + + private static async Task crtTabAddData(NoSQLClient client) + { + // Create a table + var sql = + $@"CREATE TABLE IF NOT EXISTS {TableName}(ticketNo LONG, + fullName STRING, + gender STRING, + contactPhone STRING, + confNo STRING, + bagInfo JSON, + primary key(ticketNo))"; + + 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(bag1).AsMapValue); + if (putResult.ConsumedCapacity != null) + { + Console.WriteLine(" Write used:"); + Console.WriteLine(" " + putResult.ConsumedCapacity); + } + var putResult1 = await client.PutAsync(TableName, FieldValue.FromJsonString(bag2).AsMapValue); + if (putResult1.ConsumedCapacity != null) + { + Console.WriteLine(" Write used:"); + Console.WriteLine(" " + putResult1.ConsumedCapacity); + } + var putResult2 = await client.PutAsync(TableName, FieldValue.FromJsonString(bag3).AsMapValue); + if (putResult2.ConsumedCapacity != null) + { + Console.WriteLine(" Write used:"); + Console.WriteLine(" " + putResult2.ConsumedCapacity); + } + var putResult3 = await client.PutAsync(TableName, FieldValue.FromJsonString(bag4).AsMapValue); + if (putResult3.ConsumedCapacity != null) + { + Console.WriteLine(" Write used:"); + Console.WriteLine(" " + putResult3.ConsumedCapacity); + } + } + } +} diff --git a/examples-nosql-dotnet-sdk/sqlexamples/AddData.cs b/examples-nosql-dotnet-sdk/sqlexamples/AddData.cs index f28e237..d7d62ac 100644 --- a/examples-nosql-dotnet-sdk/sqlexamples/AddData.cs +++ b/examples-nosql-dotnet-sdk/sqlexamples/AddData.cs @@ -245,9 +245,11 @@ public class AddData public static async Task Main(string[] args) { try { - // if using cloud service uncomment the code below, else if using onPremises comment it + // 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 + // 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 crtTabAddData(client); @@ -269,7 +271,7 @@ public static async Task Main(string[] args) // Get a connection handle for Oracle NoSQL Database Cloud Service private async static Task getconnection_cloud() { - // replace the region and compartment place holders with the actual values + // replace the region and compartment place holders with actual values var client = new NoSQLClient(new NoSQLConfig { Region = , diff --git a/examples-nosql-dotnet-sdk/sqlexamples/AlterTable.cs b/examples-nosql-dotnet-sdk/sqlexamples/AlterTable.cs index eb76ad3..3f35fae 100644 --- a/examples-nosql-dotnet-sdk/sqlexamples/AlterTable.cs +++ b/examples-nosql-dotnet-sdk/sqlexamples/AlterTable.cs @@ -23,9 +23,11 @@ public class AlterTable public static async Task Main(string[] args) { try { - // if using cloud service uncomment the code below, else if using onPremises comment it + // 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 + // 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); @@ -49,7 +51,7 @@ public static async Task Main(string[] args) // Get a connection handle for Oracle NoSQL Database Cloud Service private async static Task getconnection_cloud() { - // replace the region and compartment place holders with the actual values + // replace the region and compartment place holders with actual values var client = new NoSQLClient(new NoSQLConfig { Region = , diff --git a/examples-nosql-dotnet-sdk/sqlexamples/CreateTable.cs b/examples-nosql-dotnet-sdk/sqlexamples/CreateTable.cs index 21ac33b..ed460aa 100644 --- a/examples-nosql-dotnet-sdk/sqlexamples/CreateTable.cs +++ b/examples-nosql-dotnet-sdk/sqlexamples/CreateTable.cs @@ -22,9 +22,11 @@ public class CreateTable { public static async Task Main(string[] args) { try { - // if using cloud service uncomment the code below, else if using onPremises comment it + // 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 + // 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); @@ -46,11 +48,11 @@ public static async Task Main(string[] args) // Get a connection handle for Oracle NoSQL Database Cloud Service private async static Task getconnection_cloud() { - // replace the region and compartment place holders with the actual values + // replace the region and compartment place holders with actual values var client = new NoSQLClient(new NoSQLConfig { Region = , - Compartment = " + // + // where: + // - is target framework moniker, supported values + // are netcoreapp5.1 and net7.0 + // ----------------------------------------------------------------------- + public class SQLOperators + { + private const string Usage = + "Usage: dotnet run -f [-- ]"; + private const string TableName = "BaggageInfo"; + private const string bag1= @"{ + ""ticketNo"":""1762376407826"", + ""fullName"":""Dierdre Amador"", + ""gender"":""M"", + ""contactPhone"":""165-742-5715"", + ""confNo"":""ZG8Z5N"", + ""bagInfo"":[ { + ""id"" : ""7903989918469"", + ""tagNum"" : ""17657806240229"", + ""routing"" : ""JFK/MAD"", + ""lastActionCode"" : ""OFFLOAD"", + ""lastActionDesc"" : ""OFFLOAD"", + ""lastSeenStation"" : ""MAD"", + ""flightLegs"" : [ { + ""flightNo"" : ""BM495"", + ""flightDate"" : ""2019-03-07T07:00:00Z"", + ""fltRouteSrc"" : ""JFK"", + ""fltRouteDest"" : ""MAD"", + ""estimatedArrival"" : ""2019-03-07T14:00:00Z"", + ""actions"" : [ { + ""actionAt"" : ""MAD"", + ""actionCode"" : ""Offload to Carousel at MAD"", + ""actionTime"" : ""2019-03-07T13:54:00Z"" + }, { + ""actionAt"" : ""JFK"", + ""actionCode"" : ""ONLOAD to MAD"", + ""actionTime"" : ""2019-03-07T07:00:00Z"" + }, { + ""actionAt"" : ""JFK"", + ""actionCode"" : ""BagTag Scan at JFK"", + ""actionTime"" : ""2019-03-07T06:53:00Z"" + }, { + ""actionAt"" : ""JFK"", + ""actionCode"" : ""Checkin at JFK"", + ""actionTime"" : ""2019-03-07T05:03:00Z"" + } ] + } ], + ""lastSeenTimeGmt"" : ""2019-03-07T13:51:00Z"", + ""bagArrivalDate"" : ""2019-03-07T13:51:00Z"" + } ] + }"; + private const string bag2= @"{ + ""ticketNo"":""1762344493810"", + ""fullName"":""Adam Phillips"", + ""gender"":""M"", + ""contactPhone"":""893-324-1064"", + ""confNo"":""LE6J4Z"", + ""bagInfo"":[ { + ""id"" : ""79039899165297"", + ""tagNum"" : ""17657806255240"", + ""routing"" : ""MIA/LAX/MEL"", + ""lastActionCode"" : ""OFFLOAD"", + ""lastActionDesc"" : ""OFFLOAD"", + ""lastSeenStation"" : ""MEL"", + ""flightLegs"" : [ { + ""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"" + } ] + }, { + ""flightNo"" : ""BM667"", + ""flightDate"" : ""2019-02-01T06:13:00Z"", + ""fltRouteSrc"" : ""LAX"", + ""fltRouteDest"" : ""MEL"", + ""estimatedArrival"" : ""2019-02-01T16:15:00Z"", + ""actions"" : [ { + ""actionAt"" : ""MEL"", + ""actionCode"" : ""Offload to Carousel at MEL"", + ""actionTime"" : ""2019-02-01T16:15:00Z"" + }, { + ""actionAt"" : ""LAX"", + ""actionCode"" : ""ONLOAD to MEL"", + ""actionTime"" : ""2019-02-01T15:35:00Z"" + }, { + ""actionAt"" : ""LAX"", + ""actionCode"" : ""OFFLOAD from LAX"", + ""actionTime"" : ""2019-02-01T15:18:00Z"" + } ] + } ], + ""lastSeenTimeGmt"" : ""2019-02-01T16:13:00Z"", + ""bagArrivalDate"" : ""2019-02-01T16:13:00Z"" + } ] + }"; + private const string bag3= @"{ + ""ticketNo"":""1762341772625"", + ""fullName"":""Gerard Greene"", + ""gender"":""M"", + ""contactPhone"":""395-837-3772"", + ""confNo"":""MC0E7R"", + ""bagInfo"":[ { + ""id"" : ""79039899152842"", + ""tagNum"" : ""1765780626568"", + ""routing"" : ""SFO/IST/ATH/JTR"", + ""lastActionCode"" : ""OFFLOAD"", + ""lastActionDesc"" : ""OFFLOAD"", + ""lastSeenStation"" : ""JTR"", + ""flightLegs"" : [ { + ""flightNo"" : ""BM318"", + ""flightDate"" : ""2019-03-07T04:00:00Z"", + ""fltRouteSrc"" : ""SFO"", + ""fltRouteDest"" : ""IST"", + ""estimatedArrival"" : ""2019-03-07T17:00:00Z"", + ""actions"" : [ { + ""actionAt"" : ""SFO"", + ""actionCode"" : ""ONLOAD to IST"", + ""actionTime"" : ""2019-03-07T04:08:00Z"" + }, { + ""actionAt"" : ""SFO"", + ""actionCode"" : ""BagTag Scan at SFO"", + ""actionTime"" : ""2019-03-07T03:53:00Z"" + }, { + ""actionAt"" : ""SFO"", + ""actionCode"" : ""Checkin at SFO"", + ""actionTime"" : ""2019-03-07T02:20:00Z"" + } ] + }, { + ""flightNo"" : ""BM696"", + ""flightDate"" : ""2019-03-07T05:08:00Z"", + ""fltRouteSrc"" : ""IST"", + ""fltRouteDest"" : ""ATH"", + ""estimatedArrival"" : ""2019-03-08T04:10:00Z"", + ""actions"" : [ { + ""actionAt"" : ""IST"", + ""actionCode"" : ""ONLOAD to ATH"", + ""actionTime"" : ""2019-03-08T04:55:00Z"" + }, { + ""actionAt"" : ""IST"", + ""actionCode"" : ""BagTag Scan at IST"", + ""actionTime"" : ""2019-03-08T04:34:00Z"" + }, { + ""actionAt"" : ""IST"", + ""actionCode"" : ""OFFLOAD from IST"", + ""actionTime"" : ""2019-03-08T04:47:00Z"" + } ] + }, { + ""flightNo"" : ""BM665"", + ""flightDate"" : ""2019-03-07T04:08:00Z"", + ""fltRouteSrc"" : ""ATH"", + ""fltRouteDest"" : ""JTR"", + ""estimatedArrival"" : ""2019-03-07T16:10:00Z"", + ""actions"" : [ { + ""actionAt"" : ""JTR"", + ""actionCode"" : ""Offload to Carousel at JTR"", + ""actionTime"" : ""2019-03-07T16:09:00Z"" + }, { + ""actionAt"" : ""ATH"", + ""actionCode"" : ""ONLOAD to JTR"", + ""actionTime"" : ""2019-03-07T15:51:00Z"" + }, { + ""actionAt"" : ""ATH"", + ""actionCode"" : ""OFFLOAD from ATH"", + ""actionTime"" : ""2019-03-07T15:43:00Z"" + } ] + } ], + ""lastSeenTimeGmt"" : ""2019-03-07T16:01:00Z"", + ""bagArrivalDate"" : ""2019-03-07T16:01:00Z"" + } ] + }"; + private const string bag4= @"{ + ""ticketNo"":""1762320369957"", + ""fullName"":""Lorenzo Phil"", + ""gender"":""M"", + ""contactPhone"":""364-610-4444"", + ""confNo"":""QI3V6Q"", + ""bagInfo"":[ { + ""id"" : ""79039899187755"", + ""tagNum"" : ""17657806240001"", + ""routing"" : ""SFO/IST/ATH/JTR"", + ""lastActionCode"" : ""OFFLOAD"", + ""lastActionDesc"" : ""OFFLOAD"", + ""lastSeenStation"" : ""JTR"", + ""flightLegs"" : [ { + ""flightNo"" : ""BM318"", + ""flightDate"" : ""2019-03-12T03:00:00Z"", + ""fltRouteSrc"" : ""SFO"", + ""fltRouteDest"" : ""IST"", + ""estimatedArrival"" : ""2019-03-12T16:00:00Z"", + ""actions"" : [ { + ""actionAt"" : ""SFO"", + ""actionCode"" : ""ONLOAD to IST"", + ""actionTime"" : ""2019-03-12T03:11:00Z"" + }, { + ""actionAt"" : ""SFO"", + ""actionCode"" : ""BagTag Scan at SFO"", + ""actionTime"" : ""2019-03-12T02:49:00Z"" + }, { + ""actionAt"" : ""SFO"", + ""actionCode"" : ""Checkin at SFO"", + ""actionTime"" : ""2019-03-12T01:50:00Z"" + } ] + }, { + ""flightNo"" : ""BM696"", + ""flightDate"" : ""2019-03-12T04:11:00Z"", + ""fltRouteSrc"" : ""IST"", + ""fltRouteDest"" : ""ATH"", + ""estimatedArrival"" : ""2019-03-13T03:14:00Z"", + ""actions"" : [ { + ""actionAt"" : ""IST"", + ""actionCode"" : ""ONLOAD to ATH"", + ""actionTime"" : ""2019-03-13T04:10:00Z"" + }, { + ""actionAt"" : ""IST"", + ""actionCode"" : ""BagTag Scan at IST"", + ""actionTime"" : ""2019-03-13T03:56:00Z"" + }, { + ""actionAt"" : ""IST"", + ""actionCode"" : ""OFFLOAD from IST"", + ""actionTime"" : ""2019-03-13T03:59:00Z"" + } ] + }, { + ""flightNo"" : ""BM665"", + ""flightDate"" : ""2019-03-12T03:11:00Z"", + ""fltRouteSrc"" : ""ATH"", + ""fltRouteDest"" : ""JTR"", + ""estimatedArrival"" : ""2019-03-12T15:12:00Z"", + ""actions"" : [ { + ""actionAt"" : ""JTR"", + ""actionCode"" : ""Offload to Carousel at JTR"", + ""actionTime"" : ""2019-03-12T15:06:00Z"" + }, { + ""actionAt"" : ""ATH"", + ""actionCode"" : ""ONLOAD to JTR"", + ""actionTime"" : ""2019-03-12T14:16:00Z"" + }, { + ""actionAt"" : ""ATH"", + ""actionCode"" : ""OFFLOAD from ATH"", + ""actionTime"" : ""2019-03-12T14:13:00Z"" + } ] + } ], + ""lastSeenTimeGmt"" : ""2019-03-12T15:05:00Z"", + ""bagArrivalDate"" : ""2019-03-12T15:05:00Z"" + }, + { + ""id"" : ""79039899197755"", + ""tagNum"" : ""17657806340001"", + ""routing"" : ""SFO/IST/ATH/JTR"", + ""lastActionCode"" : ""OFFLOAD"", + ""lastActionDesc"" : ""OFFLOAD"", + ""lastSeenStation"" : ""JTR"", + ""flightLegs"" : [ { + ""flightNo"" : ""BM318"", + ""flightDate"" : ""2019-03-12T03:00:00Z"", + ""fltRouteSrc"" : ""SFO"", + ""fltRouteDest"" : ""IST"", + ""estimatedArrival"" : ""2019-03-12T16:40:00Z"", + ""actions"" : [ { + ""actionAt"" : ""SFO"", + ""actionCode"" : ""ONLOAD to IST"", + ""actionTime"" : ""2019-03-12T03:14:00Z"" + }, { + ""actionAt"" : ""SFO"", + ""actionCode"" : ""BagTag Scan at SFO"", + ""actionTime"" : ""2019-03-12T02:50:00Z"" + }, { + ""actionAt"" : ""SFO"", + ""actionCode"" : ""Checkin at SFO"", + ""actionTime"" : ""2019-03-12T01:58:00Z"" + } ] + }, { + ""flightNo"" : ""BM696"", + ""flightDate"" : ""2019-03-12T04:11:00Z"", + ""fltRouteSrc"" : ""IST"", + ""fltRouteDest"" : ""ATH"", + ""estimatedArrival"" : ""2019-03-13T03:18:00Z"", + ""actions"" : [ { + ""actionAt"" : ""IST"", + ""actionCode"" : ""ONLOAD to ATH"", + ""actionTime"" : ""2019-03-13T04:17:00Z"" + }, { + ""actionAt"" : ""IST"", + ""actionCode"" : ""BagTag Scan at IST"", + ""actionTime"" : ""2019-03-13T03:59:00Z"" + }, { + ""actionAt"" : ""IST"", + ""actionCode"" : ""OFFLOAD from IST"", + ""actionTime"" : ""2019-03-13T03:48:00Z"" + } ] + }, { + ""flightNo"" : ""BM665"", + ""flightDate"" : ""2019-03-12T03:11:00Z"", + ""fltRouteSrc"" : ""ATH"", + ""fltRouteDest"" : ""JTR"", + ""estimatedArrival"" : ""2019-03-12T15:12:00Z"", + ""actions"" : [ { + ""actionAt"" : ""JTR"", + ""actionCode"" : ""Offload to Carousel at JTR"", + ""actionTime"" : ""2019-03-12T15:06:00Z"" + }, { + ""actionAt"" : ""ATH"", + ""actionCode"" : ""ONLOAD to JTR"", + ""actionTime"" : ""2019-03-12T14:16:00Z"" + }, { + ""actionAt"" : ""ATH"", + ""actionCode"" : ""OFFLOAD from ATH"", + ""actionTime"" : ""2019-03-12T14:23:00Z"" + } ] + } ], + ""lastSeenTimeGmt"" : ""2019-03-12T16:05:00Z"", + ""bagArrivalDate"" : ""2019-03-12T16:25:00Z"" + } ] + }"; + private const string orderby_stmt =@"SELECT bag.fullName, bag.bagInfo[].tagNum,bag.bagInfo[].lastSeenTimeGmt FROM BaggageInfo bag + WHERE bag.bagInfo[].lastSeenStation=any ""MEL"" ORDER BY bag.bagInfo[].lastSeenTimeGmt DESC"; + private const string sortlimit_stmt =@"SELECT $bag.fullName, $bag.bagInfo.tagNum, $flt_time FROM BaggageInfo $bag, + $bag.bagInfo[].lastSeenTimeGmt $flt_time + WHERE $bag.bagInfo[].lastSeenStation=any ""JTR"" + ORDER BY $flt_time LIMIT 4"; + private const string groupsortlimit_stmt =@"SELECT $flt_src as SOURCE, count(*) as COUNT FROM BaggageInfo $bag, + $bag.bagInfo.flightLegs[0].fltRouteSrc $flt_src GROUP BY $flt_src" ; + + //replace the place holder for compartment with OCID of your compartment + 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 crtTabAddData(client); + Console.WriteLine("\nSuccess!"); + + Console.WriteLine("\nUsing ORDER BY to sort data!"); + await fetchData(client,orderby_stmt); + + Console.WriteLine("\nUsing ORDER BY and LIMIT to sort and limit data!"); + await fetchData(client,sortlimit_stmt); + + Console.WriteLine("\nUsing GROUP BY, ORDER BY and LIMIT to group, sort and limit data:"); + await fetchData(client,groupsortlimit_stmt); + + } + 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); + } + } + } + // Get a connection handle for Oracle NoSQL Database Cloud Service + private async static Task getconnection_cloud() + { + // replace the region and compartment place holders with actual values + var client = new NoSQLClient(new NoSQLConfig + { + Region = , + Compartment = "" + }); + return client; + } + // Get a connection handle for onPremise data store + private async static Task getconnection_onPrem() + { + // replace the placeholder with your fullname of your host + var client = new NoSQLClient(new NoSQLConfig + { + ServiceType = ServiceType.KVStore, + Endpoint = "http://:8080" + }); + return client; + } + + + private static async Task crtTabAddData(NoSQLClient client) + { + // Create a table + var sql = + $@"CREATE TABLE IF NOT EXISTS {TableName}(ticketNo LONG, + fullName STRING, + gender STRING, + contactPhone STRING, + confNo STRING, + bagInfo JSON, + primary key(ticketNo))"; + + 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(bag1).AsMapValue); + if (putResult.ConsumedCapacity != null) + { + Console.WriteLine(" Write used:"); + Console.WriteLine(" " + putResult.ConsumedCapacity); + } + var putResult1 = await client.PutAsync(TableName, FieldValue.FromJsonString(bag2).AsMapValue); + if (putResult1.ConsumedCapacity != null) + { + Console.WriteLine(" Write used:"); + Console.WriteLine(" " + putResult1.ConsumedCapacity); + } + var putResult2 = await client.PutAsync(TableName, FieldValue.FromJsonString(bag3).AsMapValue); + if (putResult2.ConsumedCapacity != null) + { + Console.WriteLine(" Write used:"); + Console.WriteLine(" " + putResult2.ConsumedCapacity); + } + var putResult3 = await client.PutAsync(TableName, FieldValue.FromJsonString(bag4).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 DoQuery(IAsyncEnumerable> queryEnumerable){ + Console.WriteLine(" Query results:"); + await foreach (var result in queryEnumerable) { + foreach (var row in result.Rows) + { + Console.WriteLine(); + Console.WriteLine(row.ToJsonString()); + } + } + } + } +} diff --git a/examples-nosql-dotnet-sdk/sqlexamples/Indexes.cs b/examples-nosql-dotnet-sdk/sqlexamples/Indexes.cs index 0cf79e0..e1c870a 100644 --- a/examples-nosql-dotnet-sdk/sqlexamples/Indexes.cs +++ b/examples-nosql-dotnet-sdk/sqlexamples/Indexes.cs @@ -23,9 +23,11 @@ public class Indexes public static async Task Main(string[] args) { try { - // if using cloud service uncomment the code below, else if using onPremises comment it + // 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 + // 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); @@ -48,7 +50,7 @@ public static async Task Main(string[] args) // Get a connection handle for Oracle NoSQL Database Cloud Service private async static Task getconnection_cloud() { - // replace the region and compartment place holders with the actual values + // replace the region and compartment place holders with actual values var client = new NoSQLClient(new NoSQLConfig { Region = , diff --git a/examples-nosql-dotnet-sdk/sqlexamples/ModifyData.cs b/examples-nosql-dotnet-sdk/sqlexamples/ModifyData.cs index 062eba1..786f7f2 100644 --- a/examples-nosql-dotnet-sdk/sqlexamples/ModifyData.cs +++ b/examples-nosql-dotnet-sdk/sqlexamples/ModifyData.cs @@ -357,11 +357,11 @@ public class ModifyData //Get a connection handle for Oracle NoSQL Database Cloud Service private async static Task getconnection_cloud() { - // replace the region and compartment place holders with the actual values + // replace the region and compartment place holders with actual values var client = new NoSQLClient(new NoSQLConfig { Region = , - Compartment = " + // + // where: + // - is target framework moniker, supported values + // are netcoreapp5.1 and net7.0 + // ----------------------------------------------------------------------- + public class MultiDataOps + { + private const string Usage = + "Usage: dotnet run -f [-- ]"; + 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 getconnection_cloud() + { + // replace the region and compartment place holders with actual values + var client = new NoSQLClient(new NoSQLConfig + { + Region = , + Compartment = "" + }); + return client; + } + // Get a connection handle for onPremise data store + private async static Task getconnection_onPrem() + { + // replace the placeholder with your fullname of your host + var client = new NoSQLClient(new NoSQLConfig + { + ServiceType = ServiceType.KVStore, + Endpoint = "http://: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> queryEnumerable){ + Console.WriteLine(" Query results:"); + await foreach (var result in queryEnumerable) { + foreach (var row in result.Rows) + { + Console.WriteLine(); + Console.WriteLine(row.ToJsonString()); + } + } + } + } +} diff --git a/examples-nosql-dotnet-sdk/sqlexamples/MultiWrite.cs b/examples-nosql-dotnet-sdk/sqlexamples/MultiWrite.cs new file mode 100644 index 0000000..36b752e --- /dev/null +++ b/examples-nosql-dotnet-sdk/sqlexamples/MultiWrite.cs @@ -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 + // + // where: + // - is target framework moniker, supported values + // are netcoreapp5.1 and net7.0 + // ----------------------------------------------------------------------- + public class MultiWrite + { + private const string Usage = + "Usage: dotnet run -f [-- ]"; + 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 getconnection_cloud() + { + // replace the region and compartment place holders with actual values + var client = new NoSQLClient(new NoSQLConfig + { + Region = , + Compartment = "" + }); + return client; + } + //Get a connection handle for onPremise data store + private async static Task getconnection_onPrem() + { + // replace the placeholder with your fullname of your host + var client = new NoSQLClient(new NoSQLConfig + { + ServiceType = ServiceType.KVStore, + Endpoint = "http://: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); + } + } + } + } +} diff --git a/examples-nosql-dotnet-sdk/sqlexamples/QueryData.cs b/examples-nosql-dotnet-sdk/sqlexamples/QueryData.cs index 5b5f90d..5a1957c 100644 --- a/examples-nosql-dotnet-sdk/sqlexamples/QueryData.cs +++ b/examples-nosql-dotnet-sdk/sqlexamples/QueryData.cs @@ -246,11 +246,11 @@ public class QueryData // Get a connection handle for Oracle NoSQL Database Cloud Service private async static Task getconnection_cloud() { - // replace the region and compartment place holders with the actual values + // replace the region and compartment place holders with actual values var client = new NoSQLClient(new NoSQLConfig { Region = , - Compartment = " + // + // where: + // - is target framework moniker, supported values + // are netcoreapp5.1 and net7.0 + // ----------------------------------------------------------------------- + public class SQLOperators + { + private const string Usage = + "Usage: dotnet run -f [-- ]"; + private const string TableName = "BaggageInfo"; + private const string bag1= @"{ + ""ticketNo"":""1762376407826"", + ""fullName"":""Dierdre Amador"", + ""gender"":""M"", + ""contactPhone"":""165-742-5715"", + ""confNo"":""ZG8Z5N"", + ""bagInfo"":[ { + ""id"" : ""7903989918469"", + ""tagNum"" : ""17657806240229"", + ""routing"" : ""JFK/MAD"", + ""lastActionCode"" : ""OFFLOAD"", + ""lastActionDesc"" : ""OFFLOAD"", + ""lastSeenStation"" : ""MAD"", + ""flightLegs"" : [ { + ""flightNo"" : ""BM495"", + ""flightDate"" : ""2019-03-07T07:00:00Z"", + ""fltRouteSrc"" : ""JFK"", + ""fltRouteDest"" : ""MAD"", + ""estimatedArrival"" : ""2019-03-07T14:00:00Z"", + ""actions"" : [ { + ""actionAt"" : ""MAD"", + ""actionCode"" : ""Offload to Carousel at MAD"", + ""actionTime"" : ""2019-03-07T13:54:00Z"" + }, { + ""actionAt"" : ""JFK"", + ""actionCode"" : ""ONLOAD to MAD"", + ""actionTime"" : ""2019-03-07T07:00:00Z"" + }, { + ""actionAt"" : ""JFK"", + ""actionCode"" : ""BagTag Scan at JFK"", + ""actionTime"" : ""2019-03-07T06:53:00Z"" + }, { + ""actionAt"" : ""JFK"", + ""actionCode"" : ""Checkin at JFK"", + ""actionTime"" : ""2019-03-07T05:03:00Z"" + } ] + } ], + ""lastSeenTimeGmt"" : ""2019-03-07T13:51:00Z"", + ""bagArrivalDate"" : ""2019-03-07T13:51:00Z"" + } ] + }"; + private const string bag2= @"{ + ""ticketNo"":""1762344493810"", + ""fullName"":""Adam Phillips"", + ""gender"":""M"", + ""contactPhone"":""893-324-1064"", + ""confNo"":""LE6J4Z"", + ""bagInfo"":[ { + ""id"" : ""79039899165297"", + ""tagNum"" : ""17657806255240"", + ""routing"" : ""MIA/LAX/MEL"", + ""lastActionCode"" : ""OFFLOAD"", + ""lastActionDesc"" : ""OFFLOAD"", + ""lastSeenStation"" : ""MEL"", + ""flightLegs"" : [ { + ""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"" + } ] + }, { + ""flightNo"" : ""BM667"", + ""flightDate"" : ""2019-02-01T06:13:00Z"", + ""fltRouteSrc"" : ""LAX"", + ""fltRouteDest"" : ""MEL"", + ""estimatedArrival"" : ""2019-02-01T16:15:00Z"", + ""actions"" : [ { + ""actionAt"" : ""MEL"", + ""actionCode"" : ""Offload to Carousel at MEL"", + ""actionTime"" : ""2019-02-01T16:15:00Z"" + }, { + ""actionAt"" : ""LAX"", + ""actionCode"" : ""ONLOAD to MEL"", + ""actionTime"" : ""2019-02-01T15:35:00Z"" + }, { + ""actionAt"" : ""LAX"", + ""actionCode"" : ""OFFLOAD from LAX"", + ""actionTime"" : ""2019-02-01T15:18:00Z"" + } ] + } ], + ""lastSeenTimeGmt"" : ""2019-02-01T16:13:00Z"", + ""bagArrivalDate"" : ""2019-02-01T16:13:00Z"" + } ] + }"; + private const string bag3= @"{ + ""ticketNo"":""1762341772625"", + ""fullName"":""Gerard Greene"", + ""gender"":""M"", + ""contactPhone"":""395-837-3772"", + ""confNo"":""MC0E7R"", + ""bagInfo"":[ { + ""id"" : ""79039899152842"", + ""tagNum"" : ""1765780626568"", + ""routing"" : ""SFO/IST/ATH/JTR"", + ""lastActionCode"" : ""OFFLOAD"", + ""lastActionDesc"" : ""OFFLOAD"", + ""lastSeenStation"" : ""JTR"", + ""flightLegs"" : [ { + ""flightNo"" : ""BM318"", + ""flightDate"" : ""2019-03-07T04:00:00Z"", + ""fltRouteSrc"" : ""SFO"", + ""fltRouteDest"" : ""IST"", + ""estimatedArrival"" : ""2019-03-07T17:00:00Z"", + ""actions"" : [ { + ""actionAt"" : ""SFO"", + ""actionCode"" : ""ONLOAD to IST"", + ""actionTime"" : ""2019-03-07T04:08:00Z"" + }, { + ""actionAt"" : ""SFO"", + ""actionCode"" : ""BagTag Scan at SFO"", + ""actionTime"" : ""2019-03-07T03:53:00Z"" + }, { + ""actionAt"" : ""SFO"", + ""actionCode"" : ""Checkin at SFO"", + ""actionTime"" : ""2019-03-07T02:20:00Z"" + } ] + }, { + ""flightNo"" : ""BM696"", + ""flightDate"" : ""2019-03-07T05:08:00Z"", + ""fltRouteSrc"" : ""IST"", + ""fltRouteDest"" : ""ATH"", + ""estimatedArrival"" : ""2019-03-08T04:10:00Z"", + ""actions"" : [ { + ""actionAt"" : ""IST"", + ""actionCode"" : ""ONLOAD to ATH"", + ""actionTime"" : ""2019-03-08T04:55:00Z"" + }, { + ""actionAt"" : ""IST"", + ""actionCode"" : ""BagTag Scan at IST"", + ""actionTime"" : ""2019-03-08T04:34:00Z"" + }, { + ""actionAt"" : ""IST"", + ""actionCode"" : ""OFFLOAD from IST"", + ""actionTime"" : ""2019-03-08T04:47:00Z"" + } ] + }, { + ""flightNo"" : ""BM665"", + ""flightDate"" : ""2019-03-07T04:08:00Z"", + ""fltRouteSrc"" : ""ATH"", + ""fltRouteDest"" : ""JTR"", + ""estimatedArrival"" : ""2019-03-07T16:10:00Z"", + ""actions"" : [ { + ""actionAt"" : ""JTR"", + ""actionCode"" : ""Offload to Carousel at JTR"", + ""actionTime"" : ""2019-03-07T16:09:00Z"" + }, { + ""actionAt"" : ""ATH"", + ""actionCode"" : ""ONLOAD to JTR"", + ""actionTime"" : ""2019-03-07T15:51:00Z"" + }, { + ""actionAt"" : ""ATH"", + ""actionCode"" : ""OFFLOAD from ATH"", + ""actionTime"" : ""2019-03-07T15:43:00Z"" + } ] + } ], + ""lastSeenTimeGmt"" : ""2019-03-07T16:01:00Z"", + ""bagArrivalDate"" : ""2019-03-07T16:01:00Z"" + } ] + }"; + private const string bag4= @"{ + ""ticketNo"":""1762320369957"", + ""fullName"":""Lorenzo Phil"", + ""gender"":""M"", + ""contactPhone"":""364-610-4444"", + ""confNo"":""QI3V6Q"", + ""bagInfo"":[ { + ""id"" : ""79039899187755"", + ""tagNum"" : ""17657806240001"", + ""routing"" : ""SFO/IST/ATH/JTR"", + ""lastActionCode"" : ""OFFLOAD"", + ""lastActionDesc"" : ""OFFLOAD"", + ""lastSeenStation"" : ""JTR"", + ""flightLegs"" : [ { + ""flightNo"" : ""BM318"", + ""flightDate"" : ""2019-03-12T03:00:00Z"", + ""fltRouteSrc"" : ""SFO"", + ""fltRouteDest"" : ""IST"", + ""estimatedArrival"" : ""2019-03-12T16:00:00Z"", + ""actions"" : [ { + ""actionAt"" : ""SFO"", + ""actionCode"" : ""ONLOAD to IST"", + ""actionTime"" : ""2019-03-12T03:11:00Z"" + }, { + ""actionAt"" : ""SFO"", + ""actionCode"" : ""BagTag Scan at SFO"", + ""actionTime"" : ""2019-03-12T02:49:00Z"" + }, { + ""actionAt"" : ""SFO"", + ""actionCode"" : ""Checkin at SFO"", + ""actionTime"" : ""2019-03-12T01:50:00Z"" + } ] + }, { + ""flightNo"" : ""BM696"", + ""flightDate"" : ""2019-03-12T04:11:00Z"", + ""fltRouteSrc"" : ""IST"", + ""fltRouteDest"" : ""ATH"", + ""estimatedArrival"" : ""2019-03-13T03:14:00Z"", + ""actions"" : [ { + ""actionAt"" : ""IST"", + ""actionCode"" : ""ONLOAD to ATH"", + ""actionTime"" : ""2019-03-13T04:10:00Z"" + }, { + ""actionAt"" : ""IST"", + ""actionCode"" : ""BagTag Scan at IST"", + ""actionTime"" : ""2019-03-13T03:56:00Z"" + }, { + ""actionAt"" : ""IST"", + ""actionCode"" : ""OFFLOAD from IST"", + ""actionTime"" : ""2019-03-13T03:59:00Z"" + } ] + }, { + ""flightNo"" : ""BM665"", + ""flightDate"" : ""2019-03-12T03:11:00Z"", + ""fltRouteSrc"" : ""ATH"", + ""fltRouteDest"" : ""JTR"", + ""estimatedArrival"" : ""2019-03-12T15:12:00Z"", + ""actions"" : [ { + ""actionAt"" : ""JTR"", + ""actionCode"" : ""Offload to Carousel at JTR"", + ""actionTime"" : ""2019-03-12T15:06:00Z"" + }, { + ""actionAt"" : ""ATH"", + ""actionCode"" : ""ONLOAD to JTR"", + ""actionTime"" : ""2019-03-12T14:16:00Z"" + }, { + ""actionAt"" : ""ATH"", + ""actionCode"" : ""OFFLOAD from ATH"", + ""actionTime"" : ""2019-03-12T14:13:00Z"" + } ] + } ], + ""lastSeenTimeGmt"" : ""2019-03-12T15:05:00Z"", + ""bagArrivalDate"" : ""2019-03-12T15:05:00Z"" + }, + { + ""id"" : ""79039899197755"", + ""tagNum"" : ""17657806340001"", + ""routing"" : ""SFO/IST/ATH/JTR"", + ""lastActionCode"" : ""OFFLOAD"", + ""lastActionDesc"" : ""OFFLOAD"", + ""lastSeenStation"" : ""JTR"", + ""flightLegs"" : [ { + ""flightNo"" : ""BM318"", + ""flightDate"" : ""2019-03-12T03:00:00Z"", + ""fltRouteSrc"" : ""SFO"", + ""fltRouteDest"" : ""IST"", + ""estimatedArrival"" : ""2019-03-12T16:40:00Z"", + ""actions"" : [ { + ""actionAt"" : ""SFO"", + ""actionCode"" : ""ONLOAD to IST"", + ""actionTime"" : ""2019-03-12T03:14:00Z"" + }, { + ""actionAt"" : ""SFO"", + ""actionCode"" : ""BagTag Scan at SFO"", + ""actionTime"" : ""2019-03-12T02:50:00Z"" + }, { + ""actionAt"" : ""SFO"", + ""actionCode"" : ""Checkin at SFO"", + ""actionTime"" : ""2019-03-12T01:58:00Z"" + } ] + }, { + ""flightNo"" : ""BM696"", + ""flightDate"" : ""2019-03-12T04:11:00Z"", + ""fltRouteSrc"" : ""IST"", + ""fltRouteDest"" : ""ATH"", + ""estimatedArrival"" : ""2019-03-13T03:18:00Z"", + ""actions"" : [ { + ""actionAt"" : ""IST"", + ""actionCode"" : ""ONLOAD to ATH"", + ""actionTime"" : ""2019-03-13T04:17:00Z"" + }, { + ""actionAt"" : ""IST"", + ""actionCode"" : ""BagTag Scan at IST"", + ""actionTime"" : ""2019-03-13T03:59:00Z"" + }, { + ""actionAt"" : ""IST"", + ""actionCode"" : ""OFFLOAD from IST"", + ""actionTime"" : ""2019-03-13T03:48:00Z"" + } ] + }, { + ""flightNo"" : ""BM665"", + ""flightDate"" : ""2019-03-12T03:11:00Z"", + ""fltRouteSrc"" : ""ATH"", + ""fltRouteDest"" : ""JTR"", + ""estimatedArrival"" : ""2019-03-12T15:12:00Z"", + ""actions"" : [ { + ""actionAt"" : ""JTR"", + ""actionCode"" : ""Offload to Carousel at JTR"", + ""actionTime"" : ""2019-03-12T15:06:00Z"" + }, { + ""actionAt"" : ""ATH"", + ""actionCode"" : ""ONLOAD to JTR"", + ""actionTime"" : ""2019-03-12T14:16:00Z"" + }, { + ""actionAt"" : ""ATH"", + ""actionCode"" : ""OFFLOAD from ATH"", + ""actionTime"" : ""2019-03-12T14:23:00Z"" + } ] + } ], + ""lastSeenTimeGmt"" : ""2019-03-12T16:05:00Z"", + ""bagArrivalDate"" : ""2019-03-12T16:25:00Z"" + } ] + }"; + private const string paran_expr =@"SELECT fullName, bag.bagInfo.tagNum, bag.bagInfo.routing, + bag.bagInfo[].flightLegs[].fltRouteDest FROM BaggageInfo bag + WHERE bag.bagInfo.flightLegs[].fltRouteSrc=any ""SFO"" AND + (bag.bagInfo[].flightLegs[].fltRouteDest=any ""ATH"" OR + bag.bagInfo[].flightLegs[].fltRouteDest=any ""JTR"" )"; + private const string case_expr =@"SELECT fullName, + CASE + WHEN NOT exists bag.bagInfo.flightLegs[0] + THEN ""you have no bag info"" + WHEN NOT exists bag.bagInfo.flightLegs[1] + THEN ""you have one hop"" + WHEN NOT exists bag.bagInfo.flightLegs[2] + THEN ""you have two hops."" + ELSE ""you have three hops."" + END AS NUMBER_HOPS + FROM BaggageInfo bag WHERE ticketNo=1762341772625"; + private const string seq_trn_expr =@"SELECT seq_transform(l.bagInfo[], + seq_transform( + $sq1.flightLegs[], + seq_transform( + $sq2.actions[], + { + ""at"" : $sq3.actionAt, + ""action"" : $sq3.actionCode, + ""flightNo"" : $sq2.flightNo, + ""tagNum"" : $sq1.tagNum + } + ) + ) + ) AS actions FROM baggageInfo l WHERE ticketNo=1762376407826" ; + + //replace the place holder for compartment with OCID of your compartment + 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 crtTabAddData(client); + Console.WriteLine("\nSuccess!"); + + Console.WriteLine("\nUsing Paranthesized expression:!"); + await fetchData(client,paran_expr); + + Console.WriteLine("\nUsing Case Expression!"); + await fetchData(client,case_expr); + + Console.WriteLine("\nUsing Sequence Transform Expressions!"); + await fetchData(client,seq_trn_expr); + + } + 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); + } + } + } + /// Get a connection handle for Oracle NoSQL Database Cloud Service + private async static Task getconnection_cloud() + { + // replace the region and compartment place holders with actual values + var client = new NoSQLClient(new NoSQLConfig + { + Region = , + Compartment = "" + }); + return client; + } + // Get a connection handle for onPremise data store + private async static Task getconnection_onPrem() + { + // replace the placeholder with your fullname of your host + var client = new NoSQLClient(new NoSQLConfig + { + ServiceType = ServiceType.KVStore, + Endpoint = "http://:8080" + }); + return client; + } + + private static async Task crtTabAddData(NoSQLClient client) + { + // Create a table + var sql = + $@"CREATE TABLE IF NOT EXISTS {TableName}(ticketNo LONG, + fullName STRING, + gender STRING, + contactPhone STRING, + confNo STRING, + bagInfo JSON, + primary key(ticketNo))"; + + 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(bag1).AsMapValue); + if (putResult.ConsumedCapacity != null) + { + Console.WriteLine(" Write used:"); + Console.WriteLine(" " + putResult.ConsumedCapacity); + } + var putResult1 = await client.PutAsync(TableName, FieldValue.FromJsonString(bag2).AsMapValue); + if (putResult1.ConsumedCapacity != null) + { + Console.WriteLine(" Write used:"); + Console.WriteLine(" " + putResult1.ConsumedCapacity); + } + var putResult2 = await client.PutAsync(TableName, FieldValue.FromJsonString(bag3).AsMapValue); + if (putResult2.ConsumedCapacity != null) + { + Console.WriteLine(" Write used:"); + Console.WriteLine(" " + putResult2.ConsumedCapacity); + } + var putResult3 = await client.PutAsync(TableName, FieldValue.FromJsonString(bag4).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 DoQuery(IAsyncEnumerable> queryEnumerable){ + Console.WriteLine(" Query results:"); + await foreach (var result in queryEnumerable) { + foreach (var row in result.Rows) + { + Console.WriteLine(); + Console.WriteLine(row.ToJsonString()); + } + } + } + } +} diff --git a/examples-nosql-dotnet-sdk/sqlexamples/SQLFunctions.cs b/examples-nosql-dotnet-sdk/sqlexamples/SQLFunctions.cs new file mode 100644 index 0000000..3abd995 --- /dev/null +++ b/examples-nosql-dotnet-sdk/sqlexamples/SQLFunctions.cs @@ -0,0 +1,488 @@ +// 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 + // + // where: + // - is target framework moniker, supported values + // are netcoreapp5.1 and net7.0 + // ----------------------------------------------------------------------- + public class SQLOperators + { + private const string Usage = + "Usage: dotnet run -f [-- ]"; + private const string TableName = "BaggageInfo"; + private const string bag1= @"{ + ""ticketNo"":""1762376407826"", + ""fullName"":""Dierdre Amador"", + ""gender"":""M"", + ""contactPhone"":""165-742-5715"", + ""confNo"":""ZG8Z5N"", + ""bagInfo"":[ { + ""id"" : ""7903989918469"", + ""tagNum"" : ""17657806240229"", + ""routing"" : ""JFK/MAD"", + ""lastActionCode"" : ""OFFLOAD"", + ""lastActionDesc"" : ""OFFLOAD"", + ""lastSeenStation"" : ""MAD"", + ""flightLegs"" : [ { + ""flightNo"" : ""BM495"", + ""flightDate"" : ""2019-03-07T07:00:00Z"", + ""fltRouteSrc"" : ""JFK"", + ""fltRouteDest"" : ""MAD"", + ""estimatedArrival"" : ""2019-03-07T14:00:00Z"", + ""actions"" : [ { + ""actionAt"" : ""MAD"", + ""actionCode"" : ""Offload to Carousel at MAD"", + ""actionTime"" : ""2019-03-07T13:54:00Z"" + }, { + ""actionAt"" : ""JFK"", + ""actionCode"" : ""ONLOAD to MAD"", + ""actionTime"" : ""2019-03-07T07:00:00Z"" + }, { + ""actionAt"" : ""JFK"", + ""actionCode"" : ""BagTag Scan at JFK"", + ""actionTime"" : ""2019-03-07T06:53:00Z"" + }, { + ""actionAt"" : ""JFK"", + ""actionCode"" : ""Checkin at JFK"", + ""actionTime"" : ""2019-03-07T05:03:00Z"" + } ] + } ], + ""lastSeenTimeGmt"" : ""2019-03-07T13:51:00Z"", + ""bagArrivalDate"" : ""2019-03-07T13:51:00Z"" + } ] + }"; + private const string bag2= @"{ + ""ticketNo"":""1762344493810"", + ""fullName"":""Adam Phillips"", + ""gender"":""M"", + ""contactPhone"":""893-324-1064"", + ""confNo"":""LE6J4Z"", + ""bagInfo"":[ { + ""id"" : ""79039899165297"", + ""tagNum"" : ""17657806255240"", + ""routing"" : ""MIA/LAX/MEL"", + ""lastActionCode"" : ""OFFLOAD"", + ""lastActionDesc"" : ""OFFLOAD"", + ""lastSeenStation"" : ""MEL"", + ""flightLegs"" : [ { + ""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"" + } ] + }, { + ""flightNo"" : ""BM667"", + ""flightDate"" : ""2019-02-01T06:13:00Z"", + ""fltRouteSrc"" : ""LAX"", + ""fltRouteDest"" : ""MEL"", + ""estimatedArrival"" : ""2019-02-01T16:15:00Z"", + ""actions"" : [ { + ""actionAt"" : ""MEL"", + ""actionCode"" : ""Offload to Carousel at MEL"", + ""actionTime"" : ""2019-02-01T16:15:00Z"" + }, { + ""actionAt"" : ""LAX"", + ""actionCode"" : ""ONLOAD to MEL"", + ""actionTime"" : ""2019-02-01T15:35:00Z"" + }, { + ""actionAt"" : ""LAX"", + ""actionCode"" : ""OFFLOAD from LAX"", + ""actionTime"" : ""2019-02-01T15:18:00Z"" + } ] + } ], + ""lastSeenTimeGmt"" : ""2019-02-01T16:13:00Z"", + ""bagArrivalDate"" : ""2019-02-01T16:13:00Z"" + } ] + }"; + private const string bag3= @"{ + ""ticketNo"":""1762341772625"", + ""fullName"":""Gerard Greene"", + ""gender"":""M"", + ""contactPhone"":""395-837-3772"", + ""confNo"":""MC0E7R"", + ""bagInfo"":[ { + ""id"" : ""79039899152842"", + ""tagNum"" : ""1765780626568"", + ""routing"" : ""SFO/IST/ATH/JTR"", + ""lastActionCode"" : ""OFFLOAD"", + ""lastActionDesc"" : ""OFFLOAD"", + ""lastSeenStation"" : ""JTR"", + ""flightLegs"" : [ { + ""flightNo"" : ""BM318"", + ""flightDate"" : ""2019-03-07T04:00:00Z"", + ""fltRouteSrc"" : ""SFO"", + ""fltRouteDest"" : ""IST"", + ""estimatedArrival"" : ""2019-03-07T17:00:00Z"", + ""actions"" : [ { + ""actionAt"" : ""SFO"", + ""actionCode"" : ""ONLOAD to IST"", + ""actionTime"" : ""2019-03-07T04:08:00Z"" + }, { + ""actionAt"" : ""SFO"", + ""actionCode"" : ""BagTag Scan at SFO"", + ""actionTime"" : ""2019-03-07T03:53:00Z"" + }, { + ""actionAt"" : ""SFO"", + ""actionCode"" : ""Checkin at SFO"", + ""actionTime"" : ""2019-03-07T02:20:00Z"" + } ] + }, { + ""flightNo"" : ""BM696"", + ""flightDate"" : ""2019-03-07T05:08:00Z"", + ""fltRouteSrc"" : ""IST"", + ""fltRouteDest"" : ""ATH"", + ""estimatedArrival"" : ""2019-03-08T04:10:00Z"", + ""actions"" : [ { + ""actionAt"" : ""IST"", + ""actionCode"" : ""ONLOAD to ATH"", + ""actionTime"" : ""2019-03-08T04:55:00Z"" + }, { + ""actionAt"" : ""IST"", + ""actionCode"" : ""BagTag Scan at IST"", + ""actionTime"" : ""2019-03-08T04:34:00Z"" + }, { + ""actionAt"" : ""IST"", + ""actionCode"" : ""OFFLOAD from IST"", + ""actionTime"" : ""2019-03-08T04:47:00Z"" + } ] + }, { + ""flightNo"" : ""BM665"", + ""flightDate"" : ""2019-03-07T04:08:00Z"", + ""fltRouteSrc"" : ""ATH"", + ""fltRouteDest"" : ""JTR"", + ""estimatedArrival"" : ""2019-03-07T16:10:00Z"", + ""actions"" : [ { + ""actionAt"" : ""JTR"", + ""actionCode"" : ""Offload to Carousel at JTR"", + ""actionTime"" : ""2019-03-07T16:09:00Z"" + }, { + ""actionAt"" : ""ATH"", + ""actionCode"" : ""ONLOAD to JTR"", + ""actionTime"" : ""2019-03-07T15:51:00Z"" + }, { + ""actionAt"" : ""ATH"", + ""actionCode"" : ""OFFLOAD from ATH"", + ""actionTime"" : ""2019-03-07T15:43:00Z"" + } ] + } ], + ""lastSeenTimeGmt"" : ""2019-03-07T16:01:00Z"", + ""bagArrivalDate"" : ""2019-03-07T16:01:00Z"" + } ] + }"; + private const string bag4= @"{ + ""ticketNo"":""1762320369957"", + ""fullName"":""Lorenzo Phil"", + ""gender"":""M"", + ""contactPhone"":""364-610-4444"", + ""confNo"":""QI3V6Q"", + ""bagInfo"":[ { + ""id"" : ""79039899187755"", + ""tagNum"" : ""17657806240001"", + ""routing"" : ""SFO/IST/ATH/JTR"", + ""lastActionCode"" : ""OFFLOAD"", + ""lastActionDesc"" : ""OFFLOAD"", + ""lastSeenStation"" : ""JTR"", + ""flightLegs"" : [ { + ""flightNo"" : ""BM318"", + ""flightDate"" : ""2019-03-12T03:00:00Z"", + ""fltRouteSrc"" : ""SFO"", + ""fltRouteDest"" : ""IST"", + ""estimatedArrival"" : ""2019-03-12T16:00:00Z"", + ""actions"" : [ { + ""actionAt"" : ""SFO"", + ""actionCode"" : ""ONLOAD to IST"", + ""actionTime"" : ""2019-03-12T03:11:00Z"" + }, { + ""actionAt"" : ""SFO"", + ""actionCode"" : ""BagTag Scan at SFO"", + ""actionTime"" : ""2019-03-12T02:49:00Z"" + }, { + ""actionAt"" : ""SFO"", + ""actionCode"" : ""Checkin at SFO"", + ""actionTime"" : ""2019-03-12T01:50:00Z"" + } ] + }, { + ""flightNo"" : ""BM696"", + ""flightDate"" : ""2019-03-12T04:11:00Z"", + ""fltRouteSrc"" : ""IST"", + ""fltRouteDest"" : ""ATH"", + ""estimatedArrival"" : ""2019-03-13T03:14:00Z"", + ""actions"" : [ { + ""actionAt"" : ""IST"", + ""actionCode"" : ""ONLOAD to ATH"", + ""actionTime"" : ""2019-03-13T04:10:00Z"" + }, { + ""actionAt"" : ""IST"", + ""actionCode"" : ""BagTag Scan at IST"", + ""actionTime"" : ""2019-03-13T03:56:00Z"" + }, { + ""actionAt"" : ""IST"", + ""actionCode"" : ""OFFLOAD from IST"", + ""actionTime"" : ""2019-03-13T03:59:00Z"" + } ] + }, { + ""flightNo"" : ""BM665"", + ""flightDate"" : ""2019-03-12T03:11:00Z"", + ""fltRouteSrc"" : ""ATH"", + ""fltRouteDest"" : ""JTR"", + ""estimatedArrival"" : ""2019-03-12T15:12:00Z"", + ""actions"" : [ { + ""actionAt"" : ""JTR"", + ""actionCode"" : ""Offload to Carousel at JTR"", + ""actionTime"" : ""2019-03-12T15:06:00Z"" + }, { + ""actionAt"" : ""ATH"", + ""actionCode"" : ""ONLOAD to JTR"", + ""actionTime"" : ""2019-03-12T14:16:00Z"" + }, { + ""actionAt"" : ""ATH"", + ""actionCode"" : ""OFFLOAD from ATH"", + ""actionTime"" : ""2019-03-12T14:13:00Z"" + } ] + } ], + ""lastSeenTimeGmt"" : ""2019-03-12T15:05:00Z"", + ""bagArrivalDate"" : ""2019-03-12T15:05:00Z"" + }, + { + ""id"" : ""79039899197755"", + ""tagNum"" : ""17657806340001"", + ""routing"" : ""SFO/IST/ATH/JTR"", + ""lastActionCode"" : ""OFFLOAD"", + ""lastActionDesc"" : ""OFFLOAD"", + ""lastSeenStation"" : ""JTR"", + ""flightLegs"" : [ { + ""flightNo"" : ""BM318"", + ""flightDate"" : ""2019-03-12T03:00:00Z"", + ""fltRouteSrc"" : ""SFO"", + ""fltRouteDest"" : ""IST"", + ""estimatedArrival"" : ""2019-03-12T16:40:00Z"", + ""actions"" : [ { + ""actionAt"" : ""SFO"", + ""actionCode"" : ""ONLOAD to IST"", + ""actionTime"" : ""2019-03-12T03:14:00Z"" + }, { + ""actionAt"" : ""SFO"", + ""actionCode"" : ""BagTag Scan at SFO"", + ""actionTime"" : ""2019-03-12T02:50:00Z"" + }, { + ""actionAt"" : ""SFO"", + ""actionCode"" : ""Checkin at SFO"", + ""actionTime"" : ""2019-03-12T01:58:00Z"" + } ] + }, { + ""flightNo"" : ""BM696"", + ""flightDate"" : ""2019-03-12T04:11:00Z"", + ""fltRouteSrc"" : ""IST"", + ""fltRouteDest"" : ""ATH"", + ""estimatedArrival"" : ""2019-03-13T03:18:00Z"", + ""actions"" : [ { + ""actionAt"" : ""IST"", + ""actionCode"" : ""ONLOAD to ATH"", + ""actionTime"" : ""2019-03-13T04:17:00Z"" + }, { + ""actionAt"" : ""IST"", + ""actionCode"" : ""BagTag Scan at IST"", + ""actionTime"" : ""2019-03-13T03:59:00Z"" + }, { + ""actionAt"" : ""IST"", + ""actionCode"" : ""OFFLOAD from IST"", + ""actionTime"" : ""2019-03-13T03:48:00Z"" + } ] + }, { + ""flightNo"" : ""BM665"", + ""flightDate"" : ""2019-03-12T03:11:00Z"", + ""fltRouteSrc"" : ""ATH"", + ""fltRouteDest"" : ""JTR"", + ""estimatedArrival"" : ""2019-03-12T15:12:00Z"", + ""actions"" : [ { + ""actionAt"" : ""JTR"", + ""actionCode"" : ""Offload to Carousel at JTR"", + ""actionTime"" : ""2019-03-12T15:06:00Z"" + }, { + ""actionAt"" : ""ATH"", + ""actionCode"" : ""ONLOAD to JTR"", + ""actionTime"" : ""2019-03-12T14:16:00Z"" + }, { + ""actionAt"" : ""ATH"", + ""actionCode"" : ""OFFLOAD from ATH"", + ""actionTime"" : ""2019-03-12T14:23:00Z"" + } ] + } ], + ""lastSeenTimeGmt"" : ""2019-03-12T16:05:00Z"", + ""bagArrivalDate"" : ""2019-03-12T16:25:00Z"" + } ] + }"; + private const string ts_func1 =@"SELECT timestamp_add(bag.bagInfo.flightLegs[0].estimatedArrival, ""5 minutes"") + AS ARRIVAL_TIME FROM BaggageInfo bag WHERE ticketNo=1762341772625"; + private const string ts_func2 =@"SELECT $s.ticketno, $bagInfo.bagArrivalDate, $flightLeg.flightDate, + get_duration(timestamp_diff($bagInfo.bagArrivalDate, $flightLeg.flightDate)) AS diff + FROM baggageinfo $s, + $s.bagInfo[] AS $bagInfo, $bagInfo.flightLegs[] AS $flightLeg + WHERE ticketNo=1762344493810"; + private const string string_func1 =@"SELECT substring(bag.baginfo.routing,0,3) AS Source FROM baggageInfo bag + WHERE ticketNo=1762376407826" ; + private const string string_func2 =@"SELECT fullname, length(fullname) AS fullname_length FROM BaggageInfo + WHERE ticketNo=1762320369957"; + private const string string_func3 =@"SELECT fullname FROM baggageInfo bag WHERE + EXISTS bag.bagInfo[contains($element.routing,""SFO"")]"; + //replace the place holder for compartment with OCID of your compartment + 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 crtTabAddData(client); + Console.WriteLine("\nSuccess!"); + + Console.WriteLine("\nUsing timestamp_add function!"); + await fetchData(client,ts_func1); + + Console.WriteLine("\nUsing get_duration and timestamp_diff function!"); + await fetchData(client,ts_func2); + + Console.WriteLine("\nUsing substring function!"); + await fetchData(client,string_func1); + + Console.WriteLine("\nUsing length function!"); + await fetchData(client,string_func2); + + Console.WriteLine("\nUsing contains function!"); + await fetchData(client,string_func3); + + } + 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); + } + } + } + /// Get a connection handle for Oracle NoSQL Database Cloud Service + private async static Task getconnection_cloud() + { + // replace the region and compartment place holders with actual values + var client = new NoSQLClient(new NoSQLConfig + { + Region = , + Compartment = "" + }); + return client; + } + // Get a connection handle for onPremise data store + private async static Task getconnection_onPrem() + { + // replace the placeholder with your fullname of your host + var client = new NoSQLClient(new NoSQLConfig + { + ServiceType = ServiceType.KVStore, + Endpoint = "http://:8080" + }); + return client; + } + + private static async Task crtTabAddData(NoSQLClient client) + { + // Create a table + var sql = + $@"CREATE TABLE IF NOT EXISTS {TableName}(ticketNo LONG, + fullName STRING, + gender STRING, + contactPhone STRING, + confNo STRING, + bagInfo JSON, + primary key(ticketNo))"; + + 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(bag1).AsMapValue); + if (putResult.ConsumedCapacity != null) + { + Console.WriteLine(" Write used:"); + Console.WriteLine(" " + putResult.ConsumedCapacity); + } + var putResult1 = await client.PutAsync(TableName, FieldValue.FromJsonString(bag2).AsMapValue); + if (putResult1.ConsumedCapacity != null) + { + Console.WriteLine(" Write used:"); + Console.WriteLine(" " + putResult1.ConsumedCapacity); + } + var putResult2 = await client.PutAsync(TableName, FieldValue.FromJsonString(bag3).AsMapValue); + if (putResult2.ConsumedCapacity != null) + { + Console.WriteLine(" Write used:"); + Console.WriteLine(" " + putResult2.ConsumedCapacity); + } + var putResult3 = await client.PutAsync(TableName, FieldValue.FromJsonString(bag4).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 DoQuery(IAsyncEnumerable> queryEnumerable){ + Console.WriteLine(" Query results:"); + await foreach (var result in queryEnumerable) { + foreach (var row in result.Rows) + { + Console.WriteLine(); + Console.WriteLine(row.ToJsonString()); + } + } + } + } +} diff --git a/examples-nosql-dotnet-sdk/sqlexamples/SQLOperators.cs b/examples-nosql-dotnet-sdk/sqlexamples/SQLOperators.cs new file mode 100644 index 0000000..5dc69e6 --- /dev/null +++ b/examples-nosql-dotnet-sdk/sqlexamples/SQLOperators.cs @@ -0,0 +1,486 @@ +// 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 + // + // where: + // - is target framework moniker, supported values + // are netcoreapp5.1 and net7.0 + // ----------------------------------------------------------------------- + public class SQLOperators + { + private const string Usage = + "Usage: dotnet run -f [-- ]"; + private const string TableName = "BaggageInfo"; + private const string bag1= @"{ + ""ticketNo"":""1762376407826"", + ""fullName"":""Dierdre Amador"", + ""gender"":""M"", + ""contactPhone"":""165-742-5715"", + ""confNo"":""ZG8Z5N"", + ""bagInfo"":[ { + ""id"" : ""7903989918469"", + ""tagNum"" : ""17657806240229"", + ""routing"" : ""JFK/MAD"", + ""lastActionCode"" : ""OFFLOAD"", + ""lastActionDesc"" : ""OFFLOAD"", + ""lastSeenStation"" : ""MAD"", + ""flightLegs"" : [ { + ""flightNo"" : ""BM495"", + ""flightDate"" : ""2019-03-07T07:00:00Z"", + ""fltRouteSrc"" : ""JFK"", + ""fltRouteDest"" : ""MAD"", + ""estimatedArrival"" : ""2019-03-07T14:00:00Z"", + ""actions"" : [ { + ""actionAt"" : ""MAD"", + ""actionCode"" : ""Offload to Carousel at MAD"", + ""actionTime"" : ""2019-03-07T13:54:00Z"" + }, { + ""actionAt"" : ""JFK"", + ""actionCode"" : ""ONLOAD to MAD"", + ""actionTime"" : ""2019-03-07T07:00:00Z"" + }, { + ""actionAt"" : ""JFK"", + ""actionCode"" : ""BagTag Scan at JFK"", + ""actionTime"" : ""2019-03-07T06:53:00Z"" + }, { + ""actionAt"" : ""JFK"", + ""actionCode"" : ""Checkin at JFK"", + ""actionTime"" : ""2019-03-07T05:03:00Z"" + } ] + } ], + ""lastSeenTimeGmt"" : ""2019-03-07T13:51:00Z"", + ""bagArrivalDate"" : ""2019-03-07T13:51:00Z"" + } ] + }"; + private const string bag2= @"{ + ""ticketNo"":""1762344493810"", + ""fullName"":""Adam Phillips"", + ""gender"":""M"", + ""contactPhone"":""893-324-1064"", + ""confNo"":""LE6J4Z"", + ""bagInfo"":[ { + ""id"" : ""79039899165297"", + ""tagNum"" : ""17657806255240"", + ""routing"" : ""MIA/LAX/MEL"", + ""lastActionCode"" : ""OFFLOAD"", + ""lastActionDesc"" : ""OFFLOAD"", + ""lastSeenStation"" : ""MEL"", + ""flightLegs"" : [ { + ""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"" + } ] + }, { + ""flightNo"" : ""BM667"", + ""flightDate"" : ""2019-02-01T06:13:00Z"", + ""fltRouteSrc"" : ""LAX"", + ""fltRouteDest"" : ""MEL"", + ""estimatedArrival"" : ""2019-02-01T16:15:00Z"", + ""actions"" : [ { + ""actionAt"" : ""MEL"", + ""actionCode"" : ""Offload to Carousel at MEL"", + ""actionTime"" : ""2019-02-01T16:15:00Z"" + }, { + ""actionAt"" : ""LAX"", + ""actionCode"" : ""ONLOAD to MEL"", + ""actionTime"" : ""2019-02-01T15:35:00Z"" + }, { + ""actionAt"" : ""LAX"", + ""actionCode"" : ""OFFLOAD from LAX"", + ""actionTime"" : ""2019-02-01T15:18:00Z"" + } ] + } ], + ""lastSeenTimeGmt"" : ""2019-02-01T16:13:00Z"", + ""bagArrivalDate"" : ""2019-02-01T16:13:00Z"" + } ] + }"; + private const string bag3= @"{ + ""ticketNo"":""1762341772625"", + ""fullName"":""Gerard Greene"", + ""gender"":""M"", + ""contactPhone"":""395-837-3772"", + ""confNo"":""MC0E7R"", + ""bagInfo"":[ { + ""id"" : ""79039899152842"", + ""tagNum"" : ""1765780626568"", + ""routing"" : ""SFO/IST/ATH/JTR"", + ""lastActionCode"" : ""OFFLOAD"", + ""lastActionDesc"" : ""OFFLOAD"", + ""lastSeenStation"" : ""JTR"", + ""flightLegs"" : [ { + ""flightNo"" : ""BM318"", + ""flightDate"" : ""2019-03-07T04:00:00Z"", + ""fltRouteSrc"" : ""SFO"", + ""fltRouteDest"" : ""IST"", + ""estimatedArrival"" : ""2019-03-07T17:00:00Z"", + ""actions"" : [ { + ""actionAt"" : ""SFO"", + ""actionCode"" : ""ONLOAD to IST"", + ""actionTime"" : ""2019-03-07T04:08:00Z"" + }, { + ""actionAt"" : ""SFO"", + ""actionCode"" : ""BagTag Scan at SFO"", + ""actionTime"" : ""2019-03-07T03:53:00Z"" + }, { + ""actionAt"" : ""SFO"", + ""actionCode"" : ""Checkin at SFO"", + ""actionTime"" : ""2019-03-07T02:20:00Z"" + } ] + }, { + ""flightNo"" : ""BM696"", + ""flightDate"" : ""2019-03-07T05:08:00Z"", + ""fltRouteSrc"" : ""IST"", + ""fltRouteDest"" : ""ATH"", + ""estimatedArrival"" : ""2019-03-08T04:10:00Z"", + ""actions"" : [ { + ""actionAt"" : ""IST"", + ""actionCode"" : ""ONLOAD to ATH"", + ""actionTime"" : ""2019-03-08T04:55:00Z"" + }, { + ""actionAt"" : ""IST"", + ""actionCode"" : ""BagTag Scan at IST"", + ""actionTime"" : ""2019-03-08T04:34:00Z"" + }, { + ""actionAt"" : ""IST"", + ""actionCode"" : ""OFFLOAD from IST"", + ""actionTime"" : ""2019-03-08T04:47:00Z"" + } ] + }, { + ""flightNo"" : ""BM665"", + ""flightDate"" : ""2019-03-07T04:08:00Z"", + ""fltRouteSrc"" : ""ATH"", + ""fltRouteDest"" : ""JTR"", + ""estimatedArrival"" : ""2019-03-07T16:10:00Z"", + ""actions"" : [ { + ""actionAt"" : ""JTR"", + ""actionCode"" : ""Offload to Carousel at JTR"", + ""actionTime"" : ""2019-03-07T16:09:00Z"" + }, { + ""actionAt"" : ""ATH"", + ""actionCode"" : ""ONLOAD to JTR"", + ""actionTime"" : ""2019-03-07T15:51:00Z"" + }, { + ""actionAt"" : ""ATH"", + ""actionCode"" : ""OFFLOAD from ATH"", + ""actionTime"" : ""2019-03-07T15:43:00Z"" + } ] + } ], + ""lastSeenTimeGmt"" : ""2019-03-07T16:01:00Z"", + ""bagArrivalDate"" : ""2019-03-07T16:01:00Z"" + } ] + }"; + private const string bag4= @"{ + ""ticketNo"":""1762320369957"", + ""fullName"":""Lorenzo Phil"", + ""gender"":""M"", + ""contactPhone"":""364-610-4444"", + ""confNo"":""QI3V6Q"", + ""bagInfo"":[ { + ""id"" : ""79039899187755"", + ""tagNum"" : ""17657806240001"", + ""routing"" : ""SFO/IST/ATH/JTR"", + ""lastActionCode"" : ""OFFLOAD"", + ""lastActionDesc"" : ""OFFLOAD"", + ""lastSeenStation"" : ""JTR"", + ""flightLegs"" : [ { + ""flightNo"" : ""BM318"", + ""flightDate"" : ""2019-03-12T03:00:00Z"", + ""fltRouteSrc"" : ""SFO"", + ""fltRouteDest"" : ""IST"", + ""estimatedArrival"" : ""2019-03-12T16:00:00Z"", + ""actions"" : [ { + ""actionAt"" : ""SFO"", + ""actionCode"" : ""ONLOAD to IST"", + ""actionTime"" : ""2019-03-12T03:11:00Z"" + }, { + ""actionAt"" : ""SFO"", + ""actionCode"" : ""BagTag Scan at SFO"", + ""actionTime"" : ""2019-03-12T02:49:00Z"" + }, { + ""actionAt"" : ""SFO"", + ""actionCode"" : ""Checkin at SFO"", + ""actionTime"" : ""2019-03-12T01:50:00Z"" + } ] + }, { + ""flightNo"" : ""BM696"", + ""flightDate"" : ""2019-03-12T04:11:00Z"", + ""fltRouteSrc"" : ""IST"", + ""fltRouteDest"" : ""ATH"", + ""estimatedArrival"" : ""2019-03-13T03:14:00Z"", + ""actions"" : [ { + ""actionAt"" : ""IST"", + ""actionCode"" : ""ONLOAD to ATH"", + ""actionTime"" : ""2019-03-13T04:10:00Z"" + }, { + ""actionAt"" : ""IST"", + ""actionCode"" : ""BagTag Scan at IST"", + ""actionTime"" : ""2019-03-13T03:56:00Z"" + }, { + ""actionAt"" : ""IST"", + ""actionCode"" : ""OFFLOAD from IST"", + ""actionTime"" : ""2019-03-13T03:59:00Z"" + } ] + }, { + ""flightNo"" : ""BM665"", + ""flightDate"" : ""2019-03-12T03:11:00Z"", + ""fltRouteSrc"" : ""ATH"", + ""fltRouteDest"" : ""JTR"", + ""estimatedArrival"" : ""2019-03-12T15:12:00Z"", + ""actions"" : [ { + ""actionAt"" : ""JTR"", + ""actionCode"" : ""Offload to Carousel at JTR"", + ""actionTime"" : ""2019-03-12T15:06:00Z"" + }, { + ""actionAt"" : ""ATH"", + ""actionCode"" : ""ONLOAD to JTR"", + ""actionTime"" : ""2019-03-12T14:16:00Z"" + }, { + ""actionAt"" : ""ATH"", + ""actionCode"" : ""OFFLOAD from ATH"", + ""actionTime"" : ""2019-03-12T14:13:00Z"" + } ] + } ], + ""lastSeenTimeGmt"" : ""2019-03-12T15:05:00Z"", + ""bagArrivalDate"" : ""2019-03-12T15:05:00Z"" + }, + { + ""id"" : ""79039899197755"", + ""tagNum"" : ""17657806340001"", + ""routing"" : ""SFO/IST/ATH/JTR"", + ""lastActionCode"" : ""OFFLOAD"", + ""lastActionDesc"" : ""OFFLOAD"", + ""lastSeenStation"" : ""JTR"", + ""flightLegs"" : [ { + ""flightNo"" : ""BM318"", + ""flightDate"" : ""2019-03-12T03:00:00Z"", + ""fltRouteSrc"" : ""SFO"", + ""fltRouteDest"" : ""IST"", + ""estimatedArrival"" : ""2019-03-12T16:40:00Z"", + ""actions"" : [ { + ""actionAt"" : ""SFO"", + ""actionCode"" : ""ONLOAD to IST"", + ""actionTime"" : ""2019-03-12T03:14:00Z"" + }, { + ""actionAt"" : ""SFO"", + ""actionCode"" : ""BagTag Scan at SFO"", + ""actionTime"" : ""2019-03-12T02:50:00Z"" + }, { + ""actionAt"" : ""SFO"", + ""actionCode"" : ""Checkin at SFO"", + ""actionTime"" : ""2019-03-12T01:58:00Z"" + } ] + }, { + ""flightNo"" : ""BM696"", + ""flightDate"" : ""2019-03-12T04:11:00Z"", + ""fltRouteSrc"" : ""IST"", + ""fltRouteDest"" : ""ATH"", + ""estimatedArrival"" : ""2019-03-13T03:18:00Z"", + ""actions"" : [ { + ""actionAt"" : ""IST"", + ""actionCode"" : ""ONLOAD to ATH"", + ""actionTime"" : ""2019-03-13T04:17:00Z"" + }, { + ""actionAt"" : ""IST"", + ""actionCode"" : ""BagTag Scan at IST"", + ""actionTime"" : ""2019-03-13T03:59:00Z"" + }, { + ""actionAt"" : ""IST"", + ""actionCode"" : ""OFFLOAD from IST"", + ""actionTime"" : ""2019-03-13T03:48:00Z"" + } ] + }, { + ""flightNo"" : ""BM665"", + ""flightDate"" : ""2019-03-12T03:11:00Z"", + ""fltRouteSrc"" : ""ATH"", + ""fltRouteDest"" : ""JTR"", + ""estimatedArrival"" : ""2019-03-12T15:12:00Z"", + ""actions"" : [ { + ""actionAt"" : ""JTR"", + ""actionCode"" : ""Offload to Carousel at JTR"", + ""actionTime"" : ""2019-03-12T15:06:00Z"" + }, { + ""actionAt"" : ""ATH"", + ""actionCode"" : ""ONLOAD to JTR"", + ""actionTime"" : ""2019-03-12T14:16:00Z"" + }, { + ""actionAt"" : ""ATH"", + ""actionCode"" : ""OFFLOAD from ATH"", + ""actionTime"" : ""2019-03-12T14:23:00Z"" + } ] + } ], + ""lastSeenTimeGmt"" : ""2019-03-12T16:05:00Z"", + ""bagArrivalDate"" : ""2019-03-12T16:25:00Z"" + } ] + }"; + private const string seqcomp_stmt =@"SELECT bag.bagInfo[].tagNum,bag.bagInfo[].flightLegs[].fltRouteSrc + FROM BaggageInfo bag WHERE bag.bagInfo[].flightLegs[].fltRouteSrc=any ""SFO"""; + private const string logope_stmt =@"SELECT fullName, bag.bagInfo[].tagNum, bag.bagInfo[].routing, + bag.bagInfo[].flightLegs[].fltRouteSrc + FROM BaggageInfo bag + WHERE NOT bag.bagInfo[].flightLegs[].fltRouteSrc=any ""SFO"""; + private const string valcomp_stmt =@"SELECT fullname, bag.bagInfo[].routing FROM BaggageInfo bag WHERE gender=""M""" ; + private const string inope_stmt =@"SELECT bagdet.fullName, bagdet.bagInfo[].tagNum + FROM BaggageInfo bagdet WHERE bagdet.fullName IN + (""Lucinda Beckman"", ""Adam Phillips"",""Dierdre Amador"",""Fallon Clements"")"; + private const string existsope_stmt =@"SELECT fullName, bag.ticketNo FROM BaggageInfo bag WHERE + EXISTS bag.bagInfo[$element.bagArrivalDate >=""2019-03-01T00:00:00""]"; + //replace the place holder for compartment with 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); + Console.WriteLine("\nSuccess!"); + + Console.WriteLine("\nUsing Sequence Comparison operator!"); + await fetchData(client,seqcomp_stmt); + + Console.WriteLine("\nUsing Logical operator!"); + await fetchData(client,logope_stmt); + + Console.WriteLine("\nUsing Value Comparison operator!"); + await fetchData(client,valcomp_stmt); + + Console.WriteLine("\nUsing IN operator!"); + await fetchData(client,inope_stmt); + + Console.WriteLine("\nUsing EXISTS operator!"); + await fetchData(client,existsope_stmt); + + } + 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); + } + } + } + // Get a connection handle for Oracle NoSQL Database Cloud Service + private async static Task getconnection_cloud() + { + // replace the region and compartment place holders with actual values + var client = new NoSQLClient(new NoSQLConfig + { + Region = , + Compartment = "" + }); + return client; + } + // Get a connection handle for onPremise data store + private async static Task getconnection_onPrem() + { + // replace the placeholder with your fullname of your host + var client = new NoSQLClient(new NoSQLConfig + { + ServiceType = ServiceType.KVStore, + Endpoint = "http://:8080" + }); + return client; + } + + + private static async Task crtTabAddData(NoSQLClient client) + { + // Create a table + var sql = + $@"CREATE TABLE IF NOT EXISTS {TableName}(ticketNo LONG, + fullName STRING, + gender STRING, + contactPhone STRING, + confNo STRING, + bagInfo JSON, + primary key(ticketNo))"; + + 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(bag1).AsMapValue); + if (putResult.ConsumedCapacity != null) + { + Console.WriteLine(" Write used:"); + Console.WriteLine(" " + putResult.ConsumedCapacity); + } + var putResult1 = await client.PutAsync(TableName, FieldValue.FromJsonString(bag2).AsMapValue); + if (putResult1.ConsumedCapacity != null) + { + Console.WriteLine(" Write used:"); + Console.WriteLine(" " + putResult1.ConsumedCapacity); + } + var putResult2 = await client.PutAsync(TableName, FieldValue.FromJsonString(bag3).AsMapValue); + if (putResult2.ConsumedCapacity != null) + { + Console.WriteLine(" Write used:"); + Console.WriteLine(" " + putResult2.ConsumedCapacity); + } + var putResult3 = await client.PutAsync(TableName, FieldValue.FromJsonString(bag4).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 DoQuery(IAsyncEnumerable> queryEnumerable){ + Console.WriteLine(" Query results:"); + await foreach (var result in queryEnumerable) { + foreach (var row in result.Rows) + { + Console.WriteLine(); + Console.WriteLine(row.ToJsonString()); + } + } + } + } +} diff --git a/examples-nosql-dotnet-sdk/sqlexamples/TableJoins.cs b/examples-nosql-dotnet-sdk/sqlexamples/TableJoins.cs index d15db6d..2cdc463 100644 --- a/examples-nosql-dotnet-sdk/sqlexamples/TableJoins.cs +++ b/examples-nosql-dotnet-sdk/sqlexamples/TableJoins.cs @@ -78,7 +78,7 @@ estimatedArrival TIMESTAMP(4), //Get a connection handle for Oracle NoSQL Database Cloud Service private async static Task getconnection_cloud() { - // replace the region and compartment place holders with the actual values + // replace the region and compartment place holders with actual values var client = new NoSQLClient(new NoSQLConfig { Region = , @@ -131,13 +131,15 @@ private static async Task DoQuery(IAsyncEnumerable> que } } } - //replace the place holder for compartment with the OCID of your compartmetn + //replace the place holder for compartment with OCID of your compartment public static async Task Main(string[] args) { try { - // if using cloud service uncomment the code below, else if using onPremises comment it + // 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 + // if using onPremise uncomment the code below, + // else if using cloud service comment it //var client = await getconnection_onPrem(); Console.WriteLine("Created NoSQLClient instance"); diff --git a/examples-nosql-go-sdk/sqlexamples/AddBagData.go b/examples-nosql-go-sdk/sqlexamples/AddBagData.go new file mode 100644 index 0000000..9ca14ba --- /dev/null +++ b/examples-nosql-go-sdk/sqlexamples/AddBagData.go @@ -0,0 +1,441 @@ +// Copyright (c) 2023, 2024 Oracle and/or its affiliates. +// Licensed under the Universal Permissive License v 1.0 as shown at +// https://oss.oracle.com/licenses/upl/ +package main + +import ( + "fmt" + "time" + + "github.com/oracle/nosql-go-sdk/nosqldb" + "github.com/oracle/nosql-go-sdk/nosqldb/auth/iam" + "github.com/oracle/nosql-go-sdk/nosqldb/common" + "github.com/oracle/nosql-go-sdk/nosqldb/types" +) + +// Creates a client with the supplied configurations. +// This function encapsulates environmental differences and returns a +// client handle to use for data operations. +func createClient_cloud() (*nosqldb.Client, error) { + var cfg nosqldb.Config + // replace the placeholder with your actual region identifier + region := "" + // Replace the placeholders with the actual value of config file location + // and the ocid of your compartment + sp, err := iam.NewSignatureProviderFromFile("", "", "", "") + if err != nil { + return nil, fmt.Errorf("cannot create a Signature Provider: %v", err) + } + cfg = nosqldb.Config{ + Mode: "cloud", + Region: common.Region(region), + AuthorizationProvider: sp, + } + client, err := nosqldb.NewClient(cfg) + return client, err +} + +// Creates a client with the supplied configurations for onPremise database +func createClient_onPrem() (*nosqldb.Client, error) { + var cfg nosqldb.Config + // replace the placeholder with the fullname of your host + endpoint := "http://:8080" + cfg = nosqldb.Config{ + Endpoint: endpoint, + Mode: "onprem", + } + // If using a secure store, uncomment the lines below and pass the username, + // password of the store to Config + // cfg := nosqldb.Config{ + // Mode: "onprem", + // Username: "", + // Password: []byte(""), + // Specify InsecureSkipVerify + // HTTPConfig: httputil.HTTPConfig{ + // InsecureSkipVerify: true, + // }, + client, err := nosqldb.NewClient(cfg) + return client, err +} + +// Creates a table +func createTable(client *nosqldb.Client, err error, tableName string)(){ + stmt := fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s ("+ + "ticketNo LONG," + + "fullName STRING," + + "gender STRING," + + "contactPhone STRING," + + "confNo STRING ," + + "bagInfo JSON, " + + "PRIMARY KEY(ticketNo))",tableName) + tableReq := &nosqldb.TableRequest{ + Statement: stmt, + TableLimits: &nosqldb.TableLimits{ + ReadUnits: 20, + WriteUnits: 20, + StorageGB: 1, + }, + } + tableRes, err := client.DoTableRequest(tableReq) + if err != nil { + fmt.Printf("cannot initiate CREATE TABLE request: %v\n", err) + return + } + // The create table request is asynchronous, wait for table creation + // to complete. + _, err = tableRes.WaitForCompletion(client, 60*time.Second, time.Second) + if err != nil { + fmt.Printf("Error finishing CREATE TABLE request: %v\n", err) + return + } + fmt.Println("Created table ", tableName) + return +} +//Add data to table +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("Put row: %v\nresult: %v\n", putReq.Value.Map(), putRes) +} + +func main() { + //if using cloud service uncomment the line below + client, err := createClient_cloud() + //if using onPrem uncomment the line below + //client, err := createClient_onPrem() + if err != nil { + fmt.Printf("cannot create NoSQL client: %v\n", err) + return + } + defer client.Close() + tableName := "BaggageInfo" + createTable(client, err,tableName) + //adding data + value, err := types.NewMapValueFromJSON(`{ + "ticketNo":"1762376407826", + "fullName":"Dierdre Amador", + "gender":"M", + "contactPhone":"165-742-5715", + "confNo":"ZG8Z5N", + "bagInfo":[ { + "id" : "7903989918469", + "tagNum" : "17657806240229", + "routing" : "JFK/MAD", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "MAD", + "flightLegs" : [ { + "flightNo" : "BM495", + "flightDate" : "2019-03-07T07:00:00Z", + "fltRouteSrc" : "JFK", + "fltRouteDest" : "MAD", + "estimatedArrival" : "2019-03-07T14:00:00Z", + "actions" : [ { + "actionAt" : "MAD", + "actionCode" : "Offload to Carousel at MAD", + "actionTime" : "2019-03-07T13:54:00Z" + }, { + "actionAt" : "JFK", + "actionCode" : "ONLOAD to MAD", + "actionTime" : "2019-03-07T07:00:00Z" + }, { + "actionAt" : "JFK", + "actionCode" : "BagTag Scan at JFK", + "actionTime" : "2019-03-07T06:53:00Z" + }, { + "actionAt" : "JFK", + "actionCode" : "Checkin at JFK", + "actionTime" : "2019-03-07T05:03:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-03-07T13:51:00Z", + "bagArrivalDate" : "2019-03-07T13:51:00Z" + } ] + }`) + insertData(client, err,tableName,value) + value1, err := types.NewMapValueFromJSON(`{ + "ticketNo":"1762344493810", + "fullName":"Adam Phillips", + "gender":"M", + "contactPhone":"893-324-1064", + "confNo":"LE6J4Z", + "bagInfo":[ { + "id" : "79039899165297", + "tagNum" : "17657806255240", + "routing" : "MIA/LAX/MEL", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "MEL", + "flightLegs" : [ { + "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" + } ] + }, { + "flightNo" : "BM667", + "flightDate" : "2019-02-01T06:13:00Z", + "fltRouteSrc" : "LAX", + "fltRouteDest" : "MEL", + "estimatedArrival" : "2019-02-01T16:15:00Z", + "actions" : [ { + "actionAt" : "MEL", + "actionCode" : "Offload to Carousel at MEL", + "actionTime" : "2019-02-01T16:15:00Z" + }, { + "actionAt" : "LAX", + "actionCode" : "ONLOAD to MEL", + "actionTime" : "2019-02-01T15:35:00Z" + }, { + "actionAt" : "LAX", + "actionCode" : "OFFLOAD from LAX", + "actionTime" : "2019-02-01T15:18:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-02-01T16:13:00Z", + "bagArrivalDate" : "2019-02-01T16:13:00Z" + } ] + }`) + insertData(client, err,tableName,value1) + value2, err := types.NewMapValueFromJSON(`{ + "ticketNo":"1762341772625", + "fullName":"Gerard Greene", + "gender":"M", + "contactPhone":"395-837-3772", + "confNo":"MC0E7R", + "bagInfo":[ { + "id" : "79039899152842", + "tagNum" : "1765780626568", + "routing" : "SFO/IST/ATH/JTR", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "JTR", + "flightLegs" : [ { + "flightNo" : "BM318", + "flightDate" : "2019-03-07T04:00:00Z", + "fltRouteSrc" : "SFO", + "fltRouteDest" : "IST", + "estimatedArrival" : "2019-03-07T17:00:00Z", + "actions" : [ { + "actionAt" : "SFO", + "actionCode" : "ONLOAD to IST", + "actionTime" : "2019-03-07T04:08:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "BagTag Scan at SFO", + "actionTime" : "2019-03-07T03:53:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "Checkin at SFO", + "actionTime" : "2019-03-07T02:20:00Z" + } ] + }, { + "flightNo" : "BM696", + "flightDate" : "2019-03-07T05:08:00Z", + "fltRouteSrc" : "IST", + "fltRouteDest" : "ATH", + "estimatedArrival" : "2019-03-08T04:10:00Z", + "actions" : [ { + "actionAt" : "IST", + "actionCode" : "ONLOAD to ATH", + "actionTime" : "2019-03-08T04:55:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "BagTag Scan at IST", + "actionTime" : "2019-03-08T04:34:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "OFFLOAD from IST", + "actionTime" : "2019-03-08T04:47:00Z" + } ] + }, { + "flightNo" : "BM665", + "flightDate" : "2019-03-07T04:08:00Z", + "fltRouteSrc" : "ATH", + "fltRouteDest" : "JTR", + "estimatedArrival" : "2019-03-07T16:10:00Z", + "actions" : [ { + "actionAt" : "JTR", + "actionCode" : "Offload to Carousel at JTR", + "actionTime" : "2019-03-07T16:09:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "ONLOAD to JTR", + "actionTime" : "2019-03-07T15:51:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "OFFLOAD from ATH", + "actionTime" : "2019-03-07T15:43:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-03-07T16:01:00Z", + "bagArrivalDate" : "2019-03-07T16:01:00Z" + } ] + }`) + insertData(client, err,tableName,value2) + value3, err := types.NewMapValueFromJSON(`{ + "ticketNo":"1762320369957", + "fullName":"Lorenzo Phil", + "gender":"M", + "contactPhone":"364-610-4444", + "confNo":"QI3V6Q", + "bagInfo":[ { + "id" : "79039899187755", + "tagNum" : "17657806240001", + "routing" : "SFO/IST/ATH/JTR", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "JTR", + "flightLegs" : [ { + "flightNo" : "BM318", + "flightDate" : "2019-03-12T03:00:00Z", + "fltRouteSrc" : "SFO", + "fltRouteDest" : "IST", + "estimatedArrival" : "2019-03-12T16:00:00Z", + "actions" : [ { + "actionAt" : "SFO", + "actionCode" : "ONLOAD to IST", + "actionTime" : "2019-03-12T03:11:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "BagTag Scan at SFO", + "actionTime" : "2019-03-12T02:49:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "Checkin at SFO", + "actionTime" : "2019-03-12T01:50:00Z" + } ] + }, { + "flightNo" : "BM696", + "flightDate" : "2019-03-12T04:11:00Z", + "fltRouteSrc" : "IST", + "fltRouteDest" : "ATH", + "estimatedArrival" : "2019-03-13T03:14:00Z", + "actions" : [ { + "actionAt" : "IST", + "actionCode" : "ONLOAD to ATH", + "actionTime" : "2019-03-13T04:10:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "BagTag Scan at IST", + "actionTime" : "2019-03-13T03:56:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "OFFLOAD from IST", + "actionTime" : "2019-03-13T03:59:00Z" + } ] + }, { + "flightNo" : "BM665", + "flightDate" : "2019-03-12T03:11:00Z", + "fltRouteSrc" : "ATH", + "fltRouteDest" : "JTR", + "estimatedArrival" : "2019-03-12T15:12:00Z", + "actions" : [ { + "actionAt" : "JTR", + "actionCode" : "Offload to Carousel at JTR", + "actionTime" : "2019-03-12T15:06:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "ONLOAD to JTR", + "actionTime" : "2019-03-12T14:16:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "OFFLOAD from ATH", + "actionTime" : "2019-03-12T14:13:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-03-12T15:05:00Z", + "bagArrivalDate" : "2019-03-12T15:05:00Z" + }, + { + "id" : "79039899197755", + "tagNum" : "17657806340001", + "routing" : "SFO/IST/ATH/JTR", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "JTR", + "flightLegs" : [ { + "flightNo" : "BM318", + "flightDate" : "2019-03-12T03:00:00Z", + "fltRouteSrc" : "SFO", + "fltRouteDest" : "IST", + "estimatedArrival" : "2019-03-12T16:40:00Z", + "actions" : [ { + "actionAt" : "SFO", + "actionCode" : "ONLOAD to IST", + "actionTime" : "2019-03-12T03:14:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "BagTag Scan at SFO", + "actionTime" : "2019-03-12T02:50:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "Checkin at SFO", + "actionTime" : "2019-03-12T01:58:00Z" + } ] + }, { + "flightNo" : "BM696", + "flightDate" : "2019-03-12T04:11:00Z", + "fltRouteSrc" : "IST", + "fltRouteDest" : "ATH", + "estimatedArrival" : "2019-03-13T03:18:00Z", + "actions" : [ { + "actionAt" : "IST", + "actionCode" : "ONLOAD to ATH", + "actionTime" : "2019-03-13T04:17:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "BagTag Scan at IST", + "actionTime" : "2019-03-13T03:59:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "OFFLOAD from IST", + "actionTime" : "2019-03-13T03:48:00Z" + } ] + }, { + "flightNo" : "BM665", + "flightDate" : "2019-03-12T03:11:00Z", + "fltRouteSrc" : "ATH", + "fltRouteDest" : "JTR", + "estimatedArrival" : "2019-03-12T15:12:00Z", + "actions" : [ { + "actionAt" : "JTR", + "actionCode" : "Offload to Carousel at JTR", + "actionTime" : "2019-03-12T15:06:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "ONLOAD to JTR", + "actionTime" : "2019-03-12T14:16:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "OFFLOAD from ATH", + "actionTime" : "2019-03-12T14:23:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-03-12T16:05:00Z", + "bagArrivalDate" : "2019-03-12T16:25:00Z" + } ] + }`) + insertData(client, err,tableName,value3) + fmt.Printf("Put row succeeded: \n") +} diff --git a/examples-nosql-go-sdk/sqlexamples/AddData.go b/examples-nosql-go-sdk/sqlexamples/AddData.go index 0740ff9..da9e8ec 100644 --- a/examples-nosql-go-sdk/sqlexamples/AddData.go +++ b/examples-nosql-go-sdk/sqlexamples/AddData.go @@ -1,5 +1,6 @@ // Copyright (c) 2023, 2024 Oracle and/or its affiliates. -// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/ +// Licensed under the Universal Permissive License v 1.0 as shown at +// https://oss.oracle.com/licenses/upl/ package main import ( @@ -19,7 +20,8 @@ func createClient_cloud() (*nosqldb.Client, error) { var cfg nosqldb.Config // replace the placeholder with your actual region identifier region := "" - // Replace the placeholders with the actual value of config file location and the ocid of your compartment + // Replace the placeholders with the actual value of config file location + // and the ocid of your compartment sp, err := iam.NewSignatureProviderFromFile("", "", "", "") if err != nil { return nil, fmt.Errorf("cannot create a Signature Provider: %v", err) @@ -42,7 +44,8 @@ func createClient_onPrem() (*nosqldb.Client, error) { Endpoint: endpoint, Mode: "onprem", } - // If using a secure store, uncomment the lines below and pass the username, password of the store to Config + // If using a secure store, uncomment the lines below and pass the username, + // password of the store to Config // cfg := nosqldb.Config{ // Mode: "onprem", // Username: "", @@ -76,7 +79,8 @@ func createTable(client *nosqldb.Client, err error, tableName string) { fmt.Printf("cannot initiate CREATE TABLE request: %v\n", err) return } - // The create table request is asynchronous, wait for table creation to complete. + // The create table request is asynchronous, wait for table creation + // to complete. _, err = tableRes.WaitForCompletion(client, 60*time.Second, time.Second) if err != nil { fmt.Printf("Error finishing CREATE TABLE request: %v\n", err) @@ -101,9 +105,11 @@ func insertData(client *nosqldb.Client, err error, tableName string, value1 *typ } func main() { - // if using cloud service uncomment the line below. else if using onPremises comment this line out + // if using cloud service uncomment the line below. else if using onPremises + // comment this line out client, err := createClient_cloud() - // if using onPrem uncomment the line below, else if using cloud service, comment this line + // if using onPrem uncomment the line below, else if using cloud service, + // comment this line // client, err := createClient_onPrem() if err != nil { fmt.Printf("cannot create NoSQL client: %v\n", err) diff --git a/examples-nosql-go-sdk/sqlexamples/AlterTable.go b/examples-nosql-go-sdk/sqlexamples/AlterTable.go index a35e5b3..0569990 100644 --- a/examples-nosql-go-sdk/sqlexamples/AlterTable.go +++ b/examples-nosql-go-sdk/sqlexamples/AlterTable.go @@ -1,5 +1,6 @@ // Copyright (c) 2023, 2024 Oracle and/or its affiliates. -// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/ +// Licensed under the Universal Permissive License v 1.0 as shown at +// https://oss.oracle.com/licenses/upl/ package main import ( @@ -18,7 +19,8 @@ func createClient_cloud() (*nosqldb.Client, error) { var cfg nosqldb.Config // replace the placeholder with your actual region identifier region := "" - // Replace the placeholders with the actual value of config file location and the ocid of your compartment + // Replace the placeholders with the actual value of config file location + // and the ocid of your compartment sp, err := iam.NewSignatureProviderFromFile("", "", "", "") if err != nil { return nil, fmt.Errorf("cannot create a Signature Provider: %v", err) @@ -41,7 +43,8 @@ func createClient_onPrem() (*nosqldb.Client, error) { Endpoint: endpoint, Mode: "onprem", } - // If using a secure store, uncomment the lines below and pass the username, password of the store to Config + // If using a secure store, uncomment the lines below and pass the username, + // password of the store to Config // cfg := nosqldb.Config{ // Mode: "onprem", // Username: "", @@ -75,7 +78,8 @@ func createTable(client *nosqldb.Client, err error, tableName string) { fmt.Printf("cannot initiate CREATE TABLE request: %v\n", err) return } - // The create table request is asynchronous, wait for table creation to complete. + // The create table request is asynchronous, wait for table creation + // to complete. _, err = tableRes.WaitForCompletion(client, 60*time.Second, time.Second) if err != nil { fmt.Printf("Error finishing CREATE TABLE request: %v\n", err) @@ -96,7 +100,8 @@ func alterTable(client *nosqldb.Client, err error, tableName string) { fmt.Printf("cannot initiate ALTER TABLE request: %v\n", err) return } - // The alter table request is asynchronous, wait for table alteration to complete. + // The alter table request is asynchronous, wait for table alteration + // to complete. _, err = tableRes.WaitForCompletion(client, 60*time.Second, time.Second) if err != nil { fmt.Printf("Error finishing ALTER TABLE request: %v\n", err) @@ -128,9 +133,11 @@ func dropTable(client *nosqldb.Client, err error, tableName string) { } func main() { - // if using cloud service uncomment the line below. else if using onPremises comment this line out + // if using cloud service uncomment the line below. else if using onPremises + // comment this line out client, err := createClient_cloud() - // if using onPrem uncomment the line below, else if using cloud service, comment this line + // if using onPrem uncomment the line below, else if using cloud service, + // comment this line // client, err := createClient_onPrem() if err != nil { fmt.Printf("cannot create NoSQL client: %v\n", err) diff --git a/examples-nosql-go-sdk/sqlexamples/CreateTable.go b/examples-nosql-go-sdk/sqlexamples/CreateTable.go index b471f1e..c35ec73 100644 --- a/examples-nosql-go-sdk/sqlexamples/CreateTable.go +++ b/examples-nosql-go-sdk/sqlexamples/CreateTable.go @@ -1,5 +1,6 @@ // Copyright (c) 2023, 2024 Oracle and/or its affiliates. -// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/ +// Licensed under the Universal Permissive License v 1.0 as shown at +// https://oss.oracle.com/licenses/upl/ package main import ( @@ -18,7 +19,8 @@ func createClient_cloud() (*nosqldb.Client, error) { var cfg nosqldb.Config // replace the placeholder with your actual region identifier region := "" - // Replace the placeholders with the actual value of config file location and the ocid of your compartment + // Replace the placeholders with the actual value of config file location + // and the ocid of your compartment sp, err := iam.NewSignatureProviderFromFile("", "", "", "") if err != nil { return nil, fmt.Errorf("cannot create a Signature Provider: %v", err) @@ -41,7 +43,8 @@ func createClient_onPrem() (*nosqldb.Client, error) { Endpoint: endpoint, Mode: "onprem", } - // If using a secure store, uncomment the lines below and pass the username, password of the store to Config + // If using a secure store, uncomment the lines below and pass the username, + // password of the store to Config // cfg := nosqldb.Config{ // Mode: "onprem", // Username: "", @@ -75,7 +78,8 @@ func createTable(client *nosqldb.Client, err error, tableName string) { fmt.Printf("cannot initiate CREATE TABLE request: %v\n", err) return } - // The create table request is asynchronous, wait for table creation to complete. + // The create table request is asynchronous, wait for table creation + // to complete. _, err = tableRes.WaitForCompletion(client, 60*time.Second, time.Second) if err != nil { fmt.Printf("Error finishing CREATE TABLE request: %v\n", err) @@ -85,9 +89,11 @@ func createTable(client *nosqldb.Client, err error, tableName string) { return } func main() { - // if using cloud service uncomment the line below. else if using onPremises comment this line out + // if using cloud service uncomment the line below. else if using onPremises + // comment this line out client, err := createClient_cloud() - // if using onPrem uncomment the line below, else if using cloud service, comment this line + // if using onPrem uncomment the line below, else if using cloud service, + // comment this line // client, err := createClient_onPrem() if err != nil { fmt.Printf("cannot create NoSQL client: %v\n", err) diff --git a/examples-nosql-go-sdk/sqlexamples/GroupSortData.go b/examples-nosql-go-sdk/sqlexamples/GroupSortData.go new file mode 100644 index 0000000..c3624d0 --- /dev/null +++ b/examples-nosql-go-sdk/sqlexamples/GroupSortData.go @@ -0,0 +1,496 @@ +// Copyright (c) 2023, 2024 Oracle and/or its affiliates. +// Licensed under the Universal Permissive License v 1.0 as shown at +// https://oss.oracle.com/licenses/upl/ +package main + +import ( + "fmt" + "time" + + "github.com/oracle/nosql-go-sdk/nosqldb" + "github.com/oracle/nosql-go-sdk/nosqldb/auth/iam" + "github.com/oracle/nosql-go-sdk/nosqldb/common" + "github.com/oracle/nosql-go-sdk/nosqldb/jsonutil" + "github.com/oracle/nosql-go-sdk/nosqldb/types" +) + +// Creates a client with the supplied configurations. +// This function encapsulates environmental differences and returns a +// client handle to use for data operations. +func createClient_cloud() (*nosqldb.Client, error) { + var cfg nosqldb.Config + // replace the placeholder with your actual region identifier + region := "" + // Replace the placeholders with the actual value of config file location + // and the ocid of your compartment + sp, err := iam.NewSignatureProviderFromFile("", "", "", "") + if err != nil { + return nil, fmt.Errorf("cannot create a Signature Provider: %v", err) + } + cfg = nosqldb.Config{ + Mode: "cloud", + Region: common.Region(region), + AuthorizationProvider: sp, + } + client, err := nosqldb.NewClient(cfg) + return client, err +} + +// Creates a client with the supplied configurations for onPremise database +func createClient_onPrem() (*nosqldb.Client, error) { + var cfg nosqldb.Config + // replace the placeholder with the fullname of your host + endpoint := "http://:8080" + cfg = nosqldb.Config{ + Endpoint: endpoint, + Mode: "onprem", + } + // If using a secure store, uncomment the lines below and pass the username, + // password of the store to Config + // cfg := nosqldb.Config{ + // Mode: "onprem", + // Username: "", + // Password: []byte(""), + // Specify InsecureSkipVerify + // HTTPConfig: httputil.HTTPConfig{ + // InsecureSkipVerify: true, + // }, + client, err := nosqldb.NewClient(cfg) + return client, err +} + +// Creates a table +func createTable(client *nosqldb.Client, err error, tableName string)(){ + stmt := fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s ("+ + "ticketNo LONG," + + "fullName STRING," + + "gender STRING," + + "contactPhone STRING," + + "confNo STRING ," + + "bagInfo JSON, " + + "PRIMARY KEY(ticketNo))",tableName) + tableReq := &nosqldb.TableRequest{ + Statement: stmt, + TableLimits: &nosqldb.TableLimits{ + ReadUnits: 20, + WriteUnits: 20, + StorageGB: 1, + }, + } + tableRes, err := client.DoTableRequest(tableReq) + if err != nil { + fmt.Printf("cannot initiate CREATE TABLE request: %v\n", err) + return + } + // The create table request is asynchronous, wait for table creation + // to complete. + _, err = tableRes.WaitForCompletion(client, 60*time.Second, time.Second) + if err != nil { + fmt.Printf("Error finishing CREATE TABLE request: %v\n", err) + return + } + fmt.Println("Created table ", tableName) + return +} + +//Add data to table +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("Put row: %v\nresult: %v\n", putReq.Value.Map(), putRes) +} +//fetch data from the table +func fetchData(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("Query 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())) + } +} + +func main() { + //if using cloud service uncomment the line below + client, err := createClient_cloud() + //if using onPrem uncomment the line below + //client, err := createClient_onPrem() + if err != nil { + fmt.Printf("cannot create NoSQL client: %v\n", err) + return + } + defer client.Close() + tableName := "BaggageInfo" + createTable(client, err,tableName) + //adding data + value, err := types.NewMapValueFromJSON(`{ + "ticketNo":"1762376407826", + "fullName":"Dierdre Amador", + "gender":"M", + "contactPhone":"165-742-5715", + "confNo":"ZG8Z5N", + "bagInfo":[ { + "id" : "7903989918469", + "tagNum" : "17657806240229", + "routing" : "JFK/MAD", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "MAD", + "flightLegs" : [ { + "flightNo" : "BM495", + "flightDate" : "2019-03-07T07:00:00Z", + "fltRouteSrc" : "JFK", + "fltRouteDest" : "MAD", + "estimatedArrival" : "2019-03-07T14:00:00Z", + "actions" : [ { + "actionAt" : "MAD", + "actionCode" : "Offload to Carousel at MAD", + "actionTime" : "2019-03-07T13:54:00Z" + }, { + "actionAt" : "JFK", + "actionCode" : "ONLOAD to MAD", + "actionTime" : "2019-03-07T07:00:00Z" + }, { + "actionAt" : "JFK", + "actionCode" : "BagTag Scan at JFK", + "actionTime" : "2019-03-07T06:53:00Z" + }, { + "actionAt" : "JFK", + "actionCode" : "Checkin at JFK", + "actionTime" : "2019-03-07T05:03:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-03-07T13:51:00Z", + "bagArrivalDate" : "2019-03-07T13:51:00Z" + } ] + }`) + insertData(client, err,tableName,value) + value1, err := types.NewMapValueFromJSON(`{ + "ticketNo":"1762344493810", + "fullName":"Adam Phillips", + "gender":"M", + "contactPhone":"893-324-1064", + "confNo":"LE6J4Z", + "bagInfo":[ { + "id" : "79039899165297", + "tagNum" : "17657806255240", + "routing" : "MIA/LAX/MEL", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "MEL", + "flightLegs" : [ { + "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" + } ] + }, { + "flightNo" : "BM667", + "flightDate" : "2019-02-01T06:13:00Z", + "fltRouteSrc" : "LAX", + "fltRouteDest" : "MEL", + "estimatedArrival" : "2019-02-01T16:15:00Z", + "actions" : [ { + "actionAt" : "MEL", + "actionCode" : "Offload to Carousel at MEL", + "actionTime" : "2019-02-01T16:15:00Z" + }, { + "actionAt" : "LAX", + "actionCode" : "ONLOAD to MEL", + "actionTime" : "2019-02-01T15:35:00Z" + }, { + "actionAt" : "LAX", + "actionCode" : "OFFLOAD from LAX", + "actionTime" : "2019-02-01T15:18:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-02-01T16:13:00Z", + "bagArrivalDate" : "2019-02-01T16:13:00Z" + } ] + }`) + insertData(client, err,tableName,value1) + value2, err := types.NewMapValueFromJSON(`{ + "ticketNo":"1762341772625", + "fullName":"Gerard Greene", + "gender":"M", + "contactPhone":"395-837-3772", + "confNo":"MC0E7R", + "bagInfo":[ { + "id" : "79039899152842", + "tagNum" : "1765780626568", + "routing" : "SFO/IST/ATH/JTR", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "JTR", + "flightLegs" : [ { + "flightNo" : "BM318", + "flightDate" : "2019-03-07T04:00:00Z", + "fltRouteSrc" : "SFO", + "fltRouteDest" : "IST", + "estimatedArrival" : "2019-03-07T17:00:00Z", + "actions" : [ { + "actionAt" : "SFO", + "actionCode" : "ONLOAD to IST", + "actionTime" : "2019-03-07T04:08:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "BagTag Scan at SFO", + "actionTime" : "2019-03-07T03:53:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "Checkin at SFO", + "actionTime" : "2019-03-07T02:20:00Z" + } ] + }, { + "flightNo" : "BM696", + "flightDate" : "2019-03-07T05:08:00Z", + "fltRouteSrc" : "IST", + "fltRouteDest" : "ATH", + "estimatedArrival" : "2019-03-08T04:10:00Z", + "actions" : [ { + "actionAt" : "IST", + "actionCode" : "ONLOAD to ATH", + "actionTime" : "2019-03-08T04:55:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "BagTag Scan at IST", + "actionTime" : "2019-03-08T04:34:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "OFFLOAD from IST", + "actionTime" : "2019-03-08T04:47:00Z" + } ] + }, { + "flightNo" : "BM665", + "flightDate" : "2019-03-07T04:08:00Z", + "fltRouteSrc" : "ATH", + "fltRouteDest" : "JTR", + "estimatedArrival" : "2019-03-07T16:10:00Z", + "actions" : [ { + "actionAt" : "JTR", + "actionCode" : "Offload to Carousel at JTR", + "actionTime" : "2019-03-07T16:09:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "ONLOAD to JTR", + "actionTime" : "2019-03-07T15:51:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "OFFLOAD from ATH", + "actionTime" : "2019-03-07T15:43:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-03-07T16:01:00Z", + "bagArrivalDate" : "2019-03-07T16:01:00Z" + } ] + }`) + insertData(client, err,tableName,value2) + value3, err := types.NewMapValueFromJSON(`{ + "ticketNo":"1762320369957", + "fullName":"Lorenzo Phil", + "gender":"M", + "contactPhone":"364-610-4444", + "confNo":"QI3V6Q", + "bagInfo":[ { + "id" : "79039899187755", + "tagNum" : "17657806240001", + "routing" : "SFO/IST/ATH/JTR", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "JTR", + "flightLegs" : [ { + "flightNo" : "BM318", + "flightDate" : "2019-03-12T03:00:00Z", + "fltRouteSrc" : "SFO", + "fltRouteDest" : "IST", + "estimatedArrival" : "2019-03-12T16:00:00Z", + "actions" : [ { + "actionAt" : "SFO", + "actionCode" : "ONLOAD to IST", + "actionTime" : "2019-03-12T03:11:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "BagTag Scan at SFO", + "actionTime" : "2019-03-12T02:49:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "Checkin at SFO", + "actionTime" : "2019-03-12T01:50:00Z" + } ] + }, { + "flightNo" : "BM696", + "flightDate" : "2019-03-12T04:11:00Z", + "fltRouteSrc" : "IST", + "fltRouteDest" : "ATH", + "estimatedArrival" : "2019-03-13T03:14:00Z", + "actions" : [ { + "actionAt" : "IST", + "actionCode" : "ONLOAD to ATH", + "actionTime" : "2019-03-13T04:10:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "BagTag Scan at IST", + "actionTime" : "2019-03-13T03:56:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "OFFLOAD from IST", + "actionTime" : "2019-03-13T03:59:00Z" + } ] + }, { + "flightNo" : "BM665", + "flightDate" : "2019-03-12T03:11:00Z", + "fltRouteSrc" : "ATH", + "fltRouteDest" : "JTR", + "estimatedArrival" : "2019-03-12T15:12:00Z", + "actions" : [ { + "actionAt" : "JTR", + "actionCode" : "Offload to Carousel at JTR", + "actionTime" : "2019-03-12T15:06:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "ONLOAD to JTR", + "actionTime" : "2019-03-12T14:16:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "OFFLOAD from ATH", + "actionTime" : "2019-03-12T14:13:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-03-12T15:05:00Z", + "bagArrivalDate" : "2019-03-12T15:05:00Z" + }, + { + "id" : "79039899197755", + "tagNum" : "17657806340001", + "routing" : "SFO/IST/ATH/JTR", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "JTR", + "flightLegs" : [ { + "flightNo" : "BM318", + "flightDate" : "2019-03-12T03:00:00Z", + "fltRouteSrc" : "SFO", + "fltRouteDest" : "IST", + "estimatedArrival" : "2019-03-12T16:40:00Z", + "actions" : [ { + "actionAt" : "SFO", + "actionCode" : "ONLOAD to IST", + "actionTime" : "2019-03-12T03:14:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "BagTag Scan at SFO", + "actionTime" : "2019-03-12T02:50:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "Checkin at SFO", + "actionTime" : "2019-03-12T01:58:00Z" + } ] + }, { + "flightNo" : "BM696", + "flightDate" : "2019-03-12T04:11:00Z", + "fltRouteSrc" : "IST", + "fltRouteDest" : "ATH", + "estimatedArrival" : "2019-03-13T03:18:00Z", + "actions" : [ { + "actionAt" : "IST", + "actionCode" : "ONLOAD to ATH", + "actionTime" : "2019-03-13T04:17:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "BagTag Scan at IST", + "actionTime" : "2019-03-13T03:59:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "OFFLOAD from IST", + "actionTime" : "2019-03-13T03:48:00Z" + } ] + }, { + "flightNo" : "BM665", + "flightDate" : "2019-03-12T03:11:00Z", + "fltRouteSrc" : "ATH", + "fltRouteDest" : "JTR", + "estimatedArrival" : "2019-03-12T15:12:00Z", + "actions" : [ { + "actionAt" : "JTR", + "actionCode" : "Offload to Carousel at JTR", + "actionTime" : "2019-03-12T15:06:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "ONLOAD to JTR", + "actionTime" : "2019-03-12T14:16:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "OFFLOAD from ATH", + "actionTime" : "2019-03-12T14:23:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-03-12T16:05:00Z", + "bagArrivalDate" : "2019-03-12T16:25:00Z" + } ] + }`) + insertData(client, err,tableName,value3) + fmt.Printf("Put row succeeded: \n") + + orderby_stmt := `SELECT bag.fullName, bag.bagInfo[].tagNum,bag.bagInfo[].lastSeenTimeGmt FROM BaggageInfo bag + WHERE bag.bagInfo[].lastSeenStation=any "MEL" ORDER BY bag.bagInfo[].lastSeenTimeGmt DESC` + fmt.Printf("Using ORDER BY to sort data::\n") + fetchData(client, err,tableName,orderby_stmt) + + sortlimit_stmt := `SELECT $bag.fullName, $bag.bagInfo.tagNum, $flt_time FROM BaggageInfo $bag, + $bag.bagInfo[].lastSeenTimeGmt $flt_time + WHERE $bag.bagInfo[].lastSeenStation=any "JTR" + ORDER BY $flt_time LIMIT 4` + fmt.Printf("Using ORDER BY and LIMIT to sort and limit data::\n") + fetchData(client, err,tableName,sortlimit_stmt) + + groupsortlimit_stmt := `SELECT $flt_src as SOURCE, count(*) as COUNT FROM BaggageInfo $bag, + $bag.bagInfo.flightLegs[0].fltRouteSrc $flt_src GROUP BY $flt_src` + fmt.Printf("Using GROUP BY, ORDER BY and LIMIT to group, sort and limit data::\n") + fetchData(client, err,tableName,groupsortlimit_stmt) + + + +} diff --git a/examples-nosql-go-sdk/sqlexamples/Indexes.go b/examples-nosql-go-sdk/sqlexamples/Indexes.go index bf3561b..b4a56ca 100644 --- a/examples-nosql-go-sdk/sqlexamples/Indexes.go +++ b/examples-nosql-go-sdk/sqlexamples/Indexes.go @@ -1,5 +1,6 @@ // Copyright (c) 2023, 2024 Oracle and/or its affiliates. -// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/ +// Licensed under the Universal Permissive License v 1.0 as shown at +// https://oss.oracle.com/licenses/upl/ package main import ( @@ -18,7 +19,8 @@ func createClient_cloud() (*nosqldb.Client, error) { var cfg nosqldb.Config // replace the placeholder with your actual region identifier region := "" - // Replace the placeholders with the actual value of config file location and the ocid of your compartment + // Replace the placeholders with the actual value of config file location + // and the ocid of your compartment sp, err := iam.NewSignatureProviderFromFile("", "", "", "") if err != nil { return nil, fmt.Errorf("cannot create a Signature Provider: %v", err) @@ -41,7 +43,8 @@ func createClient_onPrem() (*nosqldb.Client, error) { Endpoint: endpoint, Mode: "onprem", } - // If using a secure store, uncomment the lines below and pass the username, password of the store to Config + // If using a secure store, uncomment the lines below and pass the username, + // password of the store to Config // cfg := nosqldb.Config{ // Mode: "onprem", // Username: "", @@ -75,7 +78,8 @@ func createTable(client *nosqldb.Client, err error, tableName string) { fmt.Printf("cannot initiate CREATE TABLE request: %v\n", err) return } - // The create table request is asynchronous, wait for table creation to complete. + // The create table request is asynchronous, wait for table creation + // to complete. _, err = tableRes.WaitForCompletion(client, 60*time.Second, time.Second) if err != nil { fmt.Printf("Error finishing CREATE TABLE request: %v\n", err) @@ -97,7 +101,8 @@ func createIndex(client *nosqldb.Client, err error, tableName string) { fmt.Printf("cannot initiate CREATE INDEX request: %v\n", err) return } - // The create index request is asynchronous, wait for index creation to complete. + // The create index request is asynchronous, wait for index creation + // to complete. _, err = tableRes.WaitForCompletion(client, 60*time.Second, time.Second) if err != nil { fmt.Printf("Error finishing CREATE INDEX request: %v\n", err) @@ -129,9 +134,11 @@ func dropIndex(client *nosqldb.Client, err error, tableName string) { } func main() { - // if using cloud service uncomment the line below. else if using onPremises comment this line out + // if using cloud service uncomment the line below. else if using onPremises + // comment this line out client, err := createClient_cloud() - // if using onPrem uncomment the line below, else if using cloud service, comment this line + // if using onPrem uncomment the line below, else if using cloud service, + // comment this line // client, err := createClient_onPrem() if err != nil { fmt.Printf("cannot create NoSQL client: %v\n", err) diff --git a/examples-nosql-go-sdk/sqlexamples/ModifyData.go b/examples-nosql-go-sdk/sqlexamples/ModifyData.go index 6a697ad..32421e9 100644 --- a/examples-nosql-go-sdk/sqlexamples/ModifyData.go +++ b/examples-nosql-go-sdk/sqlexamples/ModifyData.go @@ -1,5 +1,6 @@ // Copyright (c) 2023, 2024 Oracle and/or its affiliates. -// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/ +// Licensed under the Universal Permissive License v 1.0 as shown at +// https://oss.oracle.com/licenses/upl/ package main import ( @@ -19,7 +20,8 @@ func createClient_cloud() (*nosqldb.Client, error) { var cfg nosqldb.Config // replace the placeholder with your actual region identifier region := "" - // Replace the placeholders with the actual value of config file location and the ocid of your compartment + // Replace the placeholders with the actual value of config file location + // and the ocid of your compartment sp, err := iam.NewSignatureProviderFromFile("", "", "", "") if err != nil { return nil, fmt.Errorf("cannot create a Signature Provider: %v", err) @@ -42,7 +44,8 @@ func createClient_onPrem() (*nosqldb.Client, error) { Endpoint: endpoint, Mode: "onprem", } - // If using a secure store, uncomment the lines below and pass the username, password of the store to Config + // If using a secure store, uncomment the lines below and pass the username, + // password of the store to Config //cfg := nosqldb.Config{ // Mode: "onprem", // Username: "", @@ -76,7 +79,8 @@ func createTable(client *nosqldb.Client, err error, tableName string) { fmt.Printf("cannot initiate CREATE TABLE request: %v\n", err) return } - // The create table request is asynchronous, wait for table creation to complete. + // The create table request is asynchronous, wait for table creation + // to complete. _, err = tableRes.WaitForCompletion(client, 60*time.Second, time.Second) if err != nil { fmt.Printf("Error finishing CREATE TABLE request: %v\n", err) diff --git a/examples-nosql-go-sdk/sqlexamples/MultiDataOps.go b/examples-nosql-go-sdk/sqlexamples/MultiDataOps.go new file mode 100644 index 0000000..82b050b --- /dev/null +++ b/examples-nosql-go-sdk/sqlexamples/MultiDataOps.go @@ -0,0 +1,197 @@ +// Copyright (c) 2023, 2024 Oracle and/or its affiliates. +// Licensed under the Universal Permissive License v 1.0 as shown at +// https://oss.oracle.com/licenses/upl/ +package main + +import ( + "fmt" + "time" + "github.com/oracle/nosql-go-sdk/nosqldb" + "github.com/oracle/nosql-go-sdk/nosqldb/auth/iam" + "github.com/oracle/nosql-go-sdk/nosqldb/common" + "github.com/oracle/nosql-go-sdk/nosqldb/types" +) +// Creates a client with the supplied configurations. +// This function encapsulates environmental differences and returns a +// client handle to use for data operations. +func createClient_cloud() (*nosqldb.Client, error) { + var cfg nosqldb.Config + // replace the placeholder with your actual region identifier + region := "" + // Replace the placeholders with the actual value of config file location + // and the ocid of your compartment + sp, err := iam.NewSignatureProviderFromFile("", "", "", "") + if err != nil { + return nil, fmt.Errorf("cannot create a Signature Provider: %v", err) + } + cfg = nosqldb.Config{ + Mode: "cloud", + Region: common.Region(region), + AuthorizationProvider: sp, + } + client, err := nosqldb.NewClient(cfg) + return client, err +} + +// Creates a client with the supplied configurations for onPremise database +func createClient_onPrem() (*nosqldb.Client, error) { + var cfg nosqldb.Config + // replace the placeholder with the fullname of your host + endpoint := "http://:8080" + cfg = nosqldb.Config{ + Endpoint: endpoint, + Mode: "onprem", + } + // If using a secure store, uncomment the lines below and pass the username, + // password of the store to Config + // cfg := nosqldb.Config{ + // Mode: "onprem", + // Username: "", + // Password: []byte(""), + // Specify InsecureSkipVerify + // HTTPConfig: httputil.HTTPConfig{ + // InsecureSkipVerify: true, + // }, + client, err := nosqldb.NewClient(cfg) + return client, err +} + +// Creates a table +func createTable(client *nosqldb.Client, err error, tableName string)(){ + stmt := fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s ("+ + "id INTEGER," + + "address_line1 STRING," + + "address_line2 STRING," + + "pin INTEGER ," + + "PRIMARY KEY(SHARD(pin), id))",tableName) + tableReq := &nosqldb.TableRequest{ + Statement: stmt, + TableLimits: &nosqldb.TableLimits{ + ReadUnits: 20, + WriteUnits: 20, + StorageGB: 1, + }, + } + tableRes, err := client.DoTableRequest(tableReq) + if err != nil { + fmt.Printf("cannot initiate CREATE TABLE request: %v\n", err) + return + } + // The create table request is asynchronous, wait for table creation to complete. + _, err = tableRes.WaitForCompletion(client, 60*time.Second, time.Second) + if err != nil { + fmt.Printf("Error finishing CREATE TABLE request: %v\n", err) + return + } + fmt.Println("Created table ", tableName) + return +} +//Add data to table +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("Put row: result: %v\n", putRes) +} +//fetch data from the table +func fetchRowCnt(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("Query 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 + } + } + fmt.Printf("Number of query results is : %d\n", len(results)) + +} +//delete multiple rows +func delMulRows(client *nosqldb.Client, err error, tableName string,pinval int)(){ + shardKey := &types.MapValue{} + shardKey.Put("pin", pinval) + multiDelReq := &nosqldb.MultiDeleteRequest{ + TableName: tableName, + Key: shardKey, + } + multiDelRes, err := client.MultiDelete(multiDelReq) + if err != nil { + fmt.Printf("failed to delete multiple rows: %v", err) + return + } + fmt.Printf("MultiDelete result=%v\n", multiDelRes) +} + + +func main() { + //if using cloud service uncomment the line below + client, err := createClient_cloud() + //if using onPrem uncomment the line below + //client, err := createClient_onPrem() + if err != nil { + fmt.Printf("cannot create NoSQL client: %v\n", err) + return + } + defer client.Close() + tableName := "examplesAddress" + createTable(client, err,tableName) + //adding data + value, err := types.NewMapValueFromJSON(`{ + "id": 1, + "pin": 1234567, + "address_line1": "10 Red Street", + "address_line2": "Apt 3" + }`) + insertData(client, err,tableName,value) + value1, err := types.NewMapValueFromJSON(`{ + "id": 2, + "pin": 1234567, + "address_line1": "2 Green Street", + "address_line2": "Street 9" + }`) + insertData(client, err,tableName,value1) + value2, err := types.NewMapValueFromJSON(`{ + "id": 3, + "pin": 1234567, + "address_line1": "5 Blue Ave", + "address_line2": "Floor 2" + }`) + insertData(client, err,tableName,value2) + value3, err := types.NewMapValueFromJSON(`{ + "id": 4, + "pin": 87654321, + "address_line1": "9 Yellow Boulevard", + "address_line2": "Apt 3" + }`) + insertData(client, err,tableName,value3) + fmt.Printf("Put row succeeded: \n") + stmt1 := "select * from examplesAddress" + fetchRowCnt(client, err,tableName,stmt1) + delMulRows(client, err,tableName,1234567) + fetchRowCnt(client, err,tableName,stmt1) +} diff --git a/examples-nosql-go-sdk/sqlexamples/MultiWrite.go b/examples-nosql-go-sdk/sqlexamples/MultiWrite.go new file mode 100644 index 0000000..9bb0eee --- /dev/null +++ b/examples-nosql-go-sdk/sqlexamples/MultiWrite.go @@ -0,0 +1,217 @@ +// Copyright (c) 2023, 2024 Oracle and/or its affiliates. +// Licensed under the Universal Permissive License v 1.0 as shown at +// https://oss.oracle.com/licenses/upl/ +package main + +import ( + "fmt" + "time" + "github.com/oracle/nosql-go-sdk/nosqldb" + "github.com/oracle/nosql-go-sdk/nosqldb/auth/iam" + "github.com/oracle/nosql-go-sdk/nosqldb/common" + "github.com/oracle/nosql-go-sdk/nosqldb/jsonutil" + "github.com/oracle/nosql-go-sdk/nosqldb/types" +) + +// Creates a client with the supplied configurations. +// This function encapsulates environmental differences and returns a +// client handle to use for data operations. +func createClient_cloud() (*nosqldb.Client, error) { + var cfg nosqldb.Config + // replace the placeholder with your actual region identifier + region := "" + // Replace the placeholders with the actual value of config file location + // and the ocid of your compartment + sp, err := iam.NewSignatureProviderFromFile("", "", "", "") + if err != nil { + return nil, fmt.Errorf("cannot create a Signature Provider: %v", err) + } + cfg = nosqldb.Config{ + Mode: "cloud", + Region: common.Region(region), + AuthorizationProvider: sp, + } + client, err := nosqldb.NewClient(cfg) + return client, err +} +// Creates a client with the supplied configurations for onPremise database +func createClient_onPrem() (*nosqldb.Client, error) { + var cfg nosqldb.Config + // replace the placeholder with the fullname of your host + endpoint := "http://:8080" + cfg = nosqldb.Config{ + Endpoint: endpoint, + Mode: "onprem", + } + // If using a secure store, uncomment the lines below and pass the username, + // password of the store to Config + // cfg := nosqldb.Config{ + // Mode: "onprem", + // Username: "", + // Password: []byte(""), + // Specify InsecureSkipVerify + // HTTPConfig: httputil.HTTPConfig{ + // InsecureSkipVerify: true, + // }, + client, err := nosqldb.NewClient(cfg) + return client, err +} + +// Creates a table +func createTable(client *nosqldb.Client, err error, tableName string, sql_stmt string, reg_table string)(){ + if reg_table == "true"{ + tableReq := &nosqldb.TableRequest{ + Statement: sql_stmt, + TableLimits: &nosqldb.TableLimits{ + ReadUnits: 20, + WriteUnits: 20, + StorageGB: 1, + }, + } + tableRes, err := client.DoTableRequest(tableReq) + if err != nil { + fmt.Printf("cannot initiate CREATE TABLE request: %v\n", err) + return + } + // The create table request is asynchronous, wait for table creation + // to complete. + _, err = tableRes.WaitForCompletion(client, 60*time.Second, time.Second) + if err != nil { + fmt.Printf("Error finishing CREATE TABLE request: %v\n", err) + return + } + fmt.Println("Created table ", tableName) + return + } +} +//Add data to table +func addData(client *nosqldb.Client, err error, tableName string, row_data string)(){ + value, err := types.NewMapValueFromJSON(row_data) + if err != nil { + fmt.Errorf("failed to create value from JSON: %v", err) + return + } + putReq := &nosqldb.PutRequest{ + TableName: tableName, + Value: value, + } + putRes, err := client.Put(putReq) + if err != nil { + fmt.Errorf("failed to put a row: %v", err) + return + } + fmt.Printf("Put row from JSON: %v\nresult: %v\n", putReq.Value.Map(), (putRes)) +} + +//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") + } +} +func main() { + //if using cloud service uncomment the line below + client, err := createClient_cloud() + //if using onPrem uncomment the line below + //client, err := createClient_onPrem() + if err != nil { + fmt.Printf("cannot create NoSQL client: %v\n", err) + return + } + defer client.Close() + regTableName := "ticket" + childTableName := "ticket.bagInfo" + descTableName := "ticket.bagInfo.flightLegs" + + regtbl_crtstmt := fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s ("+ + "ticketNo LONG," + + "confNo STRING," + + "PRIMARY KEY(ticketNo))",regTableName) + + childtbl_crtstmt := fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s ("+ + "id LONG," + + "tagNum LONG," + + "routing STRING," + + "lastActionCode STRING," + + "lastActionDesc STRING," + + "lastSeenStation STRING," + + "lastSeenTimeGmt TIMESTAMP(4)," + + "bagArrivalDate TIMESTAMP(4)," + + "PRIMARY KEY(id))",childTableName) + + desctbl_crtstmt := fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s ("+ + "flightDate TIMESTAMP(4),"+ + "fltRouteSrc STRING,"+ + "fltRouteDest STRING,"+ + "estimatedArrival TIMESTAMP(4),"+ + "actions JSON,"+ + "PRIMARY KEY(flightNo))",descTableName) + + regtbl_data :=`{ + "ticketNo": "1762344493810", + "confNo" : "LE6J4Z" + }` + + childtbl_data :=`{ + "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" + }` + desctbl_data= :=`{ + "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" + } ] + }` + createTable(client, err,regTableName, regtbl_crtstmt, "true") + createTable(client, err,childTableName, childtbl_crtstmt, "false") + createTable(client, err,descTableName, desctbl_crtstmt, "false") + mul_write(client,err,regTableName, regtbl_data,childTableName,childtbl_data ) + addData(client,err,descTableName,desctbl_data) +} diff --git a/examples-nosql-go-sdk/sqlexamples/Namespaces.go b/examples-nosql-go-sdk/sqlexamples/Namespaces.go index 3fbf328..79260ce 100644 --- a/examples-nosql-go-sdk/sqlexamples/Namespaces.go +++ b/examples-nosql-go-sdk/sqlexamples/Namespaces.go @@ -1,5 +1,6 @@ // Copyright (c) 2023, 2024 Oracle and/or its affiliates. -// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/ +// Licensed under the Universal Permissive License v 1.0 as shown at +// https://oss.oracle.com/licenses/upl/ package main import ( @@ -18,7 +19,8 @@ func createClient_onPrem() (*nosqldb.Client, error) { Endpoint: endpoint, Mode: "onprem", } - // If using a secure store, uncomment the lines below and pass the username, password of the store to Config + // If using a secure store, uncomment the lines below and pass the username, + // password of the store to Config // cfg := nosqldb.Config{ // Mode: "onprem", // Username: "", diff --git a/examples-nosql-go-sdk/sqlexamples/QueryData.go b/examples-nosql-go-sdk/sqlexamples/QueryData.go index b57b52f..734dac5 100644 --- a/examples-nosql-go-sdk/sqlexamples/QueryData.go +++ b/examples-nosql-go-sdk/sqlexamples/QueryData.go @@ -1,5 +1,6 @@ // Copyright (c) 2023, 2024 Oracle and/or its affiliates. -// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/ +// Licensed under the Universal Permissive License v 1.0 as shown at +// https://oss.oracle.com/licenses/upl/ package main import ( @@ -19,7 +20,8 @@ func createClient_cloud() (*nosqldb.Client, error) { var cfg nosqldb.Config // replace the placeholder with your actual region identifier region := "" - // Replace the placeholders with the actual value of config file location and the ocid of your compartment + // Replace the placeholders with the actual value of config file location + // and the ocid of your compartment sp, err := iam.NewSignatureProviderFromFile("", "", "", "") if err != nil { return nil, fmt.Errorf("cannot create a Signature Provider: %v", err) @@ -42,7 +44,8 @@ func createClient_onPrem() (*nosqldb.Client, error) { Endpoint: endpoint, Mode: "onprem", } - // If using a secure store, uncomment the lines below and pass the username, password of the store to Config + // If using a secure store, uncomment the lines below and pass the username, + // password of the store to Config // cfg := nosqldb.Config{ // Mode: "onprem", // Username: "", @@ -76,7 +79,8 @@ func createTable(client *nosqldb.Client, err error, tableName string) { fmt.Printf("cannot initiate CREATE TABLE request: %v\n", err) return } - // The create table request is asynchronous, wait for table creation to complete. + // The create table request is asynchronous, wait for table creation + // to complete. _, err = tableRes.WaitForCompletion(client, 60*time.Second, time.Second) if err != nil { fmt.Printf("Error finishing CREATE TABLE request: %v\n", err) @@ -133,11 +137,32 @@ func fetchData(client *nosqldb.Client, err error, tableName string, querystmt st fmt.Printf("\t%d: %s\n", i+1, jsonutil.AsJSON(r.Map())) } } +//fetch data from the table +func getRow(client *nosqldb.Client, err error, tableName string, colName string, Id int)(){ + key:=&types.MapValue{} + key.Put(colName, Id) + req:=&nosqldb.GetRequest{ + TableName: tableName, + Key: key, + } + res, err:=client.Get(req) + if err != nil { + fmt.Printf("GetResults() failed: %v\n", err) + return + } + if res.RowExists() { + fmt.Printf("Got row: %v\n", res.ValueAsJSON()) + } else { + fmt.Printf("The row does not exist.\n") + } +} func main() { - // if using cloud service uncomment the line below. else if using onPremises comment this line out + // if using cloud service uncomment the line below. else if using onPremises + // comment this line out client, err := createClient_cloud() - // if using onPrem uncomment the line below, else if using cloud service, comment this line + // if using onPrem uncomment the line below, else if using cloud service, + // comment this line // client, err := createClient_onPrem() if err != nil { fmt.Printf("cannot create NoSQL client: %v\n", err) @@ -370,6 +395,8 @@ func main() { }`) insertData(client, err, tableName, value2) fmt.Printf("Put row succeeded: \n") + fmt.Printf("Fetch single row based on primary key: \n") + getRow(client, err,tableName,"acct_Id",2) fmt.Printf("Fetching all data.\n") stmt1 := "select * from stream_acct" fetchData(client, err, tableName, stmt1) diff --git a/examples-nosql-go-sdk/sqlexamples/Regions.go b/examples-nosql-go-sdk/sqlexamples/Regions.go index fc07a0f..044d55c 100644 --- a/examples-nosql-go-sdk/sqlexamples/Regions.go +++ b/examples-nosql-go-sdk/sqlexamples/Regions.go @@ -1,5 +1,6 @@ // Copyright (c) 2023, 2024 Oracle and/or its affiliates. -// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/ +// Licensed under the Universal Permissive License v 1.0 as shown at +// https://oss.oracle.com/licenses/upl/ package main import ( @@ -18,7 +19,8 @@ func createClient_onPrem() (*nosqldb.Client, error) { Endpoint: endpoint, Mode: "onprem", } - // If using a secure store, uncomment the lines below and pass the username, password of the store to Config + // If using a secure store, uncomment the lines below and pass the username, + // password of the store to Config // cfg := nosqldb.Config{ // Mode: "onprem", // Username: "", @@ -81,7 +83,8 @@ func crtTabInRegion(client *nosqldb.Client, err error, tableName string) { fmt.Printf("cannot initiate CREATE TABLE request: %v\n", err) return } - // The create table request is asynchronous, wait for table creation to complete. + // The create table request is asynchronous, wait for table creation + // to complete. _, err = tableRes.WaitForCompletion(client, 60*time.Second, time.Second) if err != nil { fmt.Printf("Error finishing CREATE TABLE request: %v\n", err) diff --git a/examples-nosql-go-sdk/sqlexamples/SQLExpressions.go b/examples-nosql-go-sdk/sqlexamples/SQLExpressions.go new file mode 100644 index 0000000..aeaf776 --- /dev/null +++ b/examples-nosql-go-sdk/sqlexamples/SQLExpressions.go @@ -0,0 +1,518 @@ +// Copyright (c) 2023, 2024 Oracle and/or its affiliates. +// Licensed under the Universal Permissive License v 1.0 as shown at +// https://oss.oracle.com/licenses/upl/ +package main + +import ( + "fmt" + "time" + + "github.com/oracle/nosql-go-sdk/nosqldb" + "github.com/oracle/nosql-go-sdk/nosqldb/auth/iam" + "github.com/oracle/nosql-go-sdk/nosqldb/common" + "github.com/oracle/nosql-go-sdk/nosqldb/jsonutil" + "github.com/oracle/nosql-go-sdk/nosqldb/types" +) + +// Creates a client with the supplied configurations. +// This function encapsulates environmental differences and returns a +// client handle to use for data operations. +func createClient_cloud() (*nosqldb.Client, error) { + var cfg nosqldb.Config + // replace the placeholder with your actual region identifier + region := "" + // Replace the placeholders with the actual value of config file location + // and the ocid of your compartment + sp, err := iam.NewSignatureProviderFromFile("", "", "", "") + if err != nil { + return nil, fmt.Errorf("cannot create a Signature Provider: %v", err) + } + cfg = nosqldb.Config{ + Mode: "cloud", + Region: common.Region(region), + AuthorizationProvider: sp, + } + client, err := nosqldb.NewClient(cfg) + return client, err +} + +// Creates a client with the supplied configurations for onPremise database +func createClient_onPrem() (*nosqldb.Client, error) { + var cfg nosqldb.Config + // replace the placeholder with the fullname of your host + endpoint := "http://:8080" + cfg = nosqldb.Config{ + Endpoint: endpoint, + Mode: "onprem", + } + // If using a secure store, uncomment the lines below and pass the username, + // password of the store to Config + // cfg := nosqldb.Config{ + // Mode: "onprem", + // Username: "", + // Password: []byte(""), + // Specify InsecureSkipVerify + // HTTPConfig: httputil.HTTPConfig{ + // InsecureSkipVerify: true, + // }, + client, err := nosqldb.NewClient(cfg) + return client, err +} + +// Creates a table +func createTable(client *nosqldb.Client, err error, tableName string)(){ + stmt := fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s ("+ + "ticketNo LONG," + + "fullName STRING," + + "gender STRING," + + "contactPhone STRING," + + "confNo STRING ," + + "bagInfo JSON, " + + "PRIMARY KEY(ticketNo))",tableName) + tableReq := &nosqldb.TableRequest{ + Statement: stmt, + TableLimits: &nosqldb.TableLimits{ + ReadUnits: 20, + WriteUnits: 20, + StorageGB: 1, + }, + } + tableRes, err := client.DoTableRequest(tableReq) + if err != nil { + fmt.Printf("cannot initiate CREATE TABLE request: %v\n", err) + return + } + // The create table request is asynchronous, wait for table creation + // to complete. + _, err = tableRes.WaitForCompletion(client, 60*time.Second, time.Second) + if err != nil { + fmt.Printf("Error finishing CREATE TABLE request: %v\n", err) + return + } + fmt.Println("Created table ", tableName) + return +} + +//Add data to table +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("Put row: %v\nresult: %v\n", putReq.Value.Map(), putRes) +} +//fetch data from the table +func fetchData(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("Query 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())) + } +} + +func main() { + //if using cloud service uncomment the line below + client, err := createClient_cloud() + //if using onPrem uncomment the line below + //client, err := createClient_onPrem() + if err != nil { + fmt.Printf("cannot create NoSQL client: %v\n", err) + return + } + defer client.Close() + tableName := "BaggageInfo" + createTable(client, err,tableName) + //adding data + value, err := types.NewMapValueFromJSON(`{ + "ticketNo":"1762376407826", + "fullName":"Dierdre Amador", + "gender":"M", + "contactPhone":"165-742-5715", + "confNo":"ZG8Z5N", + "bagInfo":[ { + "id" : "7903989918469", + "tagNum" : "17657806240229", + "routing" : "JFK/MAD", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "MAD", + "flightLegs" : [ { + "flightNo" : "BM495", + "flightDate" : "2019-03-07T07:00:00Z", + "fltRouteSrc" : "JFK", + "fltRouteDest" : "MAD", + "estimatedArrival" : "2019-03-07T14:00:00Z", + "actions" : [ { + "actionAt" : "MAD", + "actionCode" : "Offload to Carousel at MAD", + "actionTime" : "2019-03-07T13:54:00Z" + }, { + "actionAt" : "JFK", + "actionCode" : "ONLOAD to MAD", + "actionTime" : "2019-03-07T07:00:00Z" + }, { + "actionAt" : "JFK", + "actionCode" : "BagTag Scan at JFK", + "actionTime" : "2019-03-07T06:53:00Z" + }, { + "actionAt" : "JFK", + "actionCode" : "Checkin at JFK", + "actionTime" : "2019-03-07T05:03:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-03-07T13:51:00Z", + "bagArrivalDate" : "2019-03-07T13:51:00Z" + } ] + }`) + insertData(client, err,tableName,value) + value1, err := types.NewMapValueFromJSON(`{ + "ticketNo":"1762344493810", + "fullName":"Adam Phillips", + "gender":"M", + "contactPhone":"893-324-1064", + "confNo":"LE6J4Z", + "bagInfo":[ { + "id" : "79039899165297", + "tagNum" : "17657806255240", + "routing" : "MIA/LAX/MEL", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "MEL", + "flightLegs" : [ { + "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" + } ] + }, { + "flightNo" : "BM667", + "flightDate" : "2019-02-01T06:13:00Z", + "fltRouteSrc" : "LAX", + "fltRouteDest" : "MEL", + "estimatedArrival" : "2019-02-01T16:15:00Z", + "actions" : [ { + "actionAt" : "MEL", + "actionCode" : "Offload to Carousel at MEL", + "actionTime" : "2019-02-01T16:15:00Z" + }, { + "actionAt" : "LAX", + "actionCode" : "ONLOAD to MEL", + "actionTime" : "2019-02-01T15:35:00Z" + }, { + "actionAt" : "LAX", + "actionCode" : "OFFLOAD from LAX", + "actionTime" : "2019-02-01T15:18:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-02-01T16:13:00Z", + "bagArrivalDate" : "2019-02-01T16:13:00Z" + } ] + }`) + insertData(client, err,tableName,value1) + value2, err := types.NewMapValueFromJSON(`{ + "ticketNo":"1762341772625", + "fullName":"Gerard Greene", + "gender":"M", + "contactPhone":"395-837-3772", + "confNo":"MC0E7R", + "bagInfo":[ { + "id" : "79039899152842", + "tagNum" : "1765780626568", + "routing" : "SFO/IST/ATH/JTR", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "JTR", + "flightLegs" : [ { + "flightNo" : "BM318", + "flightDate" : "2019-03-07T04:00:00Z", + "fltRouteSrc" : "SFO", + "fltRouteDest" : "IST", + "estimatedArrival" : "2019-03-07T17:00:00Z", + "actions" : [ { + "actionAt" : "SFO", + "actionCode" : "ONLOAD to IST", + "actionTime" : "2019-03-07T04:08:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "BagTag Scan at SFO", + "actionTime" : "2019-03-07T03:53:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "Checkin at SFO", + "actionTime" : "2019-03-07T02:20:00Z" + } ] + }, { + "flightNo" : "BM696", + "flightDate" : "2019-03-07T05:08:00Z", + "fltRouteSrc" : "IST", + "fltRouteDest" : "ATH", + "estimatedArrival" : "2019-03-08T04:10:00Z", + "actions" : [ { + "actionAt" : "IST", + "actionCode" : "ONLOAD to ATH", + "actionTime" : "2019-03-08T04:55:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "BagTag Scan at IST", + "actionTime" : "2019-03-08T04:34:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "OFFLOAD from IST", + "actionTime" : "2019-03-08T04:47:00Z" + } ] + }, { + "flightNo" : "BM665", + "flightDate" : "2019-03-07T04:08:00Z", + "fltRouteSrc" : "ATH", + "fltRouteDest" : "JTR", + "estimatedArrival" : "2019-03-07T16:10:00Z", + "actions" : [ { + "actionAt" : "JTR", + "actionCode" : "Offload to Carousel at JTR", + "actionTime" : "2019-03-07T16:09:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "ONLOAD to JTR", + "actionTime" : "2019-03-07T15:51:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "OFFLOAD from ATH", + "actionTime" : "2019-03-07T15:43:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-03-07T16:01:00Z", + "bagArrivalDate" : "2019-03-07T16:01:00Z" + } ] + }`) + insertData(client, err,tableName,value2) + value3, err := types.NewMapValueFromJSON(`{ + "ticketNo":"1762320369957", + "fullName":"Lorenzo Phil", + "gender":"M", + "contactPhone":"364-610-4444", + "confNo":"QI3V6Q", + "bagInfo":[ { + "id" : "79039899187755", + "tagNum" : "17657806240001", + "routing" : "SFO/IST/ATH/JTR", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "JTR", + "flightLegs" : [ { + "flightNo" : "BM318", + "flightDate" : "2019-03-12T03:00:00Z", + "fltRouteSrc" : "SFO", + "fltRouteDest" : "IST", + "estimatedArrival" : "2019-03-12T16:00:00Z", + "actions" : [ { + "actionAt" : "SFO", + "actionCode" : "ONLOAD to IST", + "actionTime" : "2019-03-12T03:11:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "BagTag Scan at SFO", + "actionTime" : "2019-03-12T02:49:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "Checkin at SFO", + "actionTime" : "2019-03-12T01:50:00Z" + } ] + }, { + "flightNo" : "BM696", + "flightDate" : "2019-03-12T04:11:00Z", + "fltRouteSrc" : "IST", + "fltRouteDest" : "ATH", + "estimatedArrival" : "2019-03-13T03:14:00Z", + "actions" : [ { + "actionAt" : "IST", + "actionCode" : "ONLOAD to ATH", + "actionTime" : "2019-03-13T04:10:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "BagTag Scan at IST", + "actionTime" : "2019-03-13T03:56:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "OFFLOAD from IST", + "actionTime" : "2019-03-13T03:59:00Z" + } ] + }, { + "flightNo" : "BM665", + "flightDate" : "2019-03-12T03:11:00Z", + "fltRouteSrc" : "ATH", + "fltRouteDest" : "JTR", + "estimatedArrival" : "2019-03-12T15:12:00Z", + "actions" : [ { + "actionAt" : "JTR", + "actionCode" : "Offload to Carousel at JTR", + "actionTime" : "2019-03-12T15:06:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "ONLOAD to JTR", + "actionTime" : "2019-03-12T14:16:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "OFFLOAD from ATH", + "actionTime" : "2019-03-12T14:13:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-03-12T15:05:00Z", + "bagArrivalDate" : "2019-03-12T15:05:00Z" + }, + { + "id" : "79039899197755", + "tagNum" : "17657806340001", + "routing" : "SFO/IST/ATH/JTR", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "JTR", + "flightLegs" : [ { + "flightNo" : "BM318", + "flightDate" : "2019-03-12T03:00:00Z", + "fltRouteSrc" : "SFO", + "fltRouteDest" : "IST", + "estimatedArrival" : "2019-03-12T16:40:00Z", + "actions" : [ { + "actionAt" : "SFO", + "actionCode" : "ONLOAD to IST", + "actionTime" : "2019-03-12T03:14:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "BagTag Scan at SFO", + "actionTime" : "2019-03-12T02:50:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "Checkin at SFO", + "actionTime" : "2019-03-12T01:58:00Z" + } ] + }, { + "flightNo" : "BM696", + "flightDate" : "2019-03-12T04:11:00Z", + "fltRouteSrc" : "IST", + "fltRouteDest" : "ATH", + "estimatedArrival" : "2019-03-13T03:18:00Z", + "actions" : [ { + "actionAt" : "IST", + "actionCode" : "ONLOAD to ATH", + "actionTime" : "2019-03-13T04:17:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "BagTag Scan at IST", + "actionTime" : "2019-03-13T03:59:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "OFFLOAD from IST", + "actionTime" : "2019-03-13T03:48:00Z" + } ] + }, { + "flightNo" : "BM665", + "flightDate" : "2019-03-12T03:11:00Z", + "fltRouteSrc" : "ATH", + "fltRouteDest" : "JTR", + "estimatedArrival" : "2019-03-12T15:12:00Z", + "actions" : [ { + "actionAt" : "JTR", + "actionCode" : "Offload to Carousel at JTR", + "actionTime" : "2019-03-12T15:06:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "ONLOAD to JTR", + "actionTime" : "2019-03-12T14:16:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "OFFLOAD from ATH", + "actionTime" : "2019-03-12T14:23:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-03-12T16:05:00Z", + "bagArrivalDate" : "2019-03-12T16:25:00Z" + } ] + }`) + insertData(client, err,tableName,value3) + fmt.Printf("Put row succeeded: \n") + + paran_expr := `SELECT fullName, bag.bagInfo.tagNum, bag.bagInfo.routing, + bag.bagInfo[].flightLegs[].fltRouteDest FROM BaggageInfo bag + WHERE bag.bagInfo.flightLegs[].fltRouteSrc=any "SFO" AND + (bag.bagInfo[].flightLegs[].fltRouteDest=any "ATH" OR + bag.bagInfo[].flightLegs[].fltRouteDest=any "JTR" )` + fmt.Printf("Using Paranthesized expression:\n") + fetchData(client, err,tableName,paran_expr) + + case_expr := `SELECT fullName, + CASE + WHEN NOT exists bag.bagInfo.flightLegs[0] + THEN "you have no bag info" + WHEN NOT exists bag.bagInfo.flightLegs[1] + THEN "you have one hop" + WHEN NOT exists bag.bagInfo.flightLegs[2] + THEN "you have two hops." + ELSE "you have three hops." + END AS NUMBER_HOPS + FROM BaggageInfo bag WHERE ticketNo=1762341772625` + fmt.Printf("Using Case Expression:\n") + fetchData(client, err,tableName,case_expr) + + seq_trn_expr := `SELECT seq_transform(l.bagInfo[], + seq_transform( + $sq1.flightLegs[], + seq_transform( + $sq2.actions[], + { + "at" : $sq3.actionAt, + "action" : $sq3.actionCode, + "flightNo" : $sq2.flightNo, + "tagNum" : $sq1.tagNum + } + ) + ) + ) AS actions FROM baggageInfo l WHERE ticketNo=1762376407826` + fmt.Printf("Using Sequence Transform Expressions:\n") + fetchData(client, err,tableName,seq_trn_expr) + + + +} diff --git a/examples-nosql-go-sdk/sqlexamples/SQLFunctions.go b/examples-nosql-go-sdk/sqlexamples/SQLFunctions.go new file mode 100644 index 0000000..d1fc5db --- /dev/null +++ b/examples-nosql-go-sdk/sqlexamples/SQLFunctions.go @@ -0,0 +1,506 @@ +// Copyright (c) 2023, 2024 Oracle and/or its affiliates. +// Licensed under the Universal Permissive License v 1.0 as shown at +// https://oss.oracle.com/licenses/upl/ +package main + +import ( + "fmt" + "time" + + "github.com/oracle/nosql-go-sdk/nosqldb" + "github.com/oracle/nosql-go-sdk/nosqldb/auth/iam" + "github.com/oracle/nosql-go-sdk/nosqldb/common" + "github.com/oracle/nosql-go-sdk/nosqldb/jsonutil" + "github.com/oracle/nosql-go-sdk/nosqldb/types" +) + +// Creates a client with the supplied configurations. +// This function encapsulates environmental differences and returns a +// client handle to use for data operations. +func createClient_cloud() (*nosqldb.Client, error) { + var cfg nosqldb.Config + // replace the placeholder with your actual region identifier + region := "" + // Replace the placeholders with the actual value of config file location + // and the ocid of your compartment + sp, err := iam.NewSignatureProviderFromFile("", "", "", "") + if err != nil { + return nil, fmt.Errorf("cannot create a Signature Provider: %v", err) + } + cfg = nosqldb.Config{ + Mode: "cloud", + Region: common.Region(region), + AuthorizationProvider: sp, + } + client, err := nosqldb.NewClient(cfg) + return client, err +} + +// Creates a client with the supplied configurations for onPremise database +func createClient_onPrem() (*nosqldb.Client, error) { + var cfg nosqldb.Config + // replace the placeholder with the fullname of your host + endpoint := "http://:8080" + cfg = nosqldb.Config{ + Endpoint: endpoint, + Mode: "onprem", + } + // If using a secure store, uncomment the lines below and pass the username, + // password of the store to Config + // cfg := nosqldb.Config{ + // Mode: "onprem", + // Username: "", + // Password: []byte(""), + // Specify InsecureSkipVerify + // HTTPConfig: httputil.HTTPConfig{ + // InsecureSkipVerify: true, + // }, + client, err := nosqldb.NewClient(cfg) + return client, err +} + +// Creates a table +func createTable(client *nosqldb.Client, err error, tableName string)(){ + stmt := fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s ("+ + "ticketNo LONG," + + "fullName STRING," + + "gender STRING," + + "contactPhone STRING," + + "confNo STRING ," + + "bagInfo JSON, " + + "PRIMARY KEY(ticketNo))",tableName) + tableReq := &nosqldb.TableRequest{ + Statement: stmt, + TableLimits: &nosqldb.TableLimits{ + ReadUnits: 20, + WriteUnits: 20, + StorageGB: 1, + }, + } + tableRes, err := client.DoTableRequest(tableReq) + if err != nil { + fmt.Printf("cannot initiate CREATE TABLE request: %v\n", err) + return + } + // The create table request is asynchronous, wait for table creation + // to complete. + _, err = tableRes.WaitForCompletion(client, 60*time.Second, time.Second) + if err != nil { + fmt.Printf("Error finishing CREATE TABLE request: %v\n", err) + return + } + fmt.Println("Created table ", tableName) + return +} + +//Add data to table +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("Put row: %v\nresult: %v\n", putReq.Value.Map(), putRes) +} + +//fetch data from the table +func fetchData(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("Query 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())) + } +} + +func main() { + //if using cloud service uncomment the line below + client, err := createClient_cloud() + //if using onPrem uncomment the line below + //client, err := createClient_onPrem() + if err != nil { + fmt.Printf("cannot create NoSQL client: %v\n", err) + return + } + defer client.Close() + tableName := "BaggageInfo" + createTable(client, err,tableName) + //adding data + value, err := types.NewMapValueFromJSON(`{ + "ticketNo":"1762376407826", + "fullName":"Dierdre Amador", + "gender":"M", + "contactPhone":"165-742-5715", + "confNo":"ZG8Z5N", + "bagInfo":[ { + "id" : "7903989918469", + "tagNum" : "17657806240229", + "routing" : "JFK/MAD", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "MAD", + "flightLegs" : [ { + "flightNo" : "BM495", + "flightDate" : "2019-03-07T07:00:00Z", + "fltRouteSrc" : "JFK", + "fltRouteDest" : "MAD", + "estimatedArrival" : "2019-03-07T14:00:00Z", + "actions" : [ { + "actionAt" : "MAD", + "actionCode" : "Offload to Carousel at MAD", + "actionTime" : "2019-03-07T13:54:00Z" + }, { + "actionAt" : "JFK", + "actionCode" : "ONLOAD to MAD", + "actionTime" : "2019-03-07T07:00:00Z" + }, { + "actionAt" : "JFK", + "actionCode" : "BagTag Scan at JFK", + "actionTime" : "2019-03-07T06:53:00Z" + }, { + "actionAt" : "JFK", + "actionCode" : "Checkin at JFK", + "actionTime" : "2019-03-07T05:03:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-03-07T13:51:00Z", + "bagArrivalDate" : "2019-03-07T13:51:00Z" + } ] + }`) + insertData(client, err,tableName,value) + value1, err := types.NewMapValueFromJSON(`{ + "ticketNo":"1762344493810", + "fullName":"Adam Phillips", + "gender":"M", + "contactPhone":"893-324-1064", + "confNo":"LE6J4Z", + "bagInfo":[ { + "id" : "79039899165297", + "tagNum" : "17657806255240", + "routing" : "MIA/LAX/MEL", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "MEL", + "flightLegs" : [ { + "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" + } ] + }, { + "flightNo" : "BM667", + "flightDate" : "2019-02-01T06:13:00Z", + "fltRouteSrc" : "LAX", + "fltRouteDest" : "MEL", + "estimatedArrival" : "2019-02-01T16:15:00Z", + "actions" : [ { + "actionAt" : "MEL", + "actionCode" : "Offload to Carousel at MEL", + "actionTime" : "2019-02-01T16:15:00Z" + }, { + "actionAt" : "LAX", + "actionCode" : "ONLOAD to MEL", + "actionTime" : "2019-02-01T15:35:00Z" + }, { + "actionAt" : "LAX", + "actionCode" : "OFFLOAD from LAX", + "actionTime" : "2019-02-01T15:18:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-02-01T16:13:00Z", + "bagArrivalDate" : "2019-02-01T16:13:00Z" + } ] + }`) + insertData(client, err,tableName,value1) + value2, err := types.NewMapValueFromJSON(`{ + "ticketNo":"1762341772625", + "fullName":"Gerard Greene", + "gender":"M", + "contactPhone":"395-837-3772", + "confNo":"MC0E7R", + "bagInfo":[ { + "id" : "79039899152842", + "tagNum" : "1765780626568", + "routing" : "SFO/IST/ATH/JTR", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "JTR", + "flightLegs" : [ { + "flightNo" : "BM318", + "flightDate" : "2019-03-07T04:00:00Z", + "fltRouteSrc" : "SFO", + "fltRouteDest" : "IST", + "estimatedArrival" : "2019-03-07T17:00:00Z", + "actions" : [ { + "actionAt" : "SFO", + "actionCode" : "ONLOAD to IST", + "actionTime" : "2019-03-07T04:08:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "BagTag Scan at SFO", + "actionTime" : "2019-03-07T03:53:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "Checkin at SFO", + "actionTime" : "2019-03-07T02:20:00Z" + } ] + }, { + "flightNo" : "BM696", + "flightDate" : "2019-03-07T05:08:00Z", + "fltRouteSrc" : "IST", + "fltRouteDest" : "ATH", + "estimatedArrival" : "2019-03-08T04:10:00Z", + "actions" : [ { + "actionAt" : "IST", + "actionCode" : "ONLOAD to ATH", + "actionTime" : "2019-03-08T04:55:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "BagTag Scan at IST", + "actionTime" : "2019-03-08T04:34:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "OFFLOAD from IST", + "actionTime" : "2019-03-08T04:47:00Z" + } ] + }, { + "flightNo" : "BM665", + "flightDate" : "2019-03-07T04:08:00Z", + "fltRouteSrc" : "ATH", + "fltRouteDest" : "JTR", + "estimatedArrival" : "2019-03-07T16:10:00Z", + "actions" : [ { + "actionAt" : "JTR", + "actionCode" : "Offload to Carousel at JTR", + "actionTime" : "2019-03-07T16:09:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "ONLOAD to JTR", + "actionTime" : "2019-03-07T15:51:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "OFFLOAD from ATH", + "actionTime" : "2019-03-07T15:43:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-03-07T16:01:00Z", + "bagArrivalDate" : "2019-03-07T16:01:00Z" + } ] + }`) + insertData(client, err,tableName,value2) + value3, err := types.NewMapValueFromJSON(`{ + "ticketNo":"1762320369957", + "fullName":"Lorenzo Phil", + "gender":"M", + "contactPhone":"364-610-4444", + "confNo":"QI3V6Q", + "bagInfo":[ { + "id" : "79039899187755", + "tagNum" : "17657806240001", + "routing" : "SFO/IST/ATH/JTR", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "JTR", + "flightLegs" : [ { + "flightNo" : "BM318", + "flightDate" : "2019-03-12T03:00:00Z", + "fltRouteSrc" : "SFO", + "fltRouteDest" : "IST", + "estimatedArrival" : "2019-03-12T16:00:00Z", + "actions" : [ { + "actionAt" : "SFO", + "actionCode" : "ONLOAD to IST", + "actionTime" : "2019-03-12T03:11:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "BagTag Scan at SFO", + "actionTime" : "2019-03-12T02:49:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "Checkin at SFO", + "actionTime" : "2019-03-12T01:50:00Z" + } ] + }, { + "flightNo" : "BM696", + "flightDate" : "2019-03-12T04:11:00Z", + "fltRouteSrc" : "IST", + "fltRouteDest" : "ATH", + "estimatedArrival" : "2019-03-13T03:14:00Z", + "actions" : [ { + "actionAt" : "IST", + "actionCode" : "ONLOAD to ATH", + "actionTime" : "2019-03-13T04:10:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "BagTag Scan at IST", + "actionTime" : "2019-03-13T03:56:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "OFFLOAD from IST", + "actionTime" : "2019-03-13T03:59:00Z" + } ] + }, { + "flightNo" : "BM665", + "flightDate" : "2019-03-12T03:11:00Z", + "fltRouteSrc" : "ATH", + "fltRouteDest" : "JTR", + "estimatedArrival" : "2019-03-12T15:12:00Z", + "actions" : [ { + "actionAt" : "JTR", + "actionCode" : "Offload to Carousel at JTR", + "actionTime" : "2019-03-12T15:06:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "ONLOAD to JTR", + "actionTime" : "2019-03-12T14:16:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "OFFLOAD from ATH", + "actionTime" : "2019-03-12T14:13:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-03-12T15:05:00Z", + "bagArrivalDate" : "2019-03-12T15:05:00Z" + }, + { + "id" : "79039899197755", + "tagNum" : "17657806340001", + "routing" : "SFO/IST/ATH/JTR", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "JTR", + "flightLegs" : [ { + "flightNo" : "BM318", + "flightDate" : "2019-03-12T03:00:00Z", + "fltRouteSrc" : "SFO", + "fltRouteDest" : "IST", + "estimatedArrival" : "2019-03-12T16:40:00Z", + "actions" : [ { + "actionAt" : "SFO", + "actionCode" : "ONLOAD to IST", + "actionTime" : "2019-03-12T03:14:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "BagTag Scan at SFO", + "actionTime" : "2019-03-12T02:50:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "Checkin at SFO", + "actionTime" : "2019-03-12T01:58:00Z" + } ] + }, { + "flightNo" : "BM696", + "flightDate" : "2019-03-12T04:11:00Z", + "fltRouteSrc" : "IST", + "fltRouteDest" : "ATH", + "estimatedArrival" : "2019-03-13T03:18:00Z", + "actions" : [ { + "actionAt" : "IST", + "actionCode" : "ONLOAD to ATH", + "actionTime" : "2019-03-13T04:17:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "BagTag Scan at IST", + "actionTime" : "2019-03-13T03:59:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "OFFLOAD from IST", + "actionTime" : "2019-03-13T03:48:00Z" + } ] + }, { + "flightNo" : "BM665", + "flightDate" : "2019-03-12T03:11:00Z", + "fltRouteSrc" : "ATH", + "fltRouteDest" : "JTR", + "estimatedArrival" : "2019-03-12T15:12:00Z", + "actions" : [ { + "actionAt" : "JTR", + "actionCode" : "Offload to Carousel at JTR", + "actionTime" : "2019-03-12T15:06:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "ONLOAD to JTR", + "actionTime" : "2019-03-12T14:16:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "OFFLOAD from ATH", + "actionTime" : "2019-03-12T14:23:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-03-12T16:05:00Z", + "bagArrivalDate" : "2019-03-12T16:25:00Z" + } ] + }`) + insertData(client, err,tableName,value3) + fmt.Printf("Put row succeeded: \n") + + ts_func1 := `SELECT timestamp_add(bag.bagInfo.flightLegs[0].estimatedArrival, "5 minutes") + AS ARRIVAL_TIME FROM BaggageInfo bag WHERE ticketNo=1762341772625` + fmt.Printf("Using timestamp_add function::\n") + fetchData(client, err,tableName,ts_func1) + + ts_func2 := `SELECT $s.ticketno, $bagInfo.bagArrivalDate, $flightLeg.flightDate, + get_duration(timestamp_diff($bagInfo.bagArrivalDate, $flightLeg.flightDate)) AS diff + FROM baggageinfo $s, + $s.bagInfo[] AS $bagInfo, $bagInfo.flightLegs[] AS $flightLeg + WHERE ticketNo=1762344493810` + fmt.Printf("Using get_duration and timestamp_diff function:\n") + fetchData(client, err,tableName,ts_func2) + + string_func1 := `SELECT substring(bag.baginfo.routing,0,3) AS Source FROM baggageInfo bag + WHERE ticketNo=1762376407826` + fmt.Printf("Using substring function:\n") + fetchData(client, err,tableName,string_func1) + + string_func2 := `SELECT fullname, length(fullname) AS fullname_length FROM BaggageInfo + WHERE ticketNo=1762320369957` + fmt.Printf("Using length function:\n") + fetchData(client, err,tableName,string_func2) + + string_func3 := `SELECT fullname FROM baggageInfo bag WHERE + EXISTS bag.bagInfo[contains($element.routing,"SFO")]` + fmt.Printf("Using contains function:\n") + fetchData(client, err,tableName,string_func3) + +} diff --git a/examples-nosql-go-sdk/sqlexamples/SQLOperators.go b/examples-nosql-go-sdk/sqlexamples/SQLOperators.go new file mode 100644 index 0000000..560c726 --- /dev/null +++ b/examples-nosql-go-sdk/sqlexamples/SQLOperators.go @@ -0,0 +1,502 @@ +// Copyright (c) 2023, 2024 Oracle and/or its affiliates. +// Licensed under the Universal Permissive License v 1.0 as shown at +// https://oss.oracle.com/licenses/upl/ +package main + +import ( + "fmt" + "time" + + "github.com/oracle/nosql-go-sdk/nosqldb" + "github.com/oracle/nosql-go-sdk/nosqldb/auth/iam" + "github.com/oracle/nosql-go-sdk/nosqldb/common" + "github.com/oracle/nosql-go-sdk/nosqldb/jsonutil" + "github.com/oracle/nosql-go-sdk/nosqldb/types" +) + +// Creates a client with the supplied configurations. +// This function encapsulates environmental differences and returns a +// client handle to use for data operations. +func createClient_cloud() (*nosqldb.Client, error) { + var cfg nosqldb.Config + // replace the placeholder with your actual region identifier + region := "" + // Replace the placeholders with the actual value of config file location + // and the ocid of your compartment + sp, err := iam.NewSignatureProviderFromFile("", "", "", "") + if err != nil { + return nil, fmt.Errorf("cannot create a Signature Provider: %v", err) + } + cfg = nosqldb.Config{ + Mode: "cloud", + Region: common.Region(region), + AuthorizationProvider: sp, + } + client, err := nosqldb.NewClient(cfg) + return client, err +} + +// Creates a client with the supplied configurations for onPremise database +func createClient_onPrem() (*nosqldb.Client, error) { + var cfg nosqldb.Config + // replace the placeholder with the fullname of your host + endpoint := "http://:8080" + cfg = nosqldb.Config{ + Endpoint: endpoint, + Mode: "onprem", + } + // If using a secure store, uncomment the lines below and pass the username, + // password of the store to Config + // cfg := nosqldb.Config{ + // Mode: "onprem", + // Username: "", + // Password: []byte(""), + // Specify InsecureSkipVerify + // HTTPConfig: httputil.HTTPConfig{ + // InsecureSkipVerify: true, + // }, + client, err := nosqldb.NewClient(cfg) + return client, err +} + +// Creates a table +func createTable(client *nosqldb.Client, err error, tableName string)(){ + stmt := fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s ("+ + "ticketNo LONG," + + "fullName STRING," + + "gender STRING," + + "contactPhone STRING," + + "confNo STRING ," + + "bagInfo JSON, " + + "PRIMARY KEY(ticketNo))",tableName) + tableReq := &nosqldb.TableRequest{ + Statement: stmt, + TableLimits: &nosqldb.TableLimits{ + ReadUnits: 20, + WriteUnits: 20, + StorageGB: 1, + }, + } + tableRes, err := client.DoTableRequest(tableReq) + if err != nil { + fmt.Printf("cannot initiate CREATE TABLE request: %v\n", err) + return + } + // The create table request is asynchronous, wait for table creation + // to complete. + _, err = tableRes.WaitForCompletion(client, 60*time.Second, time.Second) + if err != nil { + fmt.Printf("Error finishing CREATE TABLE request: %v\n", err) + return + } + fmt.Println("Created table ", tableName) + return +} +//Add data to table +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("Put row: %v\nresult: %v\n", putReq.Value.Map(), putRes) +} +//fetch data from the table +func fetchData(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("Query 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())) + } +} + +func main() { + //if using cloud service uncomment the line below + client, err := createClient_cloud() + //if using onPrem uncomment the line below + //client, err := createClient_onPrem() + if err != nil { + fmt.Printf("cannot create NoSQL client: %v\n", err) + return + } + defer client.Close() + tableName := "BaggageInfo" + createTable(client, err,tableName) + //adding data + value, err := types.NewMapValueFromJSON(`{ + "ticketNo":"1762376407826", + "fullName":"Dierdre Amador", + "gender":"M", + "contactPhone":"165-742-5715", + "confNo":"ZG8Z5N", + "bagInfo":[ { + "id" : "7903989918469", + "tagNum" : "17657806240229", + "routing" : "JFK/MAD", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "MAD", + "flightLegs" : [ { + "flightNo" : "BM495", + "flightDate" : "2019-03-07T07:00:00Z", + "fltRouteSrc" : "JFK", + "fltRouteDest" : "MAD", + "estimatedArrival" : "2019-03-07T14:00:00Z", + "actions" : [ { + "actionAt" : "MAD", + "actionCode" : "Offload to Carousel at MAD", + "actionTime" : "2019-03-07T13:54:00Z" + }, { + "actionAt" : "JFK", + "actionCode" : "ONLOAD to MAD", + "actionTime" : "2019-03-07T07:00:00Z" + }, { + "actionAt" : "JFK", + "actionCode" : "BagTag Scan at JFK", + "actionTime" : "2019-03-07T06:53:00Z" + }, { + "actionAt" : "JFK", + "actionCode" : "Checkin at JFK", + "actionTime" : "2019-03-07T05:03:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-03-07T13:51:00Z", + "bagArrivalDate" : "2019-03-07T13:51:00Z" + } ] + }`) + insertData(client, err,tableName,value) + value1, err := types.NewMapValueFromJSON(`{ + "ticketNo":"1762344493810", + "fullName":"Adam Phillips", + "gender":"M", + "contactPhone":"893-324-1064", + "confNo":"LE6J4Z", + "bagInfo":[ { + "id" : "79039899165297", + "tagNum" : "17657806255240", + "routing" : "MIA/LAX/MEL", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "MEL", + "flightLegs" : [ { + "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" + } ] + }, { + "flightNo" : "BM667", + "flightDate" : "2019-02-01T06:13:00Z", + "fltRouteSrc" : "LAX", + "fltRouteDest" : "MEL", + "estimatedArrival" : "2019-02-01T16:15:00Z", + "actions" : [ { + "actionAt" : "MEL", + "actionCode" : "Offload to Carousel at MEL", + "actionTime" : "2019-02-01T16:15:00Z" + }, { + "actionAt" : "LAX", + "actionCode" : "ONLOAD to MEL", + "actionTime" : "2019-02-01T15:35:00Z" + }, { + "actionAt" : "LAX", + "actionCode" : "OFFLOAD from LAX", + "actionTime" : "2019-02-01T15:18:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-02-01T16:13:00Z", + "bagArrivalDate" : "2019-02-01T16:13:00Z" + } ] + }`) + insertData(client, err,tableName,value1) + value2, err := types.NewMapValueFromJSON(`{ + "ticketNo":"1762341772625", + "fullName":"Gerard Greene", + "gender":"M", + "contactPhone":"395-837-3772", + "confNo":"MC0E7R", + "bagInfo":[ { + "id" : "79039899152842", + "tagNum" : "1765780626568", + "routing" : "SFO/IST/ATH/JTR", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "JTR", + "flightLegs" : [ { + "flightNo" : "BM318", + "flightDate" : "2019-03-07T04:00:00Z", + "fltRouteSrc" : "SFO", + "fltRouteDest" : "IST", + "estimatedArrival" : "2019-03-07T17:00:00Z", + "actions" : [ { + "actionAt" : "SFO", + "actionCode" : "ONLOAD to IST", + "actionTime" : "2019-03-07T04:08:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "BagTag Scan at SFO", + "actionTime" : "2019-03-07T03:53:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "Checkin at SFO", + "actionTime" : "2019-03-07T02:20:00Z" + } ] + }, { + "flightNo" : "BM696", + "flightDate" : "2019-03-07T05:08:00Z", + "fltRouteSrc" : "IST", + "fltRouteDest" : "ATH", + "estimatedArrival" : "2019-03-08T04:10:00Z", + "actions" : [ { + "actionAt" : "IST", + "actionCode" : "ONLOAD to ATH", + "actionTime" : "2019-03-08T04:55:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "BagTag Scan at IST", + "actionTime" : "2019-03-08T04:34:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "OFFLOAD from IST", + "actionTime" : "2019-03-08T04:47:00Z" + } ] + }, { + "flightNo" : "BM665", + "flightDate" : "2019-03-07T04:08:00Z", + "fltRouteSrc" : "ATH", + "fltRouteDest" : "JTR", + "estimatedArrival" : "2019-03-07T16:10:00Z", + "actions" : [ { + "actionAt" : "JTR", + "actionCode" : "Offload to Carousel at JTR", + "actionTime" : "2019-03-07T16:09:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "ONLOAD to JTR", + "actionTime" : "2019-03-07T15:51:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "OFFLOAD from ATH", + "actionTime" : "2019-03-07T15:43:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-03-07T16:01:00Z", + "bagArrivalDate" : "2019-03-07T16:01:00Z" + } ] + }`) + insertData(client, err,tableName,value2) + value3, err := types.NewMapValueFromJSON(`{ + "ticketNo":"1762320369957", + "fullName":"Lorenzo Phil", + "gender":"M", + "contactPhone":"364-610-4444", + "confNo":"QI3V6Q", + "bagInfo":[ { + "id" : "79039899187755", + "tagNum" : "17657806240001", + "routing" : "SFO/IST/ATH/JTR", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "JTR", + "flightLegs" : [ { + "flightNo" : "BM318", + "flightDate" : "2019-03-12T03:00:00Z", + "fltRouteSrc" : "SFO", + "fltRouteDest" : "IST", + "estimatedArrival" : "2019-03-12T16:00:00Z", + "actions" : [ { + "actionAt" : "SFO", + "actionCode" : "ONLOAD to IST", + "actionTime" : "2019-03-12T03:11:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "BagTag Scan at SFO", + "actionTime" : "2019-03-12T02:49:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "Checkin at SFO", + "actionTime" : "2019-03-12T01:50:00Z" + } ] + }, { + "flightNo" : "BM696", + "flightDate" : "2019-03-12T04:11:00Z", + "fltRouteSrc" : "IST", + "fltRouteDest" : "ATH", + "estimatedArrival" : "2019-03-13T03:14:00Z", + "actions" : [ { + "actionAt" : "IST", + "actionCode" : "ONLOAD to ATH", + "actionTime" : "2019-03-13T04:10:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "BagTag Scan at IST", + "actionTime" : "2019-03-13T03:56:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "OFFLOAD from IST", + "actionTime" : "2019-03-13T03:59:00Z" + } ] + }, { + "flightNo" : "BM665", + "flightDate" : "2019-03-12T03:11:00Z", + "fltRouteSrc" : "ATH", + "fltRouteDest" : "JTR", + "estimatedArrival" : "2019-03-12T15:12:00Z", + "actions" : [ { + "actionAt" : "JTR", + "actionCode" : "Offload to Carousel at JTR", + "actionTime" : "2019-03-12T15:06:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "ONLOAD to JTR", + "actionTime" : "2019-03-12T14:16:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "OFFLOAD from ATH", + "actionTime" : "2019-03-12T14:13:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-03-12T15:05:00Z", + "bagArrivalDate" : "2019-03-12T15:05:00Z" + }, + { + "id" : "79039899197755", + "tagNum" : "17657806340001", + "routing" : "SFO/IST/ATH/JTR", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "JTR", + "flightLegs" : [ { + "flightNo" : "BM318", + "flightDate" : "2019-03-12T03:00:00Z", + "fltRouteSrc" : "SFO", + "fltRouteDest" : "IST", + "estimatedArrival" : "2019-03-12T16:40:00Z", + "actions" : [ { + "actionAt" : "SFO", + "actionCode" : "ONLOAD to IST", + "actionTime" : "2019-03-12T03:14:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "BagTag Scan at SFO", + "actionTime" : "2019-03-12T02:50:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "Checkin at SFO", + "actionTime" : "2019-03-12T01:58:00Z" + } ] + }, { + "flightNo" : "BM696", + "flightDate" : "2019-03-12T04:11:00Z", + "fltRouteSrc" : "IST", + "fltRouteDest" : "ATH", + "estimatedArrival" : "2019-03-13T03:18:00Z", + "actions" : [ { + "actionAt" : "IST", + "actionCode" : "ONLOAD to ATH", + "actionTime" : "2019-03-13T04:17:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "BagTag Scan at IST", + "actionTime" : "2019-03-13T03:59:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "OFFLOAD from IST", + "actionTime" : "2019-03-13T03:48:00Z" + } ] + }, { + "flightNo" : "BM665", + "flightDate" : "2019-03-12T03:11:00Z", + "fltRouteSrc" : "ATH", + "fltRouteDest" : "JTR", + "estimatedArrival" : "2019-03-12T15:12:00Z", + "actions" : [ { + "actionAt" : "JTR", + "actionCode" : "Offload to Carousel at JTR", + "actionTime" : "2019-03-12T15:06:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "ONLOAD to JTR", + "actionTime" : "2019-03-12T14:16:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "OFFLOAD from ATH", + "actionTime" : "2019-03-12T14:23:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-03-12T16:05:00Z", + "bagArrivalDate" : "2019-03-12T16:25:00Z" + } ] + }`) + insertData(client, err,tableName,value3) + fmt.Printf("Put row succeeded: \n") + + seqcomp_stmt := `SELECT bag.bagInfo[].tagNum,bag.bagInfo[].flightLegs[].fltRouteSrc + FROM BaggageInfo bag WHERE bag.bagInfo[].flightLegs[].fltRouteSrc=any "SFO"` + fmt.Printf("Using Sequence Comparison operator:\n") + fetchData(client, err,tableName,seqcomp_stmt) + + logope_stmt := `SELECT fullName, bag.bagInfo[].tagNum, bag.bagInfo[].routing, + bag.bagInfo[].flightLegs[].fltRouteSrc + FROM BaggageInfo bag + WHERE NOT bag.bagInfo[].flightLegs[].fltRouteSrc=any "SFO"` + fmt.Printf("Using Logical operator:\n") + fetchData(client, err,tableName,logope_stmt) + + valcomp_stmt := `SELECT fullname, bag.bagInfo[].routing FROM BaggageInfo bag WHERE gender="M"` + fmt.Printf("Using Value Comparison operator:\n") + fetchData(client, err,tableName,valcomp_stmt) + + inope_stmt := `SELECT bagdet.fullName, bagdet.bagInfo[].tagNum FROM BaggageInfo bagdet + WHERE bagdet.fullName IN ("Lucinda Beckman", "Adam Phillips","Dierdre Amador","Fallon Clements")` + fmt.Printf("Using IN operator:\n") + fetchData(client, err,tableName,inope_stmt) + + existsope_stmt := `SELECT fullName, bag.ticketNo FROM BaggageInfo bag WHERE + EXISTS bag.bagInfo[$element.bagArrivalDate >="2019-03-01T00:00:00"]` + fmt.Printf("Using EXISTS operator:\n") + fetchData(client, err,tableName,existsope_stmt) + +} diff --git a/examples-nosql-go-sdk/sqlexamples/TableJoins.go b/examples-nosql-go-sdk/sqlexamples/TableJoins.go index 9bf40de..fd984a4 100644 --- a/examples-nosql-go-sdk/sqlexamples/TableJoins.go +++ b/examples-nosql-go-sdk/sqlexamples/TableJoins.go @@ -1,5 +1,6 @@ // Copyright (c) 2023, 2024 Oracle and/or its affiliates. -// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/ +// Licensed under the Universal Permissive License v 1.0 as shown at +// https://oss.oracle.com/licenses/upl/ package main import ( @@ -20,7 +21,8 @@ func createClient_cloud() (*nosqldb.Client, error) { var cfg nosqldb.Config // replace the placeholder with your actual region identifier region := "" - // Replace the placeholders with the actual value of config file location and the ocid of your compartment + // Replace the placeholders with the actual value of config file location + // and the ocid of your compartment sp, err := iam.NewSignatureProviderFromFile("", "", "", "") if err != nil { return nil, fmt.Errorf("cannot create a Signature Provider: %v", err) @@ -43,7 +45,8 @@ func createClient_onPrem() (*nosqldb.Client, error) { Endpoint: endpoint, Mode: "onprem", } - // If using a secure store, uncomment the lines below and pass the username, password of the store to Config + // If using a secure store, uncomment the lines below and pass the username, + // password of the store to Config //cfg := nosqldb.Config{ // Mode: "onprem", // Username: "", @@ -72,7 +75,8 @@ func createTable(client *nosqldb.Client, err error, tableName string, sql_stmt s fmt.Printf("cannot initiate CREATE TABLE request: %v\n", err) return } - // The create table request is asynchronous, wait for table creation to complete. + // The create table request is asynchronous, wait for table creation + // to complete. _, err = tableRes.WaitForCompletion(client, 60*time.Second, time.Second) if err != nil { fmt.Printf("Error finishing CREATE TABLE request: %v\n", err) @@ -89,7 +93,8 @@ func createTable(client *nosqldb.Client, err error, tableName string, sql_stmt s fmt.Printf("cannot initiate CREATE TABLE request: %v\n", err) return } - // The create table request is asynchronous, wait for table creation to complete. + // The create table request is asynchronous, wait for table creation + // to complete. _, err = tableRes.WaitForCompletion(client, 60*time.Second, time.Second) if err != nil { fmt.Printf("Error finishing CREATE TABLE request: %v\n", err) diff --git a/examples-nosql-java-sdk/sqlexamples/AddBagData.java b/examples-nosql-java-sdk/sqlexamples/AddBagData.java new file mode 100644 index 0000000..e25a0ee --- /dev/null +++ b/examples-nosql-java-sdk/sqlexamples/AddBagData.java @@ -0,0 +1,435 @@ +/* Copyright (c) 2023, 2024 Oracle and/or its affiliates. + * Licensed under the Universal Permissive License v 1.0 as shown at + * https://oss.oracle.com/licenses/upl/ +*/ +import java.io.File; +import oracle.nosql.driver.NoSQLHandle; +import oracle.nosql.driver.NoSQLHandleConfig; +import oracle.nosql.driver.NoSQLHandleFactory; +import oracle.nosql.driver.Region; +import oracle.nosql.driver.iam.SignatureProvider; +import oracle.nosql.driver.kv.StoreAccessTokenProvider; +import oracle.nosql.driver.ops.PutRequest; +import oracle.nosql.driver.ops.PutResult; +import oracle.nosql.driver.ops.TableLimits; +import oracle.nosql.driver.ops.TableRequest; +import oracle.nosql.driver.ops.TableResult; +import oracle.nosql.driver.values.MapValue; +import oracle.nosql.driver.values.FieldValue; + +public class AddBagData { + /* Name of your table */ + final static String tableName = "BaggageInfo"; + static NoSQLHandle handle; + final static String bag1 = "{"+ + "\"ticketNo\" : \"1762376407826\","+ + "\"fullName\" : \"Dierdre Amador\","+ + "\"gender\" : \"M\","+ + "\"contactPhone\" : \"165-742-5715\","+ + "\"confNo\" : \"ZG8Z5N\","+ + "\"bagInfo\" : [ {"+ + "\"id\" : \"7903989918469\","+ + "\"tagNum\" : \"17657806240229\","+ + "\"routing\" : \"JFK/MAD\","+ + "\"lastActionCode\" : \"OFFLOAD\","+ + "\"lastActionDesc\" : \"OFFLOAD\","+ + "\"lastSeenStation\" : \"MAD\","+ + "\"flightLegs\" : [ {"+ + "\"flightNo\" : \"BM495\","+ + "\"flightDate\" : \"2019-03-07T07:00:00Z\","+ + "\"fltRouteSrc\" : \"JFK\","+ + "\"fltRouteDest\" : \"MAD\","+ + "\"estimatedArrival\" : \"2019-03-07T14:00:00Z\","+ + "\"actions\" : [ {"+ + "\"actionAt\" : \"MAD\","+ + "\"actionCode\" : \"Offload to Carousel at MAD\","+ + "\"actionTime\" : \"2019-03-07T13:54:00Z\""+ + "}, {"+ + "\"actionAt\" : \"JFK\","+ + "\"actionCode\" : \"ONLOAD to MAD\","+ + "\"actionTime\" : \"2019-03-07T07:00:00Z\""+ + "}, {"+ + "\"actionAt\" : \"JFK\","+ + "\"actionCode\" : \"BagTag Scan at JFK\","+ + "\"actionTime\" : \"2019-03-07T06:53:00Z\""+ + "}, {"+ + "\"actionAt\" : \"JFK\","+ + "\"actionCode\" : \"Checkin at JFK\","+ + "\"actionTime\" : \"2019-03-07T05:03:00Z\""+ + "} ]"+ + "} ],"+ + "\"lastSeenTimeGmt\" : \"2019-03-07T13:51:00Z\","+ + "\"bagArrivalDate\" : \"2019-03-07T13:51:00Z\""+ + "} ]"+ + "}"; + + final static String bag2 = "{"+ + "\"ticketNo\" : \"1762344493810\","+ + "\"fullName\" : \"Dierdre Amador\","+ + "\"gender\" : \"M\","+ + "\"contactPhone\" : \"893-324-1064\","+ + "\"confNo\" : \"LE6J4Z\","+ + "\"bagInfo\" : [ {"+ + "\"id\" : \"79039899165297\","+ + "\"tagNum\" : \"17657806255240\","+ + "\"routing\" : \"MIA/LAX/MEL\","+ + "\"lastActionCode\" : \"OFFLOAD\","+ + "\"lastActionDesc\" : \"OFFLOAD\","+ + "\"lastSeenStation\" : \"MEL\","+ + "\"flightLegs\" : [ {"+ + "\"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\""+ + "} ]"+ + "}, {"+ + "\"flightNo\" : \"BM667\","+ + "\"flightDate\" : \"2019-02-01T06:13:00Z\","+ + "\"fltRouteSrc\" : \"LAX\","+ + "\"fltRouteDest\" : \"MEL\","+ + "\"estimatedArrival\" : \"2019-02-01T16:15:00Z\","+ + "\"actions\" : [ {"+ + "\"actionAt\" : \"MEL\","+ + "\"actionCode\" : \"Offload to Carousel at MEL\","+ + "\"actionTime\" : \"2019-02-01T16:15:00Z\""+ + "}, {"+ + "\"actionAt\" : \"LAX\","+ + "\"actionCode\" : \"ONLOAD to MEL\","+ + "\"actionTime\" : \"2019-02-01T15:35:00Z\""+ + "}, {"+ + "\"actionAt\" : \"LAX\","+ + "\"actionCode\" : \"OFFLOAD from LAX\","+ + "\"actionTime\" : \"2019-02-01T15:18:00Z\""+ + "} ]"+ + "} ],"+ + "\"lastSeenTimeGmt\" : \"2019-02-01T16:13:00Z\","+ + "\"bagArrivalDate\" : \"2019-02-01T16:13:00Z\""+ + "} ]"+ + "}"; + final static String bag3 = "{"+ + "\"ticketNo\" : \"1762341772625\","+ + "\"fullName\" : \"Gerard Greene\","+ + "\"gender\" : \"M\","+ + "\"contactPhone\" : \"395-837-3772\","+ + "\"confNo\" : \"MC0E7R\","+ + "\"bagInfo\" : [ {"+ + "\"id\" : \"79039899152842\","+ + "\"tagNum\" : \"1765780626568\","+ + "\"routing\" : \"SFO/IST/ATH/JTR\","+ + "\"lastActionCode\" : \"OFFLOAD\","+ + "\"lastActionDesc\" : \"OFFLOAD\","+ + "\"lastSeenStation\" : \"JTR\","+ + "\"flightLegs\" : [ {"+ + "\"flightNo\" : \"BM318\","+ + "\"flightDate\" : \"2019-03-07T04:00:00Z\","+ + "\"fltRouteSrc\" : \"SFO\","+ + "\"fltRouteDest\" : \"IST\","+ + "\"estimatedArrival\" : \"2019-03-07T17:00:00Z\","+ + "\"actions\" : [ {"+ + "\"actionAt\" : \"SFO\","+ + "\"actionCode\" : \"ONLOAD to IST\","+ + "\"actionTime\" : \"2019-03-07T04:08:00Z\""+ + "}, {"+ + "\"actionAt\" : \"SFO\","+ + "\"actionCode\" : \"BagTag Scan at SFO\","+ + "\"actionTime\" : \"2019-03-07T03:53:00Z\""+ + "}, {"+ + "\"actionAt\" : \"SFO\","+ + "\"actionCode\" : \"Checkin at SFO\","+ + "\"actionTime\" : \"2019-03-07T02:20:00Z\""+ + "} ]"+ + "}, {"+ + "\"flightNo\" : \"BM696\","+ + "\"flightDate\" : \"2019-03-07T05:08:00Z\","+ + "\"fltRouteSrc\" : \"IST\","+ + "\"fltRouteDest\" : \"ATH\","+ + "\"estimatedArrival\" : \"2019-03-08T04:10:00Z\","+ + "\"actions\" : [ {"+ + "\"actionAt\" : \"IST\","+ + "\"actionCode\" : \"ONLOAD to ATH\","+ + "\"actionTime\" : \"2019-03-08T04:55:00Z\""+ + "}, {"+ + "\"actionAt\" : \"IST\","+ + "\"actionCode\" : \"BagTag Scan at IST\","+ + "\"actionTime\" : \"2019-03-08T04:34:00Z\""+ + "}, {"+ + "\"actionAt\" : \"IST\","+ + "\"actionCode\" : \"OFFLOAD from IST\","+ + "\"actionTime\" : \"2019-03-08T04:47:00Z\""+ + "} ]"+ + "}, {"+ + "\"flightNo\" : \"BM665\","+ + "\"flightDate\" : \"2019-03-07T04:08:00Z\","+ + "\"fltRouteSrc\" : \"ATH\","+ + "\"fltRouteDest\" : \"JTR\","+ + "\"estimatedArrival\" : \"2019-03-07T16:10:00Z\","+ + "\"actions\" : [ {"+ + "\"actionAt\" : \"JTR\","+ + "\"actionCode\" : \"Offload to Carousel at JTR\","+ + "\"actionTime\" : \"2019-03-07T16:09:00Z\""+ + "}, {"+ + "\"actionAt\" : \"ATH\","+ + "\"actionCode\" : \"ONLOAD to JTR\","+ + "\"actionTime\" : \"2019-03-07T15:51:00Z\""+ + "}, {"+ + "\"actionAt\" : \"ATH\","+ + "\"actionCode\" : \"OFFLOAD from ATH\","+ + "\"actionTime\" : \"2019-03-07T15:43:00Z\""+ + "} ]"+ + "} ],"+ + "\"lastSeenTimeGmt\" : \"2019-03-07T16:01:00Z\","+ + "\"bagArrivalDate\" : \"2019-03-07T16:01:00Z\""+ + "} ]"+ + "}"; + + final static String bag4 = "{"+ + "\"ticketNo\" : \"1762320369957\","+ + "\"fullName\" : \"Lorenzo Phil\","+ + "\"gender\" : \"M\","+ + "\"contactPhone\" : \"364-610-4444\","+ + "\"confNo\" : \"QI3V6Q\","+ + "\"bagInfo\" : [ {"+ + "\"id\" : \"79039899187755\","+ + "\"tagNum\" : \"17657806240001\","+ + "\"routing\" : \"SFO/IST/ATH/JTR\","+ + "\"lastActionCode\" : \"OFFLOAD\","+ + "\"lastActionDesc\" : \"OFFLOAD\","+ + "\"lastSeenStation\" : \"JTR\","+ + "\"flightLegs\" : [ {"+ + "\"flightNo\" : \"BM318\","+ + "\"flightDate\" : \"2019-03-12T03:00:00Z\","+ + "\"fltRouteSrc\" : \"SFO\","+ + "\"fltRouteDest\" : \"IST\","+ + "\"estimatedArrival\" : \"2019-03-12T16:00:00Z\","+ + "\"actions\" : [ {"+ + "\"actionAt\" : \"SFO\","+ + "\"actionCode\" : \"ONLOAD to IST\","+ + "\"actionTime\" : \"2019-03-12T03:11:00Z\""+ + "}, {"+ + "\"actionAt\" : \"SFO\","+ + "\"actionCode\" : \"BagTag Scan at SFO\","+ + "\"actionTime\" : \"2019-03-12T02:49:00Z\""+ + "}, {"+ + "\"actionAt\" : \"SFO\","+ + "\"actionCode\" : \"Checkin at SFO\","+ + "\"actionTime\" : \"2019-03-12T01:50:00Z\""+ + "} ]"+ + "}, {"+ + "\"flightNo\" : \"BM696\","+ + "\"flightDate\" : \"2019-03-12T04:11:00Z\","+ + "\"fltRouteSrc\" : \"IST\","+ + "\"fltRouteDest\" : \"ATH\","+ + "\"estimatedArrival\" : \"2019-03-13T03:14:00Z\","+ + "\"actions\" : [ {"+ + "\"actionAt\" : \"IST\","+ + "\"actionCode\" : \"ONLOAD to ATH\","+ + "\"actionTime\" : \"2019-03-13T04:10:00Z\""+ + "}, {"+ + "\"actionAt\" : \"IST\","+ + "\"actionCode\" : \"BagTag Scan at IST\","+ + "\"actionTime\" : \"2019-03-13T03:56:00Z\""+ + "}, {"+ + "\"actionAt\" : \"IST\","+ + "\"actionCode\" : \"OFFLOAD from IST\","+ + "\"actionTime\" : \"2019-03-13T03:59:00Z\""+ + "} ]"+ + "}, {"+ + "\"flightNo\" : \"BM665\","+ + "\"flightDate\" : \"2019-03-12T03:11:00Z\","+ + "\"fltRouteSrc\" : \"ATH\","+ + "\"fltRouteDest\" : \"JTR\","+ + "\"estimatedArrival\" : \"2019-03-12T15:12:00Z\","+ + "\"actions\" : [ {"+ + "\"actionAt\" : \"JTR\","+ + "\"actionCode\" : \"Offload to Carousel at JTR\","+ + "\"actionTime\" : \"2019-03-12T15:06:00Z\""+ + "}, {"+ + "\"actionAt\" : \"ATH\","+ + "\"actionCode\" : \"ONLOAD to JTR\","+ + "\"actionTime\" : \"2019-03-12T14:16:00Z\""+ + "}, {"+ + "\"actionAt\" : \"ATH\","+ + "\"actionCode\" : \"OFFLOAD from ATH\","+ + "\"actionTime\" : \"2019-03-12T14:13:00Z\""+ + "} ]"+ + "} ],"+ + "\"lastSeenTimeGmt\" : \"2019-03-12T15:05:00Z\","+ + "\"bagArrivalDate\" : \"2019-03-12T15:05:00Z\""+ + "},"+ + "{"+ + "\"id\" : \"79039899197755\","+ + "\"tagNum\" : \"17657806340001\","+ + "\"routing\" : \"SFO/IST/ATH/JTR\","+ + "\"lastActionCode\" : \"OFFLOAD\","+ + "\"lastActionDesc\" : \"OFFLOAD\","+ + "\"lastSeenStation\" : \"JTR\","+ + "\"flightLegs\" : [ {"+ + "\"flightNo\" : \"BM318\","+ + "\"flightDate\" : \"2019-03-12T03:00:00Z\","+ + "\"fltRouteSrc\" : \"SFO\","+ + "\"fltRouteDest\" : \"IST\","+ + "\"estimatedArrival\" : \"2019-03-12T16:40:00Z\","+ + "\"actions\" : [ {"+ + "\"actionAt\" : \"SFO\","+ + "\"actionCode\" : \"ONLOAD to IST\","+ + "\"actionTime\" : \"2019-03-12T03:14:00Z\""+ + "}, {"+ + "\"actionAt\" : \"SFO\","+ + "\"actionCode\" : \"BagTag Scan at SFO\","+ + "\"actionTime\" : \"2019-03-12T02:50:00Z\""+ + "}, {"+ + "\"actionAt\" : \"SFO\","+ + "\"actionCode\" : \"Checkin at SFO\","+ + "\"actionTime\" : \"2019-03-12T01:58:00Z\""+ + "} ]"+ + "}, {"+ + "\"flightNo\" : \"BM696\","+ + "\"flightDate\" : \"2019-03-12T04:11:00Z\","+ + "\"fltRouteSrc\" : \"IST\","+ + "\"fltRouteDest\" : \"ATH\","+ + "\"estimatedArrival\" : \"2019-03-13T03:18:00Z\","+ + "\"actions\" : [ {"+ + "\"actionAt\" : \"IST\","+ + "\"actionCode\" : \"ONLOAD to ATH\","+ + "\"actionTime\" : \"2019-03-13T04:17:00Z\""+ + "}, {"+ + "\"actionAt\" : \"IST\","+ + "\"actionCode\" : \"BagTag Scan at IST\","+ + "\"actionTime\" : \"2019-03-13T03:59:00Z\""+ + "}, {"+ + "\"actionAt\" : \"IST\","+ + "\"actionCode\" : \"OFFLOAD from IST\","+ + "\"actionTime\" : \"2019-03-13T03:48:00Z\""+ + "} ]"+ + "}, {"+ + "\"flightNo\" : \"BM665\","+ + "\"flightDate\" : \"2019-03-12T03:11:00Z\","+ + "\"fltRouteSrc\" : \"ATH\","+ + "\"fltRouteDest\" : \"JTR\","+ + "\"estimatedArrival\" : \"2019-03-12T15:12:00Z\","+ + "\"actions\" : [ {"+ + "\"actionAt\" : \"JTR\","+ + "\"actionCode\" : \"Offload to Carousel at JTR\","+ + "\"actionTime\" : \"2019-03-12T15:06:00Z\""+ + "}, {"+ + "\"actionAt\" : \"ATH\","+ + "\"actionCode\" : \"ONLOAD to JTR\","+ + "\"actionTime\" : \"2019-03-12T14:16:00Z\""+ + "}, {"+ + "\"actionAt\" : \"ATH\","+ + "\"actionCode\" : \"OFFLOAD from ATH\","+ + "\"actionTime\" : \"2019-03-12T14:23:00Z\""+ + "} ]"+ + "} ],"+ + "\"lastSeenTimeGmt\" : \"2019-03-12T16:05:00Z\","+ + "\"bagArrivalDate\" : \"2019-03-12T16:25:00Z\""+ + "} ]"+ + "}"; + + static FieldValue val1 = FieldValue.createFromJson(bag1,null); + static FieldValue val2 = FieldValue.createFromJson(bag2,null); + static FieldValue val3 = FieldValue.createFromJson(bag3,null); + static FieldValue val4 = FieldValue.createFromJson(bag4,null); + + public static void main(String[] args) throws Exception { + /* UNCOMMENT the lines of code below if you are using Oracle NoSQL + * Database Cloud service. + * Leave the lines commented if you are using onPremise database + * Add the appropriate values of your region and compartment OCID + String region =""; + String compId =""; + handle = generateNoSQLHandleCloud(region,compId); */ + + /* UNCOMMENT the 2 lines of code below if you are using onPremise Oracle + * NoSQL Database. + * Leave the lines commented if you are using NoSQL Database Cloud Service + * Give appropriate value of your endpoint for the onPremise kvstore. + String kvstore_endpoint ="http://:8080"; + handle = generateNoSQLHandleonPrem(kvstore_endpoint); */ + try { + createTable(handle); + writeRows(handle, (MapValue)val1); + writeRows(handle, (MapValue)val2); + writeRows(handle, (MapValue)val3); + writeRows(handle, (MapValue)val4); + } catch (Exception e) { + System.err.print(e); + } finally { + handle.close(); + } + } + /* Create a NoSQL handle to access the onPremise Oracle NoSQL database */ + private static NoSQLHandle generateNoSQLHandleonPrem(String kvstore_endpoint) throws Exception { + NoSQLHandleConfig config = new NoSQLHandleConfig(kvstore_endpoint); + config.setAuthorizationProvider(new StoreAccessTokenProvider()); + /* If using a secure store, uncomment the line below and pass username, + * password of the store to StoreAccessTokenProvider + */ + /* config.setAuthorizationProvider(new StoreAccessTokenProvider(username, password)); */ + NoSQLHandle handle = NoSQLHandleFactory.createNoSQLHandle(config); + return handle; + } + + /* Create a NoSQL handle to access the cloud service */ + private static NoSQLHandle generateNoSQLHandleCloud(String region, String compId) throws Exception { + SignatureProvider ap = new SignatureProvider(); + NoSQLHandleConfig config = new NoSQLHandleConfig(region, ap); + // set your default compartment + config.setDefaultCompartment(compId); + NoSQLHandle handle = NoSQLHandleFactory.createNoSQLHandle(config); + return handle; + } + + /** + * Create a simple table and set your desired table capacity + */ + private static void createTable(NoSQLHandle handle) throws Exception { + String createTableDDL = "CREATE TABLE IF NOT EXISTS " + tableName + + "(ticketNo LONG," + + "fullName STRING," + + "gender STRING ," + + "contactPhone STRING ," + + "confNo STRING ," + + "bagInfo JSON, " + + "PRIMARY KEY(ticketNo))"; + + TableLimits limits = new TableLimits(20, 20, 1); + TableRequest treq = new TableRequest() + .setStatement(createTableDDL).setTableLimits(limits); + + TableResult tres = handle.tableRequest(treq); + /* The request is async, + * so wait for the table to become active. + */ + tres.waitForCompletion(handle, 60000, /* wait 60 sec */ + 1000); /* delay ms for poll */ + System.out.println("Table " + tableName + " is active"); + } + + /** + * Add a Row in the table + */ + 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("Wrote " + value); + } else { + System.out.println("Put failed"); + } + } +} diff --git a/examples-nosql-java-sdk/sqlexamples/AddData.java b/examples-nosql-java-sdk/sqlexamples/AddData.java index 99d3059..f39a1f1 100644 --- a/examples-nosql-java-sdk/sqlexamples/AddData.java +++ b/examples-nosql-java-sdk/sqlexamples/AddData.java @@ -1,5 +1,6 @@ /* Copyright (c) 2023, 2024 Oracle and/or its affiliates. - * Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/ + * Licensed under the Universal Permissive License v 1.0 as shown at + * https://oss.oracle.com/licenses/upl/ */ import java.io.File; import oracle.nosql.driver.NoSQLHandle; @@ -260,16 +261,21 @@ public class AddData { static FieldValue newvalue2 = FieldValue.createFromJson(acct3,null); public static void main(String[] args) throws Exception { - /* UNCOMMENT the lines of code below if you are using Oracle NoSQL Database Cloud service. Leave the lines commented if you are using onPremise database - Add the appropriate values of your region and compartment OCID + /* UNCOMMENT the lines of code below if you are using Oracle NoSQL + * Database Cloud service. Leave the lines commented if you are using + * onPremise database + * Add the appropriate values of your region and compartment OCID String region =""; String compId =""; handle = generateNoSQLHandleCloud(region,compId); */ - /* UNCOMMENT the 2 lines of code below if you are using onPremise Oracle NoSQL Database. Leave the lines commented if you are using NoSQL Database Cloud Service - give appropriate value of your endpoint for the onPremise kvstore. - String kvstore_endpoint ="http://:8080"; - handle = generateNoSQLHandleonPrem(kvstore_endpoint); */ + /* UNCOMMENT the 2 lines of code below if you are using onPremise Oracle + * NoSQL Database. + * Leave the lines commented if you are using Cloud Service + * Give appropriate value of your endpoint for the onPremise kvstore. + String kvstore_endpoint ="http://:8080"; + handle = generateNoSQLHandleonPrem(kvstore_endpoint); + */ try { createTable(handle); writeRows(handle, (MapValue)newvalue); @@ -285,7 +291,9 @@ public static void main(String[] args) throws Exception { private static NoSQLHandle generateNoSQLHandleonPrem(String kvstore_endpoint) throws Exception { NoSQLHandleConfig config = new NoSQLHandleConfig(kvstore_endpoint); config.setAuthorizationProvider(new StoreAccessTokenProvider()); - /* If using a secure store, uncomment the line below and pass the username, password of the store to StoreAccessTokenProvider */ + /* If using a secure store, uncomment the line below and pass the + * username, password of the store to StoreAccessTokenProvider + */ /* config.setAuthorizationProvider(new StoreAccessTokenProvider(username, password)); */ NoSQLHandle handle = NoSQLHandleFactory.createNoSQLHandle(config); return handle; diff --git a/examples-nosql-java-sdk/sqlexamples/AlterTable.java b/examples-nosql-java-sdk/sqlexamples/AlterTable.java index e33a559..4f2be64 100644 --- a/examples-nosql-java-sdk/sqlexamples/AlterTable.java +++ b/examples-nosql-java-sdk/sqlexamples/AlterTable.java @@ -1,5 +1,6 @@ /* Copyright (c) 2023, 2024 Oracle and/or its affiliates. - * Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/ + * Licensed under the Universal Permissive License v 1.0 as shown at + * https://oss.oracle.com/licenses/upl/ */ import java.io.File; import oracle.nosql.driver.NoSQLHandle; @@ -18,16 +19,21 @@ public class AlterTable{ final static String tableName = "stream_acct"; static NoSQLHandle handle; public static void main(String[] args) throws Exception { - /* UNCOMMENT the lines of code below if you are using Oracle NoSQL Database Cloud service. Leave the lines commented if you are using onPremise database - Add the appropriate values of your region and compartment OCID + /*UNCOMMENT the lines of code below if you are using Oracle NoSQL + * Database Cloud service. + * Leave the lines commented if you are using onPremise database + * Add the appropriate values of your region and compartment OCID String region =""; String compId =""; handle = generateNoSQLHandleCloud(region,compId); */ - /* UNCOMMENT the 2 lines of code below if you are using onPremise Oracle NoSQL Database. Leave the lines commented if you are using NoSQL Database Cloud Service - give appropriate value of your endpoint for the onPremise kvstore. + /* UNCOMMENT the 2 lines of code below if you are using onPremise Oracle + * NoSQL Database. + * Leave the lines commented if you are using NoSQL Database Cloud Service + * Give appropriate value of your endpoint for the onPremise kvstore. String kvstore_endpoint ="http://:8080"; - handle = generateNoSQLHandleonPrem(kvstore_endpoint); */ + handle = generateNoSQLHandleonPrem(kvstore_endpoint); + */ try { createTab(handle); alterTab(handle); @@ -42,7 +48,9 @@ public static void main(String[] args) throws Exception { private static NoSQLHandle generateNoSQLHandleonPrem(String kvstore_endpoint) throws Exception { NoSQLHandleConfig config = new NoSQLHandleConfig(kvstore_endpoint); config.setAuthorizationProvider(new StoreAccessTokenProvider()); - /* If using a secure store, uncomment the line below and pass the username, password of the store to StoreAccessTokenProvider */ + /*If using a secure store, uncomment the line below and pass the + * username, password of the store to StoreAccessTokenProvider + */ /* config.setAuthorizationProvider(new StoreAccessTokenProvider(username, password)); */ NoSQLHandle handle = NoSQLHandleFactory.createNoSQLHandle(config); return handle; diff --git a/examples-nosql-java-sdk/sqlexamples/CreateTable.java b/examples-nosql-java-sdk/sqlexamples/CreateTable.java index f526f4a..4c5291c 100644 --- a/examples-nosql-java-sdk/sqlexamples/CreateTable.java +++ b/examples-nosql-java-sdk/sqlexamples/CreateTable.java @@ -1,5 +1,6 @@ /* Copyright (c) 2023, 2024 Oracle and/or its affiliates. - * Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/ + * Licensed under the Universal Permissive License v 1.0 as shown at + * https://oss.oracle.com/licenses/upl/ */ import java.io.File; import oracle.nosql.driver.NoSQLHandle; @@ -18,16 +19,22 @@ public class CreateTable{ static NoSQLHandle handle; public static void main(String[] args) throws Exception { - /* UNCOMMENT the lines of code below if you are using Oracle NoSQL Database Cloud service. Leave the lines commented if you are using onPremise database - Add the appropriate values of your region and compartment OCID + /* UNCOMMENT the lines of code below if you are using Oracle NoSQL + * Database Cloud service. + * Leave the lines commented if you are using onPremise database + * Add the appropriate values of your region and compartment OCID String region =""; String compId =""; - handle = generateNoSQLHandleCloud(region,compId); */ + handle = generateNoSQLHandleCloud(region,compId); + */ - /* UNCOMMENT the 2 lines of code below if you are using onPremise Oracle NoSQL Database. Leave the lines commented if you are using NoSQL Database Cloud Service - give appropriate value of your endpoint for the onPremise kvstore. + /* UNCOMMENT the 2 lines of code below if you are using onPremise Oracle + * NoSQL Database. + * Leave the lines commented if you are using NoSQL Database Cloud Service + * Give appropriate value of your endpoint for the onPremise kvstore. String kvstore_endpoint ="http://:8080"; - handle = generateNoSQLHandleonPrem(kvstore_endpoint); */ + handle = generateNoSQLHandleonPrem(kvstore_endpoint); + */ try { createTab(handle); } catch (Exception e) { @@ -40,7 +47,9 @@ public static void main(String[] args) throws Exception { private static NoSQLHandle generateNoSQLHandleonPrem(String kvstore_endpoint) throws Exception { NoSQLHandleConfig config = new NoSQLHandleConfig(kvstore_endpoint); config.setAuthorizationProvider(new StoreAccessTokenProvider()); - /* If using a secure store, uncomment the line below and pass the username, password of the store to StoreAccessTokenProvider */ + /* If using a secure store, uncomment the line below and pass username, + * password of the store to StoreAccessTokenProvider + */ /* config.setAuthorizationProvider(new StoreAccessTokenProvider(username, password)); */ NoSQLHandle handle = NoSQLHandleFactory.createNoSQLHandle(config); return handle; diff --git a/examples-nosql-java-sdk/sqlexamples/GroupSortData.java b/examples-nosql-java-sdk/sqlexamples/GroupSortData.java new file mode 100644 index 0000000..f74c30a --- /dev/null +++ b/examples-nosql-java-sdk/sqlexamples/GroupSortData.java @@ -0,0 +1,461 @@ +/* Copyright (c) 2023, 2024 Oracle and/or its affiliates. + * Licensed under the Universal Permissive License v 1.0 as shown at + * https://oss.oracle.com/licenses/upl/ +*/ +import java.io.File; +import oracle.nosql.driver.NoSQLHandle; +import oracle.nosql.driver.NoSQLHandleConfig; +import oracle.nosql.driver.NoSQLHandleFactory; +import oracle.nosql.driver.Region; +import oracle.nosql.driver.iam.SignatureProvider; +import oracle.nosql.driver.kv.StoreAccessTokenProvider; +import oracle.nosql.driver.ops.PutRequest; +import oracle.nosql.driver.ops.PutResult; +import oracle.nosql.driver.ops.TableLimits; +import oracle.nosql.driver.ops.TableRequest; +import oracle.nosql.driver.ops.TableResult; +import oracle.nosql.driver.values.MapValue; +import oracle.nosql.driver.values.FieldValue; +import oracle.nosql.driver.ops.QueryRequest; +import oracle.nosql.driver.ops.QueryResult; +import oracle.nosql.driver.ops.QueryIterableResult; + +public class GroupSortData { + /* Name of your table */ + final static String tableName = "BaggageInfo"; + static NoSQLHandle handle; + final static String bag1 = "{"+ + "\"ticketNo\" : \"1762376407826\","+ + "\"fullName\" : \"Dierdre Amador\","+ + "\"gender\" : \"M\","+ + "\"contactPhone\" : \"165-742-5715\","+ + "\"confNo\" : \"ZG8Z5N\","+ + "\"bagInfo\" : [ {"+ + "\"id\" : \"7903989918469\","+ + "\"tagNum\" : \"17657806240229\","+ + "\"routing\" : \"JFK/MAD\","+ + "\"lastActionCode\" : \"OFFLOAD\","+ + "\"lastActionDesc\" : \"OFFLOAD\","+ + "\"lastSeenStation\" : \"MAD\","+ + "\"flightLegs\" : [ {"+ + "\"flightNo\" : \"BM495\","+ + "\"flightDate\" : \"2019-03-07T07:00:00Z\","+ + "\"fltRouteSrc\" : \"JFK\","+ + "\"fltRouteDest\" : \"MAD\","+ + "\"estimatedArrival\" : \"2019-03-07T14:00:00Z\","+ + "\"actions\" : [ {"+ + "\"actionAt\" : \"MAD\","+ + "\"actionCode\" : \"Offload to Carousel at MAD\","+ + "\"actionTime\" : \"2019-03-07T13:54:00Z\""+ + "}, {"+ + "\"actionAt\" : \"JFK\","+ + "\"actionCode\" : \"ONLOAD to MAD\","+ + "\"actionTime\" : \"2019-03-07T07:00:00Z\""+ + "}, {"+ + "\"actionAt\" : \"JFK\","+ + "\"actionCode\" : \"BagTag Scan at JFK\","+ + "\"actionTime\" : \"2019-03-07T06:53:00Z\""+ + "}, {"+ + "\"actionAt\" : \"JFK\","+ + "\"actionCode\" : \"Checkin at JFK\","+ + "\"actionTime\" : \"2019-03-07T05:03:00Z\""+ + "} ]"+ + "} ],"+ + "\"lastSeenTimeGmt\" : \"2019-03-07T13:51:00Z\","+ + "\"bagArrivalDate\" : \"2019-03-07T13:51:00Z\""+ + "} ]"+ + "}"; + + final static String bag2 = "{"+ + "\"ticketNo\" : \"1762344493810\","+ + "\"fullName\" : \"Dierdre Amador\","+ + "\"gender\" : \"M\","+ + "\"contactPhone\" : \"893-324-1064\","+ + "\"confNo\" : \"LE6J4Z\","+ + "\"bagInfo\" : [ {"+ + "\"id\" : \"79039899165297\","+ + "\"tagNum\" : \"17657806255240\","+ + "\"routing\" : \"MIA/LAX/MEL\","+ + "\"lastActionCode\" : \"OFFLOAD\","+ + "\"lastActionDesc\" : \"OFFLOAD\","+ + "\"lastSeenStation\" : \"MEL\","+ + "\"flightLegs\" : [ {"+ + "\"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\""+ + "} ]"+ + "}, {"+ + "\"flightNo\" : \"BM667\","+ + "\"flightDate\" : \"2019-02-01T06:13:00Z\","+ + "\"fltRouteSrc\" : \"LAX\","+ + "\"fltRouteDest\" : \"MEL\","+ + "\"estimatedArrival\" : \"2019-02-01T16:15:00Z\","+ + "\"actions\" : [ {"+ + "\"actionAt\" : \"MEL\","+ + "\"actionCode\" : \"Offload to Carousel at MEL\","+ + "\"actionTime\" : \"2019-02-01T16:15:00Z\""+ + "}, {"+ + "\"actionAt\" : \"LAX\","+ + "\"actionCode\" : \"ONLOAD to MEL\","+ + "\"actionTime\" : \"2019-02-01T15:35:00Z\""+ + "}, {"+ + "\"actionAt\" : \"LAX\","+ + "\"actionCode\" : \"OFFLOAD from LAX\","+ + "\"actionTime\" : \"2019-02-01T15:18:00Z\""+ + "} ]"+ + "} ],"+ + "\"lastSeenTimeGmt\" : \"2019-02-01T16:13:00Z\","+ + "\"bagArrivalDate\" : \"2019-02-01T16:13:00Z\""+ + "} ]"+ + "}"; + final static String bag3 = "{"+ + "\"ticketNo\" : \"1762341772625\","+ + "\"fullName\" : \"Gerard Greene\","+ + "\"gender\" : \"M\","+ + "\"contactPhone\" : \"395-837-3772\","+ + "\"confNo\" : \"MC0E7R\","+ + "\"bagInfo\" : [ {"+ + "\"id\" : \"79039899152842\","+ + "\"tagNum\" : \"1765780626568\","+ + "\"routing\" : \"SFO/IST/ATH/JTR\","+ + "\"lastActionCode\" : \"OFFLOAD\","+ + "\"lastActionDesc\" : \"OFFLOAD\","+ + "\"lastSeenStation\" : \"JTR\","+ + "\"flightLegs\" : [ {"+ + "\"flightNo\" : \"BM318\","+ + "\"flightDate\" : \"2019-03-07T04:00:00Z\","+ + "\"fltRouteSrc\" : \"SFO\","+ + "\"fltRouteDest\" : \"IST\","+ + "\"estimatedArrival\" : \"2019-03-07T17:00:00Z\","+ + "\"actions\" : [ {"+ + "\"actionAt\" : \"SFO\","+ + "\"actionCode\" : \"ONLOAD to IST\","+ + "\"actionTime\" : \"2019-03-07T04:08:00Z\""+ + "}, {"+ + "\"actionAt\" : \"SFO\","+ + "\"actionCode\" : \"BagTag Scan at SFO\","+ + "\"actionTime\" : \"2019-03-07T03:53:00Z\""+ + "}, {"+ + "\"actionAt\" : \"SFO\","+ + "\"actionCode\" : \"Checkin at SFO\","+ + "\"actionTime\" : \"2019-03-07T02:20:00Z\""+ + "} ]"+ + "}, {"+ + "\"flightNo\" : \"BM696\","+ + "\"flightDate\" : \"2019-03-07T05:08:00Z\","+ + "\"fltRouteSrc\" : \"IST\","+ + "\"fltRouteDest\" : \"ATH\","+ + "\"estimatedArrival\" : \"2019-03-08T04:10:00Z\","+ + "\"actions\" : [ {"+ + "\"actionAt\" : \"IST\","+ + "\"actionCode\" : \"ONLOAD to ATH\","+ + "\"actionTime\" : \"2019-03-08T04:55:00Z\""+ + "}, {"+ + "\"actionAt\" : \"IST\","+ + "\"actionCode\" : \"BagTag Scan at IST\","+ + "\"actionTime\" : \"2019-03-08T04:34:00Z\""+ + "}, {"+ + "\"actionAt\" : \"IST\","+ + "\"actionCode\" : \"OFFLOAD from IST\","+ + "\"actionTime\" : \"2019-03-08T04:47:00Z\""+ + "} ]"+ + "}, {"+ + "\"flightNo\" : \"BM665\","+ + "\"flightDate\" : \"2019-03-07T04:08:00Z\","+ + "\"fltRouteSrc\" : \"ATH\","+ + "\"fltRouteDest\" : \"JTR\","+ + "\"estimatedArrival\" : \"2019-03-07T16:10:00Z\","+ + "\"actions\" : [ {"+ + "\"actionAt\" : \"JTR\","+ + "\"actionCode\" : \"Offload to Carousel at JTR\","+ + "\"actionTime\" : \"2019-03-07T16:09:00Z\""+ + "}, {"+ + "\"actionAt\" : \"ATH\","+ + "\"actionCode\" : \"ONLOAD to JTR\","+ + "\"actionTime\" : \"2019-03-07T15:51:00Z\""+ + "}, {"+ + "\"actionAt\" : \"ATH\","+ + "\"actionCode\" : \"OFFLOAD from ATH\","+ + "\"actionTime\" : \"2019-03-07T15:43:00Z\""+ + "} ]"+ + "} ],"+ + "\"lastSeenTimeGmt\" : \"2019-03-07T16:01:00Z\","+ + "\"bagArrivalDate\" : \"2019-03-07T16:01:00Z\""+ + "} ]"+ + "}"; + + final static String bag4 = "{"+ + "\"ticketNo\" : \"1762320369957\","+ + "\"fullName\" : \"Lorenzo Phil\","+ + "\"gender\" : \"M\","+ + "\"contactPhone\" : \"364-610-4444\","+ + "\"confNo\" : \"QI3V6Q\","+ + "\"bagInfo\" : [ {"+ + "\"id\" : \"79039899187755\","+ + "\"tagNum\" : \"17657806240001\","+ + "\"routing\" : \"SFO/IST/ATH/JTR\","+ + "\"lastActionCode\" : \"OFFLOAD\","+ + "\"lastActionDesc\" : \"OFFLOAD\","+ + "\"lastSeenStation\" : \"JTR\","+ + "\"flightLegs\" : [ {"+ + "\"flightNo\" : \"BM318\","+ + "\"flightDate\" : \"2019-03-12T03:00:00Z\","+ + "\"fltRouteSrc\" : \"SFO\","+ + "\"fltRouteDest\" : \"IST\","+ + "\"estimatedArrival\" : \"2019-03-12T16:00:00Z\","+ + "\"actions\" : [ {"+ + "\"actionAt\" : \"SFO\","+ + "\"actionCode\" : \"ONLOAD to IST\","+ + "\"actionTime\" : \"2019-03-12T03:11:00Z\""+ + "}, {"+ + "\"actionAt\" : \"SFO\","+ + "\"actionCode\" : \"BagTag Scan at SFO\","+ + "\"actionTime\" : \"2019-03-12T02:49:00Z\""+ + "}, {"+ + "\"actionAt\" : \"SFO\","+ + "\"actionCode\" : \"Checkin at SFO\","+ + "\"actionTime\" : \"2019-03-12T01:50:00Z\""+ + "} ]"+ + "}, {"+ + "\"flightNo\" : \"BM696\","+ + "\"flightDate\" : \"2019-03-12T04:11:00Z\","+ + "\"fltRouteSrc\" : \"IST\","+ + "\"fltRouteDest\" : \"ATH\","+ + "\"estimatedArrival\" : \"2019-03-13T03:14:00Z\","+ + "\"actions\" : [ {"+ + "\"actionAt\" : \"IST\","+ + "\"actionCode\" : \"ONLOAD to ATH\","+ + "\"actionTime\" : \"2019-03-13T04:10:00Z\""+ + "}, {"+ + "\"actionAt\" : \"IST\","+ + "\"actionCode\" : \"BagTag Scan at IST\","+ + "\"actionTime\" : \"2019-03-13T03:56:00Z\""+ + "}, {"+ + "\"actionAt\" : \"IST\","+ + "\"actionCode\" : \"OFFLOAD from IST\","+ + "\"actionTime\" : \"2019-03-13T03:59:00Z\""+ + "} ]"+ + "}, {"+ + "\"flightNo\" : \"BM665\","+ + "\"flightDate\" : \"2019-03-12T03:11:00Z\","+ + "\"fltRouteSrc\" : \"ATH\","+ + "\"fltRouteDest\" : \"JTR\","+ + "\"estimatedArrival\" : \"2019-03-12T15:12:00Z\","+ + "\"actions\" : [ {"+ + "\"actionAt\" : \"JTR\","+ + "\"actionCode\" : \"Offload to Carousel at JTR\","+ + "\"actionTime\" : \"2019-03-12T15:06:00Z\""+ + "}, {"+ + "\"actionAt\" : \"ATH\","+ + "\"actionCode\" : \"ONLOAD to JTR\","+ + "\"actionTime\" : \"2019-03-12T14:16:00Z\""+ + "}, {"+ + "\"actionAt\" : \"ATH\","+ + "\"actionCode\" : \"OFFLOAD from ATH\","+ + "\"actionTime\" : \"2019-03-12T14:13:00Z\""+ + "} ]"+ + "} ],"+ + "\"lastSeenTimeGmt\" : \"2019-03-12T15:05:00Z\","+ + "\"bagArrivalDate\" : \"2019-03-12T15:05:00Z\""+ + "},"+ + "{"+ + "\"id\" : \"79039899197755\","+ + "\"tagNum\" : \"17657806340001\","+ + "\"routing\" : \"SFO/IST/ATH/JTR\","+ + "\"lastActionCode\" : \"OFFLOAD\","+ + "\"lastActionDesc\" : \"OFFLOAD\","+ + "\"lastSeenStation\" : \"JTR\","+ + "\"flightLegs\" : [ {"+ + "\"flightNo\" : \"BM318\","+ + "\"flightDate\" : \"2019-03-12T03:00:00Z\","+ + "\"fltRouteSrc\" : \"SFO\","+ + "\"fltRouteDest\" : \"IST\","+ + "\"estimatedArrival\" : \"2019-03-12T16:40:00Z\","+ + "\"actions\" : [ {"+ + "\"actionAt\" : \"SFO\","+ + "\"actionCode\" : \"ONLOAD to IST\","+ + "\"actionTime\" : \"2019-03-12T03:14:00Z\""+ + "}, {"+ + "\"actionAt\" : \"SFO\","+ + "\"actionCode\" : \"BagTag Scan at SFO\","+ + "\"actionTime\" : \"2019-03-12T02:50:00Z\""+ + "}, {"+ + "\"actionAt\" : \"SFO\","+ + "\"actionCode\" : \"Checkin at SFO\","+ + "\"actionTime\" : \"2019-03-12T01:58:00Z\""+ + "} ]"+ + "}, {"+ + "\"flightNo\" : \"BM696\","+ + "\"flightDate\" : \"2019-03-12T04:11:00Z\","+ + "\"fltRouteSrc\" : \"IST\","+ + "\"fltRouteDest\" : \"ATH\","+ + "\"estimatedArrival\" : \"2019-03-13T03:18:00Z\","+ + "\"actions\" : [ {"+ + "\"actionAt\" : \"IST\","+ + "\"actionCode\" : \"ONLOAD to ATH\","+ + "\"actionTime\" : \"2019-03-13T04:17:00Z\""+ + "}, {"+ + "\"actionAt\" : \"IST\","+ + "\"actionCode\" : \"BagTag Scan at IST\","+ + "\"actionTime\" : \"2019-03-13T03:59:00Z\""+ + "}, {"+ + "\"actionAt\" : \"IST\","+ + "\"actionCode\" : \"OFFLOAD from IST\","+ + "\"actionTime\" : \"2019-03-13T03:48:00Z\""+ + "} ]"+ + "}, {"+ + "\"flightNo\" : \"BM665\","+ + "\"flightDate\" : \"2019-03-12T03:11:00Z\","+ + "\"fltRouteSrc\" : \"ATH\","+ + "\"fltRouteDest\" : \"JTR\","+ + "\"estimatedArrival\" : \"2019-03-12T15:12:00Z\","+ + "\"actions\" : [ {"+ + "\"actionAt\" : \"JTR\","+ + "\"actionCode\" : \"Offload to Carousel at JTR\","+ + "\"actionTime\" : \"2019-03-12T15:06:00Z\""+ + "}, {"+ + "\"actionAt\" : \"ATH\","+ + "\"actionCode\" : \"ONLOAD to JTR\","+ + "\"actionTime\" : \"2019-03-12T14:16:00Z\""+ + "}, {"+ + "\"actionAt\" : \"ATH\","+ + "\"actionCode\" : \"OFFLOAD from ATH\","+ + "\"actionTime\" : \"2019-03-12T14:23:00Z\""+ + "} ]"+ + "} ],"+ + "\"lastSeenTimeGmt\" : \"2019-03-12T16:05:00Z\","+ + "\"bagArrivalDate\" : \"2019-03-12T16:25:00Z\""+ + "} ]"+ + "}"; + + + + static FieldValue val1 = FieldValue.createFromJson(bag1,null); + static FieldValue val2 = FieldValue.createFromJson(bag2,null); + static FieldValue val3 = FieldValue.createFromJson(bag3,null); + static FieldValue val4 = FieldValue.createFromJson(bag4,null); + + public static void main(String[] args) throws Exception { + /* UNCOMMENT the lines of code below if you are using Oracle NoSQL + * Database Cloud service. + * Leave the lines commented if you are using onPremise database + * Add the appropriate values of your region and compartment OCID + String region =""; + String compId =""; + handle = generateNoSQLHandleCloud(region,compId); */ + + /* UNCOMMENT the 2 lines of code below if you are using onPremise Oracle + * NoSQL Database. + * Leave the lines commented if you are using NoSQL Database Cloud Service + * Give appropriate value of your endpoint for the onPremise kvstore. + String kvstore_endpoint ="http://:8080"; + handle = generateNoSQLHandleonPrem(kvstore_endpoint); */ + try { + createTable(handle); + writeRows(handle, (MapValue)val1); + writeRows(handle, (MapValue)val2); + writeRows(handle, (MapValue)val3); + writeRows(handle, (MapValue)val4); + String orderby_stmt="SELECT bag.fullName, bag.bagInfo[].tagNum,bag.bagInfo[].lastSeenTimeGmt FROM BaggageInfo bag "+ + "WHERE bag.bagInfo[].lastSeenStation=any \"MEL\" ORDER BY bag.bagInfo[].lastSeenTimeGmt DESC"; + System.out.println("Using ORDER BY to sort data "); + fetchRows(handle,orderby_stmt); + String sortlimit_stmt="SELECT $bag.fullName, $bag.bagInfo.tagNum, $flt_time FROM BaggageInfo $bag, "+ + "$bag.bagInfo[].lastSeenTimeGmt $flt_time WHERE $bag.bagInfo[].lastSeenStation=any \"JTR\""+ + "ORDER BY $flt_time LIMIT 4"; + System.out.println("Using ORDER BY and LIMIT to sort and limit data "); + fetchRows(handle,sortlimit_stmt); + String groupsortlimit_stmt="SELECT $flt_src as SOURCE,count(*) as COUNT FROM BaggageInfo $bag, "+ + "$bag.bagInfo.flightLegs[0].fltRouteSrc $flt_src GROUP BY $flt_src"; + System.out.println("Using GROUP BY, ORDER BY and LIMIT to group, sort and limit data "); + fetchRows(handle,groupsortlimit_stmt); + + + } catch (Exception e) { + System.err.print(e); + } finally { + handle.close(); + } + } + /* Create a NoSQL handle to access the onPremise Oracle NoSQL database */ + private static NoSQLHandle generateNoSQLHandleonPrem(String kvstore_endpoint) throws Exception { + NoSQLHandleConfig config = new NoSQLHandleConfig(kvstore_endpoint); + config.setAuthorizationProvider(new StoreAccessTokenProvider()); + /* If using a secure store, uncomment the line below and pass username, + * password of the store to StoreAccessTokenProvider + */ + /* config.setAuthorizationProvider(new StoreAccessTokenProvider(username, password)); */ + NoSQLHandle handle = NoSQLHandleFactory.createNoSQLHandle(config); + return handle; + } + + /* Create a NoSQL handle to access the cloud service */ + private static NoSQLHandle generateNoSQLHandleCloud(String region, String compId) throws Exception { + SignatureProvider ap = new SignatureProvider(); + NoSQLHandleConfig config = new NoSQLHandleConfig(region, ap); + // set your default compartment + config.setDefaultCompartment(compId); + NoSQLHandle handle = NoSQLHandleFactory.createNoSQLHandle(config); + return handle; + } + + /** + * Create a simple table and set your desired table capacity + */ + private static void createTable(NoSQLHandle handle) throws Exception { + String createTableDDL = "CREATE TABLE IF NOT EXISTS " + tableName + + "(ticketNo LONG," + + "fullName STRING," + + "gender STRING ," + + "contactPhone STRING ," + + "confNo STRING ," + + "bagInfo JSON, " + + "PRIMARY KEY(ticketNo))"; + + TableLimits limits = new TableLimits(20, 20, 1); + TableRequest treq = new TableRequest() + .setStatement(createTableDDL).setTableLimits(limits); + + TableResult tres = handle.tableRequest(treq); + /* The request is async, + * so wait for the table to become active. + */ + tres.waitForCompletion(handle, 60000, /* wait 60 sec */ + 1000); /* delay ms for poll */ + System.out.println("Table " + tableName + " is active"); + } + + /** + * Add a Row in the table + */ + private static void writeRows(NoSQLHandle handle, MapValue value) throws Exception { + PutRequest putRequest = + new PutRequest().setValue(value).setTableName(tableName); + PutResult putResult = handle.put(putRequest); + } + //Fetch rows from the table + private static void fetchRows(NoSQLHandle handle,String sqlstmt) throws Exception { + + try ( + QueryRequest queryRequest = new QueryRequest().setStatement(sqlstmt); + QueryIterableResult results = handle.queryIterable(queryRequest)){ + for (MapValue res : results) { + System.out.println("\t" + res); + } + } + } +} diff --git a/examples-nosql-java-sdk/sqlexamples/Indexes.java b/examples-nosql-java-sdk/sqlexamples/Indexes.java index 77b20a8..7a7f278 100644 --- a/examples-nosql-java-sdk/sqlexamples/Indexes.java +++ b/examples-nosql-java-sdk/sqlexamples/Indexes.java @@ -1,5 +1,6 @@ /* Copyright (c) 2023, 2024 Oracle and/or its affiliates. - * Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/ + * Licensed under the Universal Permissive License v 1.0 as shown at + * https://oss.oracle.com/licenses/upl/ */ import java.io.File; import oracle.nosql.driver.NoSQLHandle; @@ -17,16 +18,22 @@ public class Indexes{ final static String tableName = "stream_acct"; static NoSQLHandle handle; public static void main(String[] args) throws Exception { - /* UNCOMMENT the lines of code below if you are using Oracle NoSQL Database Cloud service. Leave the lines commented if you are using onPremise database - Add the appropriate values of your region and compartment OCID + /* UNCOMMENT the lines of code below if you are using Oracle NoSQL + * Database Cloud service. + * Leave the lines commented if you are using onPremise database + * Add the appropriate values of your region and compartment OCID String region =""; String compId =""; - handle = generateNoSQLHandleCloud(region,compId); */ + handle = generateNoSQLHandleCloud(region,compId); + */ - /* UNCOMMENT the 2 lines of code below if you are using onPremise Oracle NoSQL Database. Leave the lines commented if you are using NoSQL Database Cloud Service - give appropriate value of your endpoint for the onPremise kvstore. + /* UNCOMMENT the 2 lines of code below if you are using onPremise Oracle + * NoSQL Database. + * Leave the lines commented if you are using NoSQL Database Cloud Service + * Give appropriate value of your endpoint for the onPremise kvstore. String kvstore_endpoint ="http://:8080"; - handle = generateNoSQLHandleonPrem(kvstore_endpoint); */ + handle = generateNoSQLHandleonPrem(kvstore_endpoint); + */ try { createTab(handle); crtIndex(handle); @@ -41,7 +48,9 @@ public static void main(String[] args) throws Exception { private static NoSQLHandle generateNoSQLHandleonPrem(String kvstore_endpoint) throws Exception { NoSQLHandleConfig config = new NoSQLHandleConfig(kvstore_endpoint); config.setAuthorizationProvider(new StoreAccessTokenProvider()); - /* If using a secure store, uncomment the line below and pass the username, password of the store to StoreAccessTokenProvider */ + /* If using a secure store, uncomment the line below and pass username, + * password of the store to StoreAccessTokenProvider + */ /* config.setAuthorizationProvider(new StoreAccessTokenProvider(username, password)); */ NoSQLHandle handle = NoSQLHandleFactory.createNoSQLHandle(config); return handle; diff --git a/examples-nosql-java-sdk/sqlexamples/ModifyData.java b/examples-nosql-java-sdk/sqlexamples/ModifyData.java index 230eebe..0ae79af 100644 --- a/examples-nosql-java-sdk/sqlexamples/ModifyData.java +++ b/examples-nosql-java-sdk/sqlexamples/ModifyData.java @@ -1,5 +1,6 @@ /*Copyright (c) 2023, 2024 Oracle and/or its affiliates. - * Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/ + * Licensed under the Universal Permissive License v 1.0 as shown at + * https://oss.oracle.com/licenses/upl/ */ import java.io.File; import oracle.nosql.driver.NoSQLHandle; @@ -267,16 +268,21 @@ public class ModifyData { static FieldValue newvalue2 = FieldValue.createFromJson(acct3,null); public static void main(String[] args) throws Exception { - /* UNCOMMENT the lines of code below if you are using Oracle NoSQL Database Cloud service. Leave the lines commented if you are using onPremise database - Add the appropriate values of your region and compartment OCID + /* UNCOMMENT the lines of code below if you are using Oracle NoSQL + * Database Cloud service. + * Leave the lines commented if you are using onPremise database + * Add the appropriate values of your region and compartment OCID String region =""; String compId =""; handle = generateNoSQLHandleCloud(region,compId); */ - /* UNCOMMENT the 2 lines of code below if you are using onPremise Oracle NoSQL Database. Leave the lines commented if you are using NoSQL Database Cloud Service - give appropriate value of your endpoint for the onPremise kvstore. + /* UNCOMMENT the 2 lines of code below if you are using onPremise Oracle + * NoSQL Database. + * Leave the lines commented if you are using NoSQL Database Cloud Service + * Give appropriate value of your endpoint for the onPremise kvstore. String kvstore_endpoint ="http://:8080"; - handle = generateNoSQLHandleonPrem(kvstore_endpoint); */ + handle = generateNoSQLHandleonPrem(kvstore_endpoint); + */ try { createTable(handle); writeRows(handle, (MapValue)newvalue); @@ -415,7 +421,9 @@ public static void main(String[] args) throws Exception { private static NoSQLHandle generateNoSQLHandleonPrem(String kvstore_endpoint) throws Exception { NoSQLHandleConfig config = new NoSQLHandleConfig(kvstore_endpoint); config.setAuthorizationProvider(new StoreAccessTokenProvider()); - /* If using a secure store uncomment the line below and pass the username, password of the store to StoreAccessTokenProvider*/ + /* If using a secure store uncomment the line below and pass username, + * password of the store to StoreAccessTokenProvider + */ /*config.setAuthorizationProvider(new StoreAccessTokenProvider(username, password));*/ NoSQLHandle handle = NoSQLHandleFactory.createNoSQLHandle(config); return handle; diff --git a/examples-nosql-java-sdk/sqlexamples/MultiDataOps.java b/examples-nosql-java-sdk/sqlexamples/MultiDataOps.java new file mode 100644 index 0000000..51adc00 --- /dev/null +++ b/examples-nosql-java-sdk/sqlexamples/MultiDataOps.java @@ -0,0 +1,147 @@ +/* Copyright (c) 2023, 2024 Oracle and/or its affiliates. + * Licensed under the Universal Permissive License v 1.0 as shown at + * https://oss.oracle.com/licenses/upl/ +*/ +import java.io.File; +import oracle.nosql.driver.NoSQLHandle; +import oracle.nosql.driver.NoSQLHandleConfig; +import oracle.nosql.driver.NoSQLHandleFactory; +import oracle.nosql.driver.iam.SignatureProvider; +import oracle.nosql.driver.ops.QueryRequest; +import oracle.nosql.driver.ops.QueryResult; +import oracle.nosql.driver.ops.GetRequest; +import oracle.nosql.driver.ops.GetResult; +import oracle.nosql.driver.ops.PutRequest; +import oracle.nosql.driver.ops.PutResult; +import oracle.nosql.driver.ops.TableLimits; +import oracle.nosql.driver.ops.TableRequest; +import oracle.nosql.driver.ops.TableResult; +import oracle.nosql.driver.ops.DeleteRequest; +import oracle.nosql.driver.ops.DeleteResult; +import oracle.nosql.driver.ops.MultiDeleteRequest; +import oracle.nosql.driver.ops.MultiDeleteResult; +import oracle.nosql.driver.values.MapValue; +import oracle.nosql.driver.ops.QueryIterableResult; +import oracle.nosql.driver.kv.StoreAccessTokenProvider; + +public class MultiDataOps { + /* Name of your table */ + final static String tableName = "examplesAddress"; + static NoSQLHandle handle; + + public static void main(String[] args) throws Exception { + /* UNCOMMENT the lines of code below if you are using Oracle NoSQL + * Database Cloud service. + * Leave the lines commented if you are using onPremise database + * Add the appropriate values of your region and compartment OCID + String region =""; + String compId =""; + handle = generateNoSQLHandleCloud(region,compId); */ + + /* UNCOMMENT the 2 lines of code below if you are using onPremise Oracle + * NoSQL Database. + * Leave the lines commented if you are using NoSQL Database Cloud Service + * Give appropriate value of your endpoint for the onPremise kvstore. + String kvstore_endpoint ="http://:8080"; + handle = generateNoSQLHandleonPrem(kvstore_endpoint); */ + try { + createTable(handle); + writeRows(handle,1,"10 Red Street","Apt 3",1234567); + writeRows(handle,2,"2 Green Street","Suite 9",1234567); + writeRows(handle,3,"5 Blue Ave","Floor 2",1234567); + writeRows(handle,4,"9 Yellow Boulevard","Apt 3",87654321); + String sqlstmt_allrows="select * from examplesAddress"; + fetchRowCnt(handle,sqlstmt_allrows); + delMulRows(handle,1234567); + fetchRowCnt(handle,sqlstmt_allrows); + } catch (Exception e) { + System.err.print(e); + } finally { + handle.close(); + } + } + /* Create a NoSQL handle to access the onPremise Oracle NoSQL database */ + private static NoSQLHandle generateNoSQLHandleonPrem(String kvstore_endpoint) throws Exception { + NoSQLHandleConfig config = new NoSQLHandleConfig(kvstore_endpoint); + config.setAuthorizationProvider(new StoreAccessTokenProvider()); + /* If using a secure store, uncomment the line below and pass username, + * password of the store to StoreAccessTokenProvider + */ + /* config.setAuthorizationProvider(new StoreAccessTokenProvider(username, password)); */ + NoSQLHandle handle = NoSQLHandleFactory.createNoSQLHandle(config); + return handle; + } + + /* Create a NoSQL handle to access the cloud service */ + private static NoSQLHandle generateNoSQLHandleCloud(String region, String compId) throws Exception { + SignatureProvider ap = new SignatureProvider(); + NoSQLHandleConfig config = new NoSQLHandleConfig(region, ap); + // set your default compartment + config.setDefaultCompartment(compId); + NoSQLHandle handle = NoSQLHandleFactory.createNoSQLHandle(config); + return handle; + } + /** + * Create a simple table and set your desired table capacity + */ + private static void createTable(NoSQLHandle handle) throws Exception { + String createTableDDL = + "CREATE TABLE IF NOT EXISTS " + tableName + + "(id INTEGER, " + + " address_line1 STRING, " + + " address_line2 STRING, " + + " pin INTEGER, " + + " PRIMARY KEY(SHARD(pin), id))"; + + TableLimits limits = new TableLimits(20, 20, 1); + TableRequest treq = new TableRequest() + .setStatement(createTableDDL).setTableLimits(limits); + TableResult tres = handle.tableRequest(treq); + /* The request is async, + * so wait for the table to become active. + */ + tres.waitForCompletion(handle, 60000, /* wait 60 sec */ + 1000); /* delay ms for poll */ + System.out.println("Table " + tableName + " is active"); + } + + /** + * Add a row of data + */ + private static void writeRows(NoSQLHandle handle, int idval,String add1, String aa2, int pinval) throws Exception { + MapValue value = new MapValue(). + put("id", idval).put("pin",pinval). + put("address_line1", "add1"). + put("address_line2", "add2"); + + PutRequest putRequest = new PutRequest() + .setValue(value) + .setTableName(tableName); + + PutResult putRes = handle.put(putRequest); + if (putRes.getVersion() != null) { + System.out.println("Wrote a row "); + } else { + System.out.println("Put failed"); + } + } + //Fetch rows from the table + private static void fetchRowCnt(NoSQLHandle handle,String sqlstmt) throws Exception { + QueryRequest queryRequest = new QueryRequest().setStatement(sqlstmt); + QueryResult results = handle.query(queryRequest); + System.out.println("Number of rows in table: " + results.getResults().size()); + + } + //Delete multiple rows from the table + private static void delMulRows(NoSQLHandle handle,int pinval) throws Exception { + + MapValue key = new MapValue().put("pin", 1234567); + MultiDeleteRequest multiDelRequest = new MultiDeleteRequest() + .setKey(key) + .setTableName(tableName); + + MultiDeleteResult mRes = handle.multiDelete(multiDelRequest); + System.out.println("MultiDelete result = " + mRes); + + } +} diff --git a/examples-nosql-java-sdk/sqlexamples/MultiWrite.java b/examples-nosql-java-sdk/sqlexamples/MultiWrite.java new file mode 100644 index 0000000..40ecffd --- /dev/null +++ b/examples-nosql-java-sdk/sqlexamples/MultiWrite.java @@ -0,0 +1,182 @@ +/* Copyright (c) 2023, 2024 Oracle and/or its affiliates. + * Licensed under the Universal Permissive License v 1.0 as shown at + * https://oss.oracle.com/licenses/upl/ +*/ +import java.io.File; +import oracle.nosql.driver.NoSQLHandle; +import oracle.nosql.driver.NoSQLHandleConfig; +import oracle.nosql.driver.NoSQLHandleFactory; +import oracle.nosql.driver.Region; +import oracle.nosql.driver.iam.SignatureProvider; +import oracle.nosql.driver.kv.StoreAccessTokenProvider; +import oracle.nosql.driver.ops.QueryRequest; +import oracle.nosql.driver.ops.QueryResult; +import oracle.nosql.driver.ops.GetRequest; +import oracle.nosql.driver.ops.GetResult; +import oracle.nosql.driver.ops.PutRequest; +import oracle.nosql.driver.ops.PutResult; +import oracle.nosql.driver.ops.TableLimits; +import oracle.nosql.driver.ops.TableRequest; +import oracle.nosql.driver.ops.TableResult; +import oracle.nosql.driver.values.MapValue; +import oracle.nosql.driver.ops.QueryIterableResult; +import oracle.nosql.driver.ops.WriteMultipleRequest; +import oracle.nosql.driver.ops.WriteMultipleRequest.OperationRequest; +import oracle.nosql.driver.ops.WriteMultipleResult; +import oracle.nosql.driver.ops.WriteMultipleResult.OperationResult; + +public class MultiWrite{ + /* Name of your tables */ + final static String tableName = "ticket"; + final static String childTableName = "bagInfo"; + final static String fullChildTableName = "ticket.bagInfo"; + final static String descTableName = "flightLegs"; + final static String fullDescTableName = "ticket.bagInfo.flightLegs"; + static NoSQLHandle handle; + + public static void main(String[] args) throws Exception { + /* UNCOMMENT the lines of code below if you are using Oracle NoSQL + * Database Cloud service. + * Leave the lines commented if you are using onPremise database + * Add the appropriate values of your region and compartment OCID + String region =""; + String compId =""; + handle = generateNoSQLHandleCloud(region,compId); */ + + /* UNCOMMENT the 2 lines of code below if you are using onPremise Oracle + * NoSQL Database. + * Leave the lines commented if you are using NoSQL Database Cloud Service + * Give appropriate value of your endpoint for the onPremise kvstore. + String kvstore_endpoint ="http://:8080"; + handle = generateNoSQLHandleonPrem(kvstore_endpoint); */ + try { + String crttab_stmt = "CREATE TABLE IF NOT EXISTS " + tableName + + "(ticketNo LONG," + + "confNo STRING," + + "PRIMARY KEY(ticketNo))"; + createTable(handle,crttab_stmt,true,tableName); + String crtchildtab_stmt = "CREATE TABLE IF NOT EXISTS " + fullChildTableName + + "(id LONG," + + "tagNum LONG," + + "routing STRING," + + "lastActionCode STRING," + + "lastActionDesc STRING," + + "lastSeenStation STRING," + + "lastSeenTimeGmt TIMESTAMP(4)," + + "bagArrivalDate TIMESTAMP(4)," + + "PRIMARY KEY(id))"; + createTable(handle,crtchildtab_stmt,false,childTableName); + String crtdesctab_stmt = "CREATE TABLE IF NOT EXISTS " + fullDescTableName + + "(flightNo STRING," + + "flightDate TIMESTAMP(4)," + + "fltRouteSrc STRING," + + "fltRouteDest STRING," + + "estimatedArrival TIMESTAMP(4)," + + "actions JSON," + + "PRIMARY KEY(flightNo))"; + createTable(handle,crtdesctab_stmt,false,descTableName); + String data1 ="{\"ticketNo\": \"1762344493810\","+ + "\"confNo\" : \"LE6J4Z\"}"; + 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\""+ + "}"; + writeMul(handle,tableName,data1,fullChildTableName,data2); + 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\""+ + "} ]"+ + "}"; + writeRowData(handle,"ticket.bagInfo.flightLegs",data3); + + } catch (Exception e) { + System.err.print(e); + } finally { + handle.close(); + } + } + /* Create a NoSQL handle to access the onPremise Oracle NoSQL database */ + private static NoSQLHandle generateNoSQLHandleonPrem(String kvstore_endpoint) throws Exception { + NoSQLHandleConfig config = new NoSQLHandleConfig(kvstore_endpoint); + config.setAuthorizationProvider(new StoreAccessTokenProvider()); + /* If using a secure store uncomment the line below and pass the username, + * password of the store to StoreAccessTokenProvider + */ + /* config.setAuthorizationProvider(new StoreAccessTokenProvider(username, password)); */ + NoSQLHandle handle = NoSQLHandleFactory.createNoSQLHandle(config); + return handle; + } + /* Create a NoSQL handle to access the cloud service */ + private static NoSQLHandle generateNoSQLHandleCloud(String region, String compId) throws Exception { + SignatureProvider ap = new SignatureProvider(); + NoSQLHandleConfig config = new NoSQLHandleConfig(region, ap); + /* set your default compartment*/ + config.setDefaultCompartment(compId); + NoSQLHandle handle = NoSQLHandleFactory.createNoSQLHandle(config); + return handle; + } + /** + * Creates a table and sets your desired table capacity + */ + private static void createTable(NoSQLHandle handle, String sql_stmt,boolean reg_table, String tableName) throws Exception { + TableRequest treq = new TableRequest().setStatement(sql_stmt); + if (reg_table== true){ + TableLimits limits = new TableLimits(20, 20, 1); + treq = new TableRequest().setStatement(sql_stmt).setTableLimits(limits); + } + TableResult tres = handle.tableRequest(treq); + /* The request is async, + * so wait for the table to become active. + */ + tres.waitForCompletion(handle, 60000, /* wait 60 sec */ + 1000); /* delay ms for poll */ + System.out.println("Table " + tableName + " is active"); + } + + /** + * Make a row in the table and write it + */ + private static void writeRowData(NoSQLHandle handle,String table_name,String row_data) throws Exception { + PutRequest putRequest = + new PutRequest().setValueFromJson(row_data,null).setTableName(table_name); + PutResult putResult = handle.put(putRequest); + if (putResult.getVersion() != null) { + System.out.println("Wrote " + row_data); + } else { + System.out.println("Put failed"); + } + } + 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); + } + +} diff --git a/examples-nosql-java-sdk/sqlexamples/Namespaces.java b/examples-nosql-java-sdk/sqlexamples/Namespaces.java index e8653b4..d236e59 100644 --- a/examples-nosql-java-sdk/sqlexamples/Namespaces.java +++ b/examples-nosql-java-sdk/sqlexamples/Namespaces.java @@ -1,5 +1,6 @@ /* Copyright (c) 2023, 2024 Oracle and/or its affiliates. - * Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/ + * Licensed under the Universal Permissive License v 1.0 as shown at + * https://oss.oracle.com/licenses/upl/ */ import oracle.nosql.driver.NoSQLHandle; import oracle.nosql.driver.NoSQLHandleConfig; @@ -29,7 +30,9 @@ public static void main(String[] args) throws Exception { private static NoSQLHandle generateNoSQLHandleonPrem(String kvstore_endpoint) throws Exception { NoSQLHandleConfig config = new NoSQLHandleConfig(kvstore_endpoint); config.setAuthorizationProvider(new StoreAccessTokenProvider()); - /* If using a secure store, uncomment the line below and pass the username, password of the store to StoreAccessTokenProvider */ + /* If using a secure store, uncomment the line below and pass the + * username, password of the store to StoreAccessTokenProvider + */ /* config.setAuthorizationProvider(new StoreAccessTokenProvider(username, password)); */ NoSQLHandle handle = NoSQLHandleFactory.createNoSQLHandle(config); return handle; diff --git a/examples-nosql-java-sdk/sqlexamples/QueryData.java b/examples-nosql-java-sdk/sqlexamples/QueryData.java index 135d32d..1dd1911 100644 --- a/examples-nosql-java-sdk/sqlexamples/QueryData.java +++ b/examples-nosql-java-sdk/sqlexamples/QueryData.java @@ -1,5 +1,6 @@ /* Copyright (c) 2023, 2024 Oracle and/or its affiliates. - * Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/ + * Licensed under the Universal Permissive License v 1.0 as shown at + * https://oss.oracle.com/licenses/upl/ */ import java.io.File; import oracle.nosql.driver.NoSQLHandle; @@ -265,14 +266,18 @@ public class QueryData { static FieldValue newvalue2 = FieldValue.createFromJson(acct3,null); public static void main(String[] args) throws Exception { - /* UNCOMMENT the lines of code below if you are using Oracle NoSQL Database Cloud service. Leave the lines commented if you are using onPremise database - Add the appropriate values of your region and compartment OCID + /* UNCOMMENT the lines of code below if you are using Oracle NoSQL + * Database Cloud service. + * Leave the lines commented if you are using onPremise database + * Add the appropriate values of your region and compartment OCID String region =""; String compId =""; handle = generateNoSQLHandleCloud(region,compId); */ - /* UNCOMMENT the 2 lines of code below if you are using onPremise Oracle NoSQL Database. Leave the lines commented if you are using NoSQL Database Cloud Service - give appropriate value of your endpoint for the onPremise kvstore. + /* UNCOMMENT the 2 lines of code below if you are using onPremise Oracle + * NoSQL Database. + * Leave the lines commented if you are using NoSQL Database Cloud Service + * Give appropriate value of your endpoint for the onPremise kvstore. String kvstore_endpoint ="http://:8080"; handle = generateNoSQLHandleonPrem(kvstore_endpoint); */ try { @@ -280,6 +285,8 @@ public static void main(String[] args) throws Exception { writeRows(handle, (MapValue)newvalue); writeRows(handle, (MapValue)newvalue1); writeRows(handle, (MapValue)newvalue2); + System.out.println("Fetching a single row based on the primary key"); + getRow(handle,"acct_Id",2); String sqlstmt_allrows="select * from stream_acct acct"; System.out.println("Fetching all data "); fetchRows(handle,sqlstmt_allrows); @@ -296,7 +303,9 @@ public static void main(String[] args) throws Exception { private static NoSQLHandle generateNoSQLHandleonPrem(String kvstore_endpoint) throws Exception { NoSQLHandleConfig config = new NoSQLHandleConfig(kvstore_endpoint); config.setAuthorizationProvider(new StoreAccessTokenProvider()); - /* If using a secure store, uncomment the line below and pass the username, password of the store to StoreAccessTokenProvider */ + /* If using a secure store, uncomment the line below and pass username, + * password of the store to StoreAccessTokenProvider + */ /* config.setAuthorizationProvider(new StoreAccessTokenProvider(username, password)); */ NoSQLHandle handle = NoSQLHandleFactory.createNoSQLHandle(config); return handle; @@ -358,4 +367,17 @@ private static void fetchRows(NoSQLHandle handle,String sqlstmt) throws Exceptio } } } + //Fetch single row using get API + private static void getRow(NoSQLHandle handle,String colName,int Id) throws Exception { + MapValue key = new MapValue().put(colName, Id); + GetRequest getRequest = new GetRequest().setKey(key) + .setTableName(tableName); + GetResult getRes = handle.get(getRequest); + /* on success, GetResult.getValue() returns a non-null value */ + if (getRes.getValue() != null) { + System.out.println("\t" +getRes.getValue().toString()); + } else { + System.out.println("Get Failed"); + } + } } diff --git a/examples-nosql-java-sdk/sqlexamples/Regions.java b/examples-nosql-java-sdk/sqlexamples/Regions.java index 4d77f0d..ec10f24 100644 --- a/examples-nosql-java-sdk/sqlexamples/Regions.java +++ b/examples-nosql-java-sdk/sqlexamples/Regions.java @@ -1,5 +1,6 @@ /* Copyright (c) 2023, 2024 Oracle and/or its affiliates. - * Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/ + * Licensed under the Universal Permissive License v 1.0 as shown at + * https://oss.oracle.com/licenses/upl/ */ import oracle.nosql.driver.NoSQLHandle; import oracle.nosql.driver.NoSQLHandleConfig; @@ -36,7 +37,9 @@ public static void main(String[] args) throws Exception { private static NoSQLHandle generateNoSQLHandleonPrem(String kvstore_endpoint) throws Exception { NoSQLHandleConfig config = new NoSQLHandleConfig(kvstore_endpoint); config.setAuthorizationProvider(new StoreAccessTokenProvider()); - /* If using a secure store, uncomment the line below and pass the username, password of the store to StoreAccessTokenProvider */ + /* If using a secure store, uncomment the line below and pass username, + * password of the store to StoreAccessTokenProvider + */ /* config.setAuthorizationProvider(new StoreAccessTokenProvider(username, password)); */ NoSQLHandle handle = NoSQLHandleFactory.createNoSQLHandle(config); return handle; diff --git a/examples-nosql-java-sdk/sqlexamples/SQLExpressions.java b/examples-nosql-java-sdk/sqlexamples/SQLExpressions.java new file mode 100644 index 0000000..478eb94 --- /dev/null +++ b/examples-nosql-java-sdk/sqlexamples/SQLExpressions.java @@ -0,0 +1,483 @@ +/* Copyright (c) 2023, 2024 Oracle and/or its affiliates. + * Licensed under the Universal Permissive License v 1.0 as shown at + * https://oss.oracle.com/licenses/upl/ +*/ +import java.io.File; +import oracle.nosql.driver.NoSQLHandle; +import oracle.nosql.driver.NoSQLHandleConfig; +import oracle.nosql.driver.NoSQLHandleFactory; +import oracle.nosql.driver.Region; +import oracle.nosql.driver.iam.SignatureProvider; +import oracle.nosql.driver.kv.StoreAccessTokenProvider; +import oracle.nosql.driver.ops.PutRequest; +import oracle.nosql.driver.ops.PutResult; +import oracle.nosql.driver.ops.TableLimits; +import oracle.nosql.driver.ops.TableRequest; +import oracle.nosql.driver.ops.TableResult; +import oracle.nosql.driver.values.MapValue; +import oracle.nosql.driver.values.FieldValue; +import oracle.nosql.driver.ops.QueryRequest; +import oracle.nosql.driver.ops.QueryResult; +import oracle.nosql.driver.ops.QueryIterableResult; + + +public class SQLExpressions { + /* Name of your table */ + final static String tableName = "BaggageInfo"; + static NoSQLHandle handle; + final static String bag1 = "{"+ + "\"ticketNo\" : \"1762376407826\","+ + "\"fullName\" : \"Dierdre Amador\","+ + "\"gender\" : \"M\","+ + "\"contactPhone\" : \"165-742-5715\","+ + "\"confNo\" : \"ZG8Z5N\","+ + "\"bagInfo\" : [ {"+ + "\"id\" : \"7903989918469\","+ + "\"tagNum\" : \"17657806240229\","+ + "\"routing\" : \"JFK/MAD\","+ + "\"lastActionCode\" : \"OFFLOAD\","+ + "\"lastActionDesc\" : \"OFFLOAD\","+ + "\"lastSeenStation\" : \"MAD\","+ + "\"flightLegs\" : [ {"+ + "\"flightNo\" : \"BM495\","+ + "\"flightDate\" : \"2019-03-07T07:00:00Z\","+ + "\"fltRouteSrc\" : \"JFK\","+ + "\"fltRouteDest\" : \"MAD\","+ + "\"estimatedArrival\" : \"2019-03-07T14:00:00Z\","+ + "\"actions\" : [ {"+ + "\"actionAt\" : \"MAD\","+ + "\"actionCode\" : \"Offload to Carousel at MAD\","+ + "\"actionTime\" : \"2019-03-07T13:54:00Z\""+ + "}, {"+ + "\"actionAt\" : \"JFK\","+ + "\"actionCode\" : \"ONLOAD to MAD\","+ + "\"actionTime\" : \"2019-03-07T07:00:00Z\""+ + "}, {"+ + "\"actionAt\" : \"JFK\","+ + "\"actionCode\" : \"BagTag Scan at JFK\","+ + "\"actionTime\" : \"2019-03-07T06:53:00Z\""+ + "}, {"+ + "\"actionAt\" : \"JFK\","+ + "\"actionCode\" : \"Checkin at JFK\","+ + "\"actionTime\" : \"2019-03-07T05:03:00Z\""+ + "} ]"+ + "} ],"+ + "\"lastSeenTimeGmt\" : \"2019-03-07T13:51:00Z\","+ + "\"bagArrivalDate\" : \"2019-03-07T13:51:00Z\""+ + "} ]"+ + "}"; + + final static String bag2 = "{"+ + "\"ticketNo\" : \"1762344493810\","+ + "\"fullName\" : \"Dierdre Amador\","+ + "\"gender\" : \"M\","+ + "\"contactPhone\" : \"893-324-1064\","+ + "\"confNo\" : \"LE6J4Z\","+ + "\"bagInfo\" : [ {"+ + "\"id\" : \"79039899165297\","+ + "\"tagNum\" : \"17657806255240\","+ + "\"routing\" : \"MIA/LAX/MEL\","+ + "\"lastActionCode\" : \"OFFLOAD\","+ + "\"lastActionDesc\" : \"OFFLOAD\","+ + "\"lastSeenStation\" : \"MEL\","+ + "\"flightLegs\" : [ {"+ + "\"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\""+ + "} ]"+ + "}, {"+ + "\"flightNo\" : \"BM667\","+ + "\"flightDate\" : \"2019-02-01T06:13:00Z\","+ + "\"fltRouteSrc\" : \"LAX\","+ + "\"fltRouteDest\" : \"MEL\","+ + "\"estimatedArrival\" : \"2019-02-01T16:15:00Z\","+ + "\"actions\" : [ {"+ + "\"actionAt\" : \"MEL\","+ + "\"actionCode\" : \"Offload to Carousel at MEL\","+ + "\"actionTime\" : \"2019-02-01T16:15:00Z\""+ + "}, {"+ + "\"actionAt\" : \"LAX\","+ + "\"actionCode\" : \"ONLOAD to MEL\","+ + "\"actionTime\" : \"2019-02-01T15:35:00Z\""+ + "}, {"+ + "\"actionAt\" : \"LAX\","+ + "\"actionCode\" : \"OFFLOAD from LAX\","+ + "\"actionTime\" : \"2019-02-01T15:18:00Z\""+ + "} ]"+ + "} ],"+ + "\"lastSeenTimeGmt\" : \"2019-02-01T16:13:00Z\","+ + "\"bagArrivalDate\" : \"2019-02-01T16:13:00Z\""+ + "} ]"+ + "}"; + final static String bag3 = "{"+ + "\"ticketNo\" : \"1762341772625\","+ + "\"fullName\" : \"Gerard Greene\","+ + "\"gender\" : \"M\","+ + "\"contactPhone\" : \"395-837-3772\","+ + "\"confNo\" : \"MC0E7R\","+ + "\"bagInfo\" : [ {"+ + "\"id\" : \"79039899152842\","+ + "\"tagNum\" : \"1765780626568\","+ + "\"routing\" : \"SFO/IST/ATH/JTR\","+ + "\"lastActionCode\" : \"OFFLOAD\","+ + "\"lastActionDesc\" : \"OFFLOAD\","+ + "\"lastSeenStation\" : \"JTR\","+ + "\"flightLegs\" : [ {"+ + "\"flightNo\" : \"BM318\","+ + "\"flightDate\" : \"2019-03-07T04:00:00Z\","+ + "\"fltRouteSrc\" : \"SFO\","+ + "\"fltRouteDest\" : \"IST\","+ + "\"estimatedArrival\" : \"2019-03-07T17:00:00Z\","+ + "\"actions\" : [ {"+ + "\"actionAt\" : \"SFO\","+ + "\"actionCode\" : \"ONLOAD to IST\","+ + "\"actionTime\" : \"2019-03-07T04:08:00Z\""+ + "}, {"+ + "\"actionAt\" : \"SFO\","+ + "\"actionCode\" : \"BagTag Scan at SFO\","+ + "\"actionTime\" : \"2019-03-07T03:53:00Z\""+ + "}, {"+ + "\"actionAt\" : \"SFO\","+ + "\"actionCode\" : \"Checkin at SFO\","+ + "\"actionTime\" : \"2019-03-07T02:20:00Z\""+ + "} ]"+ + "}, {"+ + "\"flightNo\" : \"BM696\","+ + "\"flightDate\" : \"2019-03-07T05:08:00Z\","+ + "\"fltRouteSrc\" : \"IST\","+ + "\"fltRouteDest\" : \"ATH\","+ + "\"estimatedArrival\" : \"2019-03-08T04:10:00Z\","+ + "\"actions\" : [ {"+ + "\"actionAt\" : \"IST\","+ + "\"actionCode\" : \"ONLOAD to ATH\","+ + "\"actionTime\" : \"2019-03-08T04:55:00Z\""+ + "}, {"+ + "\"actionAt\" : \"IST\","+ + "\"actionCode\" : \"BagTag Scan at IST\","+ + "\"actionTime\" : \"2019-03-08T04:34:00Z\""+ + "}, {"+ + "\"actionAt\" : \"IST\","+ + "\"actionCode\" : \"OFFLOAD from IST\","+ + "\"actionTime\" : \"2019-03-08T04:47:00Z\""+ + "} ]"+ + "}, {"+ + "\"flightNo\" : \"BM665\","+ + "\"flightDate\" : \"2019-03-07T04:08:00Z\","+ + "\"fltRouteSrc\" : \"ATH\","+ + "\"fltRouteDest\" : \"JTR\","+ + "\"estimatedArrival\" : \"2019-03-07T16:10:00Z\","+ + "\"actions\" : [ {"+ + "\"actionAt\" : \"JTR\","+ + "\"actionCode\" : \"Offload to Carousel at JTR\","+ + "\"actionTime\" : \"2019-03-07T16:09:00Z\""+ + "}, {"+ + "\"actionAt\" : \"ATH\","+ + "\"actionCode\" : \"ONLOAD to JTR\","+ + "\"actionTime\" : \"2019-03-07T15:51:00Z\""+ + "}, {"+ + "\"actionAt\" : \"ATH\","+ + "\"actionCode\" : \"OFFLOAD from ATH\","+ + "\"actionTime\" : \"2019-03-07T15:43:00Z\""+ + "} ]"+ + "} ],"+ + "\"lastSeenTimeGmt\" : \"2019-03-07T16:01:00Z\","+ + "\"bagArrivalDate\" : \"2019-03-07T16:01:00Z\""+ + "} ]"+ + "}"; + + final static String bag4 = "{"+ + "\"ticketNo\" : \"1762320369957\","+ + "\"fullName\" : \"Lorenzo Phil\","+ + "\"gender\" : \"M\","+ + "\"contactPhone\" : \"364-610-4444\","+ + "\"confNo\" : \"QI3V6Q\","+ + "\"bagInfo\" : [ {"+ + "\"id\" : \"79039899187755\","+ + "\"tagNum\" : \"17657806240001\","+ + "\"routing\" : \"SFO/IST/ATH/JTR\","+ + "\"lastActionCode\" : \"OFFLOAD\","+ + "\"lastActionDesc\" : \"OFFLOAD\","+ + "\"lastSeenStation\" : \"JTR\","+ + "\"flightLegs\" : [ {"+ + "\"flightNo\" : \"BM318\","+ + "\"flightDate\" : \"2019-03-12T03:00:00Z\","+ + "\"fltRouteSrc\" : \"SFO\","+ + "\"fltRouteDest\" : \"IST\","+ + "\"estimatedArrival\" : \"2019-03-12T16:00:00Z\","+ + "\"actions\" : [ {"+ + "\"actionAt\" : \"SFO\","+ + "\"actionCode\" : \"ONLOAD to IST\","+ + "\"actionTime\" : \"2019-03-12T03:11:00Z\""+ + "}, {"+ + "\"actionAt\" : \"SFO\","+ + "\"actionCode\" : \"BagTag Scan at SFO\","+ + "\"actionTime\" : \"2019-03-12T02:49:00Z\""+ + "}, {"+ + "\"actionAt\" : \"SFO\","+ + "\"actionCode\" : \"Checkin at SFO\","+ + "\"actionTime\" : \"2019-03-12T01:50:00Z\""+ + "} ]"+ + "}, {"+ + "\"flightNo\" : \"BM696\","+ + "\"flightDate\" : \"2019-03-12T04:11:00Z\","+ + "\"fltRouteSrc\" : \"IST\","+ + "\"fltRouteDest\" : \"ATH\","+ + "\"estimatedArrival\" : \"2019-03-13T03:14:00Z\","+ + "\"actions\" : [ {"+ + "\"actionAt\" : \"IST\","+ + "\"actionCode\" : \"ONLOAD to ATH\","+ + "\"actionTime\" : \"2019-03-13T04:10:00Z\""+ + "}, {"+ + "\"actionAt\" : \"IST\","+ + "\"actionCode\" : \"BagTag Scan at IST\","+ + "\"actionTime\" : \"2019-03-13T03:56:00Z\""+ + "}, {"+ + "\"actionAt\" : \"IST\","+ + "\"actionCode\" : \"OFFLOAD from IST\","+ + "\"actionTime\" : \"2019-03-13T03:59:00Z\""+ + "} ]"+ + "}, {"+ + "\"flightNo\" : \"BM665\","+ + "\"flightDate\" : \"2019-03-12T03:11:00Z\","+ + "\"fltRouteSrc\" : \"ATH\","+ + "\"fltRouteDest\" : \"JTR\","+ + "\"estimatedArrival\" : \"2019-03-12T15:12:00Z\","+ + "\"actions\" : [ {"+ + "\"actionAt\" : \"JTR\","+ + "\"actionCode\" : \"Offload to Carousel at JTR\","+ + "\"actionTime\" : \"2019-03-12T15:06:00Z\""+ + "}, {"+ + "\"actionAt\" : \"ATH\","+ + "\"actionCode\" : \"ONLOAD to JTR\","+ + "\"actionTime\" : \"2019-03-12T14:16:00Z\""+ + "}, {"+ + "\"actionAt\" : \"ATH\","+ + "\"actionCode\" : \"OFFLOAD from ATH\","+ + "\"actionTime\" : \"2019-03-12T14:13:00Z\""+ + "} ]"+ + "} ],"+ + "\"lastSeenTimeGmt\" : \"2019-03-12T15:05:00Z\","+ + "\"bagArrivalDate\" : \"2019-03-12T15:05:00Z\""+ + "},"+ + "{"+ + "\"id\" : \"79039899197755\","+ + "\"tagNum\" : \"17657806340001\","+ + "\"routing\" : \"SFO/IST/ATH/JTR\","+ + "\"lastActionCode\" : \"OFFLOAD\","+ + "\"lastActionDesc\" : \"OFFLOAD\","+ + "\"lastSeenStation\" : \"JTR\","+ + "\"flightLegs\" : [ {"+ + "\"flightNo\" : \"BM318\","+ + "\"flightDate\" : \"2019-03-12T03:00:00Z\","+ + "\"fltRouteSrc\" : \"SFO\","+ + "\"fltRouteDest\" : \"IST\","+ + "\"estimatedArrival\" : \"2019-03-12T16:40:00Z\","+ + "\"actions\" : [ {"+ + "\"actionAt\" : \"SFO\","+ + "\"actionCode\" : \"ONLOAD to IST\","+ + "\"actionTime\" : \"2019-03-12T03:14:00Z\""+ + "}, {"+ + "\"actionAt\" : \"SFO\","+ + "\"actionCode\" : \"BagTag Scan at SFO\","+ + "\"actionTime\" : \"2019-03-12T02:50:00Z\""+ + "}, {"+ + "\"actionAt\" : \"SFO\","+ + "\"actionCode\" : \"Checkin at SFO\","+ + "\"actionTime\" : \"2019-03-12T01:58:00Z\""+ + "} ]"+ + "}, {"+ + "\"flightNo\" : \"BM696\","+ + "\"flightDate\" : \"2019-03-12T04:11:00Z\","+ + "\"fltRouteSrc\" : \"IST\","+ + "\"fltRouteDest\" : \"ATH\","+ + "\"estimatedArrival\" : \"2019-03-13T03:18:00Z\","+ + "\"actions\" : [ {"+ + "\"actionAt\" : \"IST\","+ + "\"actionCode\" : \"ONLOAD to ATH\","+ + "\"actionTime\" : \"2019-03-13T04:17:00Z\""+ + "}, {"+ + "\"actionAt\" : \"IST\","+ + "\"actionCode\" : \"BagTag Scan at IST\","+ + "\"actionTime\" : \"2019-03-13T03:59:00Z\""+ + "}, {"+ + "\"actionAt\" : \"IST\","+ + "\"actionCode\" : \"OFFLOAD from IST\","+ + "\"actionTime\" : \"2019-03-13T03:48:00Z\""+ + "} ]"+ + "}, {"+ + "\"flightNo\" : \"BM665\","+ + "\"flightDate\" : \"2019-03-12T03:11:00Z\","+ + "\"fltRouteSrc\" : \"ATH\","+ + "\"fltRouteDest\" : \"JTR\","+ + "\"estimatedArrival\" : \"2019-03-12T15:12:00Z\","+ + "\"actions\" : [ {"+ + "\"actionAt\" : \"JTR\","+ + "\"actionCode\" : \"Offload to Carousel at JTR\","+ + "\"actionTime\" : \"2019-03-12T15:06:00Z\""+ + "}, {"+ + "\"actionAt\" : \"ATH\","+ + "\"actionCode\" : \"ONLOAD to JTR\","+ + "\"actionTime\" : \"2019-03-12T14:16:00Z\""+ + "}, {"+ + "\"actionAt\" : \"ATH\","+ + "\"actionCode\" : \"OFFLOAD from ATH\","+ + "\"actionTime\" : \"2019-03-12T14:23:00Z\""+ + "} ]"+ + "} ],"+ + "\"lastSeenTimeGmt\" : \"2019-03-12T16:05:00Z\","+ + "\"bagArrivalDate\" : \"2019-03-12T16:25:00Z\""+ + "} ]"+ + "}"; + + + + static FieldValue val1 = FieldValue.createFromJson(bag1,null); + static FieldValue val2 = FieldValue.createFromJson(bag2,null); + static FieldValue val3 = FieldValue.createFromJson(bag3,null); + static FieldValue val4 = FieldValue.createFromJson(bag4,null); + + public static void main(String[] args) throws Exception { + /* UNCOMMENT the lines of code below if you are using Oracle NoSQL + * Database Cloud service. + * Leave the lines commented if you are using onPremise database + * Add the appropriate values of your region and compartment OCID + String region =""; + String compId =""; + handle = generateNoSQLHandleCloud(region,compId); */ + + /* UNCOMMENT the 2 lines of code below if you are using onPremise Oracle + * NoSQL Database. + * Leave the lines commented if you are using NoSQL Database Cloud Service + * Give appropriate value of your endpoint for the onPremise kvstore. + String kvstore_endpoint ="http://:8080"; + handle = generateNoSQLHandleonPrem(kvstore_endpoint); */ + try { + createTable(handle); + writeRows(handle, (MapValue)val1); + writeRows(handle, (MapValue)val2); + writeRows(handle, (MapValue)val3); + writeRows(handle, (MapValue)val4); + String paran_expr="SELECT fullName, bag.bagInfo.tagNum, bag.bagInfo.routing, "+ + "bag.bagInfo[].flightLegs[].fltRouteDest FROM BaggageInfo bag WHERE "+ + "bag.bagInfo.flightLegs[].fltRouteSrc=any \"SFO\" AND "+ + "(bag.bagInfo[].flightLegs[].fltRouteDest=any \"ATH\" OR "+ + "bag.bagInfo[].flightLegs[].fltRouteDest=any \"JTR\" )"; + System.out.println("Using Paranthesized expression "); + fetchRows(handle,paran_expr); + String case_expr="SELECT fullName,"+ + "CASE WHEN NOT exists bag.bagInfo.flightLegs[0] "+ + "THEN \"you have no bag info\" "+ + "WHEN NOT exists bag.bagInfo.flightLegs[1] "+ + "THEN \"you have one hop\" "+ + "WHEN NOT exists bag.bagInfo.flightLegs[2] "+ + "THEN \"you have two hops.\" "+ + "ELSE \"you have three hops.\" "+ + "END AS NUMBER_HOPS "+ + "FROM BaggageInfo bag WHERE ticketNo=1762341772625"; + System.out.println("Using Case Expression "); + fetchRows(handle,case_expr); + String seq_trn_expr="SELECT seq_transform(l.bagInfo[],"+ + "seq_transform("+ + "$sq1.flightLegs[],"+ + "seq_transform("+ + "$sq2.actions[],"+ + "{"+ + "\"at\" : $sq3.actionAt,"+ + "\"action\" : $sq3.actionCode,"+ + "\"flightNo\" : $sq2.flightNo,"+ + "\"tagNum\" : $sq1.tagNum"+ + "}"+ + ")"+ + ")"+ + ") AS actions FROM baggageInfo l WHERE ticketNo=1762376407826"; + System.out.println("Using Sequence Transform Expressions "); + fetchRows(handle,seq_trn_expr); + + } catch (Exception e) { + System.err.print(e); + } finally { + handle.close(); + } + } + /* Create a NoSQL handle to access the onPremise Oracle NoSQL database */ + private static NoSQLHandle generateNoSQLHandleonPrem(String kvstore_endpoint) throws Exception { + NoSQLHandleConfig config = new NoSQLHandleConfig(kvstore_endpoint); + config.setAuthorizationProvider(new StoreAccessTokenProvider()); + /* If using a secure store, uncomment the line below and pass username, + * password of the store to StoreAccessTokenProvider + */ + /* config.setAuthorizationProvider(new StoreAccessTokenProvider(username, password)); */ + NoSQLHandle handle = NoSQLHandleFactory.createNoSQLHandle(config); + return handle; + } + + /* Create a NoSQL handle to access the cloud service */ + private static NoSQLHandle generateNoSQLHandleCloud(String region, String compId) throws Exception { + SignatureProvider ap = new SignatureProvider(); + NoSQLHandleConfig config = new NoSQLHandleConfig(region, ap); + // set your default compartment + config.setDefaultCompartment(compId); + NoSQLHandle handle = NoSQLHandleFactory.createNoSQLHandle(config); + return handle; + } + + /** + * Create a simple table and set your desired table capacity + */ + private static void createTable(NoSQLHandle handle) throws Exception { + String createTableDDL = "CREATE TABLE IF NOT EXISTS " + tableName + + "(ticketNo LONG," + + "fullName STRING," + + "gender STRING ," + + "contactPhone STRING ," + + "confNo STRING ," + + "bagInfo JSON, " + + "PRIMARY KEY(ticketNo))"; + + TableLimits limits = new TableLimits(20, 20, 1); + TableRequest treq = new TableRequest() + .setStatement(createTableDDL).setTableLimits(limits); + + TableResult tres = handle.tableRequest(treq); + /* The request is async, + * so wait for the table to become active. + */ + tres.waitForCompletion(handle, 60000, /* wait 60 sec */ + 1000); /* delay ms for poll */ + System.out.println("Table " + tableName + " is active"); + } + + /** + * Add a Row in the table + */ + private static void writeRows(NoSQLHandle handle, MapValue value) throws Exception { + PutRequest putRequest = + new PutRequest().setValue(value).setTableName(tableName); + PutResult putResult = handle.put(putRequest); + } + //Fetch rows from the table + private static void fetchRows(NoSQLHandle handle,String sqlstmt) throws Exception { + + try ( + QueryRequest queryRequest = new QueryRequest().setStatement(sqlstmt); + QueryIterableResult results = handle.queryIterable(queryRequest)){ + for (MapValue res : results) { + System.out.println("\t" + res); + } + } + } +} diff --git a/examples-nosql-java-sdk/sqlexamples/SQLFunctions.java b/examples-nosql-java-sdk/sqlexamples/SQLFunctions.java new file mode 100644 index 0000000..444e0e2 --- /dev/null +++ b/examples-nosql-java-sdk/sqlexamples/SQLFunctions.java @@ -0,0 +1,466 @@ +/* Copyright (c) 2023, 2024 Oracle and/or its affiliates. + * Licensed under the Universal Permissive License v 1.0 as shown at + * https://oss.oracle.com/licenses/upl/ +*/ +import java.io.File; +import oracle.nosql.driver.NoSQLHandle; +import oracle.nosql.driver.NoSQLHandleConfig; +import oracle.nosql.driver.NoSQLHandleFactory; +import oracle.nosql.driver.Region; +import oracle.nosql.driver.iam.SignatureProvider; +import oracle.nosql.driver.kv.StoreAccessTokenProvider; +import oracle.nosql.driver.ops.PutRequest; +import oracle.nosql.driver.ops.PutResult; +import oracle.nosql.driver.ops.TableLimits; +import oracle.nosql.driver.ops.TableRequest; +import oracle.nosql.driver.ops.TableResult; +import oracle.nosql.driver.values.MapValue; +import oracle.nosql.driver.values.FieldValue; +import oracle.nosql.driver.ops.QueryRequest; +import oracle.nosql.driver.ops.QueryResult; +import oracle.nosql.driver.ops.QueryIterableResult; + +public class SQLFunctions { + /* Name of your table */ + final static String tableName = "BaggageInfo"; + static NoSQLHandle handle; + final static String bag1 = "{"+ + "\"ticketNo\" : \"1762376407826\","+ + "\"fullName\" : \"Dierdre Amador\","+ + "\"gender\" : \"M\","+ + "\"contactPhone\" : \"165-742-5715\","+ + "\"confNo\" : \"ZG8Z5N\","+ + "\"bagInfo\" : [ {"+ + "\"id\" : \"7903989918469\","+ + "\"tagNum\" : \"17657806240229\","+ + "\"routing\" : \"JFK/MAD\","+ + "\"lastActionCode\" : \"OFFLOAD\","+ + "\"lastActionDesc\" : \"OFFLOAD\","+ + "\"lastSeenStation\" : \"MAD\","+ + "\"flightLegs\" : [ {"+ + "\"flightNo\" : \"BM495\","+ + "\"flightDate\" : \"2019-03-07T07:00:00Z\","+ + "\"fltRouteSrc\" : \"JFK\","+ + "\"fltRouteDest\" : \"MAD\","+ + "\"estimatedArrival\" : \"2019-03-07T14:00:00Z\","+ + "\"actions\" : [ {"+ + "\"actionAt\" : \"MAD\","+ + "\"actionCode\" : \"Offload to Carousel at MAD\","+ + "\"actionTime\" : \"2019-03-07T13:54:00Z\""+ + "}, {"+ + "\"actionAt\" : \"JFK\","+ + "\"actionCode\" : \"ONLOAD to MAD\","+ + "\"actionTime\" : \"2019-03-07T07:00:00Z\""+ + "}, {"+ + "\"actionAt\" : \"JFK\","+ + "\"actionCode\" : \"BagTag Scan at JFK\","+ + "\"actionTime\" : \"2019-03-07T06:53:00Z\""+ + "}, {"+ + "\"actionAt\" : \"JFK\","+ + "\"actionCode\" : \"Checkin at JFK\","+ + "\"actionTime\" : \"2019-03-07T05:03:00Z\""+ + "} ]"+ + "} ],"+ + "\"lastSeenTimeGmt\" : \"2019-03-07T13:51:00Z\","+ + "\"bagArrivalDate\" : \"2019-03-07T13:51:00Z\""+ + "} ]"+ + "}"; + + final static String bag2 = "{"+ + "\"ticketNo\" : \"1762344493810\","+ + "\"fullName\" : \"Dierdre Amador\","+ + "\"gender\" : \"M\","+ + "\"contactPhone\" : \"893-324-1064\","+ + "\"confNo\" : \"LE6J4Z\","+ + "\"bagInfo\" : [ {"+ + "\"id\" : \"79039899165297\","+ + "\"tagNum\" : \"17657806255240\","+ + "\"routing\" : \"MIA/LAX/MEL\","+ + "\"lastActionCode\" : \"OFFLOAD\","+ + "\"lastActionDesc\" : \"OFFLOAD\","+ + "\"lastSeenStation\" : \"MEL\","+ + "\"flightLegs\" : [ {"+ + "\"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\""+ + "} ]"+ + "}, {"+ + "\"flightNo\" : \"BM667\","+ + "\"flightDate\" : \"2019-02-01T06:13:00Z\","+ + "\"fltRouteSrc\" : \"LAX\","+ + "\"fltRouteDest\" : \"MEL\","+ + "\"estimatedArrival\" : \"2019-02-01T16:15:00Z\","+ + "\"actions\" : [ {"+ + "\"actionAt\" : \"MEL\","+ + "\"actionCode\" : \"Offload to Carousel at MEL\","+ + "\"actionTime\" : \"2019-02-01T16:15:00Z\""+ + "}, {"+ + "\"actionAt\" : \"LAX\","+ + "\"actionCode\" : \"ONLOAD to MEL\","+ + "\"actionTime\" : \"2019-02-01T15:35:00Z\""+ + "}, {"+ + "\"actionAt\" : \"LAX\","+ + "\"actionCode\" : \"OFFLOAD from LAX\","+ + "\"actionTime\" : \"2019-02-01T15:18:00Z\""+ + "} ]"+ + "} ],"+ + "\"lastSeenTimeGmt\" : \"2019-02-01T16:13:00Z\","+ + "\"bagArrivalDate\" : \"2019-02-01T16:13:00Z\""+ + "} ]"+ + "}"; + final static String bag3 = "{"+ + "\"ticketNo\" : \"1762341772625\","+ + "\"fullName\" : \"Gerard Greene\","+ + "\"gender\" : \"M\","+ + "\"contactPhone\" : \"395-837-3772\","+ + "\"confNo\" : \"MC0E7R\","+ + "\"bagInfo\" : [ {"+ + "\"id\" : \"79039899152842\","+ + "\"tagNum\" : \"1765780626568\","+ + "\"routing\" : \"SFO/IST/ATH/JTR\","+ + "\"lastActionCode\" : \"OFFLOAD\","+ + "\"lastActionDesc\" : \"OFFLOAD\","+ + "\"lastSeenStation\" : \"JTR\","+ + "\"flightLegs\" : [ {"+ + "\"flightNo\" : \"BM318\","+ + "\"flightDate\" : \"2019-03-07T04:00:00Z\","+ + "\"fltRouteSrc\" : \"SFO\","+ + "\"fltRouteDest\" : \"IST\","+ + "\"estimatedArrival\" : \"2019-03-07T17:00:00Z\","+ + "\"actions\" : [ {"+ + "\"actionAt\" : \"SFO\","+ + "\"actionCode\" : \"ONLOAD to IST\","+ + "\"actionTime\" : \"2019-03-07T04:08:00Z\""+ + "}, {"+ + "\"actionAt\" : \"SFO\","+ + "\"actionCode\" : \"BagTag Scan at SFO\","+ + "\"actionTime\" : \"2019-03-07T03:53:00Z\""+ + "}, {"+ + "\"actionAt\" : \"SFO\","+ + "\"actionCode\" : \"Checkin at SFO\","+ + "\"actionTime\" : \"2019-03-07T02:20:00Z\""+ + "} ]"+ + "}, {"+ + "\"flightNo\" : \"BM696\","+ + "\"flightDate\" : \"2019-03-07T05:08:00Z\","+ + "\"fltRouteSrc\" : \"IST\","+ + "\"fltRouteDest\" : \"ATH\","+ + "\"estimatedArrival\" : \"2019-03-08T04:10:00Z\","+ + "\"actions\" : [ {"+ + "\"actionAt\" : \"IST\","+ + "\"actionCode\" : \"ONLOAD to ATH\","+ + "\"actionTime\" : \"2019-03-08T04:55:00Z\""+ + "}, {"+ + "\"actionAt\" : \"IST\","+ + "\"actionCode\" : \"BagTag Scan at IST\","+ + "\"actionTime\" : \"2019-03-08T04:34:00Z\""+ + "}, {"+ + "\"actionAt\" : \"IST\","+ + "\"actionCode\" : \"OFFLOAD from IST\","+ + "\"actionTime\" : \"2019-03-08T04:47:00Z\""+ + "} ]"+ + "}, {"+ + "\"flightNo\" : \"BM665\","+ + "\"flightDate\" : \"2019-03-07T04:08:00Z\","+ + "\"fltRouteSrc\" : \"ATH\","+ + "\"fltRouteDest\" : \"JTR\","+ + "\"estimatedArrival\" : \"2019-03-07T16:10:00Z\","+ + "\"actions\" : [ {"+ + "\"actionAt\" : \"JTR\","+ + "\"actionCode\" : \"Offload to Carousel at JTR\","+ + "\"actionTime\" : \"2019-03-07T16:09:00Z\""+ + "}, {"+ + "\"actionAt\" : \"ATH\","+ + "\"actionCode\" : \"ONLOAD to JTR\","+ + "\"actionTime\" : \"2019-03-07T15:51:00Z\""+ + "}, {"+ + "\"actionAt\" : \"ATH\","+ + "\"actionCode\" : \"OFFLOAD from ATH\","+ + "\"actionTime\" : \"2019-03-07T15:43:00Z\""+ + "} ]"+ + "} ],"+ + "\"lastSeenTimeGmt\" : \"2019-03-07T16:01:00Z\","+ + "\"bagArrivalDate\" : \"2019-03-07T16:01:00Z\""+ + "} ]"+ + "}"; + + final static String bag4 = "{"+ + "\"ticketNo\" : \"1762320369957\","+ + "\"fullName\" : \"Lorenzo Phil\","+ + "\"gender\" : \"M\","+ + "\"contactPhone\" : \"364-610-4444\","+ + "\"confNo\" : \"QI3V6Q\","+ + "\"bagInfo\" : [ {"+ + "\"id\" : \"79039899187755\","+ + "\"tagNum\" : \"17657806240001\","+ + "\"routing\" : \"SFO/IST/ATH/JTR\","+ + "\"lastActionCode\" : \"OFFLOAD\","+ + "\"lastActionDesc\" : \"OFFLOAD\","+ + "\"lastSeenStation\" : \"JTR\","+ + "\"flightLegs\" : [ {"+ + "\"flightNo\" : \"BM318\","+ + "\"flightDate\" : \"2019-03-12T03:00:00Z\","+ + "\"fltRouteSrc\" : \"SFO\","+ + "\"fltRouteDest\" : \"IST\","+ + "\"estimatedArrival\" : \"2019-03-12T16:00:00Z\","+ + "\"actions\" : [ {"+ + "\"actionAt\" : \"SFO\","+ + "\"actionCode\" : \"ONLOAD to IST\","+ + "\"actionTime\" : \"2019-03-12T03:11:00Z\""+ + "}, {"+ + "\"actionAt\" : \"SFO\","+ + "\"actionCode\" : \"BagTag Scan at SFO\","+ + "\"actionTime\" : \"2019-03-12T02:49:00Z\""+ + "}, {"+ + "\"actionAt\" : \"SFO\","+ + "\"actionCode\" : \"Checkin at SFO\","+ + "\"actionTime\" : \"2019-03-12T01:50:00Z\""+ + "} ]"+ + "}, {"+ + "\"flightNo\" : \"BM696\","+ + "\"flightDate\" : \"2019-03-12T04:11:00Z\","+ + "\"fltRouteSrc\" : \"IST\","+ + "\"fltRouteDest\" : \"ATH\","+ + "\"estimatedArrival\" : \"2019-03-13T03:14:00Z\","+ + "\"actions\" : [ {"+ + "\"actionAt\" : \"IST\","+ + "\"actionCode\" : \"ONLOAD to ATH\","+ + "\"actionTime\" : \"2019-03-13T04:10:00Z\""+ + "}, {"+ + "\"actionAt\" : \"IST\","+ + "\"actionCode\" : \"BagTag Scan at IST\","+ + "\"actionTime\" : \"2019-03-13T03:56:00Z\""+ + "}, {"+ + "\"actionAt\" : \"IST\","+ + "\"actionCode\" : \"OFFLOAD from IST\","+ + "\"actionTime\" : \"2019-03-13T03:59:00Z\""+ + "} ]"+ + "}, {"+ + "\"flightNo\" : \"BM665\","+ + "\"flightDate\" : \"2019-03-12T03:11:00Z\","+ + "\"fltRouteSrc\" : \"ATH\","+ + "\"fltRouteDest\" : \"JTR\","+ + "\"estimatedArrival\" : \"2019-03-12T15:12:00Z\","+ + "\"actions\" : [ {"+ + "\"actionAt\" : \"JTR\","+ + "\"actionCode\" : \"Offload to Carousel at JTR\","+ + "\"actionTime\" : \"2019-03-12T15:06:00Z\""+ + "}, {"+ + "\"actionAt\" : \"ATH\","+ + "\"actionCode\" : \"ONLOAD to JTR\","+ + "\"actionTime\" : \"2019-03-12T14:16:00Z\""+ + "}, {"+ + "\"actionAt\" : \"ATH\","+ + "\"actionCode\" : \"OFFLOAD from ATH\","+ + "\"actionTime\" : \"2019-03-12T14:13:00Z\""+ + "} ]"+ + "} ],"+ + "\"lastSeenTimeGmt\" : \"2019-03-12T15:05:00Z\","+ + "\"bagArrivalDate\" : \"2019-03-12T15:05:00Z\""+ + "},"+ + "{"+ + "\"id\" : \"79039899197755\","+ + "\"tagNum\" : \"17657806340001\","+ + "\"routing\" : \"SFO/IST/ATH/JTR\","+ + "\"lastActionCode\" : \"OFFLOAD\","+ + "\"lastActionDesc\" : \"OFFLOAD\","+ + "\"lastSeenStation\" : \"JTR\","+ + "\"flightLegs\" : [ {"+ + "\"flightNo\" : \"BM318\","+ + "\"flightDate\" : \"2019-03-12T03:00:00Z\","+ + "\"fltRouteSrc\" : \"SFO\","+ + "\"fltRouteDest\" : \"IST\","+ + "\"estimatedArrival\" : \"2019-03-12T16:40:00Z\","+ + "\"actions\" : [ {"+ + "\"actionAt\" : \"SFO\","+ + "\"actionCode\" : \"ONLOAD to IST\","+ + "\"actionTime\" : \"2019-03-12T03:14:00Z\""+ + "}, {"+ + "\"actionAt\" : \"SFO\","+ + "\"actionCode\" : \"BagTag Scan at SFO\","+ + "\"actionTime\" : \"2019-03-12T02:50:00Z\""+ + "}, {"+ + "\"actionAt\" : \"SFO\","+ + "\"actionCode\" : \"Checkin at SFO\","+ + "\"actionTime\" : \"2019-03-12T01:58:00Z\""+ + "} ]"+ + "}, {"+ + "\"flightNo\" : \"BM696\","+ + "\"flightDate\" : \"2019-03-12T04:11:00Z\","+ + "\"fltRouteSrc\" : \"IST\","+ + "\"fltRouteDest\" : \"ATH\","+ + "\"estimatedArrival\" : \"2019-03-13T03:18:00Z\","+ + "\"actions\" : [ {"+ + "\"actionAt\" : \"IST\","+ + "\"actionCode\" : \"ONLOAD to ATH\","+ + "\"actionTime\" : \"2019-03-13T04:17:00Z\""+ + "}, {"+ + "\"actionAt\" : \"IST\","+ + "\"actionCode\" : \"BagTag Scan at IST\","+ + "\"actionTime\" : \"2019-03-13T03:59:00Z\""+ + "}, {"+ + "\"actionAt\" : \"IST\","+ + "\"actionCode\" : \"OFFLOAD from IST\","+ + "\"actionTime\" : \"2019-03-13T03:48:00Z\""+ + "} ]"+ + "}, {"+ + "\"flightNo\" : \"BM665\","+ + "\"flightDate\" : \"2019-03-12T03:11:00Z\","+ + "\"fltRouteSrc\" : \"ATH\","+ + "\"fltRouteDest\" : \"JTR\","+ + "\"estimatedArrival\" : \"2019-03-12T15:12:00Z\","+ + "\"actions\" : [ {"+ + "\"actionAt\" : \"JTR\","+ + "\"actionCode\" : \"Offload to Carousel at JTR\","+ + "\"actionTime\" : \"2019-03-12T15:06:00Z\""+ + "}, {"+ + "\"actionAt\" : \"ATH\","+ + "\"actionCode\" : \"ONLOAD to JTR\","+ + "\"actionTime\" : \"2019-03-12T14:16:00Z\""+ + "}, {"+ + "\"actionAt\" : \"ATH\","+ + "\"actionCode\" : \"OFFLOAD from ATH\","+ + "\"actionTime\" : \"2019-03-12T14:23:00Z\""+ + "} ]"+ + "} ],"+ + "\"lastSeenTimeGmt\" : \"2019-03-12T16:05:00Z\","+ + "\"bagArrivalDate\" : \"2019-03-12T16:25:00Z\""+ + "} ]"+ + "}"; + + + + static FieldValue val1 = FieldValue.createFromJson(bag1,null); + static FieldValue val2 = FieldValue.createFromJson(bag2,null); + static FieldValue val3 = FieldValue.createFromJson(bag3,null); + static FieldValue val4 = FieldValue.createFromJson(bag4,null); + + public static void main(String[] args) throws Exception { + /* UNCOMMENT the lines of code below if you are using Oracle NoSQL + * Database Cloud service. + * Leave the lines commented if you are using onPremise database + * Add the appropriate values of your region and compartment OCID + String region =""; + String compId =""; + handle = generateNoSQLHandleCloud(region,compId); */ + + /* UNCOMMENT the 2 lines of code below if you are using onPremise Oracle + * NoSQL Database. + * Leave the lines commented if you are using NoSQL Database Cloud Service + * Give appropriate value of your endpoint for the onPremise kvstore. + String kvstore_endpoint ="http://:8080"; + handle = generateNoSQLHandleonPrem(kvstore_endpoint); */ + try { + createTable(handle); + writeRows(handle, (MapValue)val1); + writeRows(handle, (MapValue)val2); + writeRows(handle, (MapValue)val3); + writeRows(handle, (MapValue)val4); + String ts_func1="SELECT timestamp_add(bag.bagInfo.flightLegs[0].estimatedArrival, \"5 minutes\")"+ + " AS ARRIVAL_TIME FROM BaggageInfo bag WHERE ticketNo=1762341772625"; + System.out.println("Using timestamp_add function "); + fetchRows(handle,ts_func1); + String ts_func2="SELECT $s.ticketno, $bagInfo.bagArrivalDate, $flightLeg.flightDate, "+ + "get_duration(timestamp_diff($bagInfo.bagArrivalDate, $flightLeg.flightDate)) AS diff "+ + "FROM baggageinfo $s, $s.bagInfo[] AS $bagInfo, $bagInfo.flightLegs[] AS $flightLeg "+ + "WHERE ticketNo=1762344493810"; + System.out.println("Using get_duration and timestamp_diff function "); + fetchRows(handle,ts_func2); + String string_func1="SELECT substring(bag.baginfo.routing,0,3) AS Source FROM baggageInfo bag WHERE ticketNo=1762376407826"; + System.out.println("Using substring function "); + fetchRows(handle,string_func1); + String string_func2="SELECT fullname, length(fullname) AS fullname_length FROM BaggageInfo WHERE ticketNo=1762320369957"; + System.out.println("Using length function "); + fetchRows(handle,string_func2); + String string_func3="SELECT fullname FROM baggageInfo bag WHERE EXISTS bag.bagInfo[contains($element.routing,\"SFO\")]"; + System.out.println("Using contains function "); + fetchRows(handle,string_func3); + + } catch (Exception e) { + System.err.print(e); + } finally { + handle.close(); + } + } + /* Create a NoSQL handle to access the onPremise Oracle NoSQL database */ + private static NoSQLHandle generateNoSQLHandleonPrem(String kvstore_endpoint) throws Exception { + NoSQLHandleConfig config = new NoSQLHandleConfig(kvstore_endpoint); + config.setAuthorizationProvider(new StoreAccessTokenProvider()); + /* If using a secure store, uncomment the line below and pass username, + * password of the store to StoreAccessTokenProvider + */ + /* config.setAuthorizationProvider(new StoreAccessTokenProvider(username, password)); */ + NoSQLHandle handle = NoSQLHandleFactory.createNoSQLHandle(config); + return handle; + } + + /* Create a NoSQL handle to access the cloud service */ + private static NoSQLHandle generateNoSQLHandleCloud(String region, String compId) throws Exception { + SignatureProvider ap = new SignatureProvider(); + NoSQLHandleConfig config = new NoSQLHandleConfig(region, ap); + // set your default compartment + config.setDefaultCompartment(compId); + NoSQLHandle handle = NoSQLHandleFactory.createNoSQLHandle(config); + return handle; + } + + /** + * Create a simple table and set your desired table capacity + */ + private static void createTable(NoSQLHandle handle) throws Exception { + String createTableDDL = "CREATE TABLE IF NOT EXISTS " + tableName + + "(ticketNo LONG," + + "fullName STRING," + + "gender STRING ," + + "contactPhone STRING ," + + "confNo STRING ," + + "bagInfo JSON, " + + "PRIMARY KEY(ticketNo))"; + + TableLimits limits = new TableLimits(20, 20, 1); + TableRequest treq = new TableRequest() + .setStatement(createTableDDL).setTableLimits(limits); + + TableResult tres = handle.tableRequest(treq); + /* The request is async, + * so wait for the table to become active. + */ + tres.waitForCompletion(handle, 60000, /* wait 60 sec */ + 1000); /* delay ms for poll */ + System.out.println("Table " + tableName + " is active"); + } + + /** + * Add a Row in the table + */ + private static void writeRows(NoSQLHandle handle, MapValue value) throws Exception { + PutRequest putRequest = + new PutRequest().setValue(value).setTableName(tableName); + PutResult putResult = handle.put(putRequest); + } + //Fetch rows from the table + private static void fetchRows(NoSQLHandle handle,String sqlstmt) throws Exception { + + try ( + QueryRequest queryRequest = new QueryRequest().setStatement(sqlstmt); + QueryIterableResult results = handle.queryIterable(queryRequest)){ + for (MapValue res : results) { + System.out.println("\t" + res); + } + } + } +} diff --git a/examples-nosql-java-sdk/sqlexamples/SQLOperators.java b/examples-nosql-java-sdk/sqlexamples/SQLOperators.java new file mode 100644 index 0000000..23a011a --- /dev/null +++ b/examples-nosql-java-sdk/sqlexamples/SQLOperators.java @@ -0,0 +1,463 @@ +/* Copyright (c) 2023, 2024 Oracle and/or its affiliates. + * Licensed under the Universal Permissive License v 1.0 as shown at + * https://oss.oracle.com/licenses/upl/ +*/ +import java.io.File; +import oracle.nosql.driver.NoSQLHandle; +import oracle.nosql.driver.NoSQLHandleConfig; +import oracle.nosql.driver.NoSQLHandleFactory; +import oracle.nosql.driver.Region; +import oracle.nosql.driver.iam.SignatureProvider; +import oracle.nosql.driver.kv.StoreAccessTokenProvider; +import oracle.nosql.driver.ops.PutRequest; +import oracle.nosql.driver.ops.PutResult; +import oracle.nosql.driver.ops.TableLimits; +import oracle.nosql.driver.ops.TableRequest; +import oracle.nosql.driver.ops.TableResult; +import oracle.nosql.driver.values.MapValue; +import oracle.nosql.driver.values.FieldValue; +import oracle.nosql.driver.ops.QueryRequest; +import oracle.nosql.driver.ops.QueryResult; +import oracle.nosql.driver.ops.QueryIterableResult; + +public class SQLOperators { + /* Name of your table */ + final static String tableName = "BaggageInfo"; + static NoSQLHandle handle; + final static String bag1 = "{"+ + "\"ticketNo\" : \"1762376407826\","+ + "\"fullName\" : \"Dierdre Amador\","+ + "\"gender\" : \"M\","+ + "\"contactPhone\" : \"165-742-5715\","+ + "\"confNo\" : \"ZG8Z5N\","+ + "\"bagInfo\" : [ {"+ + "\"id\" : \"7903989918469\","+ + "\"tagNum\" : \"17657806240229\","+ + "\"routing\" : \"JFK/MAD\","+ + "\"lastActionCode\" : \"OFFLOAD\","+ + "\"lastActionDesc\" : \"OFFLOAD\","+ + "\"lastSeenStation\" : \"MAD\","+ + "\"flightLegs\" : [ {"+ + "\"flightNo\" : \"BM495\","+ + "\"flightDate\" : \"2019-03-07T07:00:00Z\","+ + "\"fltRouteSrc\" : \"JFK\","+ + "\"fltRouteDest\" : \"MAD\","+ + "\"estimatedArrival\" : \"2019-03-07T14:00:00Z\","+ + "\"actions\" : [ {"+ + "\"actionAt\" : \"MAD\","+ + "\"actionCode\" : \"Offload to Carousel at MAD\","+ + "\"actionTime\" : \"2019-03-07T13:54:00Z\""+ + "}, {"+ + "\"actionAt\" : \"JFK\","+ + "\"actionCode\" : \"ONLOAD to MAD\","+ + "\"actionTime\" : \"2019-03-07T07:00:00Z\""+ + "}, {"+ + "\"actionAt\" : \"JFK\","+ + "\"actionCode\" : \"BagTag Scan at JFK\","+ + "\"actionTime\" : \"2019-03-07T06:53:00Z\""+ + "}, {"+ + "\"actionAt\" : \"JFK\","+ + "\"actionCode\" : \"Checkin at JFK\","+ + "\"actionTime\" : \"2019-03-07T05:03:00Z\""+ + "} ]"+ + "} ],"+ + "\"lastSeenTimeGmt\" : \"2019-03-07T13:51:00Z\","+ + "\"bagArrivalDate\" : \"2019-03-07T13:51:00Z\""+ + "} ]"+ + "}"; + + final static String bag2 = "{"+ + "\"ticketNo\" : \"1762344493810\","+ + "\"fullName\" : \"Dierdre Amador\","+ + "\"gender\" : \"M\","+ + "\"contactPhone\" : \"893-324-1064\","+ + "\"confNo\" : \"LE6J4Z\","+ + "\"bagInfo\" : [ {"+ + "\"id\" : \"79039899165297\","+ + "\"tagNum\" : \"17657806255240\","+ + "\"routing\" : \"MIA/LAX/MEL\","+ + "\"lastActionCode\" : \"OFFLOAD\","+ + "\"lastActionDesc\" : \"OFFLOAD\","+ + "\"lastSeenStation\" : \"MEL\","+ + "\"flightLegs\" : [ {"+ + "\"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\""+ + "} ]"+ + "}, {"+ + "\"flightNo\" : \"BM667\","+ + "\"flightDate\" : \"2019-02-01T06:13:00Z\","+ + "\"fltRouteSrc\" : \"LAX\","+ + "\"fltRouteDest\" : \"MEL\","+ + "\"estimatedArrival\" : \"2019-02-01T16:15:00Z\","+ + "\"actions\" : [ {"+ + "\"actionAt\" : \"MEL\","+ + "\"actionCode\" : \"Offload to Carousel at MEL\","+ + "\"actionTime\" : \"2019-02-01T16:15:00Z\""+ + "}, {"+ + "\"actionAt\" : \"LAX\","+ + "\"actionCode\" : \"ONLOAD to MEL\","+ + "\"actionTime\" : \"2019-02-01T15:35:00Z\""+ + "}, {"+ + "\"actionAt\" : \"LAX\","+ + "\"actionCode\" : \"OFFLOAD from LAX\","+ + "\"actionTime\" : \"2019-02-01T15:18:00Z\""+ + "} ]"+ + "} ],"+ + "\"lastSeenTimeGmt\" : \"2019-02-01T16:13:00Z\","+ + "\"bagArrivalDate\" : \"2019-02-01T16:13:00Z\""+ + "} ]"+ + "}"; + final static String bag3 = "{"+ + "\"ticketNo\" : \"1762341772625\","+ + "\"fullName\" : \"Gerard Greene\","+ + "\"gender\" : \"M\","+ + "\"contactPhone\" : \"395-837-3772\","+ + "\"confNo\" : \"MC0E7R\","+ + "\"bagInfo\" : [ {"+ + "\"id\" : \"79039899152842\","+ + "\"tagNum\" : \"1765780626568\","+ + "\"routing\" : \"SFO/IST/ATH/JTR\","+ + "\"lastActionCode\" : \"OFFLOAD\","+ + "\"lastActionDesc\" : \"OFFLOAD\","+ + "\"lastSeenStation\" : \"JTR\","+ + "\"flightLegs\" : [ {"+ + "\"flightNo\" : \"BM318\","+ + "\"flightDate\" : \"2019-03-07T04:00:00Z\","+ + "\"fltRouteSrc\" : \"SFO\","+ + "\"fltRouteDest\" : \"IST\","+ + "\"estimatedArrival\" : \"2019-03-07T17:00:00Z\","+ + "\"actions\" : [ {"+ + "\"actionAt\" : \"SFO\","+ + "\"actionCode\" : \"ONLOAD to IST\","+ + "\"actionTime\" : \"2019-03-07T04:08:00Z\""+ + "}, {"+ + "\"actionAt\" : \"SFO\","+ + "\"actionCode\" : \"BagTag Scan at SFO\","+ + "\"actionTime\" : \"2019-03-07T03:53:00Z\""+ + "}, {"+ + "\"actionAt\" : \"SFO\","+ + "\"actionCode\" : \"Checkin at SFO\","+ + "\"actionTime\" : \"2019-03-07T02:20:00Z\""+ + "} ]"+ + "}, {"+ + "\"flightNo\" : \"BM696\","+ + "\"flightDate\" : \"2019-03-07T05:08:00Z\","+ + "\"fltRouteSrc\" : \"IST\","+ + "\"fltRouteDest\" : \"ATH\","+ + "\"estimatedArrival\" : \"2019-03-08T04:10:00Z\","+ + "\"actions\" : [ {"+ + "\"actionAt\" : \"IST\","+ + "\"actionCode\" : \"ONLOAD to ATH\","+ + "\"actionTime\" : \"2019-03-08T04:55:00Z\""+ + "}, {"+ + "\"actionAt\" : \"IST\","+ + "\"actionCode\" : \"BagTag Scan at IST\","+ + "\"actionTime\" : \"2019-03-08T04:34:00Z\""+ + "}, {"+ + "\"actionAt\" : \"IST\","+ + "\"actionCode\" : \"OFFLOAD from IST\","+ + "\"actionTime\" : \"2019-03-08T04:47:00Z\""+ + "} ]"+ + "}, {"+ + "\"flightNo\" : \"BM665\","+ + "\"flightDate\" : \"2019-03-07T04:08:00Z\","+ + "\"fltRouteSrc\" : \"ATH\","+ + "\"fltRouteDest\" : \"JTR\","+ + "\"estimatedArrival\" : \"2019-03-07T16:10:00Z\","+ + "\"actions\" : [ {"+ + "\"actionAt\" : \"JTR\","+ + "\"actionCode\" : \"Offload to Carousel at JTR\","+ + "\"actionTime\" : \"2019-03-07T16:09:00Z\""+ + "}, {"+ + "\"actionAt\" : \"ATH\","+ + "\"actionCode\" : \"ONLOAD to JTR\","+ + "\"actionTime\" : \"2019-03-07T15:51:00Z\""+ + "}, {"+ + "\"actionAt\" : \"ATH\","+ + "\"actionCode\" : \"OFFLOAD from ATH\","+ + "\"actionTime\" : \"2019-03-07T15:43:00Z\""+ + "} ]"+ + "} ],"+ + "\"lastSeenTimeGmt\" : \"2019-03-07T16:01:00Z\","+ + "\"bagArrivalDate\" : \"2019-03-07T16:01:00Z\""+ + "} ]"+ + "}"; + + final static String bag4 = "{"+ + "\"ticketNo\" : \"1762320369957\","+ + "\"fullName\" : \"Lorenzo Phil\","+ + "\"gender\" : \"M\","+ + "\"contactPhone\" : \"364-610-4444\","+ + "\"confNo\" : \"QI3V6Q\","+ + "\"bagInfo\" : [ {"+ + "\"id\" : \"79039899187755\","+ + "\"tagNum\" : \"17657806240001\","+ + "\"routing\" : \"SFO/IST/ATH/JTR\","+ + "\"lastActionCode\" : \"OFFLOAD\","+ + "\"lastActionDesc\" : \"OFFLOAD\","+ + "\"lastSeenStation\" : \"JTR\","+ + "\"flightLegs\" : [ {"+ + "\"flightNo\" : \"BM318\","+ + "\"flightDate\" : \"2019-03-12T03:00:00Z\","+ + "\"fltRouteSrc\" : \"SFO\","+ + "\"fltRouteDest\" : \"IST\","+ + "\"estimatedArrival\" : \"2019-03-12T16:00:00Z\","+ + "\"actions\" : [ {"+ + "\"actionAt\" : \"SFO\","+ + "\"actionCode\" : \"ONLOAD to IST\","+ + "\"actionTime\" : \"2019-03-12T03:11:00Z\""+ + "}, {"+ + "\"actionAt\" : \"SFO\","+ + "\"actionCode\" : \"BagTag Scan at SFO\","+ + "\"actionTime\" : \"2019-03-12T02:49:00Z\""+ + "}, {"+ + "\"actionAt\" : \"SFO\","+ + "\"actionCode\" : \"Checkin at SFO\","+ + "\"actionTime\" : \"2019-03-12T01:50:00Z\""+ + "} ]"+ + "}, {"+ + "\"flightNo\" : \"BM696\","+ + "\"flightDate\" : \"2019-03-12T04:11:00Z\","+ + "\"fltRouteSrc\" : \"IST\","+ + "\"fltRouteDest\" : \"ATH\","+ + "\"estimatedArrival\" : \"2019-03-13T03:14:00Z\","+ + "\"actions\" : [ {"+ + "\"actionAt\" : \"IST\","+ + "\"actionCode\" : \"ONLOAD to ATH\","+ + "\"actionTime\" : \"2019-03-13T04:10:00Z\""+ + "}, {"+ + "\"actionAt\" : \"IST\","+ + "\"actionCode\" : \"BagTag Scan at IST\","+ + "\"actionTime\" : \"2019-03-13T03:56:00Z\""+ + "}, {"+ + "\"actionAt\" : \"IST\","+ + "\"actionCode\" : \"OFFLOAD from IST\","+ + "\"actionTime\" : \"2019-03-13T03:59:00Z\""+ + "} ]"+ + "}, {"+ + "\"flightNo\" : \"BM665\","+ + "\"flightDate\" : \"2019-03-12T03:11:00Z\","+ + "\"fltRouteSrc\" : \"ATH\","+ + "\"fltRouteDest\" : \"JTR\","+ + "\"estimatedArrival\" : \"2019-03-12T15:12:00Z\","+ + "\"actions\" : [ {"+ + "\"actionAt\" : \"JTR\","+ + "\"actionCode\" : \"Offload to Carousel at JTR\","+ + "\"actionTime\" : \"2019-03-12T15:06:00Z\""+ + "}, {"+ + "\"actionAt\" : \"ATH\","+ + "\"actionCode\" : \"ONLOAD to JTR\","+ + "\"actionTime\" : \"2019-03-12T14:16:00Z\""+ + "}, {"+ + "\"actionAt\" : \"ATH\","+ + "\"actionCode\" : \"OFFLOAD from ATH\","+ + "\"actionTime\" : \"2019-03-12T14:13:00Z\""+ + "} ]"+ + "} ],"+ + "\"lastSeenTimeGmt\" : \"2019-03-12T15:05:00Z\","+ + "\"bagArrivalDate\" : \"2019-03-12T15:05:00Z\""+ + "},"+ + "{"+ + "\"id\" : \"79039899197755\","+ + "\"tagNum\" : \"17657806340001\","+ + "\"routing\" : \"SFO/IST/ATH/JTR\","+ + "\"lastActionCode\" : \"OFFLOAD\","+ + "\"lastActionDesc\" : \"OFFLOAD\","+ + "\"lastSeenStation\" : \"JTR\","+ + "\"flightLegs\" : [ {"+ + "\"flightNo\" : \"BM318\","+ + "\"flightDate\" : \"2019-03-12T03:00:00Z\","+ + "\"fltRouteSrc\" : \"SFO\","+ + "\"fltRouteDest\" : \"IST\","+ + "\"estimatedArrival\" : \"2019-03-12T16:40:00Z\","+ + "\"actions\" : [ {"+ + "\"actionAt\" : \"SFO\","+ + "\"actionCode\" : \"ONLOAD to IST\","+ + "\"actionTime\" : \"2019-03-12T03:14:00Z\""+ + "}, {"+ + "\"actionAt\" : \"SFO\","+ + "\"actionCode\" : \"BagTag Scan at SFO\","+ + "\"actionTime\" : \"2019-03-12T02:50:00Z\""+ + "}, {"+ + "\"actionAt\" : \"SFO\","+ + "\"actionCode\" : \"Checkin at SFO\","+ + "\"actionTime\" : \"2019-03-12T01:58:00Z\""+ + "} ]"+ + "}, {"+ + "\"flightNo\" : \"BM696\","+ + "\"flightDate\" : \"2019-03-12T04:11:00Z\","+ + "\"fltRouteSrc\" : \"IST\","+ + "\"fltRouteDest\" : \"ATH\","+ + "\"estimatedArrival\" : \"2019-03-13T03:18:00Z\","+ + "\"actions\" : [ {"+ + "\"actionAt\" : \"IST\","+ + "\"actionCode\" : \"ONLOAD to ATH\","+ + "\"actionTime\" : \"2019-03-13T04:17:00Z\""+ + "}, {"+ + "\"actionAt\" : \"IST\","+ + "\"actionCode\" : \"BagTag Scan at IST\","+ + "\"actionTime\" : \"2019-03-13T03:59:00Z\""+ + "}, {"+ + "\"actionAt\" : \"IST\","+ + "\"actionCode\" : \"OFFLOAD from IST\","+ + "\"actionTime\" : \"2019-03-13T03:48:00Z\""+ + "} ]"+ + "}, {"+ + "\"flightNo\" : \"BM665\","+ + "\"flightDate\" : \"2019-03-12T03:11:00Z\","+ + "\"fltRouteSrc\" : \"ATH\","+ + "\"fltRouteDest\" : \"JTR\","+ + "\"estimatedArrival\" : \"2019-03-12T15:12:00Z\","+ + "\"actions\" : [ {"+ + "\"actionAt\" : \"JTR\","+ + "\"actionCode\" : \"Offload to Carousel at JTR\","+ + "\"actionTime\" : \"2019-03-12T15:06:00Z\""+ + "}, {"+ + "\"actionAt\" : \"ATH\","+ + "\"actionCode\" : \"ONLOAD to JTR\","+ + "\"actionTime\" : \"2019-03-12T14:16:00Z\""+ + "}, {"+ + "\"actionAt\" : \"ATH\","+ + "\"actionCode\" : \"OFFLOAD from ATH\","+ + "\"actionTime\" : \"2019-03-12T14:23:00Z\""+ + "} ]"+ + "} ],"+ + "\"lastSeenTimeGmt\" : \"2019-03-12T16:05:00Z\","+ + "\"bagArrivalDate\" : \"2019-03-12T16:25:00Z\""+ + "} ]"+ + "}"; + + + + static FieldValue val1 = FieldValue.createFromJson(bag1,null); + static FieldValue val2 = FieldValue.createFromJson(bag2,null); + static FieldValue val3 = FieldValue.createFromJson(bag3,null); + static FieldValue val4 = FieldValue.createFromJson(bag4,null); + + public static void main(String[] args) throws Exception { + /* UNCOMMENT the lines of code below if you are using Oracle NoSQL + * Database Cloud service. + * Leave the lines commented if you are using onPremise database + * Add the appropriate values of your region and compartment OCID + String region =""; + String compId =""; + handle = generateNoSQLHandleCloud(region,compId); */ + + /* UNCOMMENT the 2 lines of code below if you are using onPremise Oracle + * NoSQL Database. + * Leave the lines commented if you are using NoSQL Database Cloud Service + * Give appropriate value of your endpoint for the onPremise kvstore. + String kvstore_endpoint ="http://:8080"; + handle = generateNoSQLHandleonPrem(kvstore_endpoint); */ + try { + createTable(handle); + writeRows(handle, (MapValue)val1); + writeRows(handle, (MapValue)val2); + writeRows(handle, (MapValue)val3); + writeRows(handle, (MapValue)val4); + String seq_comp_ope="SELECT bag.bagInfo[].tagNum,bag.bagInfo[].flightLegs[].fltRouteSrc FROM BaggageInfo bag WHERE bag.bagInfo[].flightLegs[].fltRouteSrc=any \"SFO\""; + System.out.println("Using Sequence Comparison operator "); + fetchRows(handle,seq_comp_ope); + String logical_ope="SELECT fullName, bag.bagInfo[].tagNum, bag.bagInfo[].routing,bag.bagInfo[].flightLegs[].fltRouteSrc FROM BaggageInfo bag WHERE NOT bag.bagInfo[].flightLegs[].fltRouteSrc=any \"SFO\""; + System.out.println("Using Logical operator "); + fetchRows(handle,logical_ope); + String value_comp_ope="SELECT fullname, bag.bagInfo[].routing FROM BaggageInfo bag WHERE gender=\"M\""; + System.out.println("Using Value Comparison operator "); + fetchRows(handle,value_comp_ope); + String in_ope="SELECT bagdet.fullName, bagdet.bagInfo[].tagNum FROM BaggageInfo bagdet WHERE bagdet.fullName IN (\"Lucinda Beckman\", \"Adam Phillips\",\"Dierdre Amador\",\"Fallon Clements\")"; + System.out.println("Using IN operator "); + fetchRows(handle,in_ope); + String exists_ope="SELECT fullName, bag.ticketNo FROM BaggageInfo bag WHERE EXISTS bag.bagInfo[$element.bagArrivalDate >=\"2019-03-01T00:00:00\"]"; + System.out.println("Using EXISTS operator "); + fetchRows(handle,exists_ope); + + } catch (Exception e) { + System.err.print(e); + } finally { + handle.close(); + } + } + /* Create a NoSQL handle to access the onPremise Oracle NoSQL database */ + private static NoSQLHandle generateNoSQLHandleonPrem(String kvstore_endpoint) throws Exception { + NoSQLHandleConfig config = new NoSQLHandleConfig(kvstore_endpoint); + config.setAuthorizationProvider(new StoreAccessTokenProvider()); + /* If using a secure store, uncomment the line below and pass username, + * password of the store to StoreAccessTokenProvider + */ + /* config.setAuthorizationProvider(new StoreAccessTokenProvider(username, password)); */ + NoSQLHandle handle = NoSQLHandleFactory.createNoSQLHandle(config); + return handle; + } + + /* Create a NoSQL handle to access the cloud service */ + private static NoSQLHandle generateNoSQLHandleCloud(String region, String compId) throws Exception { + SignatureProvider ap = new SignatureProvider(); + NoSQLHandleConfig config = new NoSQLHandleConfig(region, ap); + // set your default compartment + config.setDefaultCompartment(compId); + NoSQLHandle handle = NoSQLHandleFactory.createNoSQLHandle(config); + return handle; + } + + /** + * Create a simple table and set your desired table capacity + */ + private static void createTable(NoSQLHandle handle) throws Exception { + String createTableDDL = "CREATE TABLE IF NOT EXISTS " + tableName + + "(ticketNo LONG," + + "fullName STRING," + + "gender STRING ," + + "contactPhone STRING ," + + "confNo STRING ," + + "bagInfo JSON, " + + "PRIMARY KEY(ticketNo))"; + + TableLimits limits = new TableLimits(20, 20, 1); + TableRequest treq = new TableRequest() + .setStatement(createTableDDL).setTableLimits(limits); + + TableResult tres = handle.tableRequest(treq); + /* The request is async, + * so wait for the table to become active. + */ + tres.waitForCompletion(handle, 60000, /* wait 60 sec */ + 1000); /* delay ms for poll */ + System.out.println("Table " + tableName + " is active"); + } + + /** + * Add a Row in the table + */ + private static void writeRows(NoSQLHandle handle, MapValue value) throws Exception { + PutRequest putRequest = + new PutRequest().setValue(value).setTableName(tableName); + PutResult putResult = handle.put(putRequest); + } + + //Fetch rows from the table + private static void fetchRows(NoSQLHandle handle,String sqlstmt) throws Exception { + + try ( + QueryRequest queryRequest = new QueryRequest().setStatement(sqlstmt); + QueryIterableResult results = handle.queryIterable(queryRequest)){ + for (MapValue res : results) { + System.out.println("\t" + res); + } + } + } +} diff --git a/examples-nosql-java-sdk/sqlexamples/TableJoins.java b/examples-nosql-java-sdk/sqlexamples/TableJoins.java index 8860854..bf717d6 100644 --- a/examples-nosql-java-sdk/sqlexamples/TableJoins.java +++ b/examples-nosql-java-sdk/sqlexamples/TableJoins.java @@ -1,5 +1,6 @@ /*Copyright (c) 2023, 2024 Oracle and/or its affiliates. - * Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/ + * Licensed under the Universal Permissive License v 1.0 as shown at + * https://oss.oracle.com/licenses/upl/ */ import java.io.File; import oracle.nosql.driver.NoSQLHandle; @@ -29,14 +30,18 @@ public class TableJoins{ final static String fullDescTableName = "ticket.bagInfo.flightLegs"; public static void main(String[] args) throws Exception { - /* UNCOMMENT the lines of code below if you are using Oracle NoSQL Database Cloud service. Leave the lines commented if you are using onPremise database - Add the appropriate values of your region and compartment OCID + /* UNCOMMENT the lines of code below if you are using Oracle NoSQL + * Database Cloud service. + * Leave the lines commented if you are using onPremise database + * Add the appropriate values of your region and compartment OCID String region =""; String compId =""; handle = generateNoSQLHandleCloud(region,compId); */ - /* UNCOMMENT the 2 lines of code below if you are using onPremise Oracle NoSQL Database. Leave the lines commented if you are using NoSQL Database Cloud Service - give appropriate value of your endpoint for the onPremise kvstore. + /* UNCOMMENT the 2 lines of code below if you are using onPremise Oracle + * NoSQL Database. + * Leave the lines commented if you are using NoSQL Database Cloud Service + * Give appropriate value of your endpoint for the onPremise kvstore. String kvstore_endpoint ="http://:8080"; handle = generateNoSQLHandleonPrem(kvstore_endpoint); */ try { @@ -119,7 +124,9 @@ public static void main(String[] args) throws Exception { private static NoSQLHandle generateNoSQLHandleonPrem(String kvstore_endpoint) throws Exception { NoSQLHandleConfig config = new NoSQLHandleConfig(kvstore_endpoint); config.setAuthorizationProvider(new StoreAccessTokenProvider()); - /* If using a secure store uncomment the line below and pass the username, password of the store to StoreAccessTokenProvider*/ + /* If using a secure store uncomment the line below and pass username, + * password of the store to StoreAccessTokenProvider + */ /*config.setAuthorizationProvider(new StoreAccessTokenProvider(username, password));*/ NoSQLHandle handle = NoSQLHandleFactory.createNoSQLHandle(config); return handle; diff --git a/examples-nosql-node-sdk/sqlexamples/AddBagData.js b/examples-nosql-node-sdk/sqlexamples/AddBagData.js new file mode 100644 index 0000000..5efe691 --- /dev/null +++ b/examples-nosql-node-sdk/sqlexamples/AddBagData.js @@ -0,0 +1,412 @@ +/* Copyright (c) 2023, 2024 Oracle and/or its affiliates. + * Licensed under the Universal Permissive License v 1.0 as shown at + * https://oss.oracle.com/licenses/upl/ + */ +'use strict'; + +const NoSQLClient = require('oracle-nosqldb').NoSQLClient; +const Region = require('oracle-nosqldb').Region; +const TABLE_NAME = 'BaggageInfo'; +const bag1= ` +{ + "ticketNo":"1762376407826", + "fullName":"Dierdre Amador", + "gender":"M", + "contactPhone":"165-742-5715", + "confNo":"ZG8Z5N", + "bagInfo":[ { + "id" : "7903989918469", + "tagNum" : "17657806240229", + "routing" : "JFK/MAD", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "MAD", + "flightLegs" : [ { + "flightNo" : "BM495", + "flightDate" : "2019-03-07T07:00:00Z", + "fltRouteSrc" : "JFK", + "fltRouteDest" : "MAD", + "estimatedArrival" : "2019-03-07T14:00:00Z", + "actions" : [ { + "actionAt" : "MAD", + "actionCode" : "Offload to Carousel at MAD", + "actionTime" : "2019-03-07T13:54:00Z" + }, { + "actionAt" : "JFK", + "actionCode" : "ONLOAD to MAD", + "actionTime" : "2019-03-07T07:00:00Z" + }, { + "actionAt" : "JFK", + "actionCode" : "BagTag Scan at JFK", + "actionTime" : "2019-03-07T06:53:00Z" + }, { + "actionAt" : "JFK", + "actionCode" : "Checkin at JFK", + "actionTime" : "2019-03-07T05:03:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-03-07T13:51:00Z", + "bagArrivalDate" : "2019-03-07T13:51:00Z" + } ] +}` +const bag2= ` +{ + "ticketNo":"1762344493810", + "fullName":"Adam Phillips", + "gender":"M", + "contactPhone":"893-324-1064", + "confNo":"LE6J4Z", + "bagInfo":[ { + "id" : "79039899165297", + "tagNum" : "17657806255240", + "routing" : "MIA/LAX/MEL", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "MEL", + "flightLegs" : [ { + "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" + } ] + }, { + "flightNo" : "BM667", + "flightDate" : "2019-02-01T06:13:00Z", + "fltRouteSrc" : "LAX", + "fltRouteDest" : "MEL", + "estimatedArrival" : "2019-02-01T16:15:00Z", + "actions" : [ { + "actionAt" : "MEL", + "actionCode" : "Offload to Carousel at MEL", + "actionTime" : "2019-02-01T16:15:00Z" + }, { + "actionAt" : "LAX", + "actionCode" : "ONLOAD to MEL", + "actionTime" : "2019-02-01T15:35:00Z" + }, { + "actionAt" : "LAX", + "actionCode" : "OFFLOAD from LAX", + "actionTime" : "2019-02-01T15:18:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-02-01T16:13:00Z", + "bagArrivalDate" : "2019-02-01T16:13:00Z" + } ] +}` + +const bag3= ` +{ + "ticketNo":"1762341772625", + "fullName":"Gerard Greene", + "gender":"M", + "contactPhone":"395-837-3772", + "confNo":"MC0E7R", + "bagInfo":[ { + "id" : "79039899152842", + "tagNum" : "1765780626568", + "routing" : "SFO/IST/ATH/JTR", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "JTR", + "flightLegs" : [ { + "flightNo" : "BM318", + "flightDate" : "2019-03-07T04:00:00Z", + "fltRouteSrc" : "SFO", + "fltRouteDest" : "IST", + "estimatedArrival" : "2019-03-07T17:00:00Z", + "actions" : [ { + "actionAt" : "SFO", + "actionCode" : "ONLOAD to IST", + "actionTime" : "2019-03-07T04:08:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "BagTag Scan at SFO", + "actionTime" : "2019-03-07T03:53:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "Checkin at SFO", + "actionTime" : "2019-03-07T02:20:00Z" + } ] + }, { + "flightNo" : "BM696", + "flightDate" : "2019-03-07T05:08:00Z", + "fltRouteSrc" : "IST", + "fltRouteDest" : "ATH", + "estimatedArrival" : "2019-03-08T04:10:00Z", + "actions" : [ { + "actionAt" : "IST", + "actionCode" : "ONLOAD to ATH", + "actionTime" : "2019-03-08T04:55:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "BagTag Scan at IST", + "actionTime" : "2019-03-08T04:34:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "OFFLOAD from IST", + "actionTime" : "2019-03-08T04:47:00Z" + } ] + }, { + "flightNo" : "BM665", + "flightDate" : "2019-03-07T04:08:00Z", + "fltRouteSrc" : "ATH", + "fltRouteDest" : "JTR", + "estimatedArrival" : "2019-03-07T16:10:00Z", + "actions" : [ { + "actionAt" : "JTR", + "actionCode" : "Offload to Carousel at JTR", + "actionTime" : "2019-03-07T16:09:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "ONLOAD to JTR", + "actionTime" : "2019-03-07T15:51:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "OFFLOAD from ATH", + "actionTime" : "2019-03-07T15:43:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-03-07T16:01:00Z", + "bagArrivalDate" : "2019-03-07T16:01:00Z" + } ] +}` + +const bag4= ` +{ + "ticketNo":"1762320369957", + "fullName":"Lorenzo Phil", + "gender":"M", + "contactPhone":"364-610-4444", + "confNo":"QI3V6Q", + "bagInfo":[ { + "id" : "79039899187755", + "tagNum" : "17657806240001", + "routing" : "SFO/IST/ATH/JTR", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "JTR", + "flightLegs" : [ { + "flightNo" : "BM318", + "flightDate" : "2019-03-12T03:00:00Z", + "fltRouteSrc" : "SFO", + "fltRouteDest" : "IST", + "estimatedArrival" : "2019-03-12T16:00:00Z", + "actions" : [ { + "actionAt" : "SFO", + "actionCode" : "ONLOAD to IST", + "actionTime" : "2019-03-12T03:11:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "BagTag Scan at SFO", + "actionTime" : "2019-03-12T02:49:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "Checkin at SFO", + "actionTime" : "2019-03-12T01:50:00Z" + } ] + }, { + "flightNo" : "BM696", + "flightDate" : "2019-03-12T04:11:00Z", + "fltRouteSrc" : "IST", + "fltRouteDest" : "ATH", + "estimatedArrival" : "2019-03-13T03:14:00Z", + "actions" : [ { + "actionAt" : "IST", + "actionCode" : "ONLOAD to ATH", + "actionTime" : "2019-03-13T04:10:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "BagTag Scan at IST", + "actionTime" : "2019-03-13T03:56:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "OFFLOAD from IST", + "actionTime" : "2019-03-13T03:59:00Z" + } ] + }, { + "flightNo" : "BM665", + "flightDate" : "2019-03-12T03:11:00Z", + "fltRouteSrc" : "ATH", + "fltRouteDest" : "JTR", + "estimatedArrival" : "2019-03-12T15:12:00Z", + "actions" : [ { + "actionAt" : "JTR", + "actionCode" : "Offload to Carousel at JTR", + "actionTime" : "2019-03-12T15:06:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "ONLOAD to JTR", + "actionTime" : "2019-03-12T14:16:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "OFFLOAD from ATH", + "actionTime" : "2019-03-12T14:13:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-03-12T15:05:00Z", + "bagArrivalDate" : "2019-03-12T15:05:00Z" + }, + { + "id" : "79039899197755", + "tagNum" : "17657806340001", + "routing" : "SFO/IST/ATH/JTR", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "JTR", + "flightLegs" : [ { + "flightNo" : "BM318", + "flightDate" : "2019-03-12T03:00:00Z", + "fltRouteSrc" : "SFO", + "fltRouteDest" : "IST", + "estimatedArrival" : "2019-03-12T16:40:00Z", + "actions" : [ { + "actionAt" : "SFO", + "actionCode" : "ONLOAD to IST", + "actionTime" : "2019-03-12T03:14:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "BagTag Scan at SFO", + "actionTime" : "2019-03-12T02:50:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "Checkin at SFO", + "actionTime" : "2019-03-12T01:58:00Z" + } ] + }, { + "flightNo" : "BM696", + "flightDate" : "2019-03-12T04:11:00Z", + "fltRouteSrc" : "IST", + "fltRouteDest" : "ATH", + "estimatedArrival" : "2019-03-13T03:18:00Z", + "actions" : [ { + "actionAt" : "IST", + "actionCode" : "ONLOAD to ATH", + "actionTime" : "2019-03-13T04:17:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "BagTag Scan at IST", + "actionTime" : "2019-03-13T03:59:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "OFFLOAD from IST", + "actionTime" : "2019-03-13T03:48:00Z" + } ] + }, { + "flightNo" : "BM665", + "flightDate" : "2019-03-12T03:11:00Z", + "fltRouteSrc" : "ATH", + "fltRouteDest" : "JTR", + "estimatedArrival" : "2019-03-12T15:12:00Z", + "actions" : [ { + "actionAt" : "JTR", + "actionCode" : "Offload to Carousel at JTR", + "actionTime" : "2019-03-12T15:06:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "ONLOAD to JTR", + "actionTime" : "2019-03-12T14:16:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "OFFLOAD from ATH", + "actionTime" : "2019-03-12T14:23:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-03-12T16:05:00Z", + "bagArrivalDate" : "2019-03-12T16:25:00Z" + } ] +}` +/** + * Call the main function for this example + **/ +doAddBagData(); + +async function doAddBagData() { + let handle; + try { + /* UNCOMMENT line of code below if you are using Oracle NoSQL Database + * Cloud service. Leave the line commented if you are using onPrem database + handle = await getConnection_cloud(); */ + /* UNCOMMENT line of code below if you are using onPremise Oracle NoSQL + * Database. Leave the line commented if you are using NoSQL Database + * Cloud Service + handle = await getConnection_onPrem(); */ + await createTable(handle); + let putResult = await handle.put(TABLE_NAME, JSON.parse(bag1)); + let putResult1 = await handle.put(TABLE_NAME, JSON.parse(bag2)); + let putResult2 = await handle.put(TABLE_NAME, JSON.parse(bag3)); + let putResult3 = await handle.put(TABLE_NAME, JSON.parse(bag4)); + console.log("Wrote records of BaggageInfo schema"); + } catch (error ) { + console.log(error); + } + finally { + if (handle) { + handle.close(); + } + } +} + +/* Create and return an instance of a NoSQLCLient object for cloud service */ +function getConnection_cloud() { + /* replace the placeholders for compartment and region with actual values.*/ + const Region = ``; + return new NoSQLClient({ + region: Region, + compartment: "", + }); +} +/* Create and return an instance of a NoSQLCLient object for onPremises */ +function getConnection_onPrem() { + /* replace the placeholder with the fullname of your host */ + const kvstore_endpoint = `http://:8080`; + return new NoSQLClient({ + serviceType: "KVSTORE", + endpoint: kvstore_endpoint + }); + /* if it is a secure store, comment the return statement above and + * uncomment the lines below and pass the credentials, username and password + return new NoSQLClient({ + "serviceType": "KVSTORE", + "endpoint": "", + "auth": { + "kvstore":{ + "user": "", + "password": "" + } + } + }); */ +} + +//creates a table +async function createTable(handle) { + const createDDL = `CREATE TABLE IF NOT EXISTS ${TABLE_NAME} (ticketNo LONG, + fullName STRING, + gender STRING, + contactPhone STRING, + confNo STRING, + bagInfo JSON, + primary key(ticketNo))`; + console.log('Create table: ' + createDDL); + let res = await handle.tableDDL(createDDL, { + complete: true, + tableLimits: { + readUnits: 20, + writeUnits: 20, + storageGB: 1 + } + }); + console.log('Table created: ' + TABLE_NAME); +} diff --git a/examples-nosql-node-sdk/sqlexamples/AddData.js b/examples-nosql-node-sdk/sqlexamples/AddData.js index a822cb5..efc59a2 100644 --- a/examples-nosql-node-sdk/sqlexamples/AddData.js +++ b/examples-nosql-node-sdk/sqlexamples/AddData.js @@ -1,5 +1,6 @@ /* Copyright (c) 2023, 2024 Oracle and/or its affiliates. - * Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/ + * Licensed under the Universal Permissive License v 1.0 as shown at + * https://oss.oracle.com/licenses/upl/ */ 'use strict'; const NoSQLClient = require('oracle-nosqldb').NoSQLClient; @@ -282,26 +283,33 @@ const acct3= ` doAddData(); async function doAddData() { + let handle; try { - /* UNCOMMENT the line of code below if you are using Oracle NoSQL Database Cloud service. Leave the line commented if you are using onPremise database - let handle = await getConnection_cloud(); */ - /* UNCOMMENT the line of code below if you are using onPremise Oracle NoSQL Database. Leave the line commented if you are using NoSQL Database Cloud Service - let handle = await getConnection_onPrem(); */ + /* UNCOMMENT line of code below if you are using Oracle NoSQL Database + * Cloud service. Leave the line commented if you are using onPrem database + handle = await getConnection_cloud(); */ + /* UNCOMMENT line of code below if you are using onPremise Oracle NoSQL + * Database. Leave the line commented if you are using NoSQL Database + * Cloud Service + handle = await getConnection_onPrem(); */ await createTable(handle); 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("Wrote records of acct stream schema"); - process.exit(0); } catch (error ) { console.log(error); - process.exit(-1); + } + finally { + if (handle) { + handle.close(); + } } } /* Create and return an instance of a NoSQLCLient object for cloud service */ function getConnection_cloud() { - /* replace the placeholders for compartment and region with the actual values. */ + /* replace the placeholders for compartment and region with actual values.*/ const Region = ``; return new NoSQLClient({ region: Region, diff --git a/examples-nosql-node-sdk/sqlexamples/AlterTable.js b/examples-nosql-node-sdk/sqlexamples/AlterTable.js index f126159..05a5c44 100644 --- a/examples-nosql-node-sdk/sqlexamples/AlterTable.js +++ b/examples-nosql-node-sdk/sqlexamples/AlterTable.js @@ -1,5 +1,6 @@ /* Copyright (c) 2023, 2024 Oracle and/or its affiliates. - * Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/ + * Licensed under the Universal Permissive License v 1.0 as shown at + * https://oss.oracle.com/licenses/upl/ */ 'use strict'; const NoSQLClient = require('oracle-nosqldb').NoSQLClient; @@ -11,24 +12,31 @@ const TABLE_NAME = 'stream_acct'; doaltertable(); async function doaltertable() { + let handle; try { - /* UNCOMMENT the line of code below if you are using Oracle NoSQL Database Cloud service. Leave the line commented if you are using onPremise database - let handle = await getConnection_cloud(); */ - /* UNCOMMENT the line of code below if you are using onPremise Oracle NoSQL Database. Leave the line commented if you are using NoSQL Database Cloud Service - let handle = await getConnection_onPrem(); */ + /* UNCOMMENT line of code below if you are using Oracle NoSQL Database + * Cloud service. Leave the line commented if you are using onPrem database + handle = await getConnection_cloud(); */ + /* UNCOMMENT line of code below if you are using onPremise Oracle NoSQL + * Database. Leave the line commented if you are using NoSQL Database + * Cloud Service + handle = await getConnection_onPrem(); */ await createTable(handle); await alterTable(handle); await dropTable(handle); - process.exit(0); } catch (error ) { console.log(error); - process.exit(-1); + } + finally { + if (handle) { + handle.close(); + } } } /* Create and return an instance of a NoSQLCLient object for cloud service */ function getConnection_cloud() { - /* replace the placeholders for compartment and region with the actual values. */ + /* replace the placeholders for compartment and region with actual values.*/ const Region = ``; return new NoSQLClient({ region: Region, diff --git a/examples-nosql-node-sdk/sqlexamples/CreateTable.js b/examples-nosql-node-sdk/sqlexamples/CreateTable.js index 22fff7d..36e6579 100644 --- a/examples-nosql-node-sdk/sqlexamples/CreateTable.js +++ b/examples-nosql-node-sdk/sqlexamples/CreateTable.js @@ -1,5 +1,6 @@ /* Copyright (c) 2023, 2024 Oracle and/or its affiliates. - * Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/ + * Licensed under the Universal Permissive License v 1.0 as shown at + * https://oss.oracle.com/licenses/upl/ */ 'use strict'; const NoSQLClient = require('oracle-nosqldb').NoSQLClient; @@ -10,21 +11,28 @@ const TABLE_NAME = 'stream_acct'; docreatetable(); async function docreatetable() { + let handle; try { - /* UNCOMMENT the line of code below if you are using Oracle NoSQL Database Cloud service. Leave the line commented if you are using onPremise database - let handle = await getConnection_cloud(); */ - /* UNCOMMENT the line of code below if you are using onPremise Oracle NoSQL Database. Leave the line commented if you are using NoSQL Database Cloud Service - let handle = await getConnection_onPrem(); */ + /* UNCOMMENT line of code below if you are using Oracle NoSQL Database + * Cloud service. Leave the line commented if you are using onPrem database + handle = await getConnection_cloud(); */ + /* UNCOMMENT line of code below if you are using onPremise Oracle NoSQL + * Database. Leave the line commented if you are using NoSQL Database + * Cloud Service + handle = await getConnection_onPrem(); */ await createTable(handle); - process.exit(0); } catch (error ) { console.log(error); - process.exit(-1); + } + finally { + if (handle) { + handle.close(); + } } } /* Create and return an instance of a NoSQLCLient object for cloud service */ function getConnection_cloud() { - /* replace the placeholders for compartment and region with the actual values. */ + /* replace the placeholders for compartment and region with actual values. */ const Region = ``; return new NoSQLClient({ region: Region, diff --git a/examples-nosql-node-sdk/sqlexamples/GroupSortData.js b/examples-nosql-node-sdk/sqlexamples/GroupSortData.js new file mode 100644 index 0000000..17e5011 --- /dev/null +++ b/examples-nosql-node-sdk/sqlexamples/GroupSortData.js @@ -0,0 +1,445 @@ +/* Copyright (c) 2023, 2024 Oracle and/or its affiliates. + * Licensed under the Universal Permissive License v 1.0 as shown at + * https://oss.oracle.com/licenses/upl/ + */ +'use strict'; + +const NoSQLClient = require('oracle-nosqldb').NoSQLClient; +const Region = require('oracle-nosqldb').Region; +const TABLE_NAME = 'BaggageInfo'; +const orderby_stmt = `SELECT bag.fullName, bag.bagInfo[].tagNum,bag.bagInfo[].lastSeenTimeGmt FROM BaggageInfo bag + WHERE bag.bagInfo[].lastSeenStation=any \"MEL\" ORDER BY bag.bagInfo[].lastSeenTimeGmt DESC` +const sortlimit_stmt = `SELECT $bag.fullName, $bag.bagInfo.tagNum, $flt_time FROM BaggageInfo $bag, + $bag.bagInfo[].lastSeenTimeGmt $flt_time + WHERE $bag.bagInfo[].lastSeenStation=any "JTR" + ORDER BY $flt_time LIMIT 4` +const groupsortlimit_stmt = `SELECT $flt_src as SOURCE, count(*) as COUNT FROM BaggageInfo $bag, + $bag.bagInfo.flightLegs[0].fltRouteSrc $flt_src GROUP BY $flt_src` + + +const bag1= ` +{ + "ticketNo":"1762376407826", + "fullName":"Dierdre Amador", + "gender":"M", + "contactPhone":"165-742-5715", + "confNo":"ZG8Z5N", + "bagInfo":[ { + "id" : "7903989918469", + "tagNum" : "17657806240229", + "routing" : "JFK/MAD", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "MAD", + "flightLegs" : [ { + "flightNo" : "BM495", + "flightDate" : "2019-03-07T07:00:00Z", + "fltRouteSrc" : "JFK", + "fltRouteDest" : "MAD", + "estimatedArrival" : "2019-03-07T14:00:00Z", + "actions" : [ { + "actionAt" : "MAD", + "actionCode" : "Offload to Carousel at MAD", + "actionTime" : "2019-03-07T13:54:00Z" + }, { + "actionAt" : "JFK", + "actionCode" : "ONLOAD to MAD", + "actionTime" : "2019-03-07T07:00:00Z" + }, { + "actionAt" : "JFK", + "actionCode" : "BagTag Scan at JFK", + "actionTime" : "2019-03-07T06:53:00Z" + }, { + "actionAt" : "JFK", + "actionCode" : "Checkin at JFK", + "actionTime" : "2019-03-07T05:03:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-03-07T13:51:00Z", + "bagArrivalDate" : "2019-03-07T13:51:00Z" + } ] +}` +const bag2= ` +{ + "ticketNo":"1762344493810", + "fullName":"Adam Phillips", + "gender":"M", + "contactPhone":"893-324-1064", + "confNo":"LE6J4Z", + "bagInfo":[ { + "id" : "79039899165297", + "tagNum" : "17657806255240", + "routing" : "MIA/LAX/MEL", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "MEL", + "flightLegs" : [ { + "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" + } ] + }, { + "flightNo" : "BM667", + "flightDate" : "2019-02-01T06:13:00Z", + "fltRouteSrc" : "LAX", + "fltRouteDest" : "MEL", + "estimatedArrival" : "2019-02-01T16:15:00Z", + "actions" : [ { + "actionAt" : "MEL", + "actionCode" : "Offload to Carousel at MEL", + "actionTime" : "2019-02-01T16:15:00Z" + }, { + "actionAt" : "LAX", + "actionCode" : "ONLOAD to MEL", + "actionTime" : "2019-02-01T15:35:00Z" + }, { + "actionAt" : "LAX", + "actionCode" : "OFFLOAD from LAX", + "actionTime" : "2019-02-01T15:18:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-02-01T16:13:00Z", + "bagArrivalDate" : "2019-02-01T16:13:00Z" + } ] +}` + +const bag3= ` +{ + "ticketNo":"1762341772625", + "fullName":"Gerard Greene", + "gender":"M", + "contactPhone":"395-837-3772", + "confNo":"MC0E7R", + "bagInfo":[ { + "id" : "79039899152842", + "tagNum" : "1765780626568", + "routing" : "SFO/IST/ATH/JTR", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "JTR", + "flightLegs" : [ { + "flightNo" : "BM318", + "flightDate" : "2019-03-07T04:00:00Z", + "fltRouteSrc" : "SFO", + "fltRouteDest" : "IST", + "estimatedArrival" : "2019-03-07T17:00:00Z", + "actions" : [ { + "actionAt" : "SFO", + "actionCode" : "ONLOAD to IST", + "actionTime" : "2019-03-07T04:08:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "BagTag Scan at SFO", + "actionTime" : "2019-03-07T03:53:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "Checkin at SFO", + "actionTime" : "2019-03-07T02:20:00Z" + } ] + }, { + "flightNo" : "BM696", + "flightDate" : "2019-03-07T05:08:00Z", + "fltRouteSrc" : "IST", + "fltRouteDest" : "ATH", + "estimatedArrival" : "2019-03-08T04:10:00Z", + "actions" : [ { + "actionAt" : "IST", + "actionCode" : "ONLOAD to ATH", + "actionTime" : "2019-03-08T04:55:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "BagTag Scan at IST", + "actionTime" : "2019-03-08T04:34:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "OFFLOAD from IST", + "actionTime" : "2019-03-08T04:47:00Z" + } ] + }, { + "flightNo" : "BM665", + "flightDate" : "2019-03-07T04:08:00Z", + "fltRouteSrc" : "ATH", + "fltRouteDest" : "JTR", + "estimatedArrival" : "2019-03-07T16:10:00Z", + "actions" : [ { + "actionAt" : "JTR", + "actionCode" : "Offload to Carousel at JTR", + "actionTime" : "2019-03-07T16:09:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "ONLOAD to JTR", + "actionTime" : "2019-03-07T15:51:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "OFFLOAD from ATH", + "actionTime" : "2019-03-07T15:43:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-03-07T16:01:00Z", + "bagArrivalDate" : "2019-03-07T16:01:00Z" + } ] +}` + +const bag4= ` +{ + "ticketNo":"1762320369957", + "fullName":"Lorenzo Phil", + "gender":"M", + "contactPhone":"364-610-4444", + "confNo":"QI3V6Q", + "bagInfo":[ { + "id" : "79039899187755", + "tagNum" : "17657806240001", + "routing" : "SFO/IST/ATH/JTR", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "JTR", + "flightLegs" : [ { + "flightNo" : "BM318", + "flightDate" : "2019-03-12T03:00:00Z", + "fltRouteSrc" : "SFO", + "fltRouteDest" : "IST", + "estimatedArrival" : "2019-03-12T16:00:00Z", + "actions" : [ { + "actionAt" : "SFO", + "actionCode" : "ONLOAD to IST", + "actionTime" : "2019-03-12T03:11:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "BagTag Scan at SFO", + "actionTime" : "2019-03-12T02:49:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "Checkin at SFO", + "actionTime" : "2019-03-12T01:50:00Z" + } ] + }, { + "flightNo" : "BM696", + "flightDate" : "2019-03-12T04:11:00Z", + "fltRouteSrc" : "IST", + "fltRouteDest" : "ATH", + "estimatedArrival" : "2019-03-13T03:14:00Z", + "actions" : [ { + "actionAt" : "IST", + "actionCode" : "ONLOAD to ATH", + "actionTime" : "2019-03-13T04:10:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "BagTag Scan at IST", + "actionTime" : "2019-03-13T03:56:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "OFFLOAD from IST", + "actionTime" : "2019-03-13T03:59:00Z" + } ] + }, { + "flightNo" : "BM665", + "flightDate" : "2019-03-12T03:11:00Z", + "fltRouteSrc" : "ATH", + "fltRouteDest" : "JTR", + "estimatedArrival" : "2019-03-12T15:12:00Z", + "actions" : [ { + "actionAt" : "JTR", + "actionCode" : "Offload to Carousel at JTR", + "actionTime" : "2019-03-12T15:06:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "ONLOAD to JTR", + "actionTime" : "2019-03-12T14:16:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "OFFLOAD from ATH", + "actionTime" : "2019-03-12T14:13:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-03-12T15:05:00Z", + "bagArrivalDate" : "2019-03-12T15:05:00Z" + }, + { + "id" : "79039899197755", + "tagNum" : "17657806340001", + "routing" : "SFO/IST/ATH/JTR", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "JTR", + "flightLegs" : [ { + "flightNo" : "BM318", + "flightDate" : "2019-03-12T03:00:00Z", + "fltRouteSrc" : "SFO", + "fltRouteDest" : "IST", + "estimatedArrival" : "2019-03-12T16:40:00Z", + "actions" : [ { + "actionAt" : "SFO", + "actionCode" : "ONLOAD to IST", + "actionTime" : "2019-03-12T03:14:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "BagTag Scan at SFO", + "actionTime" : "2019-03-12T02:50:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "Checkin at SFO", + "actionTime" : "2019-03-12T01:58:00Z" + } ] + }, { + "flightNo" : "BM696", + "flightDate" : "2019-03-12T04:11:00Z", + "fltRouteSrc" : "IST", + "fltRouteDest" : "ATH", + "estimatedArrival" : "2019-03-13T03:18:00Z", + "actions" : [ { + "actionAt" : "IST", + "actionCode" : "ONLOAD to ATH", + "actionTime" : "2019-03-13T04:17:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "BagTag Scan at IST", + "actionTime" : "2019-03-13T03:59:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "OFFLOAD from IST", + "actionTime" : "2019-03-13T03:48:00Z" + } ] + }, { + "flightNo" : "BM665", + "flightDate" : "2019-03-12T03:11:00Z", + "fltRouteSrc" : "ATH", + "fltRouteDest" : "JTR", + "estimatedArrival" : "2019-03-12T15:12:00Z", + "actions" : [ { + "actionAt" : "JTR", + "actionCode" : "Offload to Carousel at JTR", + "actionTime" : "2019-03-12T15:06:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "ONLOAD to JTR", + "actionTime" : "2019-03-12T14:16:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "OFFLOAD from ATH", + "actionTime" : "2019-03-12T14:23:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-03-12T16:05:00Z", + "bagArrivalDate" : "2019-03-12T16:25:00Z" + } ] +}` +/** + * Call the main function for this example + **/ +doSQLOperators(); + +async function doSQLOperators() { + let handle; + try { + /* UNCOMMENT line of code below if you are using Oracle NoSQL Database + * Cloud service. Leave the line commented if you are using onPrem database + handle = await getConnection_cloud(); */ + /* UNCOMMENT line of code below if you are using onPremise Oracle NoSQL + * Database. Leave the line commented if you are using NoSQL Database + * Cloud Service + handle = await getConnection_onPrem(); */ + await createTable(handle); + let putResult = await handle.put(TABLE_NAME, JSON.parse(bag1)); + let putResult1 = await handle.put(TABLE_NAME, JSON.parse(bag2)); + let putResult2 = await handle.put(TABLE_NAME, JSON.parse(bag3)); + let putResult3 = await handle.put(TABLE_NAME, JSON.parse(bag4)); + console.log("Wrote records of BaggageInfo schema"); + console.log("Using ORDER BY to sort data"); + await fetchData(handle,orderby_stmt); + + console.log("Using ORDER BY and LIMIT to sort and limit data"); + await fetchData(handle,sortlimit_stmt); + + console.log("Using GROUP BY, ORDER BY and LIMIT to group, sort and limit data"); + await fetchData(handle,groupsortlimit_stmt); + } catch (error ) { + console.log(error); + } + finally { + if (handle) { + handle.close(); + } + } +} + +/* Create and return an instance of a NoSQLCLient object for cloud service */ +function getConnection_cloud() { + /* replace the placeholders for compartment and region with actual values.*/ + const Region = ``; + return new NoSQLClient({ + region: Region, + compartment: "", + }); +} +/* Create and return an instance of a NoSQLCLient object for onPremises */ +function getConnection_onPrem() { + /* replace the placeholder with the fullname of your host */ + const kvstore_endpoint = `http://:8080`; + return new NoSQLClient({ + serviceType: "KVSTORE", + endpoint: kvstore_endpoint + }); + /* if it is a secure store, comment the return statement above and + * uncomment the lines below and pass the credentials, username and password + return new NoSQLClient({ + "serviceType": "KVSTORE", + "endpoint": "", + "auth": { + "kvstore":{ + "user": "", + "password": "" + } + } + }); */ +} + +//creates a table +async function createTable(handle) { + const createDDL = `CREATE TABLE IF NOT EXISTS ${TABLE_NAME} (ticketNo LONG, + fullName STRING, + gender STRING, + contactPhone STRING, + confNo STRING, + bagInfo JSON, + primary key(ticketNo))`; + console.log('Create table: ' + createDDL); + let res = await handle.tableDDL(createDDL, { + complete: true, + tableLimits: { + readUnits: 20, + writeUnits: 20, + storageGB: 1 + } + }); + console.log('Table created: ' + TABLE_NAME); +} +//fetches data from the table +async function fetchData(handle,querystmt) { + const opt = {}; + try { + do { + const result = await handle.query(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); + } +} diff --git a/examples-nosql-node-sdk/sqlexamples/Indexes.js b/examples-nosql-node-sdk/sqlexamples/Indexes.js index bf78d28..d45c05e 100644 --- a/examples-nosql-node-sdk/sqlexamples/Indexes.js +++ b/examples-nosql-node-sdk/sqlexamples/Indexes.js @@ -1,5 +1,6 @@ /* Copyright (c) 2023, 2024 Oracle and/or its affiliates. - * Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/ + * Licensed under the Universal Permissive License v 1.0 as shown at + * https://oss.oracle.com/licenses/upl/ */ 'use strict'; const NoSQLClient = require('oracle-nosqldb').NoSQLClient; @@ -11,23 +12,30 @@ const TABLE_NAME = 'stream_acct'; doindexes(); async function doindexes() { + let handle; try { - /* UNCOMMENT the line of code below if you are using Oracle NoSQL Database Cloud service. Leave the line commented if you are using onPremise database - let handle = await getConnection_cloud(); */ - /* UNCOMMENT the line of code below if you are using onPremise Oracle NoSQL Database. Leave the line commented if you are using NoSQL Database Cloud Service - let handle = await getConnection_onPrem(); */ + /* UNCOMMENT line of code below if you are using Oracle NoSQL Database + * Cloud service. Leave the line commented if you are using onPrem database + handle = await getConnection_cloud(); */ + /* UNCOMMENT line of code below if you are using onPremise Oracle NoSQL + * Database. Leave the line commented if you are using NoSQL Database + * Cloud Service + handle = await getConnection_onPrem(); */ await createTable(handle); await createIndex(handle); await dropIndex(handle); - process.exit(0); } catch (error ) { console.log(error); - process.exit(-1); + } + finally { + if (handle) { + handle.close(); + } } } /* Create and return an instance of a NoSQLCLient object for cloud service */ function getConnection_cloud() { - /* replace the placeholders for compartment and region with the actual values. */ + /* replace the placeholders for compartment and region with actual values. */ const Region = ``; return new NoSQLClient({ region: Region, diff --git a/examples-nosql-node-sdk/sqlexamples/ModifyData.js b/examples-nosql-node-sdk/sqlexamples/ModifyData.js index f0a6883..b329d34 100644 --- a/examples-nosql-node-sdk/sqlexamples/ModifyData.js +++ b/examples-nosql-node-sdk/sqlexamples/ModifyData.js @@ -1,5 +1,6 @@ /* Copyright (c) 2023, 2024 Oracle and/or its affiliates. - * Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/ + * Licensed under the Universal Permissive License v 1.0 as shown at + * https://oss.oracle.com/licenses/upl/ */ 'use strict'; const NoSQLClient = require('oracle-nosqldb').NoSQLClient; @@ -391,11 +392,15 @@ const del_stmt = 'DELETE FROM stream_acct acct1 WHERE acct1.acct_data.firstName= doModifyData(); async function doModifyData() { + let handle; try { - /*if it is a cloud service uncomment the line below, else if it is onPremise, comment the line below*/ - let handle = await getConnection_cloud(); - /*if it is a onPremise uncomment the line below,else if it is cloud service, comment the line below*/ - /*let handle = await getConnection_onPrem();*/ + /* UNCOMMENT line of code below if you are using Oracle NoSQL Database + * Cloud service. Leave the line commented if you are using onPrem database + handle = await getConnection_cloud(); */ + /* UNCOMMENT line of code below if you are using onPremise Oracle NoSQL + * Database. Leave the line commented if you are using NoSQL Database + * Cloud Service + handle = await getConnection_onPrem(); */ await createTable(handle); let putResult = await handle.put(TABLE_NAME, JSON.parse(acct1)); let putResult1 = await handle.put(TABLE_NAME, JSON.parse(acct2)); @@ -413,16 +418,19 @@ async function doModifyData() { console.log("Row deleted based on primary key"); await deleteRows(handle,del_stmt); console.log("Rows deleted"); - process.exit(0); } catch (error ) { console.log(error); - process.exit(-1); } + finally { + if (handle) { + handle.close(); + } + } } /* Create and return an instance of a NoSQLCLient object for cloud service */ function getConnection_cloud() { - /* replace the placeholders for compartment and region with the actual values. */ + /* replace the placeholders for compartment and region with actual values.*/ const Region = ``; return new NoSQLClient({ region: Region, diff --git a/examples-nosql-node-sdk/sqlexamples/MultiDataOps.js b/examples-nosql-node-sdk/sqlexamples/MultiDataOps.js new file mode 100644 index 0000000..61040eb --- /dev/null +++ b/examples-nosql-node-sdk/sqlexamples/MultiDataOps.js @@ -0,0 +1,149 @@ +/* Copyright (c) 2023, 2024 Oracle and/or its affiliates. + * Licensed under the Universal Permissive License v 1.0 as shown at + * https://oss.oracle.com/licenses/upl/ + */ +'use strict'; +const NoSQLClient = require('oracle-nosqldb').NoSQLClient; +const Region = require('oracle-nosqldb').Region; +const TABLE_NAME = 'examplesAddress'; +const add1= ` +{ + "id":1, + "address_line1":"10 Red Street", + "address_line1":"Apt 3", + "pin":1234567 +}` +const add2= ` +{ + "id":2, + "address_line1":"2 Green Street", + "address_line1":"Street 9", + "pin":1234567 +}` +const add3= ` +{ + "id":3, + "address_line1":"5 Blue Ave", + "address_line1":"Floor 9", + "pin":1234567 +}` +const add4= ` +{ + "id":4, + "address_line1":"9 Yellow Boulevard", + "address_line1":"Apt 3", + "pin":87654321 +}` +const stmt1 = 'select * from examplesAddress'; + + +/** + * Call the main function for this example + **/ +doMultiDataOps(); + +async function doMultiDataOps() { + let handle; + try { + /* UNCOMMENT line of code below if you are using Oracle NoSQL Database + * Cloud service. Leave the line commented if you are using onPrem database + handle = await getConnection_cloud(); */ + /* UNCOMMENT line of code below if you are using onPremise Oracle NoSQL + * Database. Leave the line commented if you are using NoSQL Database + * Cloud Service + handle = await getConnection_onPrem(); */ + await createTable(handle); + let putResult = await handle.put(TABLE_NAME, JSON.parse(add1)); + let putResult1 = await handle.put(TABLE_NAME, JSON.parse(add2)); + let putResult2 = await handle.put(TABLE_NAME, JSON.parse(add3)); + let putResult3 = await handle.put(TABLE_NAME, JSON.parse(add4)); + console.log("Wrote records of examplesAddress schema"); + await fetchRowCnt(handle,stmt1); + await mulRowDel(handle,1234567); + await fetchRowCnt(handle,stmt1); + } catch (error ) { + console.log(error); + } + finally { + if (handle) { + handle.close(); + } + } +} + +/* Create and return an instance of a NoSQLCLient object for cloud service */ +function getConnection_cloud() { + /* replace the placeholders for compartment and region with actual values.*/ + const Region = ``; + return new NoSQLClient({ + region: Region, + compartment: "", + }); +} +/* Create and return an instance of a NoSQLCLient object for onPremises */ +function getConnection_onPrem() { + /* replace the placeholder with the fullname of your host */ + const kvstore_endpoint = `http://:8080`; + return new NoSQLClient({ + serviceType: "KVSTORE", + endpoint: kvstore_endpoint + }); + /* if it is a secure store, comment the return statement above and + * uncomment the lines below and pass the credentials, username and password + return new NoSQLClient({ + "serviceType": "KVSTORE", + "endpoint": "", + "auth": { + "kvstore":{ + "user": "", + "password": "" + } + } + }); */ +} + +//creates a table +async function createTable(handle) { + const createDDL = `CREATE TABLE IF NOT EXISTS ${TABLE_NAME} (id INTEGER, + address_line1 STRING, + address_line2 STRING, + pin INTEGER, + PRIMARY KEY(SHARD(pin), id))`; + console.log('Create table: ' + createDDL); + let res = await handle.tableDDL(createDDL, { + complete: true, + tableLimits: { + readUnits: 20, + writeUnits: 20, + storageGB: 1 + } + }); + console.log('Table created: ' + TABLE_NAME); +} +//fetches data from the table +async function fetchRowCnt(handle,querystmt) { + const opt = {}; + try { + do { + const result = await handle.query(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); + } +} + +//deletes multiple rows +async function mulRowDel(handle,pinval){ + try { + /* Unconditional delete, should succeed.*/ + var result = await handle.deleteRange(TABLE_NAME, { pin: pinval }); + /* Expected output: delete succeeded*/ + console.log('delete ' + result.success ? 'succeeded' : 'failed'); + } catch(error) { + console.error(' Error: ' + error.message); + } +} diff --git a/examples-nosql-node-sdk/sqlexamples/MultiWrite.js b/examples-nosql-node-sdk/sqlexamples/MultiWrite.js new file mode 100644 index 0000000..d30391f --- /dev/null +++ b/examples-nosql-node-sdk/sqlexamples/MultiWrite.js @@ -0,0 +1,154 @@ +/* Copyright (c) 2023, 2024 Oracle and/or its affiliates. + * Licensed under the Universal Permissive License v 1.0 as shown at + * https://oss.oracle.com/licenses/upl/ + */ +'use strict'; + +const NoSQLClient = require('oracle-nosqldb').NoSQLClient; + +/** + * Call the main function for this example + **/ +domultiwrite(); + +async function domultiwrite() { + let handle; + try { + /* UNCOMMENT line of code below if you are using Oracle NoSQL Database + * Cloud service. Leave the line commented if you are using onPrem database + handle = await getConnection_cloud(); */ + /* UNCOMMENT line of code below if you are using onPremise Oracle NoSQL + * Database. Leave the line commented if you are using NoSQL Database + * Cloud Service + handle = await getConnection_onPrem(); */ + const table_name = 'ticket'; + const regtbl_DDL = `CREATE TABLE IF NOT EXISTS ${table_name} (ticketNo LONG, + confNo STRING, + primary key(ticketNo))`; + const childtable_name = 'ticket.bagInfo'; + const childtbl_DDL = `CREATE TABLE IF NOT EXISTS ${childtable_name} (id LONG, + tagNum LONG, + routing STRING, + lastActionCode STRING, + lastActionDesc STRING, + lastSeenStation STRING, + lastSeenTimeGmt TIMESTAMP(4), + bagArrivalDate TIMESTAMP(4), + PRIMARY KEY(id))`; + const desctable_name = 'ticket.bagInfo.flightLegs'; + const desctbl_DDL = `CREATE TABLE IF NOT EXISTS ${desctable_name} (flightDate TIMESTAMP(4), + fltRouteSrc STRING, + fltRouteDest STRING, + estimatedArrival TIMESTAMP(4), + actions JSON, + PRIMARY KEY(flightNo))`; + const data1=`{ + "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" + } ] + }` + + await createTable(handle,regtbl_DDL,'true',table_name); + await createTable(handle,childtbl_DDL,'false',childtable_name); + await createTable(handle,desctbl_DDL,'false',desctable_name); + 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); + console.log("Wrote records in ticket and bagInfo table"); + let putResult = await handle.put(desctable_name, JSON.parse(data1)); + console.log("Wrote records in flightlegs table"); + } catch (error ) { + console.log(error); + process.exit(-1); + } +} +/* Create and return an instance of a NoSQLCLient object for cloud service */ +function getConnection_cloud() { + /* replace the placeholders for compartment and region with actual values.*/ + const Region = ``; + return new NoSQLClient({ + region: Region, + compartment: "", + }); +} +/* Create and return an instance of a NoSQLCLient object for onPremises */ +function getConnection_onPrem() { + /* replace the placeholder with the fullname of your host */ + const kvstore_endpoint = `http://:8080`; + return new NoSQLClient({ + serviceType: "KVSTORE", + endpoint: kvstore_endpoint + }); + /* if it is a secure store, comment the return statement above and + * uncomment the lines below and pass the credentials, username and password + return new NoSQLClient({ + "serviceType": "KVSTORE", + "endpoint": "", + "auth": { + "kvstore":{ + "user": "", + "password": "" + } + } + }); */ +} + +//creates a table +async function createTable(handle, query_stmt, reg_table, table_name) { + let res; + if(reg_table=='true'){ + res = await handle.tableDDL(query_stmt, { + complete: true, + tableLimits: { + readUnits: 20, + writeUnits: 20, + storageGB: 1 + } + }); + } else{ + res = await handle.tableDDL(query_stmt, { + complete: true + }); + } + console.log('Table created: ' + table_name); +} diff --git a/examples-nosql-node-sdk/sqlexamples/Namespaces.js b/examples-nosql-node-sdk/sqlexamples/Namespaces.js index 3eb8654..bbd82d8 100644 --- a/examples-nosql-node-sdk/sqlexamples/Namespaces.js +++ b/examples-nosql-node-sdk/sqlexamples/Namespaces.js @@ -1,5 +1,6 @@ /* Copyright (c) 2023, 2024 Oracle and/or its affiliates. - * Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/ + * Licensed under the Universal Permissive License v 1.0 as shown at + * https://oss.oracle.com/licenses/upl/ */ 'use strict'; const NoSQLClient = require('oracle-nosqldb').NoSQLClient; @@ -11,15 +12,19 @@ const TABLE_NAME = 'stream_acct'; donamespaces(); async function donamespaces() { + let handle; try { - let handle = await getConnection_onPrem(); + handle = await getConnection_onPrem(); await createNS(handle); await dropNS(handle); - process.exit(0); } catch (error ) { console.log(error); - process.exit(-1); } + finally { + if (handle) { + handle.close(); + } + } } /* Create and return an instance of a NoSQLCLient object for onPremises */ diff --git a/examples-nosql-node-sdk/sqlexamples/QueryData.js b/examples-nosql-node-sdk/sqlexamples/QueryData.js index ed3571a..acec6f0 100644 --- a/examples-nosql-node-sdk/sqlexamples/QueryData.js +++ b/examples-nosql-node-sdk/sqlexamples/QueryData.js @@ -1,5 +1,6 @@ /* Copyright (c) 2023, 2024 Oracle and/or its affiliates. - * Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/ + * Licensed under the Universal Permissive License v 1.0 as shown at + * https://oss.oracle.com/licenses/upl/ */ 'use strict'; const NoSQLClient = require('oracle-nosqldb').NoSQLClient; @@ -285,30 +286,39 @@ const stmt2 = 'select account_expiry, acct.acct_data.lastName, acct.acct_data.co doQueryData(); async function doQueryData() { + let handle; try { - /* UNCOMMENT the line of code below if you are using Oracle NoSQL Database Cloud service. Leave the line commented if you are using onPremise database - let handle = await getConnection_cloud(); */ - /* UNCOMMENT the line of code below if you are using onPremise Oracle NoSQL Database. Leave the line commented if you are using NoSQL Database Cloud Service - let handle = await getConnection_onPrem(); */ + /* UNCOMMENT line of code below if you are using Oracle NoSQL Database + * Cloud service. Leave the line commented if you are using onPrem database + handle = await getConnection_cloud(); */ + /* UNCOMMENT line of code below if you are using onPremise Oracle NoSQL + * Database. Leave the line commented if you are using NoSQL Database + * Cloud Service + handle = await getConnection_onPrem(); */ await createTable(handle); 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("Wrote records of acct stream schema"); + console.log("Fetch one row using get API"); + await getRow(handle,2); console.log("Fetching all rows"); await fetchData(handle,stmt1); console.log("Fetching partial filtered rows"); await fetchData(handle,stmt2); - process.exit(0); } catch (error ) { console.log(error); - process.exit(-1); - } + } + finally { + if (handle) { + handle.close(); + } + } } /* Create and return an instance of a NoSQLCLient object for cloud service */ function getConnection_cloud() { - /* replace the placeholders for compartment and region with the actual values. */ + /* replace the placeholders for compartment and region with actual values.*/ const Region = ``; return new NoSQLClient({ region: Region, @@ -369,3 +379,12 @@ async function fetchData(handle,querystmt) { console.error(' Error: ' + error.message); } } +/*fetches single row with get API*/ +async function getRow(handle,idVal) { + try { + const result = await handle.get(TABLE_NAME,{acct_Id: idVal }); + console.log('Got row: %O', result.row); + } catch(error) { + console.error(' Error: ' + error.message); + } +} diff --git a/examples-nosql-node-sdk/sqlexamples/Regions.js b/examples-nosql-node-sdk/sqlexamples/Regions.js index ac55e9c..a8474f4 100644 --- a/examples-nosql-node-sdk/sqlexamples/Regions.js +++ b/examples-nosql-node-sdk/sqlexamples/Regions.js @@ -1,5 +1,6 @@ /* Copyright (c) 2023, 2024 Oracle and/or its affiliates. - * Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/ + * Licensed under the Universal Permissive License v 1.0 as shown at + * https://oss.oracle.com/licenses/upl/ */ 'use strict'; const NoSQLClient = require('oracle-nosqldb').NoSQLClient; @@ -14,17 +15,21 @@ doregions(); * create a table, write a record to the table, then read that record back */ async function doregions() { + let handle; try { - let handle = await getConnection_onPrem(); + handle = await getConnection_onPrem(); await createRegion(handle); await crtTabInRegion(handle); await dropTabInRegion(handle); await dropRegion(handle); - process.exit(0); } catch (error ) { console.log(error); - process.exit(-1); } + finally { + if (handle) { + handle.close(); + } + } } /* Create and return an instance of a NoSQLCLient object for onPremises */ diff --git a/examples-nosql-node-sdk/sqlexamples/SQLExpressions.js b/examples-nosql-node-sdk/sqlexamples/SQLExpressions.js new file mode 100644 index 0000000..3fd471b --- /dev/null +++ b/examples-nosql-node-sdk/sqlexamples/SQLExpressions.js @@ -0,0 +1,466 @@ +/* Copyright (c) 2023, 2024 Oracle and/or its affiliates. + * Licensed under the Universal Permissive License v 1.0 as shown at + * https://oss.oracle.com/licenses/upl/ + */ +'use strict'; + +const NoSQLClient = require('oracle-nosqldb').NoSQLClient; +const Region = require('oracle-nosqldb').Region; +const TABLE_NAME = 'BaggageInfo'; +const paran_expr = `SELECT fullName, bag.bagInfo.tagNum, bag.bagInfo.routing, + bag.bagInfo[].flightLegs[].fltRouteDest FROM BaggageInfo bag + WHERE bag.bagInfo.flightLegs[].fltRouteSrc=any "SFO" AND + (bag.bagInfo[].flightLegs[].fltRouteDest=any "ATH" OR + bag.bagInfo[].flightLegs[].fltRouteDest=any "JTR" )` +const case_expr = `SELECT fullName, + CASE + WHEN NOT exists bag.bagInfo.flightLegs[0] + THEN "you have no bag info" + WHEN NOT exists bag.bagInfo.flightLegs[1] + THEN "you have one hop" + WHEN NOT exists bag.bagInfo.flightLegs[2] + THEN "you have two hops." + ELSE "you have three hops." + END AS NUMBER_HOPS + FROM BaggageInfo bag WHERE ticketNo=1762341772625` +const seq_trn_expr = `SELECT seq_transform(l.bagInfo[], + seq_transform( + $sq1.flightLegs[], + seq_transform( + $sq2.actions[], + { + "at" : $sq3.actionAt, + "action" : $sq3.actionCode, + "flightNo" : $sq2.flightNo, + "tagNum" : $sq1.tagNum + } + ) + ) + ) AS actions FROM baggageInfo l WHERE ticketNo=1762376407826` + +const bag1= ` +{ + "ticketNo":"1762376407826", + "fullName":"Dierdre Amador", + "gender":"M", + "contactPhone":"165-742-5715", + "confNo":"ZG8Z5N", + "bagInfo":[ { + "id" : "7903989918469", + "tagNum" : "17657806240229", + "routing" : "JFK/MAD", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "MAD", + "flightLegs" : [ { + "flightNo" : "BM495", + "flightDate" : "2019-03-07T07:00:00Z", + "fltRouteSrc" : "JFK", + "fltRouteDest" : "MAD", + "estimatedArrival" : "2019-03-07T14:00:00Z", + "actions" : [ { + "actionAt" : "MAD", + "actionCode" : "Offload to Carousel at MAD", + "actionTime" : "2019-03-07T13:54:00Z" + }, { + "actionAt" : "JFK", + "actionCode" : "ONLOAD to MAD", + "actionTime" : "2019-03-07T07:00:00Z" + }, { + "actionAt" : "JFK", + "actionCode" : "BagTag Scan at JFK", + "actionTime" : "2019-03-07T06:53:00Z" + }, { + "actionAt" : "JFK", + "actionCode" : "Checkin at JFK", + "actionTime" : "2019-03-07T05:03:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-03-07T13:51:00Z", + "bagArrivalDate" : "2019-03-07T13:51:00Z" + } ] +}` +const bag2= ` +{ + "ticketNo":"1762344493810", + "fullName":"Adam Phillips", + "gender":"M", + "contactPhone":"893-324-1064", + "confNo":"LE6J4Z", + "bagInfo":[ { + "id" : "79039899165297", + "tagNum" : "17657806255240", + "routing" : "MIA/LAX/MEL", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "MEL", + "flightLegs" : [ { + "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" + } ] + }, { + "flightNo" : "BM667", + "flightDate" : "2019-02-01T06:13:00Z", + "fltRouteSrc" : "LAX", + "fltRouteDest" : "MEL", + "estimatedArrival" : "2019-02-01T16:15:00Z", + "actions" : [ { + "actionAt" : "MEL", + "actionCode" : "Offload to Carousel at MEL", + "actionTime" : "2019-02-01T16:15:00Z" + }, { + "actionAt" : "LAX", + "actionCode" : "ONLOAD to MEL", + "actionTime" : "2019-02-01T15:35:00Z" + }, { + "actionAt" : "LAX", + "actionCode" : "OFFLOAD from LAX", + "actionTime" : "2019-02-01T15:18:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-02-01T16:13:00Z", + "bagArrivalDate" : "2019-02-01T16:13:00Z" + } ] +}` + +const bag3= ` +{ + "ticketNo":"1762341772625", + "fullName":"Gerard Greene", + "gender":"M", + "contactPhone":"395-837-3772", + "confNo":"MC0E7R", + "bagInfo":[ { + "id" : "79039899152842", + "tagNum" : "1765780626568", + "routing" : "SFO/IST/ATH/JTR", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "JTR", + "flightLegs" : [ { + "flightNo" : "BM318", + "flightDate" : "2019-03-07T04:00:00Z", + "fltRouteSrc" : "SFO", + "fltRouteDest" : "IST", + "estimatedArrival" : "2019-03-07T17:00:00Z", + "actions" : [ { + "actionAt" : "SFO", + "actionCode" : "ONLOAD to IST", + "actionTime" : "2019-03-07T04:08:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "BagTag Scan at SFO", + "actionTime" : "2019-03-07T03:53:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "Checkin at SFO", + "actionTime" : "2019-03-07T02:20:00Z" + } ] + }, { + "flightNo" : "BM696", + "flightDate" : "2019-03-07T05:08:00Z", + "fltRouteSrc" : "IST", + "fltRouteDest" : "ATH", + "estimatedArrival" : "2019-03-08T04:10:00Z", + "actions" : [ { + "actionAt" : "IST", + "actionCode" : "ONLOAD to ATH", + "actionTime" : "2019-03-08T04:55:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "BagTag Scan at IST", + "actionTime" : "2019-03-08T04:34:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "OFFLOAD from IST", + "actionTime" : "2019-03-08T04:47:00Z" + } ] + }, { + "flightNo" : "BM665", + "flightDate" : "2019-03-07T04:08:00Z", + "fltRouteSrc" : "ATH", + "fltRouteDest" : "JTR", + "estimatedArrival" : "2019-03-07T16:10:00Z", + "actions" : [ { + "actionAt" : "JTR", + "actionCode" : "Offload to Carousel at JTR", + "actionTime" : "2019-03-07T16:09:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "ONLOAD to JTR", + "actionTime" : "2019-03-07T15:51:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "OFFLOAD from ATH", + "actionTime" : "2019-03-07T15:43:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-03-07T16:01:00Z", + "bagArrivalDate" : "2019-03-07T16:01:00Z" + } ] +}` + +const bag4= ` +{ + "ticketNo":"1762320369957", + "fullName":"Lorenzo Phil", + "gender":"M", + "contactPhone":"364-610-4444", + "confNo":"QI3V6Q", + "bagInfo":[ { + "id" : "79039899187755", + "tagNum" : "17657806240001", + "routing" : "SFO/IST/ATH/JTR", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "JTR", + "flightLegs" : [ { + "flightNo" : "BM318", + "flightDate" : "2019-03-12T03:00:00Z", + "fltRouteSrc" : "SFO", + "fltRouteDest" : "IST", + "estimatedArrival" : "2019-03-12T16:00:00Z", + "actions" : [ { + "actionAt" : "SFO", + "actionCode" : "ONLOAD to IST", + "actionTime" : "2019-03-12T03:11:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "BagTag Scan at SFO", + "actionTime" : "2019-03-12T02:49:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "Checkin at SFO", + "actionTime" : "2019-03-12T01:50:00Z" + } ] + }, { + "flightNo" : "BM696", + "flightDate" : "2019-03-12T04:11:00Z", + "fltRouteSrc" : "IST", + "fltRouteDest" : "ATH", + "estimatedArrival" : "2019-03-13T03:14:00Z", + "actions" : [ { + "actionAt" : "IST", + "actionCode" : "ONLOAD to ATH", + "actionTime" : "2019-03-13T04:10:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "BagTag Scan at IST", + "actionTime" : "2019-03-13T03:56:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "OFFLOAD from IST", + "actionTime" : "2019-03-13T03:59:00Z" + } ] + }, { + "flightNo" : "BM665", + "flightDate" : "2019-03-12T03:11:00Z", + "fltRouteSrc" : "ATH", + "fltRouteDest" : "JTR", + "estimatedArrival" : "2019-03-12T15:12:00Z", + "actions" : [ { + "actionAt" : "JTR", + "actionCode" : "Offload to Carousel at JTR", + "actionTime" : "2019-03-12T15:06:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "ONLOAD to JTR", + "actionTime" : "2019-03-12T14:16:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "OFFLOAD from ATH", + "actionTime" : "2019-03-12T14:13:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-03-12T15:05:00Z", + "bagArrivalDate" : "2019-03-12T15:05:00Z" + }, + { + "id" : "79039899197755", + "tagNum" : "17657806340001", + "routing" : "SFO/IST/ATH/JTR", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "JTR", + "flightLegs" : [ { + "flightNo" : "BM318", + "flightDate" : "2019-03-12T03:00:00Z", + "fltRouteSrc" : "SFO", + "fltRouteDest" : "IST", + "estimatedArrival" : "2019-03-12T16:40:00Z", + "actions" : [ { + "actionAt" : "SFO", + "actionCode" : "ONLOAD to IST", + "actionTime" : "2019-03-12T03:14:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "BagTag Scan at SFO", + "actionTime" : "2019-03-12T02:50:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "Checkin at SFO", + "actionTime" : "2019-03-12T01:58:00Z" + } ] + }, { + "flightNo" : "BM696", + "flightDate" : "2019-03-12T04:11:00Z", + "fltRouteSrc" : "IST", + "fltRouteDest" : "ATH", + "estimatedArrival" : "2019-03-13T03:18:00Z", + "actions" : [ { + "actionAt" : "IST", + "actionCode" : "ONLOAD to ATH", + "actionTime" : "2019-03-13T04:17:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "BagTag Scan at IST", + "actionTime" : "2019-03-13T03:59:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "OFFLOAD from IST", + "actionTime" : "2019-03-13T03:48:00Z" + } ] + }, { + "flightNo" : "BM665", + "flightDate" : "2019-03-12T03:11:00Z", + "fltRouteSrc" : "ATH", + "fltRouteDest" : "JTR", + "estimatedArrival" : "2019-03-12T15:12:00Z", + "actions" : [ { + "actionAt" : "JTR", + "actionCode" : "Offload to Carousel at JTR", + "actionTime" : "2019-03-12T15:06:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "ONLOAD to JTR", + "actionTime" : "2019-03-12T14:16:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "OFFLOAD from ATH", + "actionTime" : "2019-03-12T14:23:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-03-12T16:05:00Z", + "bagArrivalDate" : "2019-03-12T16:25:00Z" + } ] +}` +/** + * Call the main function for this example + **/ +doSQLOperators(); + +async function doSQLOperators() { + let handle; + try { + /* UNCOMMENT line of code below if you are using Oracle NoSQL Database + * Cloud service. Leave the line commented if you are using onPrem database + handle = await getConnection_cloud(); */ + /* UNCOMMENT line of code below if you are using onPremise Oracle NoSQL + * Database. Leave the line commented if you are using NoSQL Database + * Cloud Service + handle = await getConnection_onPrem(); */ + await createTable(handle); + let putResult = await handle.put(TABLE_NAME, JSON.parse(bag1)); + let putResult1 = await handle.put(TABLE_NAME, JSON.parse(bag2)); + let putResult2 = await handle.put(TABLE_NAME, JSON.parse(bag3)); + let putResult3 = await handle.put(TABLE_NAME, JSON.parse(bag4)); + console.log("Wrote records of BaggageInfo schema"); + console.log("Using Paranthesized expression"); + await fetchData(handle,paran_expr); + + console.log("Using Case Expression"); + await fetchData(handle,case_expr); + + console.log("Using Sequence Transform Expressions"); + await fetchData(handle,seq_trn_expr); + } catch (error ) { + console.log(error); + } + finally { + if (handle) { + handle.close(); + } + } +} + +/* Create and return an instance of a NoSQLCLient object for cloud service */ +function getConnection_cloud() { + /* replace the placeholders for compartment and region with actual values.*/ + const Region = ``; + return new NoSQLClient({ + region: Region, + compartment: "", + }); +} +/* Create and return an instance of a NoSQLCLient object for onPremises */ +function getConnection_onPrem() { + /* replace the placeholder with the fullname of your host */ + const kvstore_endpoint = `http://:8080`; + return new NoSQLClient({ + serviceType: "KVSTORE", + endpoint: kvstore_endpoint + }); + /* if it is a secure store, comment the return statement above and + * uncomment the lines below and pass the credentials, username and password + return new NoSQLClient({ + "serviceType": "KVSTORE", + "endpoint": "", + "auth": { + "kvstore":{ + "user": "", + "password": "" + } + } + }); */ +} + +//creates a table +async function createTable(handle) { + const createDDL = `CREATE TABLE IF NOT EXISTS ${TABLE_NAME} (ticketNo LONG, + fullName STRING, + gender STRING, + contactPhone STRING, + confNo STRING, + bagInfo JSON, + primary key(ticketNo))`; + console.log('Create table: ' + createDDL); + let res = await handle.tableDDL(createDDL, { + complete: true, + tableLimits: { + readUnits: 20, + writeUnits: 20, + storageGB: 1 + } + }); + console.log('Table created: ' + TABLE_NAME); +} +//fetches data from the table +async function fetchData(handle,querystmt) { + const opt = {}; + try { + do { + const result = await handle.query(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); + } +} diff --git a/examples-nosql-node-sdk/sqlexamples/SQLFunctions.js b/examples-nosql-node-sdk/sqlexamples/SQLFunctions.js new file mode 100644 index 0000000..e09c4b8 --- /dev/null +++ b/examples-nosql-node-sdk/sqlexamples/SQLFunctions.js @@ -0,0 +1,456 @@ +/* Copyright (c) 2023, 2024 Oracle and/or its affiliates. + * Licensed under the Universal Permissive License v 1.0 as shown at + * https://oss.oracle.com/licenses/upl/ + */ +'use strict'; + +const NoSQLClient = require('oracle-nosqldb').NoSQLClient; +const Region = require('oracle-nosqldb').Region; +const TABLE_NAME = 'BaggageInfo'; +const ts_func1 = `SELECT timestamp_add(bag.bagInfo.flightLegs[0].estimatedArrival, "5 minutes") + AS ARRIVAL_TIME FROM BaggageInfo bag WHERE ticketNo=1762341772625` +const ts_func2 = `SELECT $s.ticketno, $bagInfo.bagArrivalDate, $flightLeg.flightDate, + get_duration(timestamp_diff($bagInfo.bagArrivalDate, $flightLeg.flightDate)) AS diff + FROM baggageinfo $s, + $s.bagInfo[] AS $bagInfo, $bagInfo.flightLegs[] AS $flightLeg + WHERE ticketNo=1762344493810` +const string_func1 = `SELECT substring(bag.baginfo.routing,0,3) AS Source FROM baggageInfo bag + WHERE ticketNo=1762376407826` +const string_func2 = `SELECT fullname, length(fullname) AS fullname_length FROM BaggageInfo + WHERE ticketNo=1762320369957` +const string_func3 = `SELECT fullname FROM baggageInfo bag WHERE + EXISTS bag.bagInfo[contains($element.routing,"SFO")]` + +const bag1= ` +{ + "ticketNo":"1762376407826", + "fullName":"Dierdre Amador", + "gender":"M", + "contactPhone":"165-742-5715", + "confNo":"ZG8Z5N", + "bagInfo":[ { + "id" : "7903989918469", + "tagNum" : "17657806240229", + "routing" : "JFK/MAD", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "MAD", + "flightLegs" : [ { + "flightNo" : "BM495", + "flightDate" : "2019-03-07T07:00:00Z", + "fltRouteSrc" : "JFK", + "fltRouteDest" : "MAD", + "estimatedArrival" : "2019-03-07T14:00:00Z", + "actions" : [ { + "actionAt" : "MAD", + "actionCode" : "Offload to Carousel at MAD", + "actionTime" : "2019-03-07T13:54:00Z" + }, { + "actionAt" : "JFK", + "actionCode" : "ONLOAD to MAD", + "actionTime" : "2019-03-07T07:00:00Z" + }, { + "actionAt" : "JFK", + "actionCode" : "BagTag Scan at JFK", + "actionTime" : "2019-03-07T06:53:00Z" + }, { + "actionAt" : "JFK", + "actionCode" : "Checkin at JFK", + "actionTime" : "2019-03-07T05:03:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-03-07T13:51:00Z", + "bagArrivalDate" : "2019-03-07T13:51:00Z" + } ] +}` +const bag2= ` +{ + "ticketNo":"1762344493810", + "fullName":"Adam Phillips", + "gender":"M", + "contactPhone":"893-324-1064", + "confNo":"LE6J4Z", + "bagInfo":[ { + "id" : "79039899165297", + "tagNum" : "17657806255240", + "routing" : "MIA/LAX/MEL", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "MEL", + "flightLegs" : [ { + "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" + } ] + }, { + "flightNo" : "BM667", + "flightDate" : "2019-02-01T06:13:00Z", + "fltRouteSrc" : "LAX", + "fltRouteDest" : "MEL", + "estimatedArrival" : "2019-02-01T16:15:00Z", + "actions" : [ { + "actionAt" : "MEL", + "actionCode" : "Offload to Carousel at MEL", + "actionTime" : "2019-02-01T16:15:00Z" + }, { + "actionAt" : "LAX", + "actionCode" : "ONLOAD to MEL", + "actionTime" : "2019-02-01T15:35:00Z" + }, { + "actionAt" : "LAX", + "actionCode" : "OFFLOAD from LAX", + "actionTime" : "2019-02-01T15:18:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-02-01T16:13:00Z", + "bagArrivalDate" : "2019-02-01T16:13:00Z" + } ] +}` + +const bag3= ` +{ + "ticketNo":"1762341772625", + "fullName":"Gerard Greene", + "gender":"M", + "contactPhone":"395-837-3772", + "confNo":"MC0E7R", + "bagInfo":[ { + "id" : "79039899152842", + "tagNum" : "1765780626568", + "routing" : "SFO/IST/ATH/JTR", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "JTR", + "flightLegs" : [ { + "flightNo" : "BM318", + "flightDate" : "2019-03-07T04:00:00Z", + "fltRouteSrc" : "SFO", + "fltRouteDest" : "IST", + "estimatedArrival" : "2019-03-07T17:00:00Z", + "actions" : [ { + "actionAt" : "SFO", + "actionCode" : "ONLOAD to IST", + "actionTime" : "2019-03-07T04:08:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "BagTag Scan at SFO", + "actionTime" : "2019-03-07T03:53:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "Checkin at SFO", + "actionTime" : "2019-03-07T02:20:00Z" + } ] + }, { + "flightNo" : "BM696", + "flightDate" : "2019-03-07T05:08:00Z", + "fltRouteSrc" : "IST", + "fltRouteDest" : "ATH", + "estimatedArrival" : "2019-03-08T04:10:00Z", + "actions" : [ { + "actionAt" : "IST", + "actionCode" : "ONLOAD to ATH", + "actionTime" : "2019-03-08T04:55:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "BagTag Scan at IST", + "actionTime" : "2019-03-08T04:34:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "OFFLOAD from IST", + "actionTime" : "2019-03-08T04:47:00Z" + } ] + }, { + "flightNo" : "BM665", + "flightDate" : "2019-03-07T04:08:00Z", + "fltRouteSrc" : "ATH", + "fltRouteDest" : "JTR", + "estimatedArrival" : "2019-03-07T16:10:00Z", + "actions" : [ { + "actionAt" : "JTR", + "actionCode" : "Offload to Carousel at JTR", + "actionTime" : "2019-03-07T16:09:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "ONLOAD to JTR", + "actionTime" : "2019-03-07T15:51:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "OFFLOAD from ATH", + "actionTime" : "2019-03-07T15:43:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-03-07T16:01:00Z", + "bagArrivalDate" : "2019-03-07T16:01:00Z" + } ] +}` + +const bag4= ` +{ + "ticketNo":"1762320369957", + "fullName":"Lorenzo Phil", + "gender":"M", + "contactPhone":"364-610-4444", + "confNo":"QI3V6Q", + "bagInfo":[ { + "id" : "79039899187755", + "tagNum" : "17657806240001", + "routing" : "SFO/IST/ATH/JTR", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "JTR", + "flightLegs" : [ { + "flightNo" : "BM318", + "flightDate" : "2019-03-12T03:00:00Z", + "fltRouteSrc" : "SFO", + "fltRouteDest" : "IST", + "estimatedArrival" : "2019-03-12T16:00:00Z", + "actions" : [ { + "actionAt" : "SFO", + "actionCode" : "ONLOAD to IST", + "actionTime" : "2019-03-12T03:11:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "BagTag Scan at SFO", + "actionTime" : "2019-03-12T02:49:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "Checkin at SFO", + "actionTime" : "2019-03-12T01:50:00Z" + } ] + }, { + "flightNo" : "BM696", + "flightDate" : "2019-03-12T04:11:00Z", + "fltRouteSrc" : "IST", + "fltRouteDest" : "ATH", + "estimatedArrival" : "2019-03-13T03:14:00Z", + "actions" : [ { + "actionAt" : "IST", + "actionCode" : "ONLOAD to ATH", + "actionTime" : "2019-03-13T04:10:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "BagTag Scan at IST", + "actionTime" : "2019-03-13T03:56:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "OFFLOAD from IST", + "actionTime" : "2019-03-13T03:59:00Z" + } ] + }, { + "flightNo" : "BM665", + "flightDate" : "2019-03-12T03:11:00Z", + "fltRouteSrc" : "ATH", + "fltRouteDest" : "JTR", + "estimatedArrival" : "2019-03-12T15:12:00Z", + "actions" : [ { + "actionAt" : "JTR", + "actionCode" : "Offload to Carousel at JTR", + "actionTime" : "2019-03-12T15:06:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "ONLOAD to JTR", + "actionTime" : "2019-03-12T14:16:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "OFFLOAD from ATH", + "actionTime" : "2019-03-12T14:13:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-03-12T15:05:00Z", + "bagArrivalDate" : "2019-03-12T15:05:00Z" + }, + { + "id" : "79039899197755", + "tagNum" : "17657806340001", + "routing" : "SFO/IST/ATH/JTR", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "JTR", + "flightLegs" : [ { + "flightNo" : "BM318", + "flightDate" : "2019-03-12T03:00:00Z", + "fltRouteSrc" : "SFO", + "fltRouteDest" : "IST", + "estimatedArrival" : "2019-03-12T16:40:00Z", + "actions" : [ { + "actionAt" : "SFO", + "actionCode" : "ONLOAD to IST", + "actionTime" : "2019-03-12T03:14:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "BagTag Scan at SFO", + "actionTime" : "2019-03-12T02:50:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "Checkin at SFO", + "actionTime" : "2019-03-12T01:58:00Z" + } ] + }, { + "flightNo" : "BM696", + "flightDate" : "2019-03-12T04:11:00Z", + "fltRouteSrc" : "IST", + "fltRouteDest" : "ATH", + "estimatedArrival" : "2019-03-13T03:18:00Z", + "actions" : [ { + "actionAt" : "IST", + "actionCode" : "ONLOAD to ATH", + "actionTime" : "2019-03-13T04:17:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "BagTag Scan at IST", + "actionTime" : "2019-03-13T03:59:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "OFFLOAD from IST", + "actionTime" : "2019-03-13T03:48:00Z" + } ] + }, { + "flightNo" : "BM665", + "flightDate" : "2019-03-12T03:11:00Z", + "fltRouteSrc" : "ATH", + "fltRouteDest" : "JTR", + "estimatedArrival" : "2019-03-12T15:12:00Z", + "actions" : [ { + "actionAt" : "JTR", + "actionCode" : "Offload to Carousel at JTR", + "actionTime" : "2019-03-12T15:06:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "ONLOAD to JTR", + "actionTime" : "2019-03-12T14:16:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "OFFLOAD from ATH", + "actionTime" : "2019-03-12T14:23:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-03-12T16:05:00Z", + "bagArrivalDate" : "2019-03-12T16:25:00Z" + } ] +}` +/** + * Call the main function for this example + **/ +doSQLOperators(); + +async function doSQLOperators() { + let handle; + try { + /* UNCOMMENT line of code below if you are using Oracle NoSQL Database + * Cloud service. Leave the line commented if you are using onPrem database + handle = await getConnection_cloud(); */ + /* UNCOMMENT line of code below if you are using onPremise Oracle NoSQL + * Database. Leave the line commented if you are using NoSQL Database + * Cloud Service + handle = await getConnection_onPrem(); */ + await createTable(handle); + let putResult = await handle.put(TABLE_NAME, JSON.parse(bag1)); + let putResult1 = await handle.put(TABLE_NAME, JSON.parse(bag2)); + let putResult2 = await handle.put(TABLE_NAME, JSON.parse(bag3)); + let putResult3 = await handle.put(TABLE_NAME, JSON.parse(bag4)); + console.log("Wrote records of BaggageInfo schema"); + console.log("Using timestamp_add function:"); + await fetchData(handle,ts_func1); + + console.log("Using get_duration and timestamp_diff function:"); + await fetchData(handle,ts_func2); + + console.log("Using substring function:"); + await fetchData(handle,string_func1); + + console.log("Using length function"); + await fetchData(handle,string_func2); + + console.log("Using contains function"); + await fetchData(handle,string_func3); + } catch (error ) { + console.log(error); + } + finally { + if (handle) { + handle.close(); + } + } +} + +/* Create and return an instance of a NoSQLCLient object for cloud service */ +function getConnection_cloud() { + /* replace the placeholders for compartment and region with actual values.*/ + const Region = ``; + return new NoSQLClient({ + region: Region, + compartment: "", + }); +} +/* Create and return an instance of a NoSQLCLient object for onPremises */ +function getConnection_onPrem() { + /* replace the placeholder with the fullname of your host */ + const kvstore_endpoint = `http://:8080`; + return new NoSQLClient({ + serviceType: "KVSTORE", + endpoint: kvstore_endpoint + }); + /* if it is a secure store, comment the return statement above and + * uncomment the lines below and pass the credentials, username and password + return new NoSQLClient({ + "serviceType": "KVSTORE", + "endpoint": "", + "auth": { + "kvstore":{ + "user": "", + "password": "" + } + } + }); */ +} + +//creates a table +async function createTable(handle) { + const createDDL = `CREATE TABLE IF NOT EXISTS ${TABLE_NAME} (ticketNo LONG, + fullName STRING, + gender STRING, + contactPhone STRING, + confNo STRING, + bagInfo JSON, + primary key(ticketNo))`; + console.log('Create table: ' + createDDL); + let res = await handle.tableDDL(createDDL, { + complete: true, + tableLimits: { + readUnits: 20, + writeUnits: 20, + storageGB: 1 + } + }); + console.log('Table created: ' + TABLE_NAME); +} + +//fetches data from the table +async function fetchData(handle,querystmt) { + const opt = {}; + try { + do { + const result = await handle.query(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); + } +} diff --git a/examples-nosql-node-sdk/sqlexamples/SQLOperators.js b/examples-nosql-node-sdk/sqlexamples/SQLOperators.js new file mode 100644 index 0000000..b68f2e5 --- /dev/null +++ b/examples-nosql-node-sdk/sqlexamples/SQLOperators.js @@ -0,0 +1,455 @@ +/* Copyright (c) 2023, 2024 Oracle and/or its affiliates. + * Licensed under the Universal Permissive License v 1.0 as shown at + * https://oss.oracle.com/licenses/upl/ + */ +'use strict'; + +const NoSQLClient = require('oracle-nosqldb').NoSQLClient; +const Region = require('oracle-nosqldb').Region; +const TABLE_NAME = 'BaggageInfo'; +const seqcomp_stmt = `SELECT bag.bagInfo[].tagNum,bag.bagInfo[].flightLegs[].fltRouteSrc + FROM BaggageInfo bag WHERE bag.bagInfo[].flightLegs[].fltRouteSrc=any "SFO"` +const logope_stmt = `SELECT fullName, bag.bagInfo[].tagNum, bag.bagInfo[].routing, + bag.bagInfo[].flightLegs[].fltRouteSrc + FROM BaggageInfo bag + WHERE NOT bag.bagInfo[].flightLegs[].fltRouteSrc=any "SFO"` +const valcomp_stmt = `SELECT fullname, bag.bagInfo[].routing FROM BaggageInfo bag WHERE gender="M"` +const inope_stmt = `SELECT bagdet.fullName, bagdet.bagInfo[].tagNum + FROM BaggageInfo bagdet WHERE bagdet.fullName IN + ("Lucinda Beckman", "Adam Phillips","Dierdre Amador","Fallon Clements")` +const existsope_stmt = `SELECT fullName, bag.ticketNo FROM BaggageInfo bag WHERE + EXISTS bag.bagInfo[$element.bagArrivalDate >="2019-03-01T00:00:00"]` + +const bag1= ` +{ + "ticketNo":"1762376407826", + "fullName":"Dierdre Amador", + "gender":"M", + "contactPhone":"165-742-5715", + "confNo":"ZG8Z5N", + "bagInfo":[ { + "id" : "7903989918469", + "tagNum" : "17657806240229", + "routing" : "JFK/MAD", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "MAD", + "flightLegs" : [ { + "flightNo" : "BM495", + "flightDate" : "2019-03-07T07:00:00Z", + "fltRouteSrc" : "JFK", + "fltRouteDest" : "MAD", + "estimatedArrival" : "2019-03-07T14:00:00Z", + "actions" : [ { + "actionAt" : "MAD", + "actionCode" : "Offload to Carousel at MAD", + "actionTime" : "2019-03-07T13:54:00Z" + }, { + "actionAt" : "JFK", + "actionCode" : "ONLOAD to MAD", + "actionTime" : "2019-03-07T07:00:00Z" + }, { + "actionAt" : "JFK", + "actionCode" : "BagTag Scan at JFK", + "actionTime" : "2019-03-07T06:53:00Z" + }, { + "actionAt" : "JFK", + "actionCode" : "Checkin at JFK", + "actionTime" : "2019-03-07T05:03:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-03-07T13:51:00Z", + "bagArrivalDate" : "2019-03-07T13:51:00Z" + } ] +}` +const bag2= ` +{ + "ticketNo":"1762344493810", + "fullName":"Adam Phillips", + "gender":"M", + "contactPhone":"893-324-1064", + "confNo":"LE6J4Z", + "bagInfo":[ { + "id" : "79039899165297", + "tagNum" : "17657806255240", + "routing" : "MIA/LAX/MEL", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "MEL", + "flightLegs" : [ { + "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" + } ] + }, { + "flightNo" : "BM667", + "flightDate" : "2019-02-01T06:13:00Z", + "fltRouteSrc" : "LAX", + "fltRouteDest" : "MEL", + "estimatedArrival" : "2019-02-01T16:15:00Z", + "actions" : [ { + "actionAt" : "MEL", + "actionCode" : "Offload to Carousel at MEL", + "actionTime" : "2019-02-01T16:15:00Z" + }, { + "actionAt" : "LAX", + "actionCode" : "ONLOAD to MEL", + "actionTime" : "2019-02-01T15:35:00Z" + }, { + "actionAt" : "LAX", + "actionCode" : "OFFLOAD from LAX", + "actionTime" : "2019-02-01T15:18:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-02-01T16:13:00Z", + "bagArrivalDate" : "2019-02-01T16:13:00Z" + } ] +}` + +const bag3= ` +{ + "ticketNo":"1762341772625", + "fullName":"Gerard Greene", + "gender":"M", + "contactPhone":"395-837-3772", + "confNo":"MC0E7R", + "bagInfo":[ { + "id" : "79039899152842", + "tagNum" : "1765780626568", + "routing" : "SFO/IST/ATH/JTR", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "JTR", + "flightLegs" : [ { + "flightNo" : "BM318", + "flightDate" : "2019-03-07T04:00:00Z", + "fltRouteSrc" : "SFO", + "fltRouteDest" : "IST", + "estimatedArrival" : "2019-03-07T17:00:00Z", + "actions" : [ { + "actionAt" : "SFO", + "actionCode" : "ONLOAD to IST", + "actionTime" : "2019-03-07T04:08:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "BagTag Scan at SFO", + "actionTime" : "2019-03-07T03:53:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "Checkin at SFO", + "actionTime" : "2019-03-07T02:20:00Z" + } ] + }, { + "flightNo" : "BM696", + "flightDate" : "2019-03-07T05:08:00Z", + "fltRouteSrc" : "IST", + "fltRouteDest" : "ATH", + "estimatedArrival" : "2019-03-08T04:10:00Z", + "actions" : [ { + "actionAt" : "IST", + "actionCode" : "ONLOAD to ATH", + "actionTime" : "2019-03-08T04:55:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "BagTag Scan at IST", + "actionTime" : "2019-03-08T04:34:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "OFFLOAD from IST", + "actionTime" : "2019-03-08T04:47:00Z" + } ] + }, { + "flightNo" : "BM665", + "flightDate" : "2019-03-07T04:08:00Z", + "fltRouteSrc" : "ATH", + "fltRouteDest" : "JTR", + "estimatedArrival" : "2019-03-07T16:10:00Z", + "actions" : [ { + "actionAt" : "JTR", + "actionCode" : "Offload to Carousel at JTR", + "actionTime" : "2019-03-07T16:09:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "ONLOAD to JTR", + "actionTime" : "2019-03-07T15:51:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "OFFLOAD from ATH", + "actionTime" : "2019-03-07T15:43:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-03-07T16:01:00Z", + "bagArrivalDate" : "2019-03-07T16:01:00Z" + } ] +}` + +const bag4= ` +{ + "ticketNo":"1762320369957", + "fullName":"Lorenzo Phil", + "gender":"M", + "contactPhone":"364-610-4444", + "confNo":"QI3V6Q", + "bagInfo":[ { + "id" : "79039899187755", + "tagNum" : "17657806240001", + "routing" : "SFO/IST/ATH/JTR", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "JTR", + "flightLegs" : [ { + "flightNo" : "BM318", + "flightDate" : "2019-03-12T03:00:00Z", + "fltRouteSrc" : "SFO", + "fltRouteDest" : "IST", + "estimatedArrival" : "2019-03-12T16:00:00Z", + "actions" : [ { + "actionAt" : "SFO", + "actionCode" : "ONLOAD to IST", + "actionTime" : "2019-03-12T03:11:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "BagTag Scan at SFO", + "actionTime" : "2019-03-12T02:49:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "Checkin at SFO", + "actionTime" : "2019-03-12T01:50:00Z" + } ] + }, { + "flightNo" : "BM696", + "flightDate" : "2019-03-12T04:11:00Z", + "fltRouteSrc" : "IST", + "fltRouteDest" : "ATH", + "estimatedArrival" : "2019-03-13T03:14:00Z", + "actions" : [ { + "actionAt" : "IST", + "actionCode" : "ONLOAD to ATH", + "actionTime" : "2019-03-13T04:10:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "BagTag Scan at IST", + "actionTime" : "2019-03-13T03:56:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "OFFLOAD from IST", + "actionTime" : "2019-03-13T03:59:00Z" + } ] + }, { + "flightNo" : "BM665", + "flightDate" : "2019-03-12T03:11:00Z", + "fltRouteSrc" : "ATH", + "fltRouteDest" : "JTR", + "estimatedArrival" : "2019-03-12T15:12:00Z", + "actions" : [ { + "actionAt" : "JTR", + "actionCode" : "Offload to Carousel at JTR", + "actionTime" : "2019-03-12T15:06:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "ONLOAD to JTR", + "actionTime" : "2019-03-12T14:16:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "OFFLOAD from ATH", + "actionTime" : "2019-03-12T14:13:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-03-12T15:05:00Z", + "bagArrivalDate" : "2019-03-12T15:05:00Z" + }, + { + "id" : "79039899197755", + "tagNum" : "17657806340001", + "routing" : "SFO/IST/ATH/JTR", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "JTR", + "flightLegs" : [ { + "flightNo" : "BM318", + "flightDate" : "2019-03-12T03:00:00Z", + "fltRouteSrc" : "SFO", + "fltRouteDest" : "IST", + "estimatedArrival" : "2019-03-12T16:40:00Z", + "actions" : [ { + "actionAt" : "SFO", + "actionCode" : "ONLOAD to IST", + "actionTime" : "2019-03-12T03:14:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "BagTag Scan at SFO", + "actionTime" : "2019-03-12T02:50:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "Checkin at SFO", + "actionTime" : "2019-03-12T01:58:00Z" + } ] + }, { + "flightNo" : "BM696", + "flightDate" : "2019-03-12T04:11:00Z", + "fltRouteSrc" : "IST", + "fltRouteDest" : "ATH", + "estimatedArrival" : "2019-03-13T03:18:00Z", + "actions" : [ { + "actionAt" : "IST", + "actionCode" : "ONLOAD to ATH", + "actionTime" : "2019-03-13T04:17:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "BagTag Scan at IST", + "actionTime" : "2019-03-13T03:59:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "OFFLOAD from IST", + "actionTime" : "2019-03-13T03:48:00Z" + } ] + }, { + "flightNo" : "BM665", + "flightDate" : "2019-03-12T03:11:00Z", + "fltRouteSrc" : "ATH", + "fltRouteDest" : "JTR", + "estimatedArrival" : "2019-03-12T15:12:00Z", + "actions" : [ { + "actionAt" : "JTR", + "actionCode" : "Offload to Carousel at JTR", + "actionTime" : "2019-03-12T15:06:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "ONLOAD to JTR", + "actionTime" : "2019-03-12T14:16:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "OFFLOAD from ATH", + "actionTime" : "2019-03-12T14:23:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-03-12T16:05:00Z", + "bagArrivalDate" : "2019-03-12T16:25:00Z" + } ] +}` +/** + * Call the main function for this example + **/ +doSQLOperators(); + +async function doSQLOperators() { + let handle; + try { + /* UNCOMMENT line of code below if you are using Oracle NoSQL Database + * Cloud service. Leave the line commented if you are using onPrem database + handle = await getConnection_cloud(); */ + /* UNCOMMENT line of code below if you are using onPremise Oracle NoSQL + * Database. Leave the line commented if you are using NoSQL Database + * Cloud Service + handle = await getConnection_onPrem(); */ + await createTable(handle); + let putResult = await handle.put(TABLE_NAME, JSON.parse(bag1)); + let putResult1 = await handle.put(TABLE_NAME, JSON.parse(bag2)); + let putResult2 = await handle.put(TABLE_NAME, JSON.parse(bag3)); + let putResult3 = await handle.put(TABLE_NAME, JSON.parse(bag4)); + console.log("Wrote records of BaggageInfo schema"); + console.log("Using Sequence Comparison operator"); + await fetchData(handle,seqcomp_stmt); + + console.log("Using Logical operator"); + await fetchData(handle,logope_stmt); + + console.log("Using Value Comparison operator"); + await fetchData(handle,valcomp_stmt); + + console.log("Using IN operator"); + await fetchData(handle,inope_stmt); + + console.log("Using EXISTS operator"); + await fetchData(handle,existsope_stmt); + } catch (error ) { + console.log(error); + } + finally { + if (handle) { + handle.close(); + } + } +} + +/* Create and return an instance of a NoSQLCLient object for cloud service */ +function getConnection_cloud() { + /* replace the placeholders for compartment and region with actual values.*/ + const Region = ``; + return new NoSQLClient({ + region: Region, + compartment: "", + }); +} +/* Create and return an instance of a NoSQLCLient object for onPremises */ +function getConnection_onPrem() { + /* replace the placeholder with the fullname of your host */ + const kvstore_endpoint = `http://:8080`; + return new NoSQLClient({ + serviceType: "KVSTORE", + endpoint: kvstore_endpoint + }); + /* if it is a secure store, comment the return statement above and + * uncomment the lines below and pass the credentials, username and password + return new NoSQLClient({ + "serviceType": "KVSTORE", + "endpoint": "", + "auth": { + "kvstore":{ + "user": "", + "password": "" + } + } + }); */ +} + +//creates a table +async function createTable(handle) { + const createDDL = `CREATE TABLE IF NOT EXISTS ${TABLE_NAME} (ticketNo LONG, + fullName STRING, + gender STRING, + contactPhone STRING, + confNo STRING, + bagInfo JSON, + primary key(ticketNo))`; + console.log('Create table: ' + createDDL); + let res = await handle.tableDDL(createDDL, { + complete: true, + tableLimits: { + readUnits: 20, + writeUnits: 20, + storageGB: 1 + } + }); + console.log('Table created: ' + TABLE_NAME); +} + +//fetches data from the table +async function fetchData(handle,querystmt) { + const opt = {}; + try { + do { + const result = await handle.query(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); + } +} diff --git a/examples-nosql-node-sdk/sqlexamples/TableJoins.js b/examples-nosql-node-sdk/sqlexamples/TableJoins.js index 85b44c5..f6aaa50 100644 --- a/examples-nosql-node-sdk/sqlexamples/TableJoins.js +++ b/examples-nosql-node-sdk/sqlexamples/TableJoins.js @@ -1,5 +1,6 @@ /*Copyright (c) 2023, 2024 Oracle and/or its affiliates. - * Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/ + * Licensed under the Universal Permissive License v 1.0 as shown at + * https://oss.oracle.com/licenses/upl/ */ 'use strict'; @@ -12,10 +13,13 @@ dotablejoins(); async function dotablejoins() { try { - /*if it is a cloud service uncomment the line below, else if it is onPremise, comment the line below*/ - let handle = await getConnection_cloud(); - /*if it is a onPremise uncomment the line below,else if it is cloud service, comment the line below*/ - /*let handle = await getConnection_onPrem();*/ + /* UNCOMMENT line of code below if you are using Oracle NoSQL Database + * Cloud service. Leave the line commented if you are using onPrem database + handle = await getConnection_cloud(); */ + /* UNCOMMENT line of code below if you are using onPremise Oracle NoSQL + * Database. Leave the line commented if you are using NoSQL Database + * Cloud Service + handle = await getConnection_onPrem(); */ const table_name = 'ticket'; const regtbl_DDL = `CREATE TABLE IF NOT EXISTS ${table_name} (ticketNo LONG, confNo STRING, @@ -91,15 +95,18 @@ async function dotablejoins() { await fetchData(handle,stmt_loj); console.log("Fetching data using NESTED TABLES"); await fetchData(handle,stmt_nt); - process.exit(0); } catch (error ) { console.log(error); - process.exit(-1); } + finally { + if (handle) { + handle.close(); + } + } } /* Create and return an instance of a NoSQLCLient object for cloud service */ function getConnection_cloud() { - /* replace the placeholders for compartment and region with the actual values. */ + /* replace the placeholders for compartment and region with actual values.*/ const Region = ``; return new NoSQLClient({ region: Region, diff --git a/examples-nosql-python-sdk/sqlexamples/AddBagData.py b/examples-nosql-python-sdk/sqlexamples/AddBagData.py new file mode 100644 index 0000000..8e246f4 --- /dev/null +++ b/examples-nosql-python-sdk/sqlexamples/AddBagData.py @@ -0,0 +1,393 @@ +# Copyright (c) 2023, 2024 Oracle and/or its affiliates. +# Licensed under the Universal Permissive License v 1.0 as shown at +# https://oss.oracle.com/licenses/upl/ +import os +from borneo import (Regions, NoSQLHandle, NoSQLHandleConfig, PutRequest,QueryRequest, + TableRequest, GetRequest, TableLimits, State) +from borneo.iam import SignatureProvider +from borneo.kv import StoreAccessTokenProvider + +# Given a region, and compartment, instantiate a connection to the +# cloud service and return it +def get_connection_cloud(): + print("Connecting to the Oracle NoSQL Cloud Service") + # replace the placeholder with your region identifier + region = '' + provider = SignatureProvider() + # If using the DEFAULT profile with the config file in default location + # ~/.oci/config + config = NoSQLHandleConfig(region, provider) + # replace the placeholder with the ocid of your compartment + config.set_default_compartment("") + return(NoSQLHandle(config)) + +# Given a endpoint, instantiate a connection to onPremise Oracle NoSQL database +def get_connection_onprem(): + # replace the placeholder with the name of your host + kvstore_endpoint ='http://:8080' + provider = StoreAccessTokenProvider() + # If using a secure store, uncomment the line below and pass the username, + # password of the store to StoreAccessTokenProvider + # provider = StoreAccessTokenProvider(username, password) + return NoSQLHandle(NoSQLHandleConfig(kvstore_endpoint, provider)) + +# Create a table and set the table limits +def create_table(handle): + statement = '''create table if not exists BaggageInfo (ticketNo LONG, + fullName STRING, + gender STRING, + contactPhone STRING, + confNo STRING, + bagInfo JSON, + primary key(ticketNo))''' + request = TableRequest().set_statement(statement).set_table_limits(TableLimits(20, 10, 1)) + # Create the table, waiting for a total of 40000 milliseconds + # and polling the service every 3000 milliseconds to see if table is active + table_result = handle.do_table_request(request, 40000, 3000) + table_result.wait_for_completion(handle, 40000, 3000) + if (table_result.get_state() == State.ACTIVE): + print('Created table: BaggageInfo') + else: + raise NameError('Table BaggageInfo is in an unexpected state ' + str(table_result.get_state())) + +# Add a row of data to the table +def insert_record(handle,table_name,bag_data): + request = PutRequest().set_table_name(table_name).set_value_from_json(bag_data) + handle.put(request) + print('Loaded a row into table: BaggageInfo') + +def main(): + handle = None + bag1='''{ + "ticketNo":"1762376407826", + "fullName":"Dierdre Amador", + "gender":"M", + "contactPhone":"165-742-5715", + "confNo":"ZG8Z5N", + "bagInfo":[ { + "id" : "7903989918469", + "tagNum" : "17657806240229", + "routing" : "JFK/MAD", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "MAD", + "flightLegs" : [ { + "flightNo" : "BM495", + "flightDate" : "2019-03-07T07:00:00Z", + "fltRouteSrc" : "JFK", + "fltRouteDest" : "MAD", + "estimatedArrival" : "2019-03-07T14:00:00Z", + "actions" : [ { + "actionAt" : "MAD", + "actionCode" : "Offload to Carousel at MAD", + "actionTime" : "2019-03-07T13:54:00Z" + }, { + "actionAt" : "JFK", + "actionCode" : "ONLOAD to MAD", + "actionTime" : "2019-03-07T07:00:00Z" + }, { + "actionAt" : "JFK", + "actionCode" : "BagTag Scan at JFK", + "actionTime" : "2019-03-07T06:53:00Z" + }, { + "actionAt" : "JFK", + "actionCode" : "Checkin at JFK", + "actionTime" : "2019-03-07T05:03:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-03-07T13:51:00Z", + "bagArrivalDate" : "2019-03-07T13:51:00Z" + } ] + }''' + bag2=''' + { + "ticketNo":"1762344493810", + "fullName":"Adam Phillips", + "gender":"M", + "contactPhone":"893-324-1064", + "confNo":"LE6J4Z", + "bagInfo":[ { + "id" : "79039899165297", + "tagNum" : "17657806255240", + "routing" : "MIA/LAX/MEL", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "MEL", + "flightLegs" : [ { + "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" + } ] + }, { + "flightNo" : "BM667", + "flightDate" : "2019-02-01T06:13:00Z", + "fltRouteSrc" : "LAX", + "fltRouteDest" : "MEL", + "estimatedArrival" : "2019-02-01T16:15:00Z", + "actions" : [ { + "actionAt" : "MEL", + "actionCode" : "Offload to Carousel at MEL", + "actionTime" : "2019-02-01T16:15:00Z" + }, { + "actionAt" : "LAX", + "actionCode" : "ONLOAD to MEL", + "actionTime" : "2019-02-01T15:35:00Z" + }, { + "actionAt" : "LAX", + "actionCode" : "OFFLOAD from LAX", + "actionTime" : "2019-02-01T15:18:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-02-01T16:13:00Z", + "bagArrivalDate" : "2019-02-01T16:13:00Z" + } ] + }''' + bag3=''' + { + "ticketNo":"1762341772625", + "fullName":"Gerard Greene", + "gender":"M", + "contactPhone":"395-837-3772", + "confNo":"MC0E7R", + "bagInfo":[ { + "id" : "79039899152842", + "tagNum" : "1765780626568", + "routing" : "SFO/IST/ATH/JTR", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "JTR", + "flightLegs" : [ { + "flightNo" : "BM318", + "flightDate" : "2019-03-07T04:00:00Z", + "fltRouteSrc" : "SFO", + "fltRouteDest" : "IST", + "estimatedArrival" : "2019-03-07T17:00:00Z", + "actions" : [ { + "actionAt" : "SFO", + "actionCode" : "ONLOAD to IST", + "actionTime" : "2019-03-07T04:08:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "BagTag Scan at SFO", + "actionTime" : "2019-03-07T03:53:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "Checkin at SFO", + "actionTime" : "2019-03-07T02:20:00Z" + } ] + }, { + "flightNo" : "BM696", + "flightDate" : "2019-03-07T05:08:00Z", + "fltRouteSrc" : "IST", + "fltRouteDest" : "ATH", + "estimatedArrival" : "2019-03-08T04:10:00Z", + "actions" : [ { + "actionAt" : "IST", + "actionCode" : "ONLOAD to ATH", + "actionTime" : "2019-03-08T04:55:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "BagTag Scan at IST", + "actionTime" : "2019-03-08T04:34:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "OFFLOAD from IST", + "actionTime" : "2019-03-08T04:47:00Z" + } ] + }, { + "flightNo" : "BM665", + "flightDate" : "2019-03-07T04:08:00Z", + "fltRouteSrc" : "ATH", + "fltRouteDest" : "JTR", + "estimatedArrival" : "2019-03-07T16:10:00Z", + "actions" : [ { + "actionAt" : "JTR", + "actionCode" : "Offload to Carousel at JTR", + "actionTime" : "2019-03-07T16:09:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "ONLOAD to JTR", + "actionTime" : "2019-03-07T15:51:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "OFFLOAD from ATH", + "actionTime" : "2019-03-07T15:43:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-03-07T16:01:00Z", + "bagArrivalDate" : "2019-03-07T16:01:00Z" + } ] + }''' + bag4=''' + { + "ticketNo":"1762320369957", + "fullName":"Lorenzo Phil", + "gender":"M", + "contactPhone":"364-610-4444", + "confNo":"QI3V6Q", + "bagInfo":[ { + "id" : "79039899187755", + "tagNum" : "17657806240001", + "routing" : "SFO/IST/ATH/JTR", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "JTR", + "flightLegs" : [ { + "flightNo" : "BM318", + "flightDate" : "2019-03-12T03:00:00Z", + "fltRouteSrc" : "SFO", + "fltRouteDest" : "IST", + "estimatedArrival" : "2019-03-12T16:00:00Z", + "actions" : [ { + "actionAt" : "SFO", + "actionCode" : "ONLOAD to IST", + "actionTime" : "2019-03-12T03:11:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "BagTag Scan at SFO", + "actionTime" : "2019-03-12T02:49:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "Checkin at SFO", + "actionTime" : "2019-03-12T01:50:00Z" + } ] + }, { + "flightNo" : "BM696", + "flightDate" : "2019-03-12T04:11:00Z", + "fltRouteSrc" : "IST", + "fltRouteDest" : "ATH", + "estimatedArrival" : "2019-03-13T03:14:00Z", + "actions" : [ { + "actionAt" : "IST", + "actionCode" : "ONLOAD to ATH", + "actionTime" : "2019-03-13T04:10:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "BagTag Scan at IST", + "actionTime" : "2019-03-13T03:56:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "OFFLOAD from IST", + "actionTime" : "2019-03-13T03:59:00Z" + } ] + }, { + "flightNo" : "BM665", + "flightDate" : "2019-03-12T03:11:00Z", + "fltRouteSrc" : "ATH", + "fltRouteDest" : "JTR", + "estimatedArrival" : "2019-03-12T15:12:00Z", + "actions" : [ { + "actionAt" : "JTR", + "actionCode" : "Offload to Carousel at JTR", + "actionTime" : "2019-03-12T15:06:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "ONLOAD to JTR", + "actionTime" : "2019-03-12T14:16:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "OFFLOAD from ATH", + "actionTime" : "2019-03-12T14:13:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-03-12T15:05:00Z", + "bagArrivalDate" : "2019-03-12T15:05:00Z" + }, + { + "id" : "79039899197755", + "tagNum" : "17657806340001", + "routing" : "SFO/IST/ATH/JTR", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "JTR", + "flightLegs" : [ { + "flightNo" : "BM318", + "flightDate" : "2019-03-12T03:00:00Z", + "fltRouteSrc" : "SFO", + "fltRouteDest" : "IST", + "estimatedArrival" : "2019-03-12T16:40:00Z", + "actions" : [ { + "actionAt" : "SFO", + "actionCode" : "ONLOAD to IST", + "actionTime" : "2019-03-12T03:14:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "BagTag Scan at SFO", + "actionTime" : "2019-03-12T02:50:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "Checkin at SFO", + "actionTime" : "2019-03-12T01:58:00Z" + } ] + }, { + "flightNo" : "BM696", + "flightDate" : "2019-03-12T04:11:00Z", + "fltRouteSrc" : "IST", + "fltRouteDest" : "ATH", + "estimatedArrival" : "2019-03-13T03:18:00Z", + "actions" : [ { + "actionAt" : "IST", + "actionCode" : "ONLOAD to ATH", + "actionTime" : "2019-03-13T04:17:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "BagTag Scan at IST", + "actionTime" : "2019-03-13T03:59:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "OFFLOAD from IST", + "actionTime" : "2019-03-13T03:48:00Z" + } ] + }, { + "flightNo" : "BM665", + "flightDate" : "2019-03-12T03:11:00Z", + "fltRouteSrc" : "ATH", + "fltRouteDest" : "JTR", + "estimatedArrival" : "2019-03-12T15:12:00Z", + "actions" : [ { + "actionAt" : "JTR", + "actionCode" : "Offload to Carousel at JTR", + "actionTime" : "2019-03-12T15:06:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "ONLOAD to JTR", + "actionTime" : "2019-03-12T14:16:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "OFFLOAD from ATH", + "actionTime" : "2019-03-12T14:23:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-03-12T16:05:00Z", + "bagArrivalDate" : "2019-03-12T16:25:00Z" + } ] + }''' + # if cloud service uncomment this + handle = get_connection_cloud() + # if onPremise uncomment this + #handle = get_connection_onprem() + create_table(handle) + insert_record(handle,'BaggageInfo',bag1) + insert_record(handle,'BaggageInfo',bag2) + insert_record(handle,'BaggageInfo',bag3) + insert_record(handle,'BaggageInfo',bag4) + if handle is not None: + handle.close() + os._exit(0) + +if __name__ == "__main__": + main() diff --git a/examples-nosql-python-sdk/sqlexamples/AddData.py b/examples-nosql-python-sdk/sqlexamples/AddData.py index 947082f..6335d81 100644 --- a/examples-nosql-python-sdk/sqlexamples/AddData.py +++ b/examples-nosql-python-sdk/sqlexamples/AddData.py @@ -1,5 +1,6 @@ # Copyright (c) 2023, 2024 Oracle and/or its affiliates. -# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/ +# Licensed under the Universal Permissive License v 1.0 as shown at +# https://oss.oracle.com/licenses/upl/ import os from borneo import (Regions, NoSQLHandle, NoSQLHandleConfig, PutRequest,QueryRequest, TableRequest, GetRequest, TableLimits, State) @@ -13,18 +14,20 @@ def get_connection_cloud(): # replace the placeholder with your region identifier region = '' provider = SignatureProvider() - # If using the DEFAULT profile with the config file in default location =~/.oci/config + # If using the DEFAULT profile with the config file in default location + # ~/.oci/config config = NoSQLHandleConfig(region, provider) # replace the placeholder with the ocid of your compartment config.set_default_compartment("") return(NoSQLHandle(config)) -# Given a endpoint, instantiate a connection to the onPremise Oracle NoSQL database +# Given a endpoint, instantiate a connection to onPremise Oracle NoSQL database def get_connection_onprem(): # replace the placeholder with the name of your host kvstore_endpoint ='http://:8080' provider = StoreAccessTokenProvider() - # If using a secure store, uncomment the line below and pass the username, password of the store to StoreAccessTokenProvider + # If using a secure store, uncomment the line below and pass the username, + # password of the store to StoreAccessTokenProvider # provider = StoreAccessTokenProvider(username, password) return NoSQLHandle(NoSQLHandleConfig(kvstore_endpoint, provider)) @@ -37,7 +40,7 @@ def create_table(handle): primary key(acct_Id))''' request = TableRequest().set_statement(statement).set_table_limits(TableLimits(20, 10, 1)) # Create the table waiting for a total of 40000 milliseconds - # and polling the service every 3000 milliseconds to see if the table is active + # and polling the service every 3000 milliseconds to see if table is active table_result = handle.do_table_request(request, 40000, 3000) table_result.wait_for_completion(handle, 40000, 3000) if (table_result.get_state() == State.ACTIVE): @@ -52,6 +55,7 @@ def insert_record(handle,table_name,acct_data): print('Loaded a row into table: stream_acct') def main(): + handle = None acct1=''' { "acct_Id":1, @@ -333,6 +337,8 @@ def main(): insert_record(handle,'stream_acct',acct1) insert_record(handle,'stream_acct',acct2) insert_record(handle,'stream_acct',acct3) + if handle is not None: + handle.close() os._exit(0) if __name__ == "__main__": diff --git a/examples-nosql-python-sdk/sqlexamples/AlterTable.py b/examples-nosql-python-sdk/sqlexamples/AlterTable.py index a12d050..39f7bc0 100644 --- a/examples-nosql-python-sdk/sqlexamples/AlterTable.py +++ b/examples-nosql-python-sdk/sqlexamples/AlterTable.py @@ -1,5 +1,6 @@ # Copyright (c) 2023, 2024 Oracle and/or its affiliates. -# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/ +# Licensed under the Universal Permissive License v 1.0 as shown at +# https://oss.oracle.com/licenses/upl/ import os from borneo import (Regions, NoSQLHandle, NoSQLHandleConfig, PutRequest,QueryRequest, TableRequest, GetRequest, TableLimits, State) @@ -13,18 +14,20 @@ def get_connection_cloud(): # replace the placeholder with your region identifier region = '' provider = SignatureProvider() - # If using the DEFAULT profile with the config file in default location =~/.oci/config + # If using the DEFAULT profile with the config file in default location + # ~/.oci/config config = NoSQLHandleConfig(region, provider) # replace the placeholder with the ocid of your compartment config.set_default_compartment("") return(NoSQLHandle(config)) -# Given a endpoint, instantiate a connection to the onPremise Oracle NoSQL database +# Given a endpoint, instantiate a connection to onPremise Oracle NoSQL database def get_connection_onprem(): # replace the placeholder with the name of your host kvstore_endpoint ='http://:8080' provider = StoreAccessTokenProvider() - # If using a secure store, uncomment the line below and pass the username, password of the store to StoreAccessTokenProvider + # If using a secure store, uncomment the line below and pass the username, + # password of the store to StoreAccessTokenProvider # provider = StoreAccessTokenProvider(username, password) return NoSQLHandle(NoSQLHandleConfig(kvstore_endpoint, provider)) @@ -57,6 +60,7 @@ def drop_table(handle): print('Dropped table: stream_acct') def main(): + handle = None # if cloud service uncomment this. else if onPremise comment this line handle = get_connection_cloud() # if onPremise uncomment this. else if cloud service comment this line @@ -64,6 +68,8 @@ def main(): create_table(handle) alter_table(handle) drop_table(handle) + if handle is not None: + handle.close() os._exit(0) if __name__ == "__main__": diff --git a/examples-nosql-python-sdk/sqlexamples/CreateTable.py b/examples-nosql-python-sdk/sqlexamples/CreateTable.py index aa8ea60..77f73f8 100644 --- a/examples-nosql-python-sdk/sqlexamples/CreateTable.py +++ b/examples-nosql-python-sdk/sqlexamples/CreateTable.py @@ -1,5 +1,6 @@ # Copyright (c) 2023, 2024 Oracle and/or its affiliates. -# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/ +# Licensed under the Universal Permissive License v 1.0 as shown at +# https://oss.oracle.com/licenses/upl/ import os from borneo import (Regions, NoSQLHandle, NoSQLHandleConfig, PutRequest,QueryRequest, TableRequest, GetRequest, TableLimits, State) @@ -13,18 +14,20 @@ def get_connection_cloud(): # replace the placeholder with your region identifier region = '' provider = SignatureProvider() - # If using the DEFAULT profile with the config file in default location =~/.oci/config + # If using the DEFAULT profile with the config file in default location + # ~/.oci/config config = NoSQLHandleConfig(region, provider) #replace the placeholder with the ocid of your compartment config.set_default_compartment("") return(NoSQLHandle(config)) -# Given a endpoint, instantiate a connection to the onPremise Oracle NoSQL database +# Given a endpoint, instantiate a connection to onPremise Oracle NoSQL database def get_connection_onprem(): # replace the placeholder with the name of your host kvstore_endpoint ='http://:8080' provider = StoreAccessTokenProvider() - # If using a secure store, uncomment the line below and pass the username, password of the store to StoreAccessTokenProvider + # If using a secure store, uncomment the line below and pass the username, + # password of the store to StoreAccessTokenProvider # provider = StoreAccessTokenProvider(username, password) return NoSQLHandle(NoSQLHandleConfig(kvstore_endpoint, provider)) @@ -44,11 +47,14 @@ def create_table(handle): raise NameError('Table stream_acct is in an unexpected state ' + str(table_result.get_state())) def main(): + handle = None # if cloud service uncomment this. else if onPremise comment this line handle = get_connection_cloud() # if onPremise uncomment this. else if cloud service comment this line # handle = get_connection_onprem() create_table(handle) + if handle is not None: + handle.close() os._exit(0) if __name__ == "__main__": diff --git a/examples-nosql-python-sdk/sqlexamples/GroupSortData.py b/examples-nosql-python-sdk/sqlexamples/GroupSortData.py new file mode 100644 index 0000000..e5f990b --- /dev/null +++ b/examples-nosql-python-sdk/sqlexamples/GroupSortData.py @@ -0,0 +1,417 @@ +# Copyright (c) 2023, 2024 Oracle and/or its affiliates. +# Licensed under the Universal Permissive License v 1.0 as shown at +# https://oss.oracle.com/licenses/upl/ +import os +from borneo import (Regions, NoSQLHandle, NoSQLHandleConfig, PutRequest,QueryRequest, + TableRequest, GetRequest, TableLimits, State) +from borneo.iam import SignatureProvider +from borneo.kv import StoreAccessTokenProvider + +# Given a region, and compartment, instantiate a connection to the +# cloud service and return it +def get_connection_cloud(): + print("Connecting to the Oracle NoSQL Cloud Service") + # replace the placeholder with your region identifier + region = '' + provider = SignatureProvider() + # If using the DEFAULT profile with the config file in default location + # ~/.oci/config + config = NoSQLHandleConfig(region, provider) + # replace the placeholder with the ocid of your compartment + config.set_default_compartment("") + return(NoSQLHandle(config)) + +# Given a endpoint, instantiate a connection to onPremise Oracle NoSQL database +def get_connection_onprem(): + # replace the placeholder with the name of your host + kvstore_endpoint ='http://:8080' + provider = StoreAccessTokenProvider() + # If using a secure store, uncomment the line below and pass the username, + # password of the store to StoreAccessTokenProvider + # provider = StoreAccessTokenProvider(username, password) + return NoSQLHandle(NoSQLHandleConfig(kvstore_endpoint, provider)) + +# Create a table and set the table limits +def create_table(handle): + statement = '''create table if not exists BaggageInfo (ticketNo LONG, + fullName STRING, + gender STRING, + contactPhone STRING, + confNo STRING, + bagInfo JSON, + primary key(ticketNo))''' + request = TableRequest().set_statement(statement).set_table_limits(TableLimits(20, 10, 1)) + # Create the table, waiting for a total of 40000 milliseconds + # and polling the service every 3000 milliseconds to see if table is active + table_result = handle.do_table_request(request, 40000, 3000) + table_result.wait_for_completion(handle, 40000, 3000) + if (table_result.get_state() == State.ACTIVE): + print('Created table: BaggageInfo') + else: + raise NameError('Table BaggageInfo is in an unexpected state ' + str(table_result.get_state())) + +# Add a row of data to the table +def insert_record(handle,table_name,bag_data): + request = PutRequest().set_table_name(table_name).set_value_from_json(bag_data) + handle.put(request) + print('Loaded a row into table: BaggageInfo') + +# Fetch data from the table +def fetch_data(handle,sqlstmt): + request = QueryRequest().set_statement(sqlstmt) + print('Query results for: ' + sqlstmt) + result = handle.query(request) + for r in result.get_results(): + print('\t' + str(r)) + +def main(): + handle = None + bag1='''{ + "ticketNo":"1762376407826", + "fullName":"Dierdre Amador", + "gender":"M", + "contactPhone":"165-742-5715", + "confNo":"ZG8Z5N", + "bagInfo":[ { + "id" : "7903989918469", + "tagNum" : "17657806240229", + "routing" : "JFK/MAD", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "MAD", + "flightLegs" : [ { + "flightNo" : "BM495", + "flightDate" : "2019-03-07T07:00:00Z", + "fltRouteSrc" : "JFK", + "fltRouteDest" : "MAD", + "estimatedArrival" : "2019-03-07T14:00:00Z", + "actions" : [ { + "actionAt" : "MAD", + "actionCode" : "Offload to Carousel at MAD", + "actionTime" : "2019-03-07T13:54:00Z" + }, { + "actionAt" : "JFK", + "actionCode" : "ONLOAD to MAD", + "actionTime" : "2019-03-07T07:00:00Z" + }, { + "actionAt" : "JFK", + "actionCode" : "BagTag Scan at JFK", + "actionTime" : "2019-03-07T06:53:00Z" + }, { + "actionAt" : "JFK", + "actionCode" : "Checkin at JFK", + "actionTime" : "2019-03-07T05:03:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-03-07T13:51:00Z", + "bagArrivalDate" : "2019-03-07T13:51:00Z" + } ] + }''' + bag2=''' + { + "ticketNo":"1762344493810", + "fullName":"Adam Phillips", + "gender":"M", + "contactPhone":"893-324-1064", + "confNo":"LE6J4Z", + "bagInfo":[ { + "id" : "79039899165297", + "tagNum" : "17657806255240", + "routing" : "MIA/LAX/MEL", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "MEL", + "flightLegs" : [ { + "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" + } ] + }, { + "flightNo" : "BM667", + "flightDate" : "2019-02-01T06:13:00Z", + "fltRouteSrc" : "LAX", + "fltRouteDest" : "MEL", + "estimatedArrival" : "2019-02-01T16:15:00Z", + "actions" : [ { + "actionAt" : "MEL", + "actionCode" : "Offload to Carousel at MEL", + "actionTime" : "2019-02-01T16:15:00Z" + }, { + "actionAt" : "LAX", + "actionCode" : "ONLOAD to MEL", + "actionTime" : "2019-02-01T15:35:00Z" + }, { + "actionAt" : "LAX", + "actionCode" : "OFFLOAD from LAX", + "actionTime" : "2019-02-01T15:18:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-02-01T16:13:00Z", + "bagArrivalDate" : "2019-02-01T16:13:00Z" + } ] + }''' + bag3=''' + { + "ticketNo":"1762341772625", + "fullName":"Gerard Greene", + "gender":"M", + "contactPhone":"395-837-3772", + "confNo":"MC0E7R", + "bagInfo":[ { + "id" : "79039899152842", + "tagNum" : "1765780626568", + "routing" : "SFO/IST/ATH/JTR", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "JTR", + "flightLegs" : [ { + "flightNo" : "BM318", + "flightDate" : "2019-03-07T04:00:00Z", + "fltRouteSrc" : "SFO", + "fltRouteDest" : "IST", + "estimatedArrival" : "2019-03-07T17:00:00Z", + "actions" : [ { + "actionAt" : "SFO", + "actionCode" : "ONLOAD to IST", + "actionTime" : "2019-03-07T04:08:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "BagTag Scan at SFO", + "actionTime" : "2019-03-07T03:53:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "Checkin at SFO", + "actionTime" : "2019-03-07T02:20:00Z" + } ] + }, { + "flightNo" : "BM696", + "flightDate" : "2019-03-07T05:08:00Z", + "fltRouteSrc" : "IST", + "fltRouteDest" : "ATH", + "estimatedArrival" : "2019-03-08T04:10:00Z", + "actions" : [ { + "actionAt" : "IST", + "actionCode" : "ONLOAD to ATH", + "actionTime" : "2019-03-08T04:55:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "BagTag Scan at IST", + "actionTime" : "2019-03-08T04:34:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "OFFLOAD from IST", + "actionTime" : "2019-03-08T04:47:00Z" + } ] + }, { + "flightNo" : "BM665", + "flightDate" : "2019-03-07T04:08:00Z", + "fltRouteSrc" : "ATH", + "fltRouteDest" : "JTR", + "estimatedArrival" : "2019-03-07T16:10:00Z", + "actions" : [ { + "actionAt" : "JTR", + "actionCode" : "Offload to Carousel at JTR", + "actionTime" : "2019-03-07T16:09:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "ONLOAD to JTR", + "actionTime" : "2019-03-07T15:51:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "OFFLOAD from ATH", + "actionTime" : "2019-03-07T15:43:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-03-07T16:01:00Z", + "bagArrivalDate" : "2019-03-07T16:01:00Z" + } ] + }''' + bag4=''' + { + "ticketNo":"1762320369957", + "fullName":"Lorenzo Phil", + "gender":"M", + "contactPhone":"364-610-4444", + "confNo":"QI3V6Q", + "bagInfo":[ { + "id" : "79039899187755", + "tagNum" : "17657806240001", + "routing" : "SFO/IST/ATH/JTR", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "JTR", + "flightLegs" : [ { + "flightNo" : "BM318", + "flightDate" : "2019-03-12T03:00:00Z", + "fltRouteSrc" : "SFO", + "fltRouteDest" : "IST", + "estimatedArrival" : "2019-03-12T16:00:00Z", + "actions" : [ { + "actionAt" : "SFO", + "actionCode" : "ONLOAD to IST", + "actionTime" : "2019-03-12T03:11:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "BagTag Scan at SFO", + "actionTime" : "2019-03-12T02:49:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "Checkin at SFO", + "actionTime" : "2019-03-12T01:50:00Z" + } ] + }, { + "flightNo" : "BM696", + "flightDate" : "2019-03-12T04:11:00Z", + "fltRouteSrc" : "IST", + "fltRouteDest" : "ATH", + "estimatedArrival" : "2019-03-13T03:14:00Z", + "actions" : [ { + "actionAt" : "IST", + "actionCode" : "ONLOAD to ATH", + "actionTime" : "2019-03-13T04:10:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "BagTag Scan at IST", + "actionTime" : "2019-03-13T03:56:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "OFFLOAD from IST", + "actionTime" : "2019-03-13T03:59:00Z" + } ] + }, { + "flightNo" : "BM665", + "flightDate" : "2019-03-12T03:11:00Z", + "fltRouteSrc" : "ATH", + "fltRouteDest" : "JTR", + "estimatedArrival" : "2019-03-12T15:12:00Z", + "actions" : [ { + "actionAt" : "JTR", + "actionCode" : "Offload to Carousel at JTR", + "actionTime" : "2019-03-12T15:06:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "ONLOAD to JTR", + "actionTime" : "2019-03-12T14:16:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "OFFLOAD from ATH", + "actionTime" : "2019-03-12T14:13:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-03-12T15:05:00Z", + "bagArrivalDate" : "2019-03-12T15:05:00Z" + }, + { + "id" : "79039899197755", + "tagNum" : "17657806340001", + "routing" : "SFO/IST/ATH/JTR", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "JTR", + "flightLegs" : [ { + "flightNo" : "BM318", + "flightDate" : "2019-03-12T03:00:00Z", + "fltRouteSrc" : "SFO", + "fltRouteDest" : "IST", + "estimatedArrival" : "2019-03-12T16:40:00Z", + "actions" : [ { + "actionAt" : "SFO", + "actionCode" : "ONLOAD to IST", + "actionTime" : "2019-03-12T03:14:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "BagTag Scan at SFO", + "actionTime" : "2019-03-12T02:50:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "Checkin at SFO", + "actionTime" : "2019-03-12T01:58:00Z" + } ] + }, { + "flightNo" : "BM696", + "flightDate" : "2019-03-12T04:11:00Z", + "fltRouteSrc" : "IST", + "fltRouteDest" : "ATH", + "estimatedArrival" : "2019-03-13T03:18:00Z", + "actions" : [ { + "actionAt" : "IST", + "actionCode" : "ONLOAD to ATH", + "actionTime" : "2019-03-13T04:17:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "BagTag Scan at IST", + "actionTime" : "2019-03-13T03:59:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "OFFLOAD from IST", + "actionTime" : "2019-03-13T03:48:00Z" + } ] + }, { + "flightNo" : "BM665", + "flightDate" : "2019-03-12T03:11:00Z", + "fltRouteSrc" : "ATH", + "fltRouteDest" : "JTR", + "estimatedArrival" : "2019-03-12T15:12:00Z", + "actions" : [ { + "actionAt" : "JTR", + "actionCode" : "Offload to Carousel at JTR", + "actionTime" : "2019-03-12T15:06:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "ONLOAD to JTR", + "actionTime" : "2019-03-12T14:16:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "OFFLOAD from ATH", + "actionTime" : "2019-03-12T14:23:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-03-12T16:05:00Z", + "bagArrivalDate" : "2019-03-12T16:25:00Z" + } ] + }''' + # if cloud service uncomment this + handle = get_connection_cloud() + # if onPremise uncomment this + #handle = get_connection_onprem() + create_table(handle) + insert_record(handle,'BaggageInfo',bag1) + insert_record(handle,'BaggageInfo',bag2) + insert_record(handle,'BaggageInfo',bag3) + insert_record(handle,'BaggageInfo',bag4) + orderby_stmt = '''SELECT bag.fullName, bag.bagInfo[].tagNum,bag.bagInfo[].lastSeenTimeGmt FROM BaggageInfo bag + WHERE bag.bagInfo[].lastSeenStation=any \"MEL\" ORDER BY bag.bagInfo[].lastSeenTimeGmt DESC''' + print('Using ORDER BY to sort data:') + fetch_data(handle,orderby_stmt) + + sortlimit_stmt = '''SELECT $bag.fullName, $bag.bagInfo.tagNum, $flt_time FROM BaggageInfo $bag, + $bag.bagInfo[].lastSeenTimeGmt $flt_time + WHERE $bag.bagInfo[].lastSeenStation=any "JTR" + ORDER BY $flt_time LIMIT 4''' + print('Using ORDER BY and LIMIT to sort and limit data:') + fetch_data(handle,sortlimit_stmt) + + groupsortlimit_stmt = '''SELECT $flt_src as SOURCE, count(*) as COUNT FROM BaggageInfo $bag, + $bag.bagInfo.flightLegs[0].fltRouteSrc $flt_src GROUP BY $flt_src''' + print('Using GROUP BY, ORDER BY and LIMIT to group, sort and limit data:') + fetch_data(handle,groupsortlimit_stmt) + if handle is not None: + handle.close() + os._exit(0) + +if __name__ == "__main__": + main() diff --git a/examples-nosql-python-sdk/sqlexamples/Indexes.py b/examples-nosql-python-sdk/sqlexamples/Indexes.py index dd9e640..948e1a1 100644 --- a/examples-nosql-python-sdk/sqlexamples/Indexes.py +++ b/examples-nosql-python-sdk/sqlexamples/Indexes.py @@ -1,5 +1,6 @@ # Copyright (c) 2023, 2024 Oracle and/or its affiliates. -# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/ +# Licensed under the Universal Permissive License v 1.0 as shown at +# https://oss.oracle.com/licenses/upl/ import os from borneo import (Regions, NoSQLHandle, NoSQLHandleConfig, PutRequest,QueryRequest, TableRequest, GetRequest, TableLimits, State) @@ -12,18 +13,20 @@ def get_connection_cloud(): # replace the placeholder with your region identifier region = '' provider = SignatureProvider() - # If using the DEFAULT profile with the config file in default location =~/.oci/config + # If using the DEFAULT profile with the config file in default location + # ~/.oci/config config = NoSQLHandleConfig(region, provider) # replace the placeholder with the ocid of your compartment config.set_default_compartment("") return(NoSQLHandle(config)) -# Given a endpoint, instantiate a connection to the onPremise Oracle NoSQL database +# Given a endpoint, instantiate a connection to onPremise Oracle NoSQL database def get_connection_onprem(): # replace the placeholder with the name of your host kvstore_endpoint ='http://:8080' provider = StoreAccessTokenProvider() - # If using a secure store, uncomment the line below and pass the username, password of the store to StoreAccessTokenProvider + # If using a secure store, uncomment the line below and pass the username, + # password of the store to StoreAccessTokenProvider # provider = StoreAccessTokenProvider(username, password) return NoSQLHandle(NoSQLHandleConfig(kvstore_endpoint, provider)) @@ -61,6 +64,7 @@ def drop_index(handle): print('Index acct_episodes on the table stream_acct is dropped') def main(): + handle = None # if cloud service uncomment this. else if onPremise comment this line handle = get_connection_cloud() # if onPremise uncomment this. else if cloud service comment this line @@ -68,6 +72,8 @@ def main(): create_table(handle) create_index(handle) drop_index(handle) + if handle is not None: + handle.close() os._exit(0) if __name__ == "__main__": diff --git a/examples-nosql-python-sdk/sqlexamples/ModifyData.py b/examples-nosql-python-sdk/sqlexamples/ModifyData.py index aa0dcf2..e0e809a 100644 --- a/examples-nosql-python-sdk/sqlexamples/ModifyData.py +++ b/examples-nosql-python-sdk/sqlexamples/ModifyData.py @@ -1,5 +1,6 @@ # Copyright (c) 2023, 2024 Oracle and/or its affiliates. -# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/ +# Licensed under the Universal Permissive License v 1.0 as shown at +# https://oss.oracle.com/licenses/upl/ import os from borneo import (Regions, NoSQLHandle, NoSQLHandleConfig, PutRequest,QueryRequest, TableRequest, DeleteRequest, GetRequest, TableLimits, State) @@ -12,18 +13,20 @@ def get_connection_cloud(): #replace the placeholder with your region identifier region = '' provider = SignatureProvider() - #If using the DEFAULT profile with the config file in default location =~/.oci/config + #If using the DEFAULT profile with the config file in default location + # ~/.oci/config config = NoSQLHandleConfig(region, provider) #replace the placeholder with the ocid of your compartment config.set_default_compartment("") return(NoSQLHandle(config)) -# Given a endpoint, instantiate a connection to the onPremise Oracle NoSQL database +# Given a endpoint, instantiate a connection to onPremise Oracle NoSQL database def get_connection_onprem(): # replace the placeholder with the name of your host kvstore_endpoint ='http://:8080' provider = StoreAccessTokenProvider() - # If using a secure store, uncomment the line below and pass the username, password of the store to StoreAccessTokenProvider + # If using a secure store, uncomment the line below and pass the username, + # password of the store to StoreAccessTokenProvider # provider = StoreAccessTokenProvider(username, password) return NoSQLHandle(NoSQLHandleConfig(kvstore_endpoint, provider)) @@ -36,7 +39,7 @@ def create_table(handle): primary key(acct_Id))''' request = TableRequest().set_statement(statement).set_table_limits(TableLimits(20, 10, 1)) # Create the table, waiting for a total of 40000 milliseconds - # and polling the service every 3000 milliseconds to see if the table is active + # and polling the service every 3000 milliseconds to see if table is active table_result = handle.do_table_request(request, 40000, 3000) table_result.wait_for_completion(handle, 40000, 3000) if (table_result.get_state() == State.ACTIVE): @@ -77,6 +80,7 @@ def delete_rows(handle,sqlstmt): print('Deleted data from table: stream_acct') def main(): + handle = None acct1=''' { "acct_Id":1, @@ -475,6 +479,8 @@ def main(): # delete data based on a filter condition del_stmt ='''DELETE FROM stream_acct acct1 WHERE acct1.acct_data.firstName="Adelaide" AND acct1.acct_data.lastName="Willard"''' delete_rows(handle,del_stmt) + if handle is not None: + handle.close() os._exit(0) if __name__ == "__main__": diff --git a/examples-nosql-python-sdk/sqlexamples/MultiDataOps.py b/examples-nosql-python-sdk/sqlexamples/MultiDataOps.py new file mode 100644 index 0000000..94a6365 --- /dev/null +++ b/examples-nosql-python-sdk/sqlexamples/MultiDataOps.py @@ -0,0 +1,92 @@ +# Copyright (c) 2023, 2024 Oracle and/or its affiliates. +# Licensed under the Universal Permissive License v 1.0 as shown at +# https://oss.oracle.com/licenses/upl/ +import os +from borneo import (Regions, NoSQLHandle, NoSQLHandleConfig, PutRequest,QueryRequest,MultiDeleteRequest, + TableRequest, GetRequest, TableLimits, State) +from borneo.iam import SignatureProvider +from borneo.kv import StoreAccessTokenProvider + +# Given a region, and compartment, instantiate a connection to the +# cloud service and return it +def get_connection_cloud(): + print("Connecting to the Oracle NoSQL Cloud Service") + # replace the placeholder with your region identifier + region = '' + provider = SignatureProvider() + # If using the DEFAULT profile with the config file in default location + # ~/.oci/config + config = NoSQLHandleConfig(region, provider) + # replace the placeholder with the ocid of your compartment + config.set_default_compartment("") + return(NoSQLHandle(config)) + +# Given a endpoint, instantiate a connection to onPremise Oracle NoSQL database +def get_connection_onprem(): + # replace the placeholder with the name of your host + kvstore_endpoint ='http://:8080' + provider = StoreAccessTokenProvider() + # If using a secure store, uncomment the line below and pass the username, + # password of the store to StoreAccessTokenProvider + # provider = StoreAccessTokenProvider(username, password) + return NoSQLHandle(NoSQLHandleConfig(kvstore_endpoint, provider)) + + +# Create a table and set the table limits +def create_table(handle): + statement = '''create table if not exists examplesAddress (id INTEGER, + address_line1 STRING, + address_line2 STRING, + pin INTEGER, + PRIMARY KEY(SHARD(pin), id))''' + request = TableRequest().set_statement(statement).set_table_limits(TableLimits(20, 10, 1)) + # Create the table, waiting for a total of 40000 milliseconds + # and polling the service every 3000 milliseconds to see if table is active + table_result = handle.do_table_request(request, 40000, 3000) + table_result.wait_for_completion(handle, 40000, 3000) + if (table_result.get_state() == State.ACTIVE): + print('Created table: examplesAddress') + else: + raise NameError('Table examplesAddress is in an unexpected state ' + str(table_result.get_state())) + +# Add a row of data to the table +def insert_record(handle,table_name,idval,addr1,addr2,pinval): + value = {'id': idval, 'address_line1': addr1, 'address_line2': addr2, 'pin': pinval} + request = PutRequest().set_table_name(table_name).set_value(value) + handle.put(request) + print('Loaded a row into table: examplesAddress') + +# Fetch data from the table +def fetch_rowCnt(handle,sqlstmt): + request = QueryRequest().set_statement(sqlstmt) + print('Row count for: ' + sqlstmt) + result = handle.query(request) + for r in result.get_results(): + print('\t' + str(r)) + +#delete multiple rows +def multirow_delete(handle,table_name,pinval): + request = MultiDeleteRequest().set_table_name(table_name).set_key({'pin': pinval}) + result = handle.multi_delete(request) + +def main(): + handle = None + # if cloud service uncomment this + handle = get_connection_cloud() + # if onPremise uncomment this + #handle = get_connection_onprem() + create_table(handle) + insert_record(handle,'examplesAddress',1,'10 Red Street','Apt 3',1234567) + insert_record(handle,'examplesAddress',2,'2 Green Street','Street 9',1234567) + insert_record(handle,'examplesAddress',3,'5 Blue Ave','Floor 9',1234567) + insert_record(handle,'examplesAddress',4,'9 Yellow Boulevard','Apt 3',87654321) + sqlstmt = 'select * from examplesAddress' + fetch_rowCnt(handle,sqlstmt) + multirow_delete(handle,'examplesAddress',1234567) + fetch_rowCnt(handle,sqlstmt) + if handle is not None: + handle.close() + os._exit(0) + +if __name__ == "__main__": + main() diff --git a/examples-nosql-python-sdk/sqlexamples/MultiWrite.py b/examples-nosql-python-sdk/sqlexamples/MultiWrite.py new file mode 100644 index 0000000..c181b50 --- /dev/null +++ b/examples-nosql-python-sdk/sqlexamples/MultiWrite.py @@ -0,0 +1,142 @@ +# Copyright (c) 2023, 2024 Oracle and/or its affiliates. +# Licensed under the Universal Permissive License v 1.0 as shown at +# https://oss.oracle.com/licenses/upl/ +import os +from borneo import (Regions, NoSQLHandle, NoSQLHandleConfig, PutRequest,QueryRequest, + TableRequest, GetRequest, TableLimits, State, WriteMultipleRequest) +from borneo.iam import SignatureProvider +from borneo.kv import StoreAccessTokenProvider + +# Given a region, and compartment, instantiate a connection to the +# cloud service and return it +def get_connection_cloud(): + print("Connecting to the Oracle NoSQL Cloud Service") + # replace the placeholder with your region identifier + region = '' + provider = SignatureProvider() + # If using the DEFAULT profile with the config file in default location + # ~/.oci/config + config = NoSQLHandleConfig(region, provider) + # replace the placeholder with the ocid of your compartment + config.set_default_compartment("") + return(NoSQLHandle(config)) + +# Given a endpoint, instantiate a connection to onPremise Oracle NoSQL database +def get_connection_onprem(): + # replace the placeholder with the name of your host + kvstore_endpoint ='http://:8080' + provider = StoreAccessTokenProvider() + # If using a secure store, uncomment the line below and pass the username, + # password of the store to StoreAccessTokenProvider + # provider = StoreAccessTokenProvider(username, password) + return NoSQLHandle(NoSQLHandleConfig(kvstore_endpoint, provider)) + +# Create a table and set the table limits +def create_table(handle,statement,table_flag,table_name): + if (table_flag == 'true'): + request = TableRequest().set_statement(statement).set_table_limits(TableLimits(20, 10, 1)) + else: + request = TableRequest().set_statement(statement) + + # Create the table, waiting for a total of 40000 milliseconds + # and polling the service every 3000 milliseconds to see if table is active + table_result = handle.do_table_request(request, 40000, 3000) + table_result.wait_for_completion(handle, 40000, 3000) + if (table_result.get_state() == State.ACTIVE): + print('Created table: ',table_name) + else: + raise NameError('Table is in an unexpected state ' + str(table_result.get_state())) + +def insert_record(handle,table_name,tbl_data): + request = PutRequest().set_table_name(table_name).set_value_from_json(tbl_data) + handle.put(request) + print('Loaded a row into table:',table_name) + +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) + + +def main(): + handle = None + # if cloud service uncomment this + handle = get_connection_cloud() + # if onPremise uncomment this + #handle = get_connection_onprem() + tbl_name='ticket' + regtbl_crtstmt = '''create table if not exists ticket (ticketNo LONG, + confNo STRING, + PRIMARY KEY(ticketNo))''' + childtbl_name='ticket.bagInfo' + childtbl_crtstmt = '''create table if not exists ticket.bagInfo (id LONG, + tagNum LONG, + routing STRING, + lastActionCode STRING, + lastActionDesc STRING, + lastSeenStation STRING, + lastSeenTimeGmt TIMESTAMP(4), + bagArrivalDate TIMESTAMP(4), + PRIMARY KEY(id))''' + desctbl_crtstmt = '''create table if not exists ticket.bagInfo.flightLegs (flightNo STRING, + flightDate TIMESTAMP(4), + fltRouteSrc STRING, + fltRouteDest STRING, + estimatedArrival TIMESTAMP(4), + actions JSON, + PRIMARY KEY(flightNo))''' + desctbl_name='flightLegs' + create_table(handle,regtbl_crtstmt,'true',tbl_name) + create_table(handle,childtbl_crtstmt,'false',childtbl_name) + create_table(handle,desctbl_crtstmt,'false',desctbl_name) + data1='''{ + "ticketNo": "1762344493810", + "confNo" : "LE6J4Z" + }''' + 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" + }''' + 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" + } ] + }''' + mul_write(handle,'ticket',data1,'ticket.bagInfo',data2) + insert_record(handle,'ticket.bagInfo.flightLegs',data3) + if handle is not None: + handle.close() + os._exit(0) + +if __name__ == "__main__": + main() diff --git a/examples-nosql-python-sdk/sqlexamples/Namespaces.py b/examples-nosql-python-sdk/sqlexamples/Namespaces.py index 66cfc41..17e0749 100644 --- a/examples-nosql-python-sdk/sqlexamples/Namespaces.py +++ b/examples-nosql-python-sdk/sqlexamples/Namespaces.py @@ -1,17 +1,19 @@ # Copyright (c) 2023, 2024 Oracle and/or its affiliates. -# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/ +# Licensed under the Universal Permissive License v 1.0 as shown at +# https://oss.oracle.com/licenses/upl/ import os from borneo import (Regions, NoSQLHandle, NoSQLHandleConfig, PutRequest,QueryRequest, TableRequest, SystemRequest, GetRequest, TableLimits, State) from borneo.iam import SignatureProvider from borneo.kv import StoreAccessTokenProvider -# Given a endpoint, instantiate a connection to the onPremise Oracle NoSQL database +# Given a endpoint, instantiate a connection to onPremise Oracle NoSQL database def get_connection_onprem(): # replace the placeholder with the name of your host kvstore_endpoint ='http://:8080' provider = StoreAccessTokenProvider() - # If using a secure store, uncomment the line below and pass the username, password of the store to StoreAccessTokenProvider + # If using a secure store, uncomment the line below and pass the username, + # password of the store to StoreAccessTokenProvider # provider = StoreAccessTokenProvider(username, password) return NoSQLHandle(NoSQLHandleConfig(kvstore_endpoint, provider)) @@ -32,9 +34,12 @@ def drop_ns(handle): print('Namespace: ns1 is dropped') def main(): + handle = None handle = get_connection_onprem() create_ns(handle) drop_ns(handle) + if handle is not None: + handle.close() os._exit(0) if __name__ == "__main__": diff --git a/examples-nosql-python-sdk/sqlexamples/QueryData.py b/examples-nosql-python-sdk/sqlexamples/QueryData.py index 6857367..50150bc 100644 --- a/examples-nosql-python-sdk/sqlexamples/QueryData.py +++ b/examples-nosql-python-sdk/sqlexamples/QueryData.py @@ -1,5 +1,6 @@ # Copyright (c) 2023, 2024 Oracle and/or its affiliates. -# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/ +# Licensed under the Universal Permissive License v 1.0 as shown at +# https://oss.oracle.com/licenses/upl/ import os from borneo import (Regions, NoSQLHandle, NoSQLHandleConfig, PutRequest,QueryRequest, TableRequest, GetRequest, TableLimits, State) @@ -13,18 +14,20 @@ def get_connection_cloud(): # replace the placeholder with your region identifier region = '' provider = SignatureProvider() - # If using the DEFAULT profile with the config file in default location =~/.oci/config + # If using the DEFAULT profile with the config file in default location + # ~/.oci/config config = NoSQLHandleConfig(region, provider) # replace the placeholder with the ocid of your compartment config.set_default_compartment("") return(NoSQLHandle(config)) -# Given a endpoint, instantiate a connection to the onPremise Oracle NoSQL database +# Given a endpoint, instantiate a connection to onPremise Oracle NoSQL database def get_connection_onprem(): # replace the placeholder with the name of your host kvstore_endpoint ='http://:8080' provider = StoreAccessTokenProvider() - # If using a secure store, uncomment the line below and pass the username, password of the store to StoreAccessTokenProvider + # If using a secure store, uncomment the line below and pass the username, + # password of the store to StoreAccessTokenProvider # provider = StoreAccessTokenProvider(username, password) return NoSQLHandle(NoSQLHandleConfig(kvstore_endpoint, provider)) @@ -37,7 +40,7 @@ def create_table(handle): primary key(acct_Id))''' request = TableRequest().set_statement(statement).set_table_limits(TableLimits(20, 10, 1)) # Create the table, waiting for a total of 40000 milliseconds - # and polling the service every 3000 milliseconds to see if the table is active + # and polling the service every 3000 milliseconds to see if table is active table_result = handle.do_table_request(request, 40000, 3000) table_result.wait_for_completion(handle, 40000, 3000) if (table_result.get_state() == State.ACTIVE): @@ -59,8 +62,16 @@ def fetch_data(handle,sqlstmt): for r in result.get_results(): print('\t' + str(r)) +# Fetch single row using get API +def getRow(handle,colName,Id): + request = GetRequest().set_table_name('stream_acct') + request.set_key({colName: Id}) + print('Query results: ') + result = handle.get(request) + print('Query results are' + str(result.get_value())) def main(): + handle = None acct1=''' { "acct_Id":1, @@ -342,12 +353,16 @@ def main(): insert_record(handle,'stream_acct',acct1) insert_record(handle,'stream_acct',acct2) insert_record(handle,'stream_acct',acct3) + print('Fetching a single row based on the primary key') + getRow(handle,'acct_Id',2) sqlstmt = 'select * from stream_acct' print('Fetching all data from the table:') fetch_data(handle,sqlstmt) sqlstmt = 'select account_expiry, acct.acct_data.lastName, acct.acct_data.contentStreamed[].showName from stream_acct acct where acct_id=1' print('Fetching partial data filtered from the table:') fetch_data(handle,sqlstmt) + if handle is not None: + handle.close() os._exit(0) if __name__ == "__main__": diff --git a/examples-nosql-python-sdk/sqlexamples/Regions.py b/examples-nosql-python-sdk/sqlexamples/Regions.py index 4d247f7..df84b18 100644 --- a/examples-nosql-python-sdk/sqlexamples/Regions.py +++ b/examples-nosql-python-sdk/sqlexamples/Regions.py @@ -1,17 +1,19 @@ # Copyright (c) 2023, 2024 Oracle and/or its affiliates. -# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/ +# Licensed under the Universal Permissive License v 1.0 as shown at +# https://oss.oracle.com/licenses/upl/ import os from borneo import (Regions, NoSQLHandle, NoSQLHandleConfig, PutRequest,QueryRequest, TableRequest, SystemRequest, GetRequest, TableLimits, State) from borneo.iam import SignatureProvider from borneo.kv import StoreAccessTokenProvider -# Given a endpoint, instantiate a connection to the onPremise Oracle NoSQL database +# Given a endpoint, instantiate a connection to onPremise Oracle NoSQL database def get_connection_onprem(): # replace the placeholder with the name of your host kvstore_endpoint ='http://:8080' provider = StoreAccessTokenProvider() - # If using a secure store, uncomment the line below and pass the username, password of the store to StoreAccessTokenProvider + # If using a secure store, uncomment the line below and pass the username, + # password of the store to StoreAccessTokenProvider # provider = StoreAccessTokenProvider(username, password) return NoSQLHandle(NoSQLHandleConfig(kvstore_endpoint, provider)) @@ -62,11 +64,14 @@ def drop_region(handle): print('Region LON is dropped') def main(): + handle = None handle = get_connection_onprem() create_region(handle) create_tab_region(handle) drop_tab_region(handle) drop_region(handle) + if handle is not None: + handle.close() os._exit(0) if __name__ == "__main__": diff --git a/examples-nosql-python-sdk/sqlexamples/SQLExpressions.py b/examples-nosql-python-sdk/sqlexamples/SQLExpressions.py new file mode 100644 index 0000000..e9ce685 --- /dev/null +++ b/examples-nosql-python-sdk/sqlexamples/SQLExpressions.py @@ -0,0 +1,437 @@ +# Copyright (c) 2023, 2024 Oracle and/or its affiliates. +# Licensed under the Universal Permissive License v 1.0 as shown at +# https://oss.oracle.com/licenses/upl/ +import os +from borneo import (Regions, NoSQLHandle, NoSQLHandleConfig, PutRequest,QueryRequest, + TableRequest, GetRequest, TableLimits, State) +from borneo.iam import SignatureProvider +from borneo.kv import StoreAccessTokenProvider + +# Given a region, and compartment, instantiate a connection to the +# cloud service and return it +def get_connection_cloud(): + print("Connecting to the Oracle NoSQL Cloud Service") + # replace the placeholder with your region identifier + region = '' + provider = SignatureProvider() + # If using the DEFAULT profile with the config file in default location + # ~/.oci/config + config = NoSQLHandleConfig(region, provider) + # replace the placeholder with the ocid of your compartment + config.set_default_compartment("") + return(NoSQLHandle(config)) + +# Given a endpoint, instantiate a connection to onPremise Oracle NoSQL database +def get_connection_onprem(): + # replace the placeholder with the name of your host + kvstore_endpoint ='http://:8080' + provider = StoreAccessTokenProvider() + # If using a secure store, uncomment the line below and pass the username, + # password of the store to StoreAccessTokenProvider + # provider = StoreAccessTokenProvider(username, password) + return NoSQLHandle(NoSQLHandleConfig(kvstore_endpoint, provider)) + +# Create a table and set the table limits +def create_table(handle): + statement = '''create table if not exists BaggageInfo (ticketNo LONG, + fullName STRING, + gender STRING, + contactPhone STRING, + confNo STRING, + bagInfo JSON, + primary key(ticketNo))''' + request = TableRequest().set_statement(statement).set_table_limits(TableLimits(20, 10, 1)) + # Create the table, waiting for a total of 40000 milliseconds + # and polling the service every 3000 milliseconds to see if table is active + table_result = handle.do_table_request(request, 40000, 3000) + table_result.wait_for_completion(handle, 40000, 3000) + if (table_result.get_state() == State.ACTIVE): + print('Created table: BaggageInfo') + else: + raise NameError('Table BaggageInfo is in an unexpected state ' + str(table_result.get_state())) + +# Add a row of data to the table +def insert_record(handle,table_name,bag_data): + request = PutRequest().set_table_name(table_name).set_value_from_json(bag_data) + handle.put(request) + print('Loaded a row into table: BaggageInfo') + +# Fetch data from the table +def fetch_data(handle,sqlstmt): + request = QueryRequest().set_statement(sqlstmt) + print('Query results for: ' + sqlstmt) + result = handle.query(request) + for r in result.get_results(): + print('\t' + str(r)) + +def main(): + handle = None + bag1='''{ + "ticketNo":"1762376407826", + "fullName":"Dierdre Amador", + "gender":"M", + "contactPhone":"165-742-5715", + "confNo":"ZG8Z5N", + "bagInfo":[ { + "id" : "7903989918469", + "tagNum" : "17657806240229", + "routing" : "JFK/MAD", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "MAD", + "flightLegs" : [ { + "flightNo" : "BM495", + "flightDate" : "2019-03-07T07:00:00Z", + "fltRouteSrc" : "JFK", + "fltRouteDest" : "MAD", + "estimatedArrival" : "2019-03-07T14:00:00Z", + "actions" : [ { + "actionAt" : "MAD", + "actionCode" : "Offload to Carousel at MAD", + "actionTime" : "2019-03-07T13:54:00Z" + }, { + "actionAt" : "JFK", + "actionCode" : "ONLOAD to MAD", + "actionTime" : "2019-03-07T07:00:00Z" + }, { + "actionAt" : "JFK", + "actionCode" : "BagTag Scan at JFK", + "actionTime" : "2019-03-07T06:53:00Z" + }, { + "actionAt" : "JFK", + "actionCode" : "Checkin at JFK", + "actionTime" : "2019-03-07T05:03:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-03-07T13:51:00Z", + "bagArrivalDate" : "2019-03-07T13:51:00Z" + } ] + }''' + bag2=''' + { + "ticketNo":"1762344493810", + "fullName":"Adam Phillips", + "gender":"M", + "contactPhone":"893-324-1064", + "confNo":"LE6J4Z", + "bagInfo":[ { + "id" : "79039899165297", + "tagNum" : "17657806255240", + "routing" : "MIA/LAX/MEL", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "MEL", + "flightLegs" : [ { + "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" + } ] + }, { + "flightNo" : "BM667", + "flightDate" : "2019-02-01T06:13:00Z", + "fltRouteSrc" : "LAX", + "fltRouteDest" : "MEL", + "estimatedArrival" : "2019-02-01T16:15:00Z", + "actions" : [ { + "actionAt" : "MEL", + "actionCode" : "Offload to Carousel at MEL", + "actionTime" : "2019-02-01T16:15:00Z" + }, { + "actionAt" : "LAX", + "actionCode" : "ONLOAD to MEL", + "actionTime" : "2019-02-01T15:35:00Z" + }, { + "actionAt" : "LAX", + "actionCode" : "OFFLOAD from LAX", + "actionTime" : "2019-02-01T15:18:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-02-01T16:13:00Z", + "bagArrivalDate" : "2019-02-01T16:13:00Z" + } ] + }''' + bag3=''' + { + "ticketNo":"1762341772625", + "fullName":"Gerard Greene", + "gender":"M", + "contactPhone":"395-837-3772", + "confNo":"MC0E7R", + "bagInfo":[ { + "id" : "79039899152842", + "tagNum" : "1765780626568", + "routing" : "SFO/IST/ATH/JTR", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "JTR", + "flightLegs" : [ { + "flightNo" : "BM318", + "flightDate" : "2019-03-07T04:00:00Z", + "fltRouteSrc" : "SFO", + "fltRouteDest" : "IST", + "estimatedArrival" : "2019-03-07T17:00:00Z", + "actions" : [ { + "actionAt" : "SFO", + "actionCode" : "ONLOAD to IST", + "actionTime" : "2019-03-07T04:08:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "BagTag Scan at SFO", + "actionTime" : "2019-03-07T03:53:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "Checkin at SFO", + "actionTime" : "2019-03-07T02:20:00Z" + } ] + }, { + "flightNo" : "BM696", + "flightDate" : "2019-03-07T05:08:00Z", + "fltRouteSrc" : "IST", + "fltRouteDest" : "ATH", + "estimatedArrival" : "2019-03-08T04:10:00Z", + "actions" : [ { + "actionAt" : "IST", + "actionCode" : "ONLOAD to ATH", + "actionTime" : "2019-03-08T04:55:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "BagTag Scan at IST", + "actionTime" : "2019-03-08T04:34:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "OFFLOAD from IST", + "actionTime" : "2019-03-08T04:47:00Z" + } ] + }, { + "flightNo" : "BM665", + "flightDate" : "2019-03-07T04:08:00Z", + "fltRouteSrc" : "ATH", + "fltRouteDest" : "JTR", + "estimatedArrival" : "2019-03-07T16:10:00Z", + "actions" : [ { + "actionAt" : "JTR", + "actionCode" : "Offload to Carousel at JTR", + "actionTime" : "2019-03-07T16:09:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "ONLOAD to JTR", + "actionTime" : "2019-03-07T15:51:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "OFFLOAD from ATH", + "actionTime" : "2019-03-07T15:43:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-03-07T16:01:00Z", + "bagArrivalDate" : "2019-03-07T16:01:00Z" + } ] + }''' + bag4=''' + { + "ticketNo":"1762320369957", + "fullName":"Lorenzo Phil", + "gender":"M", + "contactPhone":"364-610-4444", + "confNo":"QI3V6Q", + "bagInfo":[ { + "id" : "79039899187755", + "tagNum" : "17657806240001", + "routing" : "SFO/IST/ATH/JTR", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "JTR", + "flightLegs" : [ { + "flightNo" : "BM318", + "flightDate" : "2019-03-12T03:00:00Z", + "fltRouteSrc" : "SFO", + "fltRouteDest" : "IST", + "estimatedArrival" : "2019-03-12T16:00:00Z", + "actions" : [ { + "actionAt" : "SFO", + "actionCode" : "ONLOAD to IST", + "actionTime" : "2019-03-12T03:11:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "BagTag Scan at SFO", + "actionTime" : "2019-03-12T02:49:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "Checkin at SFO", + "actionTime" : "2019-03-12T01:50:00Z" + } ] + }, { + "flightNo" : "BM696", + "flightDate" : "2019-03-12T04:11:00Z", + "fltRouteSrc" : "IST", + "fltRouteDest" : "ATH", + "estimatedArrival" : "2019-03-13T03:14:00Z", + "actions" : [ { + "actionAt" : "IST", + "actionCode" : "ONLOAD to ATH", + "actionTime" : "2019-03-13T04:10:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "BagTag Scan at IST", + "actionTime" : "2019-03-13T03:56:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "OFFLOAD from IST", + "actionTime" : "2019-03-13T03:59:00Z" + } ] + }, { + "flightNo" : "BM665", + "flightDate" : "2019-03-12T03:11:00Z", + "fltRouteSrc" : "ATH", + "fltRouteDest" : "JTR", + "estimatedArrival" : "2019-03-12T15:12:00Z", + "actions" : [ { + "actionAt" : "JTR", + "actionCode" : "Offload to Carousel at JTR", + "actionTime" : "2019-03-12T15:06:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "ONLOAD to JTR", + "actionTime" : "2019-03-12T14:16:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "OFFLOAD from ATH", + "actionTime" : "2019-03-12T14:13:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-03-12T15:05:00Z", + "bagArrivalDate" : "2019-03-12T15:05:00Z" + }, + { + "id" : "79039899197755", + "tagNum" : "17657806340001", + "routing" : "SFO/IST/ATH/JTR", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "JTR", + "flightLegs" : [ { + "flightNo" : "BM318", + "flightDate" : "2019-03-12T03:00:00Z", + "fltRouteSrc" : "SFO", + "fltRouteDest" : "IST", + "estimatedArrival" : "2019-03-12T16:40:00Z", + "actions" : [ { + "actionAt" : "SFO", + "actionCode" : "ONLOAD to IST", + "actionTime" : "2019-03-12T03:14:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "BagTag Scan at SFO", + "actionTime" : "2019-03-12T02:50:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "Checkin at SFO", + "actionTime" : "2019-03-12T01:58:00Z" + } ] + }, { + "flightNo" : "BM696", + "flightDate" : "2019-03-12T04:11:00Z", + "fltRouteSrc" : "IST", + "fltRouteDest" : "ATH", + "estimatedArrival" : "2019-03-13T03:18:00Z", + "actions" : [ { + "actionAt" : "IST", + "actionCode" : "ONLOAD to ATH", + "actionTime" : "2019-03-13T04:17:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "BagTag Scan at IST", + "actionTime" : "2019-03-13T03:59:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "OFFLOAD from IST", + "actionTime" : "2019-03-13T03:48:00Z" + } ] + }, { + "flightNo" : "BM665", + "flightDate" : "2019-03-12T03:11:00Z", + "fltRouteSrc" : "ATH", + "fltRouteDest" : "JTR", + "estimatedArrival" : "2019-03-12T15:12:00Z", + "actions" : [ { + "actionAt" : "JTR", + "actionCode" : "Offload to Carousel at JTR", + "actionTime" : "2019-03-12T15:06:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "ONLOAD to JTR", + "actionTime" : "2019-03-12T14:16:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "OFFLOAD from ATH", + "actionTime" : "2019-03-12T14:23:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-03-12T16:05:00Z", + "bagArrivalDate" : "2019-03-12T16:25:00Z" + } ] + }''' + # if cloud service uncomment this + handle = get_connection_cloud() + # if onPremise uncomment this + #handle = get_connection_onprem() + create_table(handle) + insert_record(handle,'BaggageInfo',bag1) + insert_record(handle,'BaggageInfo',bag2) + insert_record(handle,'BaggageInfo',bag3) + insert_record(handle,'BaggageInfo',bag4) + paran_expr = '''SELECT fullName, bag.bagInfo.tagNum, bag.bagInfo.routing, + bag.bagInfo[].flightLegs[].fltRouteDest FROM BaggageInfo bag + WHERE bag.bagInfo.flightLegs[].fltRouteSrc=any "SFO" AND + (bag.bagInfo[].flightLegs[].fltRouteDest=any "ATH" OR + bag.bagInfo[].flightLegs[].fltRouteDest=any "JTR" )''' + print('Using Paranthesized expression:') + fetch_data(handle,paran_expr) + case_expr = '''SELECT fullName, + CASE + WHEN NOT exists bag.bagInfo.flightLegs[0] + THEN "you have no bag info" + WHEN NOT exists bag.bagInfo.flightLegs[1] + THEN "you have one hop" + WHEN NOT exists bag.bagInfo.flightLegs[2] + THEN "you have two hops." + ELSE "you have three hops." + END AS NUMBER_HOPS + FROM BaggageInfo bag WHERE ticketNo=1762341772625''' + print('Using Case Expression:') + fetch_data(handle,case_expr) + seq_trn_expr = '''SELECT seq_transform(l.bagInfo[], + seq_transform( + $sq1.flightLegs[], + seq_transform( + $sq2.actions[], + { + "at" : $sq3.actionAt, + "action" : $sq3.actionCode, + "flightNo" : $sq2.flightNo, + "tagNum" : $sq1.tagNum + } + ) + ) + ) AS actions FROM baggageInfo l WHERE ticketNo=1762376407826''' + print('Using Sequence Transform Expressions:') + fetch_data(handle,seq_trn_expr) + if handle is not None: + handle.close() + os._exit(0) + +if __name__ == "__main__": + main() diff --git a/examples-nosql-python-sdk/sqlexamples/SQLFunctions.py b/examples-nosql-python-sdk/sqlexamples/SQLFunctions.py new file mode 100644 index 0000000..a037a1a --- /dev/null +++ b/examples-nosql-python-sdk/sqlexamples/SQLFunctions.py @@ -0,0 +1,424 @@ +# Copyright (c) 2023, 2024 Oracle and/or its affiliates. +# Licensed under the Universal Permissive License v 1.0 as shown at +# https://oss.oracle.com/licenses/upl/ +import os +from borneo import (Regions, NoSQLHandle, NoSQLHandleConfig, PutRequest,QueryRequest, + TableRequest, GetRequest, TableLimits, State) +from borneo.iam import SignatureProvider +from borneo.kv import StoreAccessTokenProvider + +# Given a region, and compartment, instantiate a connection to the +# cloud service and return it +def get_connection_cloud(): + print("Connecting to the Oracle NoSQL Cloud Service") + # replace the placeholder with your region identifier + region = '' + provider = SignatureProvider() + # If using the DEFAULT profile with the config file in default location + # ~/.oci/config + config = NoSQLHandleConfig(region, provider) + # replace the placeholder with the ocid of your compartment + config.set_default_compartment("") + return(NoSQLHandle(config)) + +# Given a endpoint, instantiate a connection to onPremise Oracle NoSQL database +def get_connection_onprem(): + # replace the placeholder with the name of your host + kvstore_endpoint ='http://:8080' + provider = StoreAccessTokenProvider() + # If using a secure store, uncomment the line below and pass the username, + # password of the store to StoreAccessTokenProvider + # provider = StoreAccessTokenProvider(username, password) + return NoSQLHandle(NoSQLHandleConfig(kvstore_endpoint, provider)) + +# Create a table and set the table limits +def create_table(handle): + statement = '''create table if not exists BaggageInfo (ticketNo LONG, + fullName STRING, + gender STRING, + contactPhone STRING, + confNo STRING, + bagInfo JSON, + primary key(ticketNo))''' + request = TableRequest().set_statement(statement).set_table_limits(TableLimits(20, 10, 1)) + # Create the table, waiting for a total of 40000 milliseconds + # and polling the service every 3000 milliseconds to see if table is active + table_result = handle.do_table_request(request, 40000, 3000) + table_result.wait_for_completion(handle, 40000, 3000) + if (table_result.get_state() == State.ACTIVE): + print('Created table: BaggageInfo') + else: + raise NameError('Table BaggageInfo is in an unexpected state ' + str(table_result.get_state())) + +# Add a row of data to the table +def insert_record(handle,table_name,bag_data): + request = PutRequest().set_table_name(table_name).set_value_from_json(bag_data) + handle.put(request) + print('Loaded a row into table: BaggageInfo') + +# Fetch data from the table +def fetch_data(handle,sqlstmt): + request = QueryRequest().set_statement(sqlstmt) + print('Query results for: ' + sqlstmt) + result = handle.query(request) + for r in result.get_results(): + print('\t' + str(r)) + +def main(): + handle = None + bag1='''{ + "ticketNo":"1762376407826", + "fullName":"Dierdre Amador", + "gender":"M", + "contactPhone":"165-742-5715", + "confNo":"ZG8Z5N", + "bagInfo":[ { + "id" : "7903989918469", + "tagNum" : "17657806240229", + "routing" : "JFK/MAD", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "MAD", + "flightLegs" : [ { + "flightNo" : "BM495", + "flightDate" : "2019-03-07T07:00:00Z", + "fltRouteSrc" : "JFK", + "fltRouteDest" : "MAD", + "estimatedArrival" : "2019-03-07T14:00:00Z", + "actions" : [ { + "actionAt" : "MAD", + "actionCode" : "Offload to Carousel at MAD", + "actionTime" : "2019-03-07T13:54:00Z" + }, { + "actionAt" : "JFK", + "actionCode" : "ONLOAD to MAD", + "actionTime" : "2019-03-07T07:00:00Z" + }, { + "actionAt" : "JFK", + "actionCode" : "BagTag Scan at JFK", + "actionTime" : "2019-03-07T06:53:00Z" + }, { + "actionAt" : "JFK", + "actionCode" : "Checkin at JFK", + "actionTime" : "2019-03-07T05:03:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-03-07T13:51:00Z", + "bagArrivalDate" : "2019-03-07T13:51:00Z" + } ] + }''' + bag2=''' + { + "ticketNo":"1762344493810", + "fullName":"Adam Phillips", + "gender":"M", + "contactPhone":"893-324-1064", + "confNo":"LE6J4Z", + "bagInfo":[ { + "id" : "79039899165297", + "tagNum" : "17657806255240", + "routing" : "MIA/LAX/MEL", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "MEL", + "flightLegs" : [ { + "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" + } ] + }, { + "flightNo" : "BM667", + "flightDate" : "2019-02-01T06:13:00Z", + "fltRouteSrc" : "LAX", + "fltRouteDest" : "MEL", + "estimatedArrival" : "2019-02-01T16:15:00Z", + "actions" : [ { + "actionAt" : "MEL", + "actionCode" : "Offload to Carousel at MEL", + "actionTime" : "2019-02-01T16:15:00Z" + }, { + "actionAt" : "LAX", + "actionCode" : "ONLOAD to MEL", + "actionTime" : "2019-02-01T15:35:00Z" + }, { + "actionAt" : "LAX", + "actionCode" : "OFFLOAD from LAX", + "actionTime" : "2019-02-01T15:18:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-02-01T16:13:00Z", + "bagArrivalDate" : "2019-02-01T16:13:00Z" + } ] + }''' + bag3=''' + { + "ticketNo":"1762341772625", + "fullName":"Gerard Greene", + "gender":"M", + "contactPhone":"395-837-3772", + "confNo":"MC0E7R", + "bagInfo":[ { + "id" : "79039899152842", + "tagNum" : "1765780626568", + "routing" : "SFO/IST/ATH/JTR", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "JTR", + "flightLegs" : [ { + "flightNo" : "BM318", + "flightDate" : "2019-03-07T04:00:00Z", + "fltRouteSrc" : "SFO", + "fltRouteDest" : "IST", + "estimatedArrival" : "2019-03-07T17:00:00Z", + "actions" : [ { + "actionAt" : "SFO", + "actionCode" : "ONLOAD to IST", + "actionTime" : "2019-03-07T04:08:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "BagTag Scan at SFO", + "actionTime" : "2019-03-07T03:53:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "Checkin at SFO", + "actionTime" : "2019-03-07T02:20:00Z" + } ] + }, { + "flightNo" : "BM696", + "flightDate" : "2019-03-07T05:08:00Z", + "fltRouteSrc" : "IST", + "fltRouteDest" : "ATH", + "estimatedArrival" : "2019-03-08T04:10:00Z", + "actions" : [ { + "actionAt" : "IST", + "actionCode" : "ONLOAD to ATH", + "actionTime" : "2019-03-08T04:55:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "BagTag Scan at IST", + "actionTime" : "2019-03-08T04:34:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "OFFLOAD from IST", + "actionTime" : "2019-03-08T04:47:00Z" + } ] + }, { + "flightNo" : "BM665", + "flightDate" : "2019-03-07T04:08:00Z", + "fltRouteSrc" : "ATH", + "fltRouteDest" : "JTR", + "estimatedArrival" : "2019-03-07T16:10:00Z", + "actions" : [ { + "actionAt" : "JTR", + "actionCode" : "Offload to Carousel at JTR", + "actionTime" : "2019-03-07T16:09:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "ONLOAD to JTR", + "actionTime" : "2019-03-07T15:51:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "OFFLOAD from ATH", + "actionTime" : "2019-03-07T15:43:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-03-07T16:01:00Z", + "bagArrivalDate" : "2019-03-07T16:01:00Z" + } ] + }''' + bag4=''' + { + "ticketNo":"1762320369957", + "fullName":"Lorenzo Phil", + "gender":"M", + "contactPhone":"364-610-4444", + "confNo":"QI3V6Q", + "bagInfo":[ { + "id" : "79039899187755", + "tagNum" : "17657806240001", + "routing" : "SFO/IST/ATH/JTR", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "JTR", + "flightLegs" : [ { + "flightNo" : "BM318", + "flightDate" : "2019-03-12T03:00:00Z", + "fltRouteSrc" : "SFO", + "fltRouteDest" : "IST", + "estimatedArrival" : "2019-03-12T16:00:00Z", + "actions" : [ { + "actionAt" : "SFO", + "actionCode" : "ONLOAD to IST", + "actionTime" : "2019-03-12T03:11:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "BagTag Scan at SFO", + "actionTime" : "2019-03-12T02:49:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "Checkin at SFO", + "actionTime" : "2019-03-12T01:50:00Z" + } ] + }, { + "flightNo" : "BM696", + "flightDate" : "2019-03-12T04:11:00Z", + "fltRouteSrc" : "IST", + "fltRouteDest" : "ATH", + "estimatedArrival" : "2019-03-13T03:14:00Z", + "actions" : [ { + "actionAt" : "IST", + "actionCode" : "ONLOAD to ATH", + "actionTime" : "2019-03-13T04:10:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "BagTag Scan at IST", + "actionTime" : "2019-03-13T03:56:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "OFFLOAD from IST", + "actionTime" : "2019-03-13T03:59:00Z" + } ] + }, { + "flightNo" : "BM665", + "flightDate" : "2019-03-12T03:11:00Z", + "fltRouteSrc" : "ATH", + "fltRouteDest" : "JTR", + "estimatedArrival" : "2019-03-12T15:12:00Z", + "actions" : [ { + "actionAt" : "JTR", + "actionCode" : "Offload to Carousel at JTR", + "actionTime" : "2019-03-12T15:06:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "ONLOAD to JTR", + "actionTime" : "2019-03-12T14:16:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "OFFLOAD from ATH", + "actionTime" : "2019-03-12T14:13:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-03-12T15:05:00Z", + "bagArrivalDate" : "2019-03-12T15:05:00Z" + }, + { + "id" : "79039899197755", + "tagNum" : "17657806340001", + "routing" : "SFO/IST/ATH/JTR", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "JTR", + "flightLegs" : [ { + "flightNo" : "BM318", + "flightDate" : "2019-03-12T03:00:00Z", + "fltRouteSrc" : "SFO", + "fltRouteDest" : "IST", + "estimatedArrival" : "2019-03-12T16:40:00Z", + "actions" : [ { + "actionAt" : "SFO", + "actionCode" : "ONLOAD to IST", + "actionTime" : "2019-03-12T03:14:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "BagTag Scan at SFO", + "actionTime" : "2019-03-12T02:50:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "Checkin at SFO", + "actionTime" : "2019-03-12T01:58:00Z" + } ] + }, { + "flightNo" : "BM696", + "flightDate" : "2019-03-12T04:11:00Z", + "fltRouteSrc" : "IST", + "fltRouteDest" : "ATH", + "estimatedArrival" : "2019-03-13T03:18:00Z", + "actions" : [ { + "actionAt" : "IST", + "actionCode" : "ONLOAD to ATH", + "actionTime" : "2019-03-13T04:17:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "BagTag Scan at IST", + "actionTime" : "2019-03-13T03:59:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "OFFLOAD from IST", + "actionTime" : "2019-03-13T03:48:00Z" + } ] + }, { + "flightNo" : "BM665", + "flightDate" : "2019-03-12T03:11:00Z", + "fltRouteSrc" : "ATH", + "fltRouteDest" : "JTR", + "estimatedArrival" : "2019-03-12T15:12:00Z", + "actions" : [ { + "actionAt" : "JTR", + "actionCode" : "Offload to Carousel at JTR", + "actionTime" : "2019-03-12T15:06:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "ONLOAD to JTR", + "actionTime" : "2019-03-12T14:16:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "OFFLOAD from ATH", + "actionTime" : "2019-03-12T14:23:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-03-12T16:05:00Z", + "bagArrivalDate" : "2019-03-12T16:25:00Z" + } ] + }''' + # if cloud service uncomment this + handle = get_connection_cloud() + # if onPremise uncomment this + #handle = get_connection_onprem() + create_table(handle) + insert_record(handle,'BaggageInfo',bag1) + insert_record(handle,'BaggageInfo',bag2) + insert_record(handle,'BaggageInfo',bag3) + insert_record(handle,'BaggageInfo',bag4) + ts_func1 = '''SELECT timestamp_add(bag.bagInfo.flightLegs[0].estimatedArrival, "5 minutes") + AS ARRIVAL_TIME FROM BaggageInfo bag WHERE ticketNo=1762341772625''' + print('Using timestamp_add function:') + fetch_data(handle,ts_func1) + ts_func2 = '''SELECT $s.ticketno, $bagInfo.bagArrivalDate, $flightLeg.flightDate, + get_duration(timestamp_diff($bagInfo.bagArrivalDate, $flightLeg.flightDate)) AS diff + FROM baggageinfo $s, + $s.bagInfo[] AS $bagInfo, $bagInfo.flightLegs[] AS $flightLeg + WHERE ticketNo=1762344493810''' + print('Using get_duration and timestamp_diff function:') + fetch_data(handle,ts_func2) + string_func1 = '''SELECT substring(bag.baginfo.routing,0,3) AS Source FROM baggageInfo bag + WHERE ticketNo=1762376407826''' + print('Using substring function:') + fetch_data(handle,string_func1) + string_func2 = '''SELECT fullname, length(fullname) AS fullname_length FROM BaggageInfo + WHERE ticketNo=1762320369957''' + print('Using length function:') + fetch_data(handle,string_func2) + string_func3 = '''SELECT fullname FROM baggageInfo bag WHERE + EXISTS bag.bagInfo[contains($element.routing,"SFO")]''' + print('Using contains function:') + fetch_data(handle,string_func3) + if handle is not None: + handle.close() + os._exit(0) + +if __name__ == "__main__": + main() diff --git a/examples-nosql-python-sdk/sqlexamples/SQLOperators.py b/examples-nosql-python-sdk/sqlexamples/SQLOperators.py new file mode 100644 index 0000000..e949d79 --- /dev/null +++ b/examples-nosql-python-sdk/sqlexamples/SQLOperators.py @@ -0,0 +1,424 @@ +# Copyright (c) 2023, 2024 Oracle and/or its affiliates. +# Licensed under the Universal Permissive License v 1.0 as shown at +# https://oss.oracle.com/licenses/upl/ +import os +from borneo import (Regions, NoSQLHandle, NoSQLHandleConfig, PutRequest,QueryRequest, + TableRequest, GetRequest, TableLimits, State) +from borneo.iam import SignatureProvider +from borneo.kv import StoreAccessTokenProvider + +# Given a region, and compartment, instantiate a connection to the +# cloud service and return it +def get_connection_cloud(): + print("Connecting to the Oracle NoSQL Cloud Service") + # replace the placeholder with your region identifier + region = '' + provider = SignatureProvider() + # If using the DEFAULT profile with the config file in default location + # ~/.oci/config + config = NoSQLHandleConfig(region, provider) + # replace the placeholder with the ocid of your compartment + config.set_default_compartment("") + return(NoSQLHandle(config)) + +# Given a endpoint, instantiate a connection to onPremise Oracle NoSQL database +def get_connection_onprem(): + # replace the placeholder with the name of your host + kvstore_endpoint ='http://:8080' + provider = StoreAccessTokenProvider() + # If using a secure store, uncomment the line below and pass the username, + # password of the store to StoreAccessTokenProvider + # provider = StoreAccessTokenProvider(username, password) + return NoSQLHandle(NoSQLHandleConfig(kvstore_endpoint, provider)) + +# Create a table and set the table limits +def create_table(handle): + statement = '''create table if not exists BaggageInfo (ticketNo LONG, + fullName STRING, + gender STRING, + contactPhone STRING, + confNo STRING, + bagInfo JSON, + primary key(ticketNo))''' + request = TableRequest().set_statement(statement).set_table_limits(TableLimits(20, 10, 1)) + # Create the table, waiting for a total of 40000 milliseconds + # and polling the service every 3000 milliseconds to see if table is active + table_result = handle.do_table_request(request, 40000, 3000) + table_result.wait_for_completion(handle, 40000, 3000) + if (table_result.get_state() == State.ACTIVE): + print('Created table: BaggageInfo') + else: + raise NameError('Table BaggageInfo is in an unexpected state ' + str(table_result.get_state())) + +# Add a row of data to the table +def insert_record(handle,table_name,bag_data): + request = PutRequest().set_table_name(table_name).set_value_from_json(bag_data) + handle.put(request) + print('Loaded a row into table: BaggageInfo') + +# Fetch data from the table +def fetch_data(handle,sqlstmt): + request = QueryRequest().set_statement(sqlstmt) + print('Query results for: ' + sqlstmt) + result = handle.query(request) + for r in result.get_results(): + print('\t' + str(r)) + +def main(): + handle = None + bag1='''{ + "ticketNo":"1762376407826", + "fullName":"Dierdre Amador", + "gender":"M", + "contactPhone":"165-742-5715", + "confNo":"ZG8Z5N", + "bagInfo":[ { + "id" : "7903989918469", + "tagNum" : "17657806240229", + "routing" : "JFK/MAD", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "MAD", + "flightLegs" : [ { + "flightNo" : "BM495", + "flightDate" : "2019-03-07T07:00:00Z", + "fltRouteSrc" : "JFK", + "fltRouteDest" : "MAD", + "estimatedArrival" : "2019-03-07T14:00:00Z", + "actions" : [ { + "actionAt" : "MAD", + "actionCode" : "Offload to Carousel at MAD", + "actionTime" : "2019-03-07T13:54:00Z" + }, { + "actionAt" : "JFK", + "actionCode" : "ONLOAD to MAD", + "actionTime" : "2019-03-07T07:00:00Z" + }, { + "actionAt" : "JFK", + "actionCode" : "BagTag Scan at JFK", + "actionTime" : "2019-03-07T06:53:00Z" + }, { + "actionAt" : "JFK", + "actionCode" : "Checkin at JFK", + "actionTime" : "2019-03-07T05:03:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-03-07T13:51:00Z", + "bagArrivalDate" : "2019-03-07T13:51:00Z" + } ] + }''' + bag2=''' + { + "ticketNo":"1762344493810", + "fullName":"Adam Phillips", + "gender":"M", + "contactPhone":"893-324-1064", + "confNo":"LE6J4Z", + "bagInfo":[ { + "id" : "79039899165297", + "tagNum" : "17657806255240", + "routing" : "MIA/LAX/MEL", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "MEL", + "flightLegs" : [ { + "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" + } ] + }, { + "flightNo" : "BM667", + "flightDate" : "2019-02-01T06:13:00Z", + "fltRouteSrc" : "LAX", + "fltRouteDest" : "MEL", + "estimatedArrival" : "2019-02-01T16:15:00Z", + "actions" : [ { + "actionAt" : "MEL", + "actionCode" : "Offload to Carousel at MEL", + "actionTime" : "2019-02-01T16:15:00Z" + }, { + "actionAt" : "LAX", + "actionCode" : "ONLOAD to MEL", + "actionTime" : "2019-02-01T15:35:00Z" + }, { + "actionAt" : "LAX", + "actionCode" : "OFFLOAD from LAX", + "actionTime" : "2019-02-01T15:18:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-02-01T16:13:00Z", + "bagArrivalDate" : "2019-02-01T16:13:00Z" + } ] + }''' + bag3=''' + { + "ticketNo":"1762341772625", + "fullName":"Gerard Greene", + "gender":"M", + "contactPhone":"395-837-3772", + "confNo":"MC0E7R", + "bagInfo":[ { + "id" : "79039899152842", + "tagNum" : "1765780626568", + "routing" : "SFO/IST/ATH/JTR", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "JTR", + "flightLegs" : [ { + "flightNo" : "BM318", + "flightDate" : "2019-03-07T04:00:00Z", + "fltRouteSrc" : "SFO", + "fltRouteDest" : "IST", + "estimatedArrival" : "2019-03-07T17:00:00Z", + "actions" : [ { + "actionAt" : "SFO", + "actionCode" : "ONLOAD to IST", + "actionTime" : "2019-03-07T04:08:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "BagTag Scan at SFO", + "actionTime" : "2019-03-07T03:53:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "Checkin at SFO", + "actionTime" : "2019-03-07T02:20:00Z" + } ] + }, { + "flightNo" : "BM696", + "flightDate" : "2019-03-07T05:08:00Z", + "fltRouteSrc" : "IST", + "fltRouteDest" : "ATH", + "estimatedArrival" : "2019-03-08T04:10:00Z", + "actions" : [ { + "actionAt" : "IST", + "actionCode" : "ONLOAD to ATH", + "actionTime" : "2019-03-08T04:55:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "BagTag Scan at IST", + "actionTime" : "2019-03-08T04:34:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "OFFLOAD from IST", + "actionTime" : "2019-03-08T04:47:00Z" + } ] + }, { + "flightNo" : "BM665", + "flightDate" : "2019-03-07T04:08:00Z", + "fltRouteSrc" : "ATH", + "fltRouteDest" : "JTR", + "estimatedArrival" : "2019-03-07T16:10:00Z", + "actions" : [ { + "actionAt" : "JTR", + "actionCode" : "Offload to Carousel at JTR", + "actionTime" : "2019-03-07T16:09:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "ONLOAD to JTR", + "actionTime" : "2019-03-07T15:51:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "OFFLOAD from ATH", + "actionTime" : "2019-03-07T15:43:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-03-07T16:01:00Z", + "bagArrivalDate" : "2019-03-07T16:01:00Z" + } ] + }''' + bag4=''' + { + "ticketNo":"1762320369957", + "fullName":"Lorenzo Phil", + "gender":"M", + "contactPhone":"364-610-4444", + "confNo":"QI3V6Q", + "bagInfo":[ { + "id" : "79039899187755", + "tagNum" : "17657806240001", + "routing" : "SFO/IST/ATH/JTR", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "JTR", + "flightLegs" : [ { + "flightNo" : "BM318", + "flightDate" : "2019-03-12T03:00:00Z", + "fltRouteSrc" : "SFO", + "fltRouteDest" : "IST", + "estimatedArrival" : "2019-03-12T16:00:00Z", + "actions" : [ { + "actionAt" : "SFO", + "actionCode" : "ONLOAD to IST", + "actionTime" : "2019-03-12T03:11:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "BagTag Scan at SFO", + "actionTime" : "2019-03-12T02:49:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "Checkin at SFO", + "actionTime" : "2019-03-12T01:50:00Z" + } ] + }, { + "flightNo" : "BM696", + "flightDate" : "2019-03-12T04:11:00Z", + "fltRouteSrc" : "IST", + "fltRouteDest" : "ATH", + "estimatedArrival" : "2019-03-13T03:14:00Z", + "actions" : [ { + "actionAt" : "IST", + "actionCode" : "ONLOAD to ATH", + "actionTime" : "2019-03-13T04:10:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "BagTag Scan at IST", + "actionTime" : "2019-03-13T03:56:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "OFFLOAD from IST", + "actionTime" : "2019-03-13T03:59:00Z" + } ] + }, { + "flightNo" : "BM665", + "flightDate" : "2019-03-12T03:11:00Z", + "fltRouteSrc" : "ATH", + "fltRouteDest" : "JTR", + "estimatedArrival" : "2019-03-12T15:12:00Z", + "actions" : [ { + "actionAt" : "JTR", + "actionCode" : "Offload to Carousel at JTR", + "actionTime" : "2019-03-12T15:06:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "ONLOAD to JTR", + "actionTime" : "2019-03-12T14:16:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "OFFLOAD from ATH", + "actionTime" : "2019-03-12T14:13:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-03-12T15:05:00Z", + "bagArrivalDate" : "2019-03-12T15:05:00Z" + }, + { + "id" : "79039899197755", + "tagNum" : "17657806340001", + "routing" : "SFO/IST/ATH/JTR", + "lastActionCode" : "OFFLOAD", + "lastActionDesc" : "OFFLOAD", + "lastSeenStation" : "JTR", + "flightLegs" : [ { + "flightNo" : "BM318", + "flightDate" : "2019-03-12T03:00:00Z", + "fltRouteSrc" : "SFO", + "fltRouteDest" : "IST", + "estimatedArrival" : "2019-03-12T16:40:00Z", + "actions" : [ { + "actionAt" : "SFO", + "actionCode" : "ONLOAD to IST", + "actionTime" : "2019-03-12T03:14:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "BagTag Scan at SFO", + "actionTime" : "2019-03-12T02:50:00Z" + }, { + "actionAt" : "SFO", + "actionCode" : "Checkin at SFO", + "actionTime" : "2019-03-12T01:58:00Z" + } ] + }, { + "flightNo" : "BM696", + "flightDate" : "2019-03-12T04:11:00Z", + "fltRouteSrc" : "IST", + "fltRouteDest" : "ATH", + "estimatedArrival" : "2019-03-13T03:18:00Z", + "actions" : [ { + "actionAt" : "IST", + "actionCode" : "ONLOAD to ATH", + "actionTime" : "2019-03-13T04:17:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "BagTag Scan at IST", + "actionTime" : "2019-03-13T03:59:00Z" + }, { + "actionAt" : "IST", + "actionCode" : "OFFLOAD from IST", + "actionTime" : "2019-03-13T03:48:00Z" + } ] + }, { + "flightNo" : "BM665", + "flightDate" : "2019-03-12T03:11:00Z", + "fltRouteSrc" : "ATH", + "fltRouteDest" : "JTR", + "estimatedArrival" : "2019-03-12T15:12:00Z", + "actions" : [ { + "actionAt" : "JTR", + "actionCode" : "Offload to Carousel at JTR", + "actionTime" : "2019-03-12T15:06:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "ONLOAD to JTR", + "actionTime" : "2019-03-12T14:16:00Z" + }, { + "actionAt" : "ATH", + "actionCode" : "OFFLOAD from ATH", + "actionTime" : "2019-03-12T14:23:00Z" + } ] + } ], + "lastSeenTimeGmt" : "2019-03-12T16:05:00Z", + "bagArrivalDate" : "2019-03-12T16:25:00Z" + } ] + }''' + # if cloud service uncomment this + handle = get_connection_cloud() + # if onPremise uncomment this + #handle = get_connection_onprem() + create_table(handle) + insert_record(handle,'BaggageInfo',bag1) + insert_record(handle,'BaggageInfo',bag2) + insert_record(handle,'BaggageInfo',bag3) + insert_record(handle,'BaggageInfo',bag4) + seqcomp_stmt = '''SELECT bag.bagInfo[].tagNum,bag.bagInfo[].flightLegs[].fltRouteSrc + FROM BaggageInfo bag WHERE bag.bagInfo[].flightLegs[].fltRouteSrc=any "SFO"''' + print('Using Sequence Comparison operator:') + fetch_data(handle,seqcomp_stmt) + logope_stmt = '''SELECT fullName, bag.bagInfo[].tagNum, bag.bagInfo[].routing, + bag.bagInfo[].flightLegs[].fltRouteSrc + FROM BaggageInfo bag + WHERE NOT bag.bagInfo[].flightLegs[].fltRouteSrc=any "SFO"''' + print('Using Logical operator:') + fetch_data(handle,logope_stmt) + valcomp_stmt = '''SELECT fullname, bag.bagInfo[].routing + FROM BaggageInfo bag WHERE gender="M"''' + print('Using Value Comparison operator:') + fetch_data(handle,valcomp_stmt) + inope_stmt = '''SELECT bagdet.fullName, bagdet.bagInfo[].tagNum + FROM BaggageInfo bagdet WHERE bagdet.fullName IN + ("Lucinda Beckman", "Adam Phillips","Dierdre Amador","Fallon Clements")''' + print('Using IN operator:') + fetch_data(handle,inope_stmt) + existsope_stmt = '''SELECT fullName, bag.ticketNo FROM BaggageInfo bag WHERE + EXISTS bag.bagInfo[$element.bagArrivalDate >="2019-03-01T00:00:00"]''' + print('Using EXISTS operator:') + fetch_data(handle,existsope_stmt) + if handle is not None: + handle.close() + os._exit(0) + +if __name__ == "__main__": + main() diff --git a/examples-nosql-python-sdk/sqlexamples/TableJoins.py b/examples-nosql-python-sdk/sqlexamples/TableJoins.py index b3930e6..6911a80 100644 --- a/examples-nosql-python-sdk/sqlexamples/TableJoins.py +++ b/examples-nosql-python-sdk/sqlexamples/TableJoins.py @@ -1,5 +1,6 @@ # Copyright (c) 2023, 2024 Oracle and/or its affiliates. -# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/ +# Licensed under the Universal Permissive License v 1.0 as shown at +# https://oss.oracle.com/licenses/upl/ import os from borneo import (Regions, NoSQLHandle, NoSQLHandleConfig, PutRequest,QueryRequest, TableRequest, GetRequest, TableLimits, State) @@ -13,18 +14,20 @@ def get_connection_cloud(): #replace the placeholder with your region identifier region = '' provider = SignatureProvider() - #If using the DEFAULT profile with the config file in default location =~/.oci/config + #If using the DEFAULT profile with the config file in default location + # ~/.oci/config config = NoSQLHandleConfig(region, provider) #replace the placeholder with the ocid of your compartment config.set_default_compartment("") return(NoSQLHandle(config)) -# Given a endpoint, instantiate a connection to the onPremise Oracle NoSQL database +# Given a endpoint, instantiate a connection to onPremise Oracle NoSQL database def get_connection_onprem(): #replace the placeholder with the name of your host kvstore_endpoint ='http://:8080' provider = StoreAccessTokenProvider() - #If using a secure store, uncomment the line below and pass the username, password of the store to StoreAccessTokenProvider + #If using a secure store, uncomment the line below and pass the username, + # password of the store to StoreAccessTokenProvider #provider = StoreAccessTokenProvider(username, password) return NoSQLHandle(NoSQLHandleConfig(kvstore_endpoint, provider)) @@ -36,7 +39,7 @@ def create_table(handle,statement,table_flag,table_name): request = TableRequest().set_statement(statement) # Create the table, waiting for a total of 40000 milliseconds - # and polling the service every 3000 milliseconds to see if the table is active + # and polling the service every 3000 milliseconds to see if table is active table_result = handle.do_table_request(request, 40000, 3000) table_result.wait_for_completion(handle, 40000, 3000) if (table_result.get_state() == State.ACTIVE): @@ -58,6 +61,7 @@ def fetch_data(handle,sqlstmt): print('\t' + str(r)) def main(): + handle = None # if cloud service uncomment this handle = get_connection_cloud() # if onPremise uncomment this @@ -133,6 +137,8 @@ def main(): sql_stmt_nt='SELECT * FROM NESTED TABLES (ticket a descendants(ticket.bagInfo.flightLegs b))' print('Fetching data using NESTED TABLES ') fetch_data(handle,sql_stmt_nt) + if handle is not None: + handle.close() os._exit(0) if __name__ == "__main__":