Couchbase Client Java

来源:互联网 发布:四平软件 怎么样 编辑:程序博客网 时间:2024/05/22 16:04

javac -cp couchbase-client-1.0.3.jar:spymemcached-2.8.2.jar Main.java
java -cp .:couchbase-client-1.0.3.jar:spymemcached-2.8.2.jar:jettison-1.1.jar:netty-3.2.0.Final.jar:commons-codec-1.5.jar Main

 

Java客户端会自动判断数据大小,决定是否启用客户端压缩。跨语言使用可能导致Java存的数据不能被C客户端读取,因此需要关闭客户端压缩。

关闭客户端压缩

            SerializingTranscoder st = (SerializingTranscoder)client.getTranscoder();

            st.setCompressionThreshold(Integer.MAX_VALUE);

import java.net.URI;import java.util.LinkedList;import java.util.List;import java.util.concurrent.TimeUnit;import com.couchbase.client.CouchbaseClient;import net.spy.memcached.internal.GetFuture;import net.spy.memcached.internal.OperationFuture;public class Main {  public static final int EXP_TIME = 10;  public static final String KEY = "spoon";  public static final String VALUE = "Hello World!";  public static void main(String args[]) {    // Set the URIs and get a client    List<URI> uris = new LinkedList<URI>();    Boolean do_delete = false; //     // Connect to localhost or to the appropriate URI    uris.add(URI.create("http://10.10.135.40:8091/pools"));    CouchbaseClient client = null;    try {      client = new CouchbaseClient(uris, "default", "");    } catch (Exception e) {      System.err.println("Error connecting to Couchbase: "        + e.getMessage());      System.exit(0);    }    // Do an asynchronous set    OperationFuture<Boolean> setOp = client.set(KEY, EXP_TIME, VALUE);    // Do a synchrononous get    Object getObject = client.get(KEY);    // Do an asynchronous get    GetFuture getOp = client.asyncGet(KEY);    // Do an asynchronous delete    OperationFuture<Boolean> delOp = null;    if (do_delete) {      delOp = client.delete(KEY);    }    // Shutdown the client    // client.shutdown(3, TimeUnit.SECONDS);    // Now we want to see what happened with our data    // Check to see if our set succeeded    try {      if (setOp.get().booleanValue()) {        System.out.println("Set Succeeded");      } else {        System.err.println("Set failed: "            + setOp.getStatus().getMessage());      }    } catch (Exception e) {      System.err.println("Exception while doing set: "          + e.getMessage());    }    // Print the value from synchronous get    if (getObject != null) {      System.out.println("Synchronous Get Suceeded: "          + (String) getObject);    } else {      System.err.println("Synchronous Get failed");    }    // Check to see if ayncGet succeeded    try {      if ((getObject = getOp.get()) != null) {        System.out.println("Asynchronous Get Succeeded: "            + getObject);      } else {        System.err.println("Asynchronous Get failed: "            + getOp.getStatus().getMessage());      }    } catch (Exception e) {      System.err.println("Exception while doing Aynchronous Get: "          + e.getMessage());    }    long startTime = System.currentTimeMillis();    int count = 10000;    for(int i = 0; i < count; i++) {        Object getObjectEx = client.get(KEY);        if(getObject == null)            System.err.println("Synchronous Get failed");    }    long endTime = System.currentTimeMillis();    System.out.println("QPS:" + count * 1000 / (endTime - startTime) );    client.shutdown(3, TimeUnit.SECONDS);    // Check to see if our delete succeeded    if (do_delete) {      try {        if (delOp.get().booleanValue()) {          System.out.println("Delete Succeeded");        } else {          System.err.println("Delete failed: " +               delOp.getStatus().getMessage());        }      } catch (Exception e) {        System.err.println("Exception while doing delete: "            + e.getMessage());      }    }  }}


原创粉丝点击