Driver Amazon QLDB untuk .NET - Tutorial mulai cepat - Amazon Quantum Ledger Database (Amazon QLDB)

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

Driver Amazon QLDB untuk .NET - Tutorial mulai cepat

Dalam tutorial ini, Anda belajar cara mengatur aplikasi sederhana menggunakan driver Amazon QLDB untuk .NET. Panduan ini mencakup langkah-langkah untuk menginstal driver dan contoh kode pendek dari operasi pembuatan, pembacaan, pembaruan, dan penghapusan (CRUD).

Prasyarat

Sebelum memulai, pastikan Anda melakukan hal berikut:

  1. Lengkapi driverPrasyarat untuk .NET, jika Anda belum memilhnya. Ini termasuk mendaftarAWS, memberikan akses terprogram untuk pengembangan, dan menginstal .NET Core SDK.

  2. Buat buku besar bernamaquick-start.

    Untuk mempelajari cara membuat buku besar, lihatOperasi dasar untuk buku besar Amazon QLDB atauLangkah 1: Membuat buku besar baru di Memulai konsol.

Langkah 1: Siapkan proyek Anda

Pertama, siapkan proyek .NET Anda.

  1. Untuk membuat dan menjalankan aplikasi template, masukkandotnet perintah berikut pada terminal seperti bash, PowerShell, atau Command Prompt.

    $ dotnet new console --output Amazon.QLDB.QuickStartGuide $ dotnet run --project Amazon.QLDB.QuickStartGuide

    Template ini membuat folder bernamaAmazon.QLDB.QuickStartGuide. Dalam folder itu, itu menciptakan sebuah proyek dengan nama yang sama dan file bernamaProgram.cs. Program ini berisi kode yang menampilkan outputHello World!.

  2. Gunakan manajer NuGet paket untuk menginstal driver QLDB untuk .NET. Sebaiknya gunakan Visual Studio atau IDE pilihan Anda untuk menambahkan dependensi ke proyek Anda. Nama paket driver adalah Amazon.qldb.driver.

    • Misalnya di Visual Studio, buka NuGet Package Manager Console pada menu Tools. Kemudian, masukkan perintah berikut diPM> prompt.

      PM> Install-Package Amazon.QLDB.Driver
    • Atau, Anda dapat memasukkan perintah berikut di terminal Anda.

      $ cd Amazon.QLDB.QuickStartGuide $ dotnet add package Amazon.QLDB.Driver

    Menginstal driver juga menginstal dependensinya, termasuk library AWS SDK for .NETdan Amazon Ion.

  3. Instal pustaka serialisasi driver.

    PM> Install-Package Amazon.QLDB.Driver.Serialization
  4. Buka file Program.cs.

    Kemudian, secara bertahap menambahkan contoh kode dalam langkah-langkah berikut untuk mencoba beberapa operasi CRUD dasar. Atau, Anda dapat melewati step-by-step tutorial dan sebagai gantinya menjalankan aplikasi lengkap.

catatan
  • Memilih antara API sinkron dan asinkron - Driver menyediakan API sinkron dan asinkron. Untuk aplikasi permintaan tinggi yang menangani beberapa permintaan tanpa memblokir, sebaiknya gunakan API asinkron untuk meningkatkan kinerja. Pengemudi menawarkan API sinkron sebagai kenyamanan tambahan untuk basis kode yang ada yang ditulis secara serempak.

    Tutorial ini mencakup contoh kode sinkron dan asinkron. Untuk informasi lebih lanjut tentang API, lihatAsyncQldbDriver antarmuka IQldbDriver dan I dalam dokumentasi API.

  • Memproses data Amazon Ion - Tutorial ini memberikan contoh kode untuk memproses data Amazon Ion menggunakan pemeta objek Ion secara default. QLDB memperkenalkan pemetaan objek Ion dalam versi 1.3.0 dari driver .NET. Jika berlaku, tutorial ini juga menyediakan contoh kode menggunakan pustaka Ion standar sebagai alternatif. Untuk mempelajari selengkapnya, lihat Bekerja dengan Amazon Ion.

Langkah 2: Inisialisasi driver

Inisialisasi instance driver yang terhubung ke buku besar bernamaquick-start. Tambahkan kode berikut keProgram.cs file Anda.

Async
using Amazon.QLDB.Driver; using Amazon.QLDB.Driver.Generic; using Amazon.QLDB.Driver.Serialization; namespace Amazon.QLDB.QuickStartGuide { class Program { public class Person { public string FirstName { get; set; } public string LastName { get; set; } public int Age { get; set; } public override string ToString() { return FirstName + ", " + LastName + ", " + Age.ToString(); } } static async Task Main(string[] args) { Console.WriteLine("Create the async QLDB driver"); IAsyncQldbDriver driver = AsyncQldbDriver.Builder() .WithLedger("quick-start") .WithSerializer(new ObjectSerializer()) .Build(); } } }
Sync
using Amazon.QLDB.Driver; using Amazon.QLDB.Driver.Generic; using Amazon.QLDB.Driver.Serialization; namespace Amazon.QLDB.QuickStartGuide { class Program { public class Person { public string FirstName { get; set; } public string LastName { get; set; } public int Age { get; set; } public override string ToString() { return FirstName + ", " + LastName + ", " + Age.ToString(); } } static void Main(string[] args) { Console.WriteLine("Create the sync QLDB driver"); IQldbDriver driver = QldbDriver.Builder() .WithLedger("quick-start") .WithSerializer(new ObjectSerializer()) .Build(); } } }
Async
using System; using System.Threading.Tasks; using Amazon.IonDotnet.Tree; using Amazon.IonDotnet.Tree.Impl; using Amazon.QLDB.Driver; using IAsyncResult = Amazon.QLDB.Driver.IAsyncResult; namespace Amazon.QLDB.QuickStartGuide { class Program { static IValueFactory valueFactory = new ValueFactory(); static async Task Main(string[] args) { Console.WriteLine("Create the async QLDB driver"); IAsyncQldbDriver driver = AsyncQldbDriver.Builder() .WithLedger("quick-start") .Build(); } } }
Sync
using System; using Amazon.IonDotnet.Tree; using Amazon.IonDotnet.Tree.Impl; using Amazon.QLDB.Driver; namespace Amazon.QLDB.QuickStartGuide { class Program { static IValueFactory valueFactory = new ValueFactory(); static void Main(string[] args) { Console.WriteLine("Create the sync QLDB Driver"); IQldbDriver driver = QldbDriver.Builder() .WithLedger("quick-start") .Build(); } } }

Langkah 3: Membuat tabel dan indeks

Untuk sisa tutorial ini melalui Langkah 6, Anda perlu menambahkan contoh kode berikut ke contoh kode sebelumnya.

Pada langkah ini, kode berikut menunjukkan bagaimana menjalankanCREATE TABLE danCREATE INDEX pernyataan. Ini menciptakan tabel bernamaPerson dan indeks untukfirstName bidang pada tabel itu. Indeks diperlukan untuk mengoptimalkan kinerja kueri dan membantu membatasi pengecualian konflik kontrol konkurensi (OCC) yang optimis.

Async
Console.WriteLine("Creating the table and index"); // Creates the table and the index in the same transaction. // Note: Any code within the lambda can potentially execute multiple times due to retries. // For more information, see: https://docs.aws.amazon.com/qldb/latest/developerguide/driver-retry-policy await driver.Execute(async txn => { await txn.Execute("CREATE TABLE Person"); await txn.Execute("CREATE INDEX ON Person(firstName)"); });
Sync
Console.WriteLine("Creating the tables and index"); // Creates the table and the index in the same transaction. // Note: Any code within the lambda can potentially execute multiple times due to retries. // For more information, see: https://docs.aws.amazon.com/qldb/latest/developerguide/driver-retry-policy driver.Execute(txn => { txn.Execute("CREATE TABLE Person"); txn.Execute("CREATE INDEX ON Person(firstName)"); });

Langkah 4: Masukkan dokumen

Contoh kode berikut ini menunjukkan cara menjalankanINSERT pernyataan. QLDB mendukung bahasa kueri PartiQL (kompatibel dengan SQL) dan format data Amazon Ion (superset JSON).

Tambahkan kode berikut yang menyisipkan dokumen ke dalamPerson tabel.

Async
Console.WriteLine("Inserting a document"); Person myPerson = new Person { FirstName = "John", LastName = "Doe", Age = 32 }; await driver.Execute(async txn => { IQuery<Person> myQuery = txn.Query<Person>("INSERT INTO Person ?", myPerson); await txn.Execute(myQuery); });
Sync
Console.WriteLine("Inserting a document"); Person myPerson = new Person { FirstName = "John", LastName = "Doe", Age = 32 }; driver.Execute(txn => { IQuery<Person> myQuery = txn.Query<Person>("INSERT INTO Person ?", myPerson); txn.Execute(myQuery); });
Async
Console.WriteLine("Inserting a document"); // This is one way of creating Ion values. We can also use an IonLoader. // For more details, see: https://docs.aws.amazon.com/qldb/latest/developerguide/driver-cookbook-dotnet.html#cookbook-dotnet.ion IIonValue ionPerson = valueFactory.NewEmptyStruct(); ionPerson.SetField("firstName", valueFactory.NewString("John")); ionPerson.SetField("lastName", valueFactory.NewString("Doe")); ionPerson.SetField("age", valueFactory.NewInt(32)); await driver.Execute(async txn => { await txn.Execute("INSERT INTO Person ?", ionPerson); });
Sync
Console.WriteLine("Inserting a document"); // This is one way of creating Ion values, we can also use an IonLoader. // For more details, see: https://docs.aws.amazon.com/qldb/latest/developerguide/driver-cookbook-dotnet.html#cookbook-dotnet.ion IIonValue ionPerson = valueFactory.NewEmptyStruct(); ionPerson.SetField("firstName", valueFactory.NewString("John")); ionPerson.SetField("lastName", valueFactory.NewString("Doe")); ionPerson.SetField("age", valueFactory.NewInt(32)); driver.Execute(txn => { txn.Execute("INSERT INTO Person ?", ionPerson); });
Tip

Untuk menyisipkan beberapa dokumen dengan menggunakanSISIPKAN pernyataan tunggal, Anda dapat melewati parameter jenis daftar Ion untuk pernyataan sebagai berikut.

// people is an Ion list txn.Execute("INSERT INTO Person ?", people);

Anda tidak menyertakan variabel placeholder (?) dalam kurung sudut ganda (<<...>>) saat meneruskan daftar Ion. Dalam pernyataan PartiQL manual, kurung sudut ganda menunjukkan koleksi unordered dikenal sebagai tas.

Langkah 5: Cari Dokumen

Contoh kode berikut ini menunjukkan cara menjalankanSELECT pernyataan.

Tambahkan kode berikut yang meminta dokumen dariPerson tabel.

Async
Console.WriteLine("Querying the table"); // The result from driver.Execute() is buffered into memory because once the // transaction is committed, streaming the result is no longer possible. IAsyncResult<Person> selectResult = await driver.Execute(async txn => { IQuery<Person> myQuery = txn.Query<Person>("SELECT * FROM Person WHERE FirstName = ?", "John"); return await txn.Execute(myQuery); }); await foreach (Person person in selectResult) { Console.WriteLine(person); // John, Doe, 32 }
Sync
Console.WriteLine("Querying the table"); // The result from driver.Execute() is buffered into memory because once the // transaction is committed, streaming the result is no longer possible. IResult<Person> selectResult = driver.Execute(txn => { IQuery<Person> myQuery = txn.Query<Person>("SELECT * FROM Person WHERE FirstName = ?", "John"); return txn.Execute(myQuery); }); foreach (Person person in selectResult) { Console.WriteLine(person); // John, Doe, 32 }
Async
Console.WriteLine("Querying the table"); IIonValue ionFirstName = valueFactory.NewString("John"); // The result from driver.Execute() is buffered into memory because once the // transaction is committed, streaming the result is no longer possible. IAsyncResult selectResult = await driver.Execute(async txn => { return await txn.Execute("SELECT * FROM Person WHERE firstName = ?", ionFirstName); }); await foreach (IIonValue row in selectResult) { Console.WriteLine(row.GetField("firstName").StringValue); Console.WriteLine(row.GetField("lastName").StringValue); Console.WriteLine(row.GetField("age").IntValue); }
Sync
Console.WriteLine("Querying the table"); IIonValue ionFirstName = valueFactory.NewString("John"); // The result from driver.Execute() is buffered into memory because once the // transaction is committed, streaming the result is no longer possible. IResult selectResult = driver.Execute(txn => { return txn.Execute("SELECT * FROM Person WHERE firstName = ?", ionFirstName); }); foreach (IIonValue row in selectResult) { Console.WriteLine(row.GetField("firstName").StringValue); Console.WriteLine(row.GetField("lastName").StringValue); Console.WriteLine(row.GetField("age").IntValue); }

Contoh ini menggunakan tanda tanya (?) sebagai placeholder variabel untuk meneruskan informasi dokumen untuk pernyataan. Bila Anda menggunakan placeholder, Anda harus lulus nilai jenisIonValue.

Langkah 6: Perbarui Dokumen

Contoh kode berikut ini menunjukkan cara menjalankanUPDATE pernyataan.

  1. Tambahkan kode berikut yang memperbarui dokumen dalamPerson tabel dengan memperbaruiage ke 42.

    Async
    Console.WriteLine("Updating the document"); await driver.Execute(async txn => { IQuery<Person> myQuery = txn.Query<Person>("UPDATE Person SET Age = ? WHERE FirstName = ?", 42, "John"); await txn.Execute(myQuery); });
    Sync
    Console.WriteLine("Updating the document"); driver.Execute(txn => { IQuery<Person> myQuery = txn.Query<Person>("UPDATE Person SET Age = ? WHERE FirstName = ?", 42, "John"); txn.Execute(myQuery); });
  2. Kueri dokumen lagi untuk melihat nilai yang diperbarui.

    Async
    Console.WriteLine("Querying the table for the updated document"); IAsyncResult<Person> updateResult = await driver.Execute(async txn => { IQuery<Person> myQuery = txn.Query<Person>("SELECT * FROM Person WHERE FirstName = ?", "John"); return await txn.Execute(myQuery); }); await foreach (Person person in updateResult) { Console.WriteLine(person); // John, Doe, 42 }
    Sync
    Console.WriteLine("Querying the table for the updated document"); IResult<Person> updateResult = driver.Execute(txn => { IQuery<Person> myQuery = txn.Query<Person>("SELECT * FROM Person WHERE FirstName = ?", "John"); return txn.Execute(myQuery); }); foreach (Person person in updateResult) { Console.WriteLine(person); // John, Doe, 42 }
  3. Untuk menjalankan aplikasi, masukkan perintah berikut dari direktori induk direktoriAmazon.QLDB.QuickStartGuide proyek Anda.

    $ dotnet run --project Amazon.QLDB.QuickStartGuide
  1. Tambahkan kode berikut yang memperbarui dokumen dalamPerson tabel dengan memperbaruiage ke 42.

    Async
    Console.WriteLine("Updating the document"); IIonValue ionIntAge = valueFactory.NewInt(42); IIonValue ionFirstName2 = valueFactory.NewString("John"); await driver.Execute(async txn => { await txn.Execute("UPDATE Person SET age = ? WHERE firstName = ?", ionIntAge, ionFirstName2); });
    Sync
    Console.WriteLine("Updating a document"); IIonValue ionIntAge = valueFactory.NewInt(42); IIonValue ionFirstName2 = valueFactory.NewString("John"); driver.Execute(txn => { txn.Execute("UPDATE Person SET age = ? WHERE firstName = ?", ionIntAge, ionFirstName2); });
  2. Kueri dokumen lagi untuk melihat nilai yang diperbarui.

    Async
    Console.WriteLine("Querying the table for the updated document"); IIonValue ionFirstName3 = valueFactory.NewString("John"); IAsyncResult updateResult = await driver.Execute(async txn => { return await txn.Execute("SELECT * FROM Person WHERE firstName = ?", ionFirstName3 ); }); await foreach (IIonValue row in updateResult) { Console.WriteLine(row.GetField("firstName").StringValue); Console.WriteLine(row.GetField("lastName").StringValue); Console.WriteLine(row.GetField("age").IntValue); }
    Sync
    Console.WriteLine("Querying the table for the updated document"); IIonValue ionFirstName3 = valueFactory.NewString("John"); IResult updateResult = driver.Execute(txn => { return txn.Execute("SELECT * FROM Person WHERE firstName = ?", ionFirstName3); }); foreach (IIonValue row in updateResult) { Console.WriteLine(row.GetField("firstName").StringValue); Console.WriteLine(row.GetField("lastName").StringValue); Console.WriteLine(row.GetField("age").IntValue); }
  3. Untuk menjalankan aplikasi, masukkan perintah berikut dari direktori induk direktoriAmazon.QLDB.QuickStartGuide proyek Anda.

    $ dotnet run --project Amazon.QLDB.QuickStartGuide

Menjalankan aplikasi lengkap

Contoh kode berikut adalah versi lengkap dariProgram.cs aplikasi. Alih-alih melakukan langkah-langkah sebelumnya secara individual, Anda juga dapat menyalin dan menjalankan contoh kode ini dari awal hingga akhir. Aplikasi ini menunjukkan beberapa operasi CRUD dasar pada buku besar bernamaquick-start.

catatan

Sebelum Anda menjalankan kode ini, pastikan bahwa Anda belum memiliki tabel aktif bernamaPerson dalamquick-start buku besar.

Async
using Amazon.QLDB.Driver; using Amazon.QLDB.Driver.Generic; using Amazon.QLDB.Driver.Serialization; namespace Amazon.QLDB.QuickStartGuide { class Program { public class Person { public string FirstName { get; set; } public string LastName { get; set; } public int Age { get; set; } public override string ToString() { return FirstName + ", " + LastName + ", " + Age.ToString(); } } static async Task Main(string[] args) { Console.WriteLine("Create the async QLDB driver"); IAsyncQldbDriver driver = AsyncQldbDriver.Builder() .WithLedger("quick-start") .WithSerializer(new ObjectSerializer()) .Build(); Console.WriteLine("Creating the table and index"); // Creates the table and the index in the same transaction. // Note: Any code within the lambda can potentially execute multiple times due to retries. // For more information, see: https://docs.aws.amazon.com/qldb/latest/developerguide/driver-retry-policy await driver.Execute(async txn => { await txn.Execute("CREATE TABLE Person"); await txn.Execute("CREATE INDEX ON Person(firstName)"); }); Console.WriteLine("Inserting a document"); Person myPerson = new Person { FirstName = "John", LastName = "Doe", Age = 32 }; await driver.Execute(async txn => { IQuery<Person> myQuery = txn.Query<Person>("INSERT INTO Person ?", myPerson); await txn.Execute(myQuery); }); Console.WriteLine("Querying the table"); // The result from driver.Execute() is buffered into memory because once the // transaction is committed, streaming the result is no longer possible. IAsyncResult<Person> selectResult = await driver.Execute(async txn => { IQuery<Person> myQuery = txn.Query<Person>("SELECT * FROM Person WHERE FirstName = ?", "John"); return await txn.Execute(myQuery); }); await foreach (Person person in selectResult) { Console.WriteLine(person); // John, Doe, 32 } Console.WriteLine("Updating the document"); await driver.Execute(async txn => { IQuery<Person> myQuery = txn.Query<Person>("UPDATE Person SET Age = ? WHERE FirstName = ?", 42, "John"); await txn.Execute(myQuery); }); Console.WriteLine("Querying the table for the updated document"); IAsyncResult<Person> updateResult = await driver.Execute(async txn => { IQuery<Person> myQuery = txn.Query<Person>("SELECT * FROM Person WHERE FirstName = ?", "John"); return await txn.Execute(myQuery); }); await foreach (Person person in updateResult) { Console.WriteLine(person); // John, Doe, 42 } } } }
Sync
using Amazon.QLDB.Driver; using Amazon.QLDB.Driver.Generic; using Amazon.QLDB.Driver.Serialization; namespace Amazon.QLDB.QuickStartGuide { class Program { public class Person { public string FirstName { get; set; } public string LastName { get; set; } public int Age { get; set; } public override string ToString() { return FirstName + ", " + LastName + ", " + Age.ToString(); } } static void Main(string[] args) { Console.WriteLine("Create the sync QLDB driver"); IQldbDriver driver = QldbDriver.Builder() .WithLedger("quick-start") .WithSerializer(new ObjectSerializer()) .Build(); Console.WriteLine("Creating the table and index"); // Creates the table and the index in the same transaction. // Note: Any code within the lambda can potentially execute multiple times due to retries. // For more information, see: https://docs.aws.amazon.com/qldb/latest/developerguide/driver-retry-policy driver.Execute(txn => { txn.Execute("CREATE TABLE Person"); txn.Execute("CREATE INDEX ON Person(firstName)"); }); Console.WriteLine("Inserting a document"); Person myPerson = new Person { FirstName = "John", LastName = "Doe", Age = 32 }; driver.Execute(txn => { IQuery<Person> myQuery = txn.Query<Person>("INSERT INTO Person ?", myPerson); txn.Execute(myQuery); }); Console.WriteLine("Querying the table"); // The result from driver.Execute() is buffered into memory because once the // transaction is committed, streaming the result is no longer possible. IResult<Person> selectResult = driver.Execute(txn => { IQuery<Person> myQuery = txn.Query<Person>("SELECT * FROM Person WHERE FirstName = ?", "John"); return txn.Execute(myQuery); }); foreach (Person person in selectResult) { Console.WriteLine(person); // John, Doe, 32 } Console.WriteLine("Updating the document"); driver.Execute(txn => { IQuery<Person> myQuery = txn.Query<Person>("UPDATE Person SET Age = ? WHERE FirstName = ?", 42, "John"); txn.Execute(myQuery); }); Console.WriteLine("Querying the table for the updated document"); IResult<Person> updateResult = driver.Execute(txn => { IQuery<Person> myQuery = txn.Query<Person>("SELECT * FROM Person WHERE FirstName = ?", "John"); return txn.Execute(myQuery); }); foreach (Person person in updateResult) { Console.WriteLine(person); // John, Doe, 42 } } } }
Async
using System; using System.Threading.Tasks; using Amazon.IonDotnet.Tree; using Amazon.IonDotnet.Tree.Impl; using Amazon.QLDB.Driver; using IAsyncResult = Amazon.QLDB.Driver.IAsyncResult; namespace Amazon.QLDB.QuickStartGuide { class Program { static IValueFactory valueFactory = new ValueFactory(); static async Task Main(string[] args) { Console.WriteLine("Create the async QLDB driver"); IAsyncQldbDriver driver = AsyncQldbDriver.Builder() .WithLedger("quick-start") .Build(); Console.WriteLine("Creating the table and index"); // Creates the table and the index in the same transaction. // Note: Any code within the lambda can potentially execute multiple times due to retries. // For more information, see: https://docs.aws.amazon.com/qldb/latest/developerguide/driver-retry-policy await driver.Execute(async txn => { await txn.Execute("CREATE TABLE Person"); await txn.Execute("CREATE INDEX ON Person(firstName)"); }); Console.WriteLine("Inserting a document"); // This is one way of creating Ion values. We can also use an IonLoader. // For more details, see: https://docs.aws.amazon.com/qldb/latest/developerguide/driver-cookbook-dotnet.html#cookbook-dotnet.ion IIonValue ionPerson = valueFactory.NewEmptyStruct(); ionPerson.SetField("firstName", valueFactory.NewString("John")); ionPerson.SetField("lastName", valueFactory.NewString("Doe")); ionPerson.SetField("age", valueFactory.NewInt(32)); await driver.Execute(async txn => { await txn.Execute("INSERT INTO Person ?", ionPerson); }); Console.WriteLine("Querying the table"); IIonValue ionFirstName = valueFactory.NewString("John"); // The result from driver.Execute() is buffered into memory because once the // transaction is committed, streaming the result is no longer possible. IAsyncResult selectResult = await driver.Execute(async txn => { return await txn.Execute("SELECT * FROM Person WHERE firstName = ?", ionFirstName); }); await foreach (IIonValue row in selectResult) { Console.WriteLine(row.GetField("firstName").StringValue); Console.WriteLine(row.GetField("lastName").StringValue); Console.WriteLine(row.GetField("age").IntValue); } Console.WriteLine("Updating the document"); IIonValue ionIntAge = valueFactory.NewInt(42); IIonValue ionFirstName2 = valueFactory.NewString("John"); await driver.Execute(async txn => { await txn.Execute("UPDATE Person SET age = ? WHERE firstName = ?", ionIntAge, ionFirstName2); }); Console.WriteLine("Querying the table for the updated document"); IIonValue ionFirstName3 = valueFactory.NewString("John"); IAsyncResult updateResult = await driver.Execute(async txn => { return await txn.Execute("SELECT * FROM Person WHERE firstName = ?", ionFirstName3); }); await foreach (IIonValue row in updateResult) { Console.WriteLine(row.GetField("firstName").StringValue); Console.WriteLine(row.GetField("lastName").StringValue); Console.WriteLine(row.GetField("age").IntValue); } } } }
Sync
using System; using Amazon.IonDotnet.Tree; using Amazon.IonDotnet.Tree.Impl; using Amazon.QLDB.Driver; namespace Amazon.QLDB.QuickStartGuide { class Program { static IValueFactory valueFactory = new ValueFactory(); static void Main(string[] args) { Console.WriteLine("Create the sync QLDB Driver"); IQldbDriver driver = QldbDriver.Builder() .WithLedger("quick-start") .Build(); Console.WriteLine("Creating the tables and index"); // Creates the table and the index in the same transaction. // Note: Any code within the lambda can potentially execute multiple times due to retries. // For more information, see: https://docs.aws.amazon.com/qldb/latest/developerguide/driver-retry-policy driver.Execute(txn => { txn.Execute("CREATE TABLE Person"); txn.Execute("CREATE INDEX ON Person(firstName)"); }); Console.WriteLine("Inserting a document"); // This is one way of creating Ion values. We can also use an IonLoader. // For more details, see: https://docs.aws.amazon.com/qldb/latest/developerguide/driver-cookbook-dotnet.html#cookbook-dotnet.ion IIonValue ionPerson = valueFactory.NewEmptyStruct(); ionPerson.SetField("firstName", valueFactory.NewString("John")); ionPerson.SetField("lastName", valueFactory.NewString("Doe")); ionPerson.SetField("age", valueFactory.NewInt(32)); driver.Execute(txn => { txn.Execute("INSERT INTO Person ?", ionPerson); }); Console.WriteLine("Querying the table"); IIonValue ionFirstName = valueFactory.NewString("John"); // The result from driver.Execute() is buffered into memory because once the // transaction is committed, streaming the result is no longer possible. IResult selectResult = driver.Execute(txn => { return txn.Execute("SELECT * FROM Person WHERE firstName = ?", ionFirstName); }); foreach (IIonValue row in selectResult) { Console.WriteLine(row.GetField("firstName").StringValue); Console.WriteLine(row.GetField("lastName").StringValue); Console.WriteLine(row.GetField("age").IntValue); } Console.WriteLine("Updating a document"); IIonValue ionIntAge = valueFactory.NewInt(42); IIonValue ionFirstName2 = valueFactory.NewString("John"); driver.Execute(txn => { txn.Execute("UPDATE Person SET age = ? WHERE firstName = ?", ionIntAge, ionFirstName2); }); Console.WriteLine("Querying the table for the updated document"); IIonValue ionFirstName3 = valueFactory.NewString("John"); IResult updateResult = driver.Execute(txn => { return txn.Execute("SELECT * FROM Person WHERE firstName = ?", ionFirstName3); }); foreach (IIonValue row in updateResult) { Console.WriteLine(row.GetField("firstName").StringValue); Console.WriteLine(row.GetField("lastName").StringValue); Console.WriteLine(row.GetField("age").IntValue); } } } }

Untuk menjalankan aplikasi lengkap, masukkan perintah berikut dari direktori induk direktoriAmazon.QLDB.QuickStartGuide proyek Anda.

$ dotnet run --project Amazon.QLDB.QuickStartGuide