简体   繁体   English

Java + Membase + spymemcached 批量操作

[英]Java + Membase + spymemcached bulk operations

I am currently writing an application that requires bulk operations on a key/value store, at this time I am using membase.我目前正在编写一个需要对键/值存储进行批量操作的应用程序,此时我正在使用 membase。

spymemcached allows bulk get, but not bulk CAS or add; spymemcached 允许批量获取,但不允许批量 CAS 或添加; 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. “客户端”是单个 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? Future.get() 的阻塞调用似乎效率很低,有没有更好的方法? 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谢谢,马库斯

I'll try to address as many things here as possible.我将尝试在这里尽可能多地解决问题。

Spymemcached allows bulk get, but not bulk CAS or add; Spymemcached 允许批量获取,但不允许批量 CAS 或添加; 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.我完全同意这里,但我们可能不会 go 通过向 MemcachedClient 添加 bulkAdd、bulkCas、... Spymemcached currently, by default optimizes get and set operations by sending them as bulk sets or bulk gets whenever it can.目前,Spymemcached 默认优化 get 和 set 操作,尽可能将它们作为批量集或批量获取发送。 There are plans to extend this to all memcached operations, but it hasn't been done yet.有计划将此扩展到所有 memcached 操作,但尚未完成。 When it is though it will happen behind the scenes and probably won't be directly available to spymemcached users.虽然它会在幕后发生,并且可能不会直接提供给 spymemcached 用户。

The blocking call on Future.get() seems highly inefficient? Future.get() 上的阻塞调用似乎效率很低?

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.对于单独的添加操作,您的代码可能只会阻塞第一个操作,因为在那之后所有其他操作都已经完成,因此在第一次调用之后对 Future.get() 的调用将立即返回。

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.您可以扩展 MemcachedClient class 并复制现有函数之一并将您自己的代码添加到回调 function。

Also, are the following operations possible (or planning to be implemented)?此外,以下操作是否可能(或计划实施)?

Spymemcached implements all of the functionality that memcached offers. Spymemcached 实现了 memcached 提供的所有功能。 The operations you mention can already be build with the the functionality Spymemcached has.您提到的操作已经可以使用 Spymemcached 的功能构建。

-Add or return existing value - 添加或返回现有值

add keyA.添加密钥A。 If it fails get keyA.如果失败则获取keyA。

-Delete if value equals a known value - 如果值等于已知值,则删除

get keyA.获取密钥A。 If keyA equals a known value, delete keyA如果 keyA 等于已知值,则删除 keyA

-Set if value equals a known value - 如果值等于已知值,则设置

get keyA.获取密钥A。 If keyA equals a known value, set keyB.如果 keyA 等于已知值,则设置 keyB。

If you are looking to get new options added to memcached then you should post your requests on memcached.org.如果您希望将新选项添加到 memcached,那么您应该在 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.老实说,memcached 的人喜欢让 api 尽可能小,所以我认为这些可能不会被添加。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM