-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: add snippets for getting started guide (#1600)
* docs: add snippets for getting started guide * docs: add golang sample * chore: add tags * docs: add node.js sample * docs: add Python samples * docs: add python and psql samples and tests * fix: rename psql directory * feat: more Python samples * feat: add python + psql snippets * feat: add Java snippets * feat: add Go snippets * docs: add more samples * docs: add Node.js sample for create_connection * docs: more node.js samples * docs: add COPY sample * docs: more samples * docs: more samples * docs: add transaction samples * docs: tag samples * docs: add more samples * chore: add sample runner * chore: add Java sample runner * chore: add test runner for Go * chore: add nodejs sample runner * docs: add dotnet samples * chore: remove accidentally committed files * docs: add dotnet samples * chore: fix formatting issues * chore: cleanup dotnet samples * test: create test runner for dotnet * build: run on Ubuntu due to Docker dependency * docs: add python sample runner * chore: add missing Python sample * chore: fix python sample runner * chore: update psql samples * chore: add echo messages to psql scripts * test: add messages to psql tests * chore: cleanup
- Loading branch information
Showing
144 changed files
with
6,951 additions
and
6 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
on: | ||
push: | ||
branches: | ||
- postgresql-dialect | ||
pull_request: | ||
workflow_dispatch: | ||
name: snippets | ||
jobs: | ||
psql-snippets: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- name: "Install postgresql-client-14" | ||
run: | | ||
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list' | ||
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add - | ||
sudo apt-get update | ||
sudo apt-get install postgresql-client-14 | ||
- name: Run psql snippets | ||
working-directory: ./samples/snippets/psql-snippets/tests | ||
run: ./test_samples.sh | ||
java-snippets: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- name: Setup Java | ||
uses: actions/setup-java@v4 | ||
with: | ||
distribution: zulu | ||
java-version: 17 | ||
- name: Run Java Snippets | ||
working-directory: ./samples/snippets/java-snippets | ||
run: mvn test -B -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn | ||
go-snippets: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: actions/setup-go@v5 | ||
with: | ||
go-version: '1.20.7' | ||
- name: Run Go snippets | ||
working-directory: ./samples/snippets/golang-snippets | ||
run: go test | ||
python-snippets: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: actions/setup-python@v5 | ||
with: | ||
python-version: '3.9' | ||
- run: python --version | ||
- name: Install pip | ||
run: python -m pip install --upgrade pip | ||
- name: Run Python snippets | ||
working-directory: ./samples/snippets/python-snippets | ||
run: | | ||
pip install -r requirements.txt | ||
python -m unittest | ||
nodejs-snippets: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: actions/setup-node@v4 | ||
with: | ||
node-version: 20 | ||
- run: node --version | ||
- name: Run Node.js snippets | ||
working-directory: ./samples/snippets/nodejs-snippets | ||
run: | | ||
npm install | ||
npm test | ||
dotnet-snippets: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: actions/setup-dotnet@v4 | ||
with: | ||
dotnet-version: '6.0.x' | ||
- run: node --version | ||
- name: Run dotnet snippets | ||
working-directory: ./samples/snippets/dotnet-snippets | ||
run: dotnet test |
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,34 @@ | ||
// Copyright 2024 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// [START spanner_add_column] | ||
using Npgsql; | ||
|
||
namespace dotnet_snippets; | ||
|
||
public static class AddColumnSample | ||
{ | ||
public static void AddColumn(string host, int port, string database) | ||
{ | ||
var connectionString = $"Host={host};Port={port};Database={database};SSL Mode=Disable;Pooling=False"; | ||
using var connection = new NpgsqlConnection(connectionString); | ||
connection.Open(); | ||
|
||
using var cmd = connection.CreateCommand(); | ||
cmd.CommandText = "alter table albums add column marketing_budget bigint"; | ||
cmd.ExecuteNonQuery(); | ||
Console.WriteLine("Added marketing_budget column"); | ||
} | ||
} | ||
// [END spanner_add_column] |
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,37 @@ | ||
// Copyright 2024 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// [START spanner_create_connection] | ||
using Npgsql; | ||
|
||
namespace dotnet_snippets; | ||
|
||
public static class CreateConnectionSample | ||
{ | ||
public static void CreateConnection(string host, int port, string database) | ||
{ | ||
var connectionString = $"Host={host};Port={port};Database={database};SSL Mode=Disable;Pooling=False"; | ||
using var connection = new NpgsqlConnection(connectionString); | ||
connection.Open(); | ||
|
||
using var cmd = new NpgsqlCommand("select 'Hello World!' as hello", connection); | ||
using var reader = cmd.ExecuteReader(); | ||
while (reader.Read()) | ||
{ | ||
var greeting = reader.GetString(0); | ||
Console.WriteLine($"Greeting from Cloud Spanner PostgreSQL: {greeting}"); | ||
} | ||
} | ||
} | ||
// [END spanner_create_connection] |
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,53 @@ | ||
// Copyright 2024 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// [START spanner_create_database] | ||
using Npgsql; | ||
|
||
namespace dotnet_snippets; | ||
|
||
public static class CreateTablesSample | ||
{ | ||
public static void CreateTables(string host, int port, string database) | ||
{ | ||
var connectionString = $"Host={host};Port={port};Database={database};SSL Mode=Disable;Pooling=False"; | ||
using var connection = new NpgsqlConnection(connectionString); | ||
connection.Open(); | ||
|
||
// Create two tables in one batch. | ||
var batch = connection.CreateBatch(); | ||
batch.BatchCommands.Add(new NpgsqlBatchCommand( | ||
"create table singers (" | ||
+ " singer_id bigint primary key not null," | ||
+ " first_name varchar(1024)," | ||
+ " last_name varchar(1024)," | ||
+ " singer_info bytea," | ||
+ " full_name varchar(2048) generated always as (\n" | ||
+ " case when first_name is null then last_name\n" | ||
+ " when last_name is null then first_name\n" | ||
+ " else first_name || ' ' || last_name\n" | ||
+ " end) stored" | ||
+ ")")); | ||
batch.BatchCommands.Add(new NpgsqlBatchCommand( | ||
"create table albums (" | ||
+ " singer_id bigint not null," | ||
+ " album_id bigint not null," | ||
+ " album_title varchar," | ||
+ " primary key (singer_id, album_id)" | ||
+ ") interleave in parent singers on delete cascade")); | ||
batch.ExecuteNonQuery(); | ||
Console.WriteLine($"Created Singers & Albums tables in database: [{database}]"); | ||
} | ||
} | ||
// [END spanner_create_database] |
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,45 @@ | ||
// Copyright 2024 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// [START spanner_data_boost] | ||
using Npgsql; | ||
|
||
namespace dotnet_snippets; | ||
|
||
public static class DataBoostSample | ||
{ | ||
public static void DataBoost(string host, int port, string database) | ||
{ | ||
var connectionString = $"Host={host};Port={port};Database={database};SSL Mode=Disable;Pooling=False"; | ||
using var connection = new NpgsqlConnection(connectionString); | ||
connection.Open(); | ||
|
||
using var cmd = connection.CreateCommand(); | ||
// This enables Data Boost for all partitioned queries on this connection. | ||
cmd.CommandText = "set spanner.data_boost_enabled=true"; | ||
cmd.ExecuteNonQuery(); | ||
|
||
|
||
// Run a partitioned query. This query will use Data Boost. | ||
cmd.CommandText = "run partitioned query " | ||
+ "select singer_id, first_name, last_name " | ||
+ "from singers"; | ||
using var reader = cmd.ExecuteReader(); | ||
while (reader.Read()) | ||
{ | ||
Console.WriteLine($"{reader["singer_id"]} {reader["first_name"]} {reader["last_name"]}"); | ||
} | ||
} | ||
} | ||
// [END spanner_data_boost] |
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,52 @@ | ||
// Copyright 2024 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// [START spanner_ddl_batch] | ||
using Npgsql; | ||
|
||
namespace dotnet_snippets; | ||
|
||
public static class DdlBatchSample | ||
{ | ||
public static void DdlBatch(string host, int port, string database) | ||
{ | ||
var connectionString = $"Host={host};Port={port};Database={database};SSL Mode=Disable;Pooling=False"; | ||
using var connection = new NpgsqlConnection(connectionString); | ||
connection.Open(); | ||
|
||
// Create two new tables in one batch. | ||
var batch = connection.CreateBatch(); | ||
batch.BatchCommands.Add(new NpgsqlBatchCommand( | ||
"CREATE TABLE venues (" | ||
+ " venue_id bigint not null primary key," | ||
+ " name varchar(1024)," | ||
+ " description jsonb" | ||
+ ")")); | ||
batch.BatchCommands.Add(new NpgsqlBatchCommand( | ||
"CREATE TABLE concerts (" | ||
+ " concert_id bigint not null primary key ," | ||
+ " venue_id bigint not null," | ||
+ " singer_id bigint not null," | ||
+ " start_time timestamptz," | ||
+ " end_time timestamptz," | ||
+ " constraint fk_concerts_venues foreign key" | ||
+ " (venue_id) references venues (venue_id)," | ||
+ " constraint fk_concerts_singers foreign key" | ||
+ " (singer_id) references singers (singer_id)" | ||
+ ")")); | ||
batch.ExecuteNonQuery(); | ||
Console.WriteLine("Added venues and concerts tables"); | ||
} | ||
} | ||
// [END spanner_ddl_batch] |
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,40 @@ | ||
// Copyright 2024 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// [START spanner_partitioned_dml] | ||
using Npgsql; | ||
|
||
namespace dotnet_snippets; | ||
|
||
public static class PartitionedDmlSample | ||
{ | ||
public static void PartitionedDml(string host, int port, string database) | ||
{ | ||
var connectionString = $"Host={host};Port={port};Database={database};SSL Mode=Disable;Pooling=False"; | ||
using var connection = new NpgsqlConnection(connectionString); | ||
connection.Open(); | ||
|
||
// Enable Partitioned DML on this connection. | ||
using var cmd = connection.CreateCommand(); | ||
cmd.CommandText = "set spanner.autocommit_dml_mode='partitioned_non_atomic'"; | ||
cmd.ExecuteNonQuery(); | ||
|
||
// Back-fill a default value for the MarketingBudget column. | ||
cmd.CommandText = "update albums set marketing_budget=0 where marketing_budget is null"; | ||
var lowerBoundUpdateCount = cmd.ExecuteNonQuery(); | ||
|
||
Console.WriteLine($"Updated at least {lowerBoundUpdateCount} albums"); | ||
} | ||
} | ||
// [END spanner_partitioned_dml] |
Oops, something went wrong.