本文整理汇总了Java中com.couchbase.client.CouchbaseClient类的典型用法代码示例。如果您正苦于以下问题:Java CouchbaseClient类的具体用法?Java CouchbaseClient怎么用?Java CouchbaseClient使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
CouchbaseClient类属于com.couchbase.client包,在下文中一共展示了CouchbaseClient类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: connect
import com.couchbase.client.CouchbaseClient; //导入依赖的package包/类
@Override
public void connect() throws IOException
{
String[] tokens = uriString.split(",");
URI uri = null;
for (String url : tokens) {
try {
uri = new URI("http", url, "/pools", null, null);
} catch (URISyntaxException ex) {
DTThrowable.rethrow(ex);
}
baseURIs.add(uri);
}
try {
CouchbaseConnectionFactoryBuilder cfb = new CouchbaseConnectionFactoryBuilder();
cfb.setOpTimeout(timeout); // wait up to 10 seconds for an operation to succeed
cfb.setOpQueueMaxBlockTime(blockTime); // wait up to 10 second when trying to enqueue an operation
client = new CouchbaseClient(cfb.buildCouchbaseConnection(baseURIs, bucket, password));
//client = new CouchbaseClient(baseURIs, "default", "");
} catch (IOException e) {
logger.error("Error connecting to Couchbase:", e);
DTThrowable.rethrow(e);
}
}
示例2: test
import com.couchbase.client.CouchbaseClient; //导入依赖的package包/类
@Test
public void test()
{
URI uri = null;
ArrayList<URI> nodes = new ArrayList<URI>();
// Add one or more nodes of your cluster (exchange the IP with yours)
nodes.add(URI.create("http://localhost:8091/pools"));
// Try to connect to the client
try {
client = new CouchbaseClient(nodes, "default", "");
} catch (Exception e) {
throw new RuntimeException(e);
}
TestPojo obj = new TestPojo();
obj.setName("test");
obj.setPhone(123344555);
HashMap<String, Integer> map = new HashMap<String, Integer>();
map.put("test", 12345);
obj.setMap(map);
future = processKeyValue("key", obj);
future.addListener(listener);
}
示例3: initDB
import com.couchbase.client.CouchbaseClient; //导入依赖的package包/类
@PostConstruct
private void initDB() {
try {
// Get an instance of Couchbase
List<URI> hosts = Arrays.asList(
new URI("http://localhost:8091/pools")
);
// Get an instance of Couchbase
// Name of the Bucket to connect to
String bucket = "default";
// Password of the bucket (empty) string if none
String password = "";
// Connect to the Cluster
client = new CouchbaseClient(hosts, bucket, password);
} catch (URISyntaxException | IOException ex) {
Logger.getLogger(PersonSessionBean.class.getName()).log(Level.SEVERE, null, ex);
}
}
示例4: CouchbaseClientFactory
import com.couchbase.client.CouchbaseClient; //导入依赖的package包/类
/**
* @param nodes
* @param bucketName
* @param password
* @param timeoutMillis
*/
public CouchbaseClientFactory(String nodes, String bucketName, String password, Integer timeoutMillis) {
info = "CouchbaseClientFactory{" + nodes + ", " + bucketName + ", " + password + "}";
log.info(info);
ArrayList<URI> nodesList = new ArrayList<URI>();
for (String node : nodes.split(",")) {
nodesList.add(URI.create("http://" + node + ":8091/pools"));
}
CouchbaseConnectionFactoryBuilder builder = new CouchbaseConnectionFactoryBuilder();
builder.setOpTimeout(timeoutMillis);//2.5 by default
// builder.setObsTimeout(timeoutMillis);
try {
client = new CouchbaseClient(builder.buildCouchbaseConnection(nodesList, bucketName, password));
} catch (Exception e) {
log.error("Error connecting to Couchbase: ", e);
throw new RuntimeException(e);
}
}
示例5: createClient
import com.couchbase.client.CouchbaseClient; //导入依赖的package包/类
/**
* Method to instance the couchbase client that will interact with the server
*/
protected void createClient() {
try {
CouchbaseConnectionFactoryBuilder builder = new CouchbaseConnectionFactoryBuilder();
builder.setOpTimeout(operationTimeout);
builder.setViewTimeout(viewTimeout);
builder.setObsTimeout(observerTimeout);
builder.setViewConnsPerNode(viewConnsPerNode);
// normally the worker size should be equal to the number of processors
// org.apache.http.impl.nio.reactor.IOReactorConfig.AVAIL_PROCS
builder.setViewWorkerSize(Runtime.getRuntime().availableProcessors());
client = new CouchbaseClient(builder.buildCouchbaseConnection(bootstrapUris(), bucketName, bucketPassword));
} catch (Exception e) {
logger.error("Failed to connect to couchbase server", e);
throw new RuntimeException(e);
}
}
示例6: setup
import com.couchbase.client.CouchbaseClient; //导入依赖的package包/类
@Override
protected void setup(Context context) throws IOException, InterruptedException {
Configuration conf = context.getConfiguration();
ExportArgs args;
try {
args = new ExportArgs(conf);
} catch (ArgsException e) {
throw new IllegalArgumentException(e);
}
// Create and configure queue.
int queueSize = conf.getInt(PROPERTY_QUEUE_SIZE, 4096);
bulkSize = conf.getInt(PROPERTY_BULK_SIZE, 1024);
queue = new LinkedBlockingQueue<HadoopInput<T>>(queueSize);
LOGGER.info("Connecting to Couchbase...");
couchbaseClient = new CouchbaseClient(args.getUrls(), args.getBucket(), args.getPassword());
LOGGER.info("Connected to Couchbase.");
// Start the consumer thread.
LOGGER.info("Starting consumer thread...");
consumer = new Consumer(context);
consumer.start();
LOGGER.info("Consumer thread started.");
}
示例7: getObject
import com.couchbase.client.CouchbaseClient; //导入依赖的package包/类
@Override
public Object getObject() throws IOException {
List<URI> addresses = getAddresses(connectionURI);
long timeout = Math.max(readTimeout, writeTimeout);
//TODO make all the below properties configurable via properties file
ConnectionFactoryBuilder builder = new CouchbaseConnectionFactoryBuilder()
.setOpTimeout(timeout) // wait up to timeout seconds for an operation to succeed
.setOpQueueMaxBlockTime(enqueueTimeout) // wait up to 'enqueueTimeout' seconds when trying to enqueue an operation
.setDaemon(true)
.setProtocol(Protocol.BINARY)
.setHashAlg(DefaultHashAlgorithm.KETAMA_HASH)
.setFailureMode(FailureMode.Redistribute)
.setInitialObservers(Collections.singleton((ConnectionObserver) new CouchbaseAlerter()));
if(transcoder!=null) {
builder.setTranscoder(transcoder);
}
//assuming there isn't any password set for Couchbase
CouchbaseConnectionFactory connectionFactory
= ((CouchbaseConnectionFactoryBuilder)builder).buildCouchbaseConnection(addresses, "default", "", "");
return new CouchbaseClient(connectionFactory);
}
示例8: getRowData
import com.couchbase.client.CouchbaseClient; //导入依赖的package包/类
@Override
public Object getRowData() {
try {
if (!cached_.contains(getRowIndex())) {
int min = getRowIndex() / PAGE_SIZE * PAGE_SIZE;
CouchbaseClient client = getClient();
View view = getView();
Query query = new Query();
query.setIncludeDocs(true);
query.setSkip(min);
query.setLimit(PAGE_SIZE);
ViewResponse result = client.query(view, query);
int i = min;
for (ViewRow row : result) {
cache_.put(i, new CouchbaseViewEntry(row));
cached_.add(i);
i++;
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return cache_.get(this.getRowIndex());
}
示例9: insert
import com.couchbase.client.CouchbaseClient; //导入依赖的package包/类
/**
* 新增JSON结构数据.
*
* @param objectToSave
* 要保存的对象, 需从({@link BucketEntity}继承.
* @throws BucketException
*/
public final void insert(final BucketEntity objectToSave)
throws BucketException {
BucketManager.ensureNotIterable(objectToSave);
final String bucketName = this.bucketManager.getBucketByEntity(objectToSave
.getClass());
callCountIncr(bucketName, "insert");
final CouchbaseClient client = this.bucketManager.get(bucketName);
execute(new BucketCallback<Boolean>() {
@Override
public Boolean doInBucket() throws InterruptedException,
ExecutionException {
String json = JsonUtil.toJson(objectToSave);
if (logger.isDebugEnabled()){
logger.debug("insert, bucket:{}, data:{}", bucketName, json);
}
return client.add(objectToSave.getCouchbaseKey(), 0, json).get();
}
});
}
示例10: _save
import com.couchbase.client.CouchbaseClient; //导入依赖的package包/类
/**
* 更新JSON结构数据.
*
* @param objectToSave
* 要保存的对象数组, 每个对象需从({@link BucketEntity}继承.
* @param deletedExpired
* 若对象是删除状态的话,是否物理删除数据, deletedExpired=true则物理删除.
* @throws BucketException
*/
private String _save(final BucketEntity objectToSave,
final Boolean deletedExpired) throws BucketException {
BucketManager.ensureNotIterable(objectToSave);
String bucketName = this.bucketManager.getBucketByEntity(objectToSave
.getClass());
final CouchbaseClient client = this.bucketManager.get(bucketName);
execute(new BucketCallback<Boolean>() {
@Override
public Boolean doInBucket() throws InterruptedException,
ExecutionException {
if (deletedExpired && objectToSave.isDeleted()) {
client.touch(objectToSave.getCouchbaseKey(), BucketManager.EXP_DELETED);
return true;
} else {
String json = JsonUtil.toJson(objectToSave);
return client.set(objectToSave.getCouchbaseKey(), 0, json).get();
}
}
});
return bucketName;
}
示例11: update
import com.couchbase.client.CouchbaseClient; //导入依赖的package包/类
/**
* 替换记录
*
* @param objectToSave
* 要替换的对象, 每个对象需从({@link BucketEntity}继承.
* @throws BucketException
*/
public void update(final BucketEntity objectToSave) throws BucketException {
BucketManager.ensureNotIterable(objectToSave);
String bucketName = this.bucketManager.getBucketByEntity(objectToSave
.getClass());
final CouchbaseClient client = this.bucketManager.get(bucketName);
execute(new BucketCallback<Boolean>() {
@Override
public Boolean doInBucket() throws InterruptedException,
ExecutionException {
String json = JsonUtil.toJson(objectToSave);
return client.replace(objectToSave.getCouchbaseKey(), 0, json).get();
}
});
callCountIncr(bucketName, "update");
}
示例12: erase
import com.couchbase.client.CouchbaseClient; //导入依赖的package包/类
/**
* 物理删除记录.
*
* @param objectToRemove
* 要删除的对象, 每个对象需从({@link BucketEntity}继承.
* @throws BucketException
*/
public void erase(final BucketEntity objectToRemove) throws BucketException {
BucketManager.ensureNotIterable(objectToRemove);
String bucketName = this.bucketManager.getBucketByEntity(objectToRemove
.getClass());
final CouchbaseClient client = this.bucketManager.get(bucketName);
execute(new BucketCallback<OperationFuture<Boolean>>() {
@Override
public OperationFuture<Boolean> doInBucket() {
return client.delete(objectToRemove.getCouchbaseKey());
}
});
callCountIncr(bucketName, "erase");
}
示例13: findById
import com.couchbase.client.CouchbaseClient; //导入依赖的package包/类
/**
* 按主键查询记录.
*
* @param entityClass
* 记录对应的实体类, 需要继承{@link BucketEntity}
* @param id
* 主键id
* @return 返回对象<T>
* @throws BucketException
*/
public <T> T findById(final Class<T> entityClass, final Long id)
throws BucketException {
String bucketName = this.bucketManager.getBucketByEntity(entityClass);
callCountIncr(bucketName, "findById");
final CouchbaseClient client = this.bucketManager.get(bucketName);
final String key = BucketManager.getCKeyByEntity(entityClass, id);
return execute(new BucketCallback<T>() {
@Override
public T doInBucket() {
Object temp = client.get(key);
if (temp == null) {
return null;
}
return JsonUtil.asT(entityClass, (String) temp);
}
});
}
示例14: queryView
import com.couchbase.client.CouchbaseClient; //导入依赖的package包/类
/**
* 查询视图.
*
* @param clzz
* 记录对应的实体类, 需要继承{@link BucketEntity}
* @param viewName
* 视图名称.
* @param query
* 查询语句{@link Query}
* @return JSON结果值{@link ViewResponse}
* @throws BucketException
*/
public ViewResponse queryView(Class<?> clzz, final String viewName,
final Query query) throws BucketException {
final String designName = clzz.getSimpleName();
String bucketName = this.bucketManager.getBucketByEntity(clzz);
callCountIncr(bucketName+":"+viewName, "queryView");
final Timer.Context context = MetricCollectorImpl.current().getTimer(this.getClass(), bucketName+":"+viewName+":queryView");
try {
final CouchbaseClient client = this.bucketManager.get(bucketName);
return execute(new BucketCallback<ViewResponse>() {
@Override
public ViewResponse doInBucket() {
final View view = client.getView(designName, viewName);
return client.query(view, query);
}
});
} finally {
context.stop();
}
}
示例15: connect
import com.couchbase.client.CouchbaseClient; //导入依赖的package包/类
@Override
public void connect() throws IOException
{
super.connect();
logger.debug("connection established");
try {
CouchbaseConnectionFactoryBuilder cfb = new CouchbaseConnectionFactoryBuilder();
cfb.setOpTimeout(timeout); // wait up to 10 seconds for an operation to succeed
cfb.setOpQueueMaxBlockTime(blockTime); // wait up to 10 second when trying to enqueue an operation
clientMeta = new CouchbaseClient(cfb.buildCouchbaseConnection(baseURIs, bucketMeta, passwordMeta));
} catch (IOException e) {
logger.error("Error connecting to Couchbase: ", e);
DTThrowable.rethrow(e);
}
}