-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add examples #49
Merged
Add examples #49
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
cc6f977
Add examples
XmasApple 0cc6170
Update CHANGELOG.md
XmasApple 8d457d0
Revert "Update CHANGELOG.md"
XmasApple 10a0be2
Merge branch 'main' into examples
XmasApple 32f31e5
Update README.md
XmasApple 5c339d4
Merge branch 'main' into examples
XmasApple File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# YDB .NET SDK Examples | ||
|
||
## Prerequisites | ||
.NET 6 | ||
|
||
## Running examples | ||
|
||
1. Clone repository | ||
```bash | ||
git clone https://github.com/ydb-platform/ydb-dotnet-sdk.git | ||
``` | ||
|
||
2. Build solution | ||
```bash | ||
cd ydb-dotnet-sdk/examples/src | ||
dotnet build | ||
``` | ||
|
||
3. Run example | ||
```bash | ||
cd <ExampleName> | ||
dotnet run -e <Endpoint> -d <Database> | ||
``` | ||
|
||
## Provided examples | ||
|
||
### BasicExample | ||
Demonstrates basic operations with YDB, including: | ||
* Driver initialization | ||
* Table client initialization | ||
* Table creation via SchemeQuery (DDL) | ||
* Data queries (OLTP) & transactions (read, modify) | ||
* Interactive transactions | ||
* ReadTable for streaming read of table contents | ||
* ScanQuery for streaming wide queries (OLAP) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
using System; | ||
using System.Security.Cryptography.X509Certificates; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Logging; | ||
using Ydb.Sdk.Auth; | ||
using Ydb.Sdk.Services.Table; | ||
|
||
namespace Ydb.Sdk.Examples; | ||
|
||
internal partial class BasicExample : TableExampleBase | ||
{ | ||
private BasicExample(TableClient client, string database, string path) | ||
: base(client, database, path) | ||
{ | ||
} | ||
|
||
public static async Task Run( | ||
string endpoint, | ||
string database, | ||
ICredentialsProvider credentialsProvider, | ||
X509Certificate? customServerCertificate, | ||
string path, | ||
ILoggerFactory loggerFactory) | ||
{ | ||
var config = new DriverConfig( | ||
endpoint: endpoint, | ||
database: database, | ||
credentials: credentialsProvider, | ||
customServerCertificate: customServerCertificate | ||
); | ||
|
||
await using var driver = await Driver.CreateInitialized( | ||
config: config, | ||
loggerFactory: loggerFactory | ||
); | ||
|
||
using var tableClient = new TableClient(driver, new TableClientConfig()); | ||
|
||
var example = new BasicExample(tableClient, database, path); | ||
|
||
await example.SchemeQuery(); | ||
await example.FillData(); | ||
await example.SimpleSelect(1); | ||
await example.SimpleUpsert(10, "Coming soon", DateTime.UtcNow); | ||
await example.SimpleSelect(10); | ||
await example.InteractiveTx(); | ||
await example.ReadTable(); | ||
await example.ScanQuery(DateTime.Parse("2007-01-01")); | ||
} | ||
|
||
private static ExecuteDataQuerySettings DefaultDataQuerySettings => | ||
new() | ||
{ | ||
// Indicates that client is no longer interested in the result of operation after the | ||
// specified duration starting from the moment when operation arrives at the server. | ||
// Status code TIMEOUT will be returned from server in case when operation result in | ||
// not available in the specified time period. This status code doesn't indicate the result | ||
// of operation, it might be completed or cancelled. | ||
OperationTimeout = TimeSpan.FromSeconds(1), | ||
|
||
// Transport timeout from the moment operation was sent to server. It is useful in case | ||
// of possible network issues, to that query doesn't hang forever. | ||
// It is recommended to set this value to a larger value than OperationTimeout to give | ||
// server some time to issue a response. | ||
TransportTimeout = TimeSpan.FromSeconds(5), | ||
|
||
// Keep query compilation result in query cache or not. Should be false for ad-hoc queries, | ||
// and true (default) for high-RPS queries. | ||
KeepInQueryCache = false | ||
}; | ||
|
||
private ExecuteDataQuerySettings DefaultCachedDataQuerySettings | ||
{ | ||
get | ||
{ | ||
var settings = DefaultDataQuerySettings; | ||
settings.KeepInQueryCache = true; | ||
return settings; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
|
||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<AssemblyName>Ydb.Sdk.Examples.BasicExample</AssemblyName> | ||
<RootNamespace>Ydb.Sdk.Examples</RootNamespace> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<RepositoryType>git</RepositoryType> | ||
<RepositoryUrl>https://github.com/ydb-platform/ydb-dotnet-examples</RepositoryUrl> | ||
<PackageProjectUrl>https://github.com/ydb-platform/ydb-dotnet-examples</PackageProjectUrl> | ||
<Company>YANDEX LLC</Company> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="CommandLineParser" Version="2.8.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="5.0.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Common\Common.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Ydb.Sdk.Services.Table; | ||
using Ydb.Sdk.Value; | ||
|
||
namespace Ydb.Sdk.Examples; | ||
|
||
internal partial class BasicExample | ||
{ | ||
private async Task SimpleSelect(ulong id) | ||
{ | ||
var response = await Client.SessionExec(async session => | ||
{ | ||
var query = @$" | ||
PRAGMA TablePathPrefix('{BasePath}'); | ||
DECLARE $id AS Uint64; | ||
SELECT | ||
series_id, | ||
title, | ||
release_date | ||
FROM series | ||
WHERE series_id = $id; | ||
"; | ||
return await session.ExecuteDataQuery( | ||
query: query, | ||
txControl: TxControl.BeginSerializableRW().Commit(), | ||
parameters: new Dictionary<string, YdbValue> | ||
{ | ||
{ "$id", YdbValue.MakeUint64(id) } | ||
}, | ||
settings: DefaultCachedDataQuerySettings | ||
); | ||
}); | ||
|
||
response.Status.EnsureSuccess(); | ||
|
||
var queryResponse = (ExecuteDataQueryResponse)response; | ||
var resultSet = queryResponse.Result.ResultSets[0]; | ||
|
||
Console.WriteLine($"> SimpleSelect, " + | ||
$"columns: {resultSet.Columns.Count}, " + | ||
$"rows: {resultSet.Rows.Count}, " + | ||
$"truncated: {resultSet.Truncated}"); | ||
|
||
foreach (var row in resultSet.Rows) | ||
{ | ||
Console.WriteLine($"> Series, " + | ||
$"series_id: {(ulong?)row["series_id"]}, " + | ||
$"title: {(string?)row["title"]}, " + | ||
$"release_date: {(DateTime?)row["release_date"]}"); | ||
} | ||
} | ||
|
||
private async Task SimpleUpsert(ulong id, string title, DateTime date) | ||
{ | ||
var response = await Client.SessionExec(async session => | ||
{ | ||
var query = @$" | ||
PRAGMA TablePathPrefix('{BasePath}'); | ||
DECLARE $id AS Uint64; | ||
DECLARE $title AS Utf8; | ||
DECLARE $release_date AS Date; | ||
UPSERT INTO series (series_id, title, release_date) VALUES | ||
($id, $title, $release_date); | ||
"; | ||
return await session.ExecuteDataQuery( | ||
query: query, | ||
txControl: TxControl.BeginSerializableRW().Commit(), | ||
parameters: new Dictionary<string, YdbValue> | ||
{ | ||
{ "$id", YdbValue.MakeUint64(id) }, | ||
{ "$title", YdbValue.MakeUtf8(title) }, | ||
{ "$release_date", YdbValue.MakeDate(date) } | ||
}, | ||
settings: DefaultCachedDataQuerySettings | ||
); | ||
}); | ||
|
||
response.Status.EnsureSuccess(); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no need changelog for examples