Restful API - 处理大量数据 [英] Restful API - handling large amounts of data

查看:94
本文介绍了Restful API - 处理大量数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了自己的 Restful API,并且想知道处理从 API 返回的大量记录的最佳方法.

I have written my own Restful API and am wondering about the best way to deal with large amounts of records returned from the API.

例如,如果我使用 GET 方法到 myapi.co.uk/messages/ 这将带回所有消息记录的 XML,这在某些情况下可能是 1000 的.这使得使用 API 非常缓慢.

For example, if I use GET method to myapi.co.uk/messages/ this will bring back the XML for all message records, which in some cases could be 1000's. This makes using the API very sluggish.

谁能建议最好的处理方法?批量返回结果并在请求中指定批量大小是否标准?

Can anyone suggest the best way of dealing with this? Is it standard to return results in batches and to specify batch size in the request?

推荐答案

您可以更改 API 以包含其他参数以限制应用程序返回的数据范围.

You can change your API to include additional parameters to limit the scope of data returned by your application.

例如,您可以添加 limitoffset 参数来获取一小部分.这就是按照 REST 进行分页的方式.像这样的请求将导致从消息集合中获取 10 个资源,从 21 日到 30 日.通过这种方式,您可以请求获取庞大数据集的特定部分:

For instance, you could add limit and offset parameters to fetch just a little part. This is how pagination can be done in accordance with REST. A request like this would result in fetching 10 resources from the messages collection, from 21st to 30th. This way you can ask for a specific portion of a huge data set:

myapi.co.uk/messages?limit=10&offset=20 

减少有效负载的另一种方法是仅请求资源表示的某些部分.以下是 Facebook 的做法:

Another way to decrease the payload would be to only ask for certain parts of your resources' representation. Here's how facebook does it:

/joe.smith/friends?fields=id,name,picture

请记住,在使用这两种方法中的任何一种时,您必须为客户端提供一种发现每个资源的方法.您不能假设他们只会查看参数并开始更改它们以搜索数据.这将违反 REST 范式.为他们提供必要的超链接以避免它.

Remember that while using either of these methods, you have to provide a way for the client to discover each of the resources. You can't assume they'll just look at the parameters and start changing them in search of data. That would be a violation of the REST paradigm. Provide them with the necessary hyperlinks to avoid it.

我强烈建议您通过 apigee(截屏视频称为教一只狗休息").那里讨论了解决日常问题的良好做法和巧妙想法.

I strongly recommend viewing this presentation on RESTful API design by apigee (the screencast is called "Teach a Dog to REST"). Good practices and neat ideas to approach everyday problems are discussed there.

自我发布此答案以来,视频已更新多次,您可以查看 2013 年 1 月的第三版

The video has been updated a number of times since I posted this answer, you can check out the 3rd edition from January 2013

这篇关于Restful API - 处理大量数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆