Java +会员数据库spymemcached 批量操作
我目前正在编写一个需要对键/值存储进行批量操作的应用程序,此时我正在使用 membase。
pymemcached 允许批量获取,但不允许批量 CAS 或添加;我认为如果实施的话将会被广泛使用的功能。
此时我的一组批量操作的代码大致如下所示。
“客户端”是单个 MemcachedClient。
ArrayList<Future<Boolean>> futures = new ArrayList<Future<Boolean>>();
ArrayList<String> bulkGet = new ArrayList<String>();
for(int i=0; i<50; i++){
String key = "Key_" + x + "_" + i;
Future<Boolean> fut = client.add(key, 0, "Value_" + x + "_" + i);
futures.add(fut);
bulkGet.add(key);
}
int count = 0;
for(Future<Boolean> fut : futures){
if(fut.get()==true){
count++;
}
}
System.out.println("Added " + count + " records.");
Map<String,Object> bulkGot = client.getBulk(bulkGet);
System.out.println("Retrieved " + bulkGot.size() + " records");
对 Future.get() 的阻塞调用似乎效率很低,有更好的方法吗?在我的实际场景中,我希望能够在期货返回后立即处理它们(可能会或可能不会按照发送的顺序?)。
另外,以下操作是否可行(或计划实施)?
- 添加或返回现有值
- 如果值等于已知值则删除
- 如果值等于已知值则设置
谢谢, 马库斯
I am currently writing an application that requires bulk operations on a key/value store, at this time I am using membase.
spymemcached allows bulk get, but not bulk CAS or add; features that I think would be widely used if implemented.
At this point my code for a set of bulk operations is roughly as shown below.
"client" is a single MemcachedClient.
ArrayList<Future<Boolean>> futures = new ArrayList<Future<Boolean>>();
ArrayList<String> bulkGet = new ArrayList<String>();
for(int i=0; i<50; i++){
String key = "Key_" + x + "_" + i;
Future<Boolean> fut = client.add(key, 0, "Value_" + x + "_" + i);
futures.add(fut);
bulkGet.add(key);
}
int count = 0;
for(Future<Boolean> fut : futures){
if(fut.get()==true){
count++;
}
}
System.out.println("Added " + count + " records.");
Map<String,Object> bulkGot = client.getBulk(bulkGet);
System.out.println("Retrieved " + bulkGot.size() + " records");
The blocking call on Future.get() seems highly inefficient, is there a better way? In my actual scenario I'd like the ability to handle the futures as soon as they return (which may or may not be in the order in which they were sent?).
Also, are the following operations possible (or planning to be implemented)?
-Add or return existing value
-Delete if value equals a known value
-Set if value equals a known value
Thanks,
Marcus
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将尽力在这里解决尽可能多的问题。
Spymemcached 允许批量获取,但不允许批量 CAS 或添加;我认为如果实施的话将会被广泛使用的功能。
我完全同意这一点,但我们可能不会通过向 MemcachedClient 添加bulkAdd、bulkCas、...函数来解决这个问题。目前,Spymemcached 默认情况下通过尽可能以批量集或批量获取的方式发送它们来优化获取和设置操作。有计划将其扩展到所有 memcached 操作,但尚未完成。当它出现时,它会在幕后发生,并且可能不会直接提供给spymemcached 用户。
对 Future.get() 的阻塞调用看起来效率很低?
是的,我将在下面解决这个问题,但还要注意,如果这是一个批量操作,那么您的代码仍然会阻塞一点。对于单独的添加操作,您的代码可能只会阻塞第一个操作,因为之后所有其他操作都已经完成,因此在第一次调用之后对 Future.get() 的调用将立即返回。
那么有没有更好的办法呢?
您可以扩展 MemcachedClient 类并复制现有函数之一并将您自己的代码添加到回调函数中。
另外,以下操作是否可行(或计划实施)?
Spymemcached 实现了 memcached 提供的所有功能。您提到的操作已经可以使用 Spymemcached 的功能来构建。
-添加或返回现有值
添加keyA。如果失败获取keyA。
- 如果值等于已知值
获取 keyA,则删除。如果 keyA 等于已知值,则删除 keyA
-如果 value 等于已知值,则
设置获取 keyA。如果 keyA 等于已知值,则设置 keyB。
如果您希望将新选项添加到 memcached,那么您应该在 memcached.org 上发布您的请求。不过说实话,memcached 的开发人员喜欢让 api 尽可能小,所以我认为这些不太可能会被添加。
I'll try to address as many things here as possible.
Spymemcached allows bulk get, but not bulk CAS or add; features that I think would be widely used if implemented.
I completely agree here, but we probably wouldn't go about this by adding bulkAdd, bulkCas, ... functions to MemcachedClient. Spymemcached currently, by default optimizes get and set operations by sending them as bulk sets or bulk gets whenever it can. There are plans to extend this to all memcached operations, but it hasn't been done yet. When it is though it will happen behind the scenes and probably won't be directly available to spymemcached users.
The blocking call on Future.get() seems highly inefficient?
Yes, which I will address below, but also note that if this was a bulk operation then your code would still block for a little bit. With individual add operations you code would probably only block for the first operation because after that all of the other operations will have already finished so calls to Future.get() after the first call would return immediately.
So is there a better way?
You can extend the MemcachedClient class and copy one of the existing functions and add your own code to the callback function.
Also, are the following operations possible (or planning to be implemented)?
Spymemcached implements all of the functionality that memcached offers. The operations you mention can already be build with the the functionality Spymemcached has.
-Add or return existing value
add keyA. If it fails get keyA.
-Delete if value equals a known value
get keyA. If keyA equals a known value, delete keyA
-Set if value equals a known value
get keyA. If keyA equals a known value, set keyB.
If you are looking to get new options added to memcached then you should post your requests on memcached.org. To be honest though, the memcached guys like to keep the api as small as possible so I don't think it is likely these will get addded.