{GCP Cloud Scheduler}cron ジョブをスケジュールして実行する

 

https://cloud.google.com/scheduler/docs/schedule-run-cron-job?hl=ja

https://qiita.com/chidakiyo/items/2bbedfdb29a70acd4e45

-- 1. 前作業

gcloud init
gcloud auth list

gcloud --version

gcloud projects create project01-9999999 \
--name="project01"

gcloud config list
gcloud config set project project01-9999999
gcloud config set compute/region asia-northeast1 --quiet
gcloud config set compute/zone asia-northeast1-a --quiet

gcloud beta billing accounts list
gcloud beta billing projects link project01-9999999 --billing-account=111111-111111-111111

gcloud services enable compute.googleapis.com --project project01-9999999

gcloud components update

 

-- 2. Cloud Scheduler API と Pub/Sub API を有効化

gcloud services list --enabled


gcloud services enable cloudscheduler.googleapis.com pubsub.googleapis.com \
--project project01-9999999

 


-- 3. トピックの作成

gcloud pubsub topics create topic01

gcloud pubsub topics list


-- 4. サブスクリプションの作成

gcloud pubsub subscriptions create ss01 \
--topic=topic01

gcloud pubsub subscriptions list


-- 5. Cloud Scheduler ジョブの作成

gcloud scheduler jobs create pubsub job01 \
--schedule "* * * * *" \
--topic topic01 \
--location asia-northeast1 \
--message-body "Hello world!"

gcloud scheduler jobs list \
--location asia-northeast1


-- 6. Cloud Scheduler ジョブを手動実行する

gcloud scheduler jobs run job01 \
--location asia-northeast1

 

-- 7. Pub/Sub で結果を確認する

gcloud pubsub subscriptions pull ss01 \
--limit 100

 

-- 8. クリーンアップ

gcloud scheduler jobs delete job01 \
--location asia-northeast1 \
--quiet


gcloud projects list

gcloud projects delete project01-9999999 \
--quiet

 

{Azure Logic Apps}チュートリアル: Azure Logic Apps を使用して、スケジュールに基づいて実行される自動化ワークフローを作成する

 

https://learn.microsoft.com/ja-jp/azure/logic-apps/tutorial-build-schedule-recurring-logic-app-workflow
https://learn.microsoft.com/ja-jp/azure/logic-apps/concepts-schedule-automated-recurring-tasks-workflows


Azure Logic Apps は、自動化された定期的なワークフローを作成し、スケジュールに従って実行するのに役立ちます。


-- 1. 前作業

az login --use-device-code
az account show

az version

az configure --list-defaults
az configure --defaults location=japaneast
az configure --list-defaults

az group create \
--name rg9999999 \
--location japaneast

az group list
az upgrade

 


-- 2. Service Bus 名前空間を作成

az servicebus namespace create \
--resource-group rg9999999 \
--name ns123 \
--location japaneast \
--sku Basic

az servicebus namespace list \
--resource-group rg9999999


-- 3. 名前空間にキューを作成

az servicebus queue create \
--resource-group rg9999999 \
--namespace-name ns123 \
--name queue01

az servicebus queue list \
--resource-group rg9999999 \
--namespace-name ns123

 

-- 4. 名前空間のプライマリ接続文字列を取得

az servicebus namespace authorization-rule keys list \
--resource-group rg9999999 \
--namespace-name ns123 \
--name RootManageSharedAccessKey \
--query primaryConnectionString \
--output tsv

 

-- 5. ロジック アプリを作成する


トリガー: スケジュール
アクション: キューにメッセージ送信

 

※リソースとして、API接続servicebusが必要であるが、
コマンドで作成する方法が不明、API接続のコンソールでも作成ボタンがないため、
コンソールからロジック アプリを作成する


az logic workflow list \
--resource-group rg9999999 \
--filter "(State eq 'Enabled')" \
--output "table"

az logic workflow show \
--resource-group rg9999999 \
--name flow123


-- 6. キュー確認アプリを実行する

pip install azure-servicebus

vim a.py

from azure.servicebus import ServiceBusClient, ServiceBusMessage

CONNECTION_STR = "Endpoint=sb://ns123.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=11111111111111111111111111111111111111111111"
QUEUE_NAME = "queue01"


# create a Service Bus client using the connection string
servicebus_client = ServiceBusClient.from_connection_string(conn_str=CONNECTION_STR, logging_enable=True)

with servicebus_client:
    # get the Queue Receiver object for the queue
    receiver = servicebus_client.get_queue_receiver(queue_name=QUEUE_NAME, max_wait_time=3600)
    with receiver:
        for msg in receiver:
            print("Received: " + str(msg))
            # complete the message so that the message is removed from the queue
            receiver.complete_message(msg)


python a.py


-- 7. クリーンアップ

az logic workflow delete \
--resource-group rg9999999 \
--name flow123 \
--yes

 

az group list

az group delete \
--name rg9999999 \
--yes

 

 

{GCP API Gateway}クイックスタート: gcloud コマンドライン ツールを使用して API Gateway に API をデプロイする

 

https://cloud.google.com/api-gateway/docs/secure-traffic-gcloud?hl=ja

https://dev.classmethod.jp/articles/google-cloud-api-gateway-handson/


-- 1. 前作業

gcloud init
gcloud auth list

gcloud --version

gcloud projects create project01-9999999 \
--name="project01"

gcloud config list
gcloud config set project project01-9999999
gcloud config set compute/region asia-northeast1 --quiet
gcloud config set compute/zone asia-northeast1-a --quiet

gcloud beta billing accounts list
gcloud beta billing projects link project01-9999999 --billing-account=111111-111111-111111

gcloud services enable compute.googleapis.com --project project01-9999999

gcloud components update


-- 2. 必要なサービスを有効にする

gcloud services list
gcloud services list --enabled

gcloud services enable apigateway.googleapis.com
gcloud services enable servicemanagement.googleapis.com
gcloud services enable servicecontrol.googleapis.com

gcloud services enable cloudfunctions.googleapis.com
gcloud services enable cloudbuild.googleapis.com
gcloud services enable artifactregistry.googleapis.com

gcloud services enable run.googleapis.com

 


-- 3. API バックエンドをデプロイする

-- 3.1 NVM のインストール

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.2/install.sh | bash

-- 3.2 Node.js と npm(Node Package Manager)のインストール

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

nvm install stable
nvm alias default stable
node -v


-- 3.3 Node.js 用 Google Cloud クライアント ライブラリのインストール

npm install --save @google-cloud/storage

npm install --save @google-cloud/functions-framework


-- 3.4 サンプルコードを取得する

git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git
cd nodejs-docs-samples/functions/helloworld/helloworldGet

 

-- 3.5 関数のデプロイ

 

gcloud functions deploy helloGET \
--runtime=nodejs16 \
--region=asia-northeast1 \
--source=. \
--entry-point=helloGET \
--trigger-http \
--allow-unauthenticated


gcloud functions describe helloGET \
--gen2 \
--region=asia-northeast1 \
--format="value(serviceConfig.uri)"

curl https://asia-northeast1-project01-9999999.cloudfunctions.net/helloGET


-- 4. API の作成

gcloud api-gateway apis create api01

gcloud api-gateway apis describe api01

 


-- 5. API 構成の作成

vim a.yaml

# a.yaml
swagger: '2.0'
info:
  title: api01 test
  description: Sample API on API Gateway with a Google Cloud Functions backend
  version: 1.0.0
schemes:
  - https
produces:
  - application/json
paths:
  /hello:
    get:
      summary: Greet a user
      operationId: hello
      x-google-backend:
        address: https://asia-northeast1-project01-9999999.cloudfunctions.net/helloGET
      responses:
        '200':
          description: A successful response
          schema:
            type: string


gcloud iam service-accounts list

gcloud api-gateway api-configs create config01 \
--api=api01 \
--openapi-spec=a.yaml \
--backend-auth-service-account=222222222222-compute@developer.gserviceaccount.com

 


gcloud api-gateway api-configs describe config01 \
--api=api01

 

-- 6. ゲートウェイを作成する

gcloud api-gateway gateways create gateway01 \
--api=api01 \
--api-config=config01 \
--location=asia-northeast1

gcloud api-gateway gateways describe gateway01 \
--location=asia-northeast1


-- 7. API デプロイのテスト

curl https://gateway01-11111111.an.gateway.dev/hello

 

 

-- 8. クリーンアップ

gcloud api-gateway gateways delete gateway01 \
--location=asia-northeast1 \
--quiet


gcloud api-gateway api-configs delete config01 \
--api=api01 \
--quiet

gcloud api-gateway apis delete api01 \
--quiet

 

gcloud projects list

gcloud projects delete project01-9999999 \
--quiet

 

 

 

{Azure API Management}クイック スタート: Azure CLI を使用して新しい Azure API Management サービス インスタンスを作成する

 

https://learn.microsoft.com/ja-jp/azure/api-management/get-started-create-service-instance-cli

https://www.softbank.jp/biz/blog/cloud-technology/articles/202103/azure-api-management/

 

-- 1. 前作業

az login --use-device-code
az account show

az version

az configure --list-defaults
az configure --defaults location=japaneast
az configure --list-defaults

az group create \
--name rg9999999 \
--location japaneast

az group list
az upgrade

 

-- 2. API Management サービス インスタンスを作成


az apim create \
--resource-group rg9999999 \
--name api123 \
--publisher-name test \
--publisher-email hoge@example.com \
--sku-name Developer


約1時間かかる

 

az apim list \
--resource-group rg9999999


az apim show \
--resource-group rg9999999 \
--name api123 \
--output table

 

-- 3. バックエンド API の作成

Echo API
がデフォルトで存在する


az apim api create \
--resource-group rg9999999 \
--service-name api123 \
--api-id backend01 \
--display-name backend01 \
--path '/conference' \
--api-type http \
--protocols https \
--service-url https://conferenceapi.azurewebsites.net?format=json


az apim api list \
--resource-group rg9999999 \
--service-name api123


az apim api show \
--resource-group rg9999999 \
--service-name api123 \
--api-id backend01

 

-- 4. API 操作の追加

az apim api operation create \
--resource-group rg9999999 \
--service-name api123 \
--api-id backend01 \
--operation-id getsessions \
--display-name getsessions \
--method GET \
--url-template "/sessions"


az apim api operation list \
--resource-group rg9999999 \
--service-name api123 \
--api-id backend01

az apim api operation show \
--resource-group rg9999999 \
--service-name api123 \
--api-id backend01 \
--operation-id getsessions

 

-- 5. Azure portal での新しい API のテスト

 


-- 6. 製品を作成して発行する

Starter
Unlimited
がデフォルトで存在する


az apim product create \
--resource-group rg9999999 \
--service-name api123 \
--product-id product01 \
--product-name product01 \
--description product01 \
--subscription-required false \
--state published


az apim product list \
--resource-group rg9999999 \
--service-name api123

az apim product show \
--resource-group rg9999999 \
--service-name api123 \
--product-id product01

 

-- 7. 製品に API を追加する

az apim product api add \
--resource-group rg9999999 \
--service-name api123 \
--product-id product01 \
--api-id backend01


az apim product api list \
--resource-group rg9999999 \
--service-name api123 \
--product-id product01 \
--output table

 

-- 8. 製品の API にアクセスする

curl https://api123.azure-api.net/conference/sessions

 


-- 9. クリーンアップ

az apim delete \
--resource-group rg9999999 \
--name api123 \
--yes


az group list

az group delete \
--name rg9999999 \
--yes

 

MongoDBコマンド

 

前提
OS : Rocky Linux 9.1
MongoDB version : 6.0.3


mongosh


データベース作成
use db01

データベース一覧
show dbs

データベース削除
db.dropDatabase()


コレクション作成
db.createCollection('tab1')

コレクション一覧
show collections

コレクション削除
db.tab1.drop()


データ追加
db.tab1.insertOne(
   { _id: 0, col1: "val10", col2 : 20, col3 : ["val310","val320"], col4: { col41: 410, col42: "val420" } }
)

db.tab1.insertMany([
   { _id: 1, col1: "val11", col2 : 21, col3 : ["val311","val321"], col4: { col41: 411, col42: "val421" } },
   { _id: 2, col1: "val12", col2 : 22, col3 : ["val312","val322"], col4: { col41: 412, col42: "val422" } },
])

件数取得
db.tab1.countDocuments()


データ取得

db.tab1.find()
db.tab1.findOne()

射影
db.tab1.find({}, {col1: 1, col2: 1} )
db.tab1.find({}, {col3: 1} )


選択
db.tab1.find({ col1: "val10" })


射影+選択
db.tab1.find({ col1: "val10" }, {col1: 1})


ソート
db.tab1.find().sort({col2:  1})
db.tab1.find().sort({col2: -1})


データ削除
db.tab1.deleteOne({ col1: "val10" })

db.tab1.deleteMany({})


インデックス作成
db.tab1.createIndex({col1: 1})

インデックス一覧
db.tab1.getIndexes()

インデックス削除
db.tab1.dropIndex('col1_1')


状態確認
db.stats()
db.tab1.stats()

mongostat
mongotop

 


実行計画取得
db.tab1.find({ col1: "val10" }).explain()


インポート
cat << 'EOF' > a.csv
_id,col1,col2,col3
100,AAA,あ,10
200,BBB,い,20
300,CCC,う,30
400,DDD,え,40
EOF

cat a.csv


mongoimport \
--type=csv \
--file=a.csv \
--headerline \
--collection=tab2 \
mongodb://127.0.0.1:27017/db01

mongosh --quiet --eval 'db.tab2.find()' mongodb://127.0.0.1:27017/db01

 


エクスポート
mongoexport \
--type=csv \
--collection=tab2 \
--fields='_id,col1,col2,col3' \
mongodb://127.0.0.1:27017/db01 > b.csv

cat b.csv


バックアップ


mongodump \
--db=db01 \
--out=dump \
mongodb://127.0.0.1:27017

ls -lR dump


リストア

show dbs
use db01
db.dropDatabase();
show dbs


mongorestore \
--db=db01 \
mongodb://127.0.0.1:27017 \
dump/db01

 

スロークエリログ

db.getProfilingStatus()

db.setProfilingLevel(1, { slowms: 0 })

db.setProfilingLevel(0)

tail -f /var/log/mongodb/mongod.log

 

トランザクション

※レプリーション環境必要
スタンドアローンの場合、トランザクション内でデータ追加時下記エラー発生
MongoServerError: Transaction numbers are only allowed on a replica set member or mongos


show dbs

db.getSiblingDB("db01").tab1.insertOne( { col1: "val11"} )
db.getSiblingDB("db01").tab2.insertOne( { col1: "val21"} )

show dbs


db.getSiblingDB("db01").tab1.find()
db.getSiblingDB("db01").tab2.find()

session = db.getMongo().startSession()

session.startTransaction()
session.getDatabase("db01").tab1.insertOne( { col1: "val12"} )
session.getDatabase("db01").tab2.insertOne( { col1: "val22"} )
session.commitTransaction()

db.getSiblingDB("db01").tab1.find()
db.getSiblingDB("db01").tab2.find()

session.startTransaction()
session.getDatabase("db01").tab1.insertOne( { col1: "val13"} )
session.getDatabase("db01").tab2.insertOne( { col1: "val23"} )
session.abortTransaction()

db.getSiblingDB("db01").tab1.find()
db.getSiblingDB("db01").tab2.find()


トランザクション内部でもコミットするまで参照できない