DynamoDB PartiQL – 첫 번째 부분: 삽입
AWS CLI
AWS CLI에서 실행되며, 현재 버전 2에서는 PartiQL을 사용할 수 없습니다.
[opc@a tmp]$ aws --version
aws-cli/2.1.3 Python/3.7.3 Linux/4.14.35-2025.400.9.el7uek.x86_64 exe/x86_64.oracle.7
[opc@a tmp]$ aws dynamodb execute-statement
usage: aws [options]   [ ...] [parameters]
aws: error: argument operation: Invalid choice, valid choices are:
cd /var/tmp
wget -c https://s3.amazonaws.com/aws-cli/awscli-bundle.zip
unzip -o awscli-bundle.zip
sudo ./awscli-bundle/install -i /var/tmp/aws-cli/awscli1
alias aws='/var/tmp/aws-cli/awscli1/bin/aws "$@" --output json | jq --compact-output'
 
 현재 이 기능은 모든 지역에 배치된 것이 아니기 때문에, 만약 당신이 그것을 볼 수 없다면 다른 지역을 검사하십시오.그것에 대한 공고는 거기에 있다. You now can use a SQL-compatible query language to query, insert, update, and delete table data in Amazon DynamoDB.내 블로그 글을 읽을 때, 위의 모든 내용이 유행이 지났을 수도 있습니다. 모든 지역에서 사용할 수 있고, 최신 AWS CLI를 사용할 수 있습니다.이 특성의 문서는 개발자 가이드에서 찾을 수 있습니다: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/ql-reference.html
DDL
DDL과 유사한 CREATE TABLE가 없으므로 일반적인 API(콘솔, CLI 또는 API)를 사용하여 DynamoDB를 만들어야 합니다.
aws dynamodb create-table --attribute-definitions \
  AttributeName=MyKeyPart,AttributeType=N \
  AttributeName=MyKeySort,AttributeType=N \
 --key-schema \
  AttributeName=MyKeyPart,KeyType=HASH \
  AttributeName=MyKeySort,KeyType=RANGE \
 --billing-mode PROVISIONED \
 --provisioned-throughput ReadCapacityUnits=25,WriteCapacityUnits=25 \
 --table-name Demo
삽입
간단한 insert 문을 사용하여 새 항목을 삽입할 수 있습니다.
[opc@a aws]$ aws dynamodb execute-statement --statement \
"insert into Demo value {'MyKeyPart':1,'MyKeySort':1,'MyUnstructuredData':'here is my first insert :)'}"
{"Items":[]}
인터랙티브 쿼리 콘솔에서 코드(SQL 키워드, 테이블 및 속성 식별자)와 데이터(예를 들어 내가 사용하는 숫자나 문자열)를 혼합해서 사용하는 것은 편리하지만 정확한 인코딩 방식은 아니다.모든 API와 마찬가지로 변수 인코딩을 사용하여 실행할 때 매개 변수로 전달해야 합니다.다음과 같은 이점을 제공합니다.
[opc@a aws]$ aws dynamodb execute-statement --statement \
"insert into Demo value {'MyKeyPart':?,'MyKeySort':?,'MyUnstructuredData':?}" \
  --parameters '[{"N":"2"},{"N":"2"},{"S":"use parameters when embedding SQL in programs"}]'
{"Items":[]}
텍스트를 바꾸는 것이 아니라 귀속 변수라는 것을 기억해야 합니다.문자열의 경우 SQL 텍스트에 따옴표로 묶을 수 없습니다.데이터 유형은 매개변수 목록에 선언됩니다.
수신할 데이터가 있을 때, 항목마다 네트워크가 왕복하는 것을 원하지 않습니다.대량 삽입을 보내는 방법은 다음과 같습니다.
[opc@a aws]$ aws dynamodb batch-execute-statement --statements \
'[ {"Statement":"insert into Demo value '"{'MyKeyPart':?,'MyKeySort':?,'MyUnstructuredData':?}"'","Parameters":[{"N":"2"},{"N":"1"},{"S":"a"}]} , {"Statement":"insert into Demo value '"{'MyKeyPart':?,'MyKeySort':?,'MyUnstructuredData':?}"'","Parameters":[{"N":"2"},{"N":"2"},{"S":"b"}]} , {"Statement":"insert into Demo value '"{'MyKeyPart':?,'MyKeySort':?,'MyUnstructuredData':?}"'","Parameters":[{"N":"2"},{"N":"3"},{"S":"c"}]} , {"Statement":"insert into Demo value '"{'MyKeyPart':?,'MyKeySort':?,'MyUnstructuredData':?}"'","Parameters":[{"N":"2"},{"N":"4"},{"S":"d"}]} , {"Statement":"insert into Demo value '"{'MyKeyPart':?,'MyKeySort':?,'MyUnstructuredData':?}"'","Parameters":[{"N":"2"},{"N":"5"},{"S":"e"}]} ]'
{"Responses":[{"TableName":"Demo"},{"TableName":"Demo","Error":{"Message":"Duplicate primary key exists in table","Code":"DuplicateItem"}},{"TableName":"Demo"},{"TableName":"Demo"},{"TableName":"Demo"}]}
마지막으로, 다음은 프레젠테이션 테이블에 나와 있는 내용입니다.
 
 문 목록에 반복 항목이 이미 있으면 전체 호출이 거부됩니다.
[opc@a aws]$ aws dynamodb batch-execute-statement --statements '[ {"Statement":"insert into Demo value '"{'MyKeyPart':?,'MyKeySort':?,'MyUnstructuredData':?}"'","Parameters":[{"N":"9"},{"N":"9"},{"S":"a"}]} , {"Statement":"insert into Demo value '"{'MyKeyPart':?,'MyKeySort':?,'MyUnstructuredData':?}"'","Parameters":[{"N":"9"},{"N":"9"},{"S":"b"}]} , {"Statement":"insert into Demo value '"{'MyKeyPart':?,'MyKeySort':?,'MyUnstructuredData':?}"'","Parameters":[{"N":"9"},{"N":"1"},{"S":"c"}]} , {"Statement":"insert into Demo value '"{'MyKeyPart':?,'MyKeySort':?,'MyUnstructuredData':?}"'","Parameters":[{"N":"9"},{"N":"2"},{"S":"d"}]} , {"Statement":"insert into Demo value '"{'MyKeyPart':?,'MyKeySort':?,'MyUnstructuredData':?}"'","Parameters":[{"N":"9"},{"N":"3"},{"S":"e"}]} ]'
An error occurred (ValidationException) when calling the BatchExecuteStatement operation: Provided list of item keys contains duplicates
불행히도, 나는 어쩔 수 없이 이곳의 한마디 한마디를 되풀이했다.나는 하나의 API를 더 좋아한다. 이 API에서 나는 매개 변수화 문장을 준비하고 많은 값으로 실행한다. 예를 들어 SQL 데이터베이스에 있는 그룹 인터페이스 등이다.그러나 NoSQL에서는 각 항목마다 고유한 구조가 있으며 다른 항목과는 다를 수 있습니다.
고르다
SELECT 문구로 프로젝트를 살펴보겠습니다.
[opc@a aws]$ aws dynamodb execute-statement --statement "select * from Demo"
{"Items":[
 {"MyKeyPart":{"N":"2"},"MyUnstructuredData":{"S":"a"},"MyKeySort":{"N":"1"}}
,{"MyKeyPart":{"N":"2"},"MyUnstructuredData":{"S":"use parameters when embedding SQL in programs"}
,"MyKeySort":{"N":"2"}},{"MyKeyPart":{"N":"2"},"MyUnstructuredData":{"S":"c"}
,"MyKeySort":{"N":"3"}},{"MyKeyPart":{"N":"2"},"MyUnstructuredData":{"S":"d"}
,"MyKeySort":{"N":"4"}},{"MyKeyPart":{"N":"2"},"MyUnstructuredData":{"S":"e"}
,"MyKeySort":{"N":"5"}},{"MyKeyPart":{"N":"1"},"MyUnstructuredData":{"S":"here is my first insert :)"}
,"MyKeySort":{"N":"1"}}]}
[opc@a aws]$ aws dynamodb execute-statement --statement "select * from Demo order by MyKeyPart,MyKeySort"     
                                                
An error occurred (ValidationException) when calling the ExecuteStatement operation: Must have WHERE clause in the statement when using ORDER BY clause.
[opc@a aws]$ aws dynamodb execute-statement --statement "select MyKeyPart,MyKeySort from Demo where MyKeyPart=2 order by MyKeyPart,MyKeySort"
{"Items":[{"MyKeyPart":{"N":"2"},"MyKeySort":{"N":"1"}},{"MyKeyPart":{"N":"2"},"MyKeySort":{"N":"2"}},{"MyKeyPart":{"N":"2"},"MyKeySort":{"N":"3"}},{"MyKeyPart":{"N":"2"},"MyKeySort":{"N":"4"}},{"MyKeyPart":{"N":"2"},"MyKeySort":{"N":"5"}}]}
Reference
이 문제에 관하여(DynamoDB PartiQL – 첫 번째 부분: 삽입), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/aws-heroes/dynamodb-partiql-part-i-insert-1a0텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)