Couchbase Client Java

发布时间:2017-6-29 9:48:39编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"Couchbase Client Java",主要涉及到Couchbase Client Java方面的内容,对于Couchbase Client Java感兴趣的同学可以参考一下。

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());
      }
    }
  }
}




上一篇:fastjson autotype is not support
下一篇:Filebeat日志收集器 安装和配置

相关文章

相关评论

本站评论功能暂时取消,后续此功能例行通知。

一、不得利用本站危害国家安全、泄露国家秘密,不得侵犯国家社会集体的和公民的合法权益,不得利用本站制作、复制和传播不法有害信息!

二、互相尊重,对自己的言论和行为负责。

好贷网好贷款