RDS 解析程序映射模板参考 - Amazon AppSync
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 Amazon Web Services 服务入门

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

RDS 解析程序映射模板参考

这些区域有:Amazon AppSync利用 RDS 解析程序映射模板,开发人员可将 SQL 查询发送到 Amazon Aurora Serverless 数据 API,并获取这些查询的结果。

请求映射模板

RDS 请求映射模板相当简单:

{ "version": "2018-05-29", "statements": [], "variableMap": {} }

下面是 RDS 请求映射模板的 JSON 架构表示形式(解析后):

{ "definitions": {}, "$schema": "https://json-schema.org/draft-07/schema#", "$id": "https://example.com/root.json", "type": "object", "title": "The Root Schema", "required": [ "version", "statements", "variableMap" ], "properties": { "version": { "$id": "#/properties/version", "type": "string", "title": "The Version Schema", "default": "", "examples": [ "2018-05-29" ], "enum": [ "2018-05-29" ], "pattern": "^(.*)$" }, "statements": { "$id": "#/properties/statements", "type": "array", "title": "The Statements Schema", "items": { "$id": "#/properties/statements/items", "type": "string", "title": "The Items Schema", "default": "", "examples": [ "SELECT * from BOOKS" ], "pattern": "^(.*)$" } }, "variableMap": { "$id": "#/properties/variableMap", "type": "object", "title": "The Variablemap Schema" } } }

以下是带静态查询的请求映射模板示例:

{ "version": "2018-05-29", "statements": [ "select title, isbn13 from BOOKS where author = 'Mark Twain'" ] }

Version

版本字段定义模板使用的版本,这一点对于所有请求映射模板都相同。版本字段是必需的。值为 “2018-05-29” 是 Amazon RDS 映射模板唯一支持的版本。

"version": "2018-05-29"

Statements

语句数组是开发人员提供的查询的占位符。目前,每个请求映射模板最多支持两个查询。以下是可能的:

{ "version": "2018-05-29", "statements": [ $util.toJson("insert into BOOKS VALUES ('$ctx.args.newBook.author', '$ctx.args.newBook.title', '$ctx.args.newBook.ISBN13')"), $util.toJson("select * from BOOKS WHERE isbn13 = '$ctx.args.newBook.isbn13'") ] }

Amazon AppSync每个请求映射模板最多支持两个语句。

VariableMap

variableMap 是一个可选字段,它包含可用于使 SQL 语句更短且更易读的别名。例如,以下是可能的:

{ "version": "2018-05-29", "statements": [ "insert into BOOKS VALUES (:AUTHOR, :TITLE, :ISBN13)", "select * from BOOKS WHERE isbn13 = :ISBN13" ], "variableMap": { ":AUTHOR": $util.toJson($ctx.args.newBook.author), ":TITLE": $util.toJson($ctx.args.newBook.title), ":ISBN13": $util.toJson($ctx.args.newBook.isbn13) } }

Amazon AppSync将使用变量映射值构造发送到 Amazon Aurora Serverless 数据 API 的查询。例如,两个查询Amazon AppSync将发送到 Amazon RDS 将是(假设“$ctx.args.newBook.author"='Mark Twain'"$ctx.args.newBook.title"='Adventures of Huckleberry Finn'"$ctx.args.newBook.isbn13"='978-1948132817'):

INSERT INTO BOOKS VALUES ('Mark Twain', 'Adventures of Huckleberry Finn', '978-1948132817');

SELECT from BOOKS where isb13='978-1948132817';