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

查看:89
本文介绍了宁静的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.

例如,如果我对myapi.co.uk/messages/使用GET方法,这将为所有消息记录恢复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进行分页的方法.这样的请求将导致从21日到30日从消息集合中获取10个资源.这样,您就可以要求海量数据集中的特定部分:

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.

我强烈建议您通过关于RESTful API设计的演示文稿. com/> apigee (截屏视频称为教狗REST").在那里讨论了解决日常问题的良好做法和精巧想法.

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

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

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