forked from apigee/devrel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup_spanner.sh
executable file
·58 lines (48 loc) · 2.99 KB
/
setup_spanner.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#! /bin/bash
# shellcheck disable=SC2206
# Copyright 2021 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.
echo
echo Using Apigee X project \""$PROJECT_ID"\", instance \""$SPANNER_INSTANCE"\", database \""$SPANNER_DATABASE"\" in region \""$SPANNER_REGION"\" for CUSTOMER_USERID \""$CUSTOMER_USERID"\"
# Create instance
gcloud spanner instances create "$SPANNER_INSTANCE" --project="$PROJECT_ID" --config="$SPANNER_REGION" --description="Product Catalog Instance" --nodes=1
# Set default instance
gcloud config set spanner/instance "$SPANNER_INSTANCE" --project="$PROJECT_ID"
gcloud spanner databases create "$SPANNER_DATABASE" --project="$PROJECT_ID" --instance "$SPANNER_INSTANCE"
# Create database
gcloud spanner databases ddl update "$SPANNER_DATABASE" --project="$PROJECT_ID" \
--ddl='CREATE TABLE products (productid STRING(20) NOT NULL, name STRING(100), description STRING(1024), price FLOAT64, discount FLOAT64, image STRING(1024)) PRIMARY KEY(productid);'
# Add product data to Spanner
# Array of product Id data to be combined with Ids
DATAS[0]="description=Bamboo glass jar,discount=0,image=products_Images/bamboo-glass-jar.jpg,name=Bamboo glass jar,price=19.99"
DATAS[1]="description=Hotest hairdryer,discount=0,image=products_Images/hairdryer.jpg,name=Hairdryer,price=84.99"
DATAS[2]="description=Most comfortable loafers,discount=0,image=products_Images/loafers.jpg,name=Loafers,price=38.99"
DATAS[3]="description=Best Coffee Mug,discount=0,image=products_Images/mug.jpg,name=Coffee Mug,price=4.20"
DATAS[4]="description=The ultimate sunglasses,discount=0,image=products_Images/sunglasses.jpg,name=Aviator Sunglasses,price=42.42"
# Get list of IDs (5 total) from BigQuery based on the CUSTOMER_USERID to create in Spanner.
# Sort ascending, opposite of propensity to buy, to demonstrate different results.
# Result is one ID per line
IDS_JSON=$(bq query --project_id="$PROJECT_ID" --format json --nouse_legacy_sql \
"SELECT * FROM \`$PROJECT_ID.bqml.prod_recommendations\` AS A where A.userid = \"$CUSTOMER_USERID\" \
ORDER BY A.predicted_session_duration_confidence ASC")
IDS=$(echo "$IDS_JSON" | jq -r .[].itemId)
# Convert to an array
# shellcheck disable=SC2206
IDS_ARR=($IDS)
for ((i = 0; i < ${#IDS_ARR[@]}; i++))
do
echo -n "${IDS_ARR[$i]} - "
gcloud spanner rows insert --project="$PROJECT_ID" --database="$SPANNER_DATABASE" --table=products --data="productid=${IDS_ARR[$i]},${DATAS[$i]}"
done
gcloud spanner databases execute-sql "$SPANNER_DATABASE" --project="$PROJECT_ID" --sql='SELECT * FROM products'