Data Kiosk 用例指南
How to use the Data Kiosk API.
API 版本:2023-11-15
什么是 Data Kiosk API?
用于 Data Kiosk 的销售伙伴 API 允许您提交来自各种架构的 GraphQL 查询,以帮助销售伙伴管理他们的业务。
要点
- Data Kiosk 可以在
getQuery
操作的响应中返回数据文档或错误文档。在查询成功但未返回数据的情况下,它也可以不返回任何文档。- The data document contains the output in JSONL format when a successful request is processed with
DONE
processing status. - 错误文档采用 JSON 格式,包含有关处理状态为
FATAL
的失败请求的错误消息。
- The data document contains the output in JSONL format when a successful request is processed with
- 在
getQuery
操作的响应中,如果DONE
的processingStatus
中没有errorDocumentId
或dataDocumentId
,则表示请求完成时没有数据,或者在给定的日期范围内没有数据。请使用不同的参数重试请求。 - Data Kiosk API 不支持查询排程。
- Data Kiosk API 不支持查询积累订单情况。Data Kiosk 限制每个销售伙伴和每个 GraphQL 查询的并发非终端查询的次数。
教程:使用 GraphQL 查询请求和筛选数据
本教程介绍了如何使用 GraphQL 查询来请求和筛选数据。
先决条件:
要完成本教程,您需要:
- The appropriate role for the data you're requesting. This tutorial uses Sales and Traffic data in its examples, which requires the Brand Analytics role.
步骤 1:创建查询请求
Create an async query request by specifying the query. The query must be a valid GraphQL query which adheres to the schema. The query input parameters can be modified as desired and response fields can be included or excluded in any order.
Call the createQuery
operation, providing the query in the body:
主体参数
名称 | Type | 描述 | 必填项 |
---|---|---|---|
query |
string | The GraphQL query that is compliant with the schema. A query can be at most 8,000 characters after unnecessary whitespace is removed. Refer to the schema to build your query. | 有帮助 |
请求示例
示例 1:为 salesAndTrafficByDate
创建查询请求,以获取响应中字段的子集。
POST https://sellingpartnerapi-na.amazon.com/dataKiosk/2023-11-15/queries
{
"query": "{analytics_salesAndTraffic_2023_11_15{salesAndTrafficByDate(startDate:\"2023-01-01\" endDate:\"2023-10-31\" aggregateBy:DAY marketplaceIds:[\"ATVPDKIKX0DER\"]){endDate marketplaceId startDate traffic{averageOfferCount browserPageViews averageParentItems browserPageViewsB2B browserSessions}sales{averageSalesPerOrderItem{amount currencyCode}averageSalesPerOrderItemB2B{amount currencyCode}averageSellingPrice{amount currencyCode}claimsGranted refundRate unitsOrderedB2B unitsRefunded unitsShipped}}}}"
}
示例 2:为 salesAndTrafficByAsin
创建查询请求,以获取响应中字段的子集。
POST https://sellingpartnerapi-na.amazon.com/dataKiosk/2023-11-15/queries
{
"query": "{analytics_salesAndTraffic_2023_11_15{salesAndTrafficByAsin(startDate:\"2023-01-01\" endDate:\"2023-10-31\" aggregateBy:PARENT marketplaceIds:[\"ATVPDKIKX0DER\"]){childAsin endDate marketplaceId parentAsin sales{orderedProductSales{amount currencyCode}totalOrderItems totalOrderItemsB2B}sku startDate traffic{browserPageViews browserPageViewsB2B browserPageViewsPercentage browserPageViewsPercentageB2B browserSessionPercentage unitSessionPercentageB2B unitSessionPercentage}}}}"
}
响应
成功的响应包括以下属性:
名称 | Type | 描述 |
---|---|---|
queryId |
string | 查询的标识符。仅在与销售伙伴 ID 结合使用时,此标识符才是唯一的。 |
响应示例
{
"queryId": "012345678901"
}
步骤 2:确认查询处理已完成
After you call the createQuery
operation, Amazon receives the request and begins processing the query. Confirm that processing has completed before you continue. To confirm that the query processing is complete, refer to Verify that the query processing is complete.
步骤 3:获取检索查询结果所需的信息
调用 getDocument
操作,以获取检索查询结果文档的内容所需的信息。这包括文档的预签名 URL。如果文档已压缩,Content-Encoding
标头将显示压缩算法。请注意,这与报告 API 提供压缩信息的方式不同。
路径参数
名称 | Type | 描述 | 必填项 |
---|---|---|---|
documentId |
string | 查询结果文档的标识符。仅在与销售伙伴 ID 结合使用时,此标识符才是唯一的。 | 有帮助 |
请求示例
GET https://sellingpartnerapi-na.amazon.com/dataKiosk/2023-11-15/documents/DOC-b8b0-4226-b4b9-0ee058ea5760
响应
成功的响应包括以下内容:
名称 | Type | 描述 |
---|---|---|
documentId |
string | 查询文档的标识符。仅在与销售伙伴 ID 结合使用时,此标识符才是唯一的。 |
documentUrl |
string | 可用于检索 Data Kiosk 文档的预签名 URL。此 URL 将在五分钟后过期。如果 Data Kiosk 文档已压缩,Content-Encoding 标头将显示压缩算法。 |
响应示例
{
"documentId": "DOC-b8b0-4226-b4b9-0ee058ea5760",
"documentUrl": "https://d34o8swod1owfl.cloudfront.net/SampleResult%2BKey%3DSample%2BINITVEC%3D58+fa+bf+a7+08+11+95+0f+c1+a8+c6+e0+d5+6f+ae+c8"
}
保存用于以下步骤的 documentUrl
。
步骤 4:下载文档
您必须使用步骤 3 中返回的信息下载查询文档。
以下示例代码演示了如何下载 Data Kiosk 文档。使用上一个步骤中的 documentUrl
值作为 DownloadExample
类的download
方法的 url
参数的自变数。
注意:您必须始终维护静态加密。鉴于查询结果文档可能包含敏感信息,因此不得在磁盘上存储未加密的查询结果文档内容,即使是暂时存储也是如此。以下示例代码证实了这一原则:
// DownloadExample.java
// This example is for use with the Selling Partner API for Data Kiosk, Version: 2023-11-15
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.squareup.okhttp.ResponseBody;
/**
* Example that downloads a Data Kiosk document.
*/
public class DownloadExample {
public static void main(String[] args) {
String url = "<URL from the getDocument response>";
DownloadExample obj = new DownloadExample();
try {
obj.download(url);
} catch (IOException e) {
// Handle exceptions here.
e.printStackTrace();
}
}
/**
* Download the document from the given Amazon Simple Storage Service (Amazon S3) URL.
*
* @param url the signed Amazon S3 URL to retrieve the document from.
* @throws IOException when there is an error reading the response.
*/
public void download(String url) throws IOException {
OkHttpClient httpclient = new OkHttpClient();
Request request = new Request.Builder()
.url(url)
.get()
.build();
Response response = httpclient.newCall(request).execute();
if (!response.isSuccessful()) {
System.out.printf("Call to download content was unsuccessful with response code: %d and message: %s%n",
response.code(), response.message());
return;
}
try (ResponseBody responseBody = response.body();
InputStream inputStream = responseBody.byteStream();
// Note: If the Data Kiosk document is compressed, the 'Content-Encoding' header will indicate the
// compression algorithm. Most HTTP clients are capable of automatically decompressing downloaded files
// based on the 'Content-Encoding' header.
// More Information: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Encoding
//
// The OkHttp client will automatically set the "Accept-Encoding" header and respect the
// "Content-Encoding" header, so it is not required to unzip the stream.
// For clients which do not automatically decompress, wrapping the stream in a GZIP stream may be
// required, for example:
// InputStream unzippedInputStream = new GZIPInputStream(inputStream);
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
BufferedReader reader = new BufferedReader(inputStreamReader)) {
String line;
while ((line = reader.readLine()) != null) {
// Process line by line.
System.out.println(line);
}
}
}
}
Updated 23 days ago