Collect, Manage and Share All The Answers Your Team Needs: The Enterprise Q&A Platform
Collect, Manage and Share All The Answers Your Team Needs: The Enterprise Q&A Platform
com
#127
CONTENTS INCLUDE:
n
JPA 2.0
By Mike Keith
DZone, Inc.
www.dzone.com
Couchbase API: Ultra -fast, Open Source NoSQL Data Access from Java, Ruby & .NET
#176
Couchbase API:
CONTENTS INCLUDE:
Connection Management
Store Operations
Connecting to Couchbase
Writing and Updating Data
Getting stats from Couchbase
The set method stores a value to the database with a specified key.
client.set(key, expiry, value [, persistTo] ,[ replicateTo])
client.set(key, expiry, value, transcoder)
JAVA
CONNECTION MANAGEMENT
Parameters:
string key
int expiry
Object value
Value to be stored
enum persistTo
enum replicateTo
Transcoder<T>
transcoder
Parameters:
string urls
String username
String password
RETRIEVE OPERATIONS
Parameters:
long TimeValue
enum TimeUnit
TimeUnit.SECONDS, TImeUnit.MINUTES
STORE OPERATIONS
Key
Keys can be any UTF-8 string up to 250 characters long. Metadata for keys
is 125 bytes + length of key and always kept in RAM.
Value
Add Operations
The add method adds a value to the database with the specified key, but
will fail if the key already exists in the database.
client.add(key, expiry, value [, persistTo] ,[ replicateTo] )
client.add(key, expiry, value, transcoder)
Replace Operations
The replace method will replace an existing key with a new value but will
fail if the key does not exist in the database.
client.replace(key, expiry, value [, persistTo] ,[ replicateTo])
client.replace(key, expiry, value, transcoder)
DZone, Inc.
www.dzone.com
Couchbase API
client.getAndTouch(key, expiry)
client.getAndTouch(key, expiry, transcoder)
client.getBulk(keycollection)
client.getBulk(keyn)
client.getBulk(transcoder, keyn)
client.getBulk(keycollection, transcoder)
client.get(key, transcoder)
client.asyncGetLock(key [,getl-expiry])
client.asyncGetLock(key [,getl-expiry], transcoder)
client.getAndLock(key, getl-expiry)
client.getAndLock(key, getl-expiry, transcoder)
client.asyncGets(key)
client.asyncGets(key, transcoder)
client.gets(key)
client.gets(key, transcoder)
client.unlock(key, casunique)
Parameters:
String key
Collection<String> keycollection
String keyn
long casunique
int getl-expiry
Transcoder<T> transcoder
Object value
Value to be stored
enum persistTo
enum replicateTo
Transcoder<T>
transcoder
QUERY OPERATIONS
int expiry
int expiry
With Couchbase Server 2.0, you can add the power of views and querying
those views to your applications.
Create a view object to be used when querying a view :
client.getView(ddocname, viewname)
Parameters:
String ddocname
String viewname
Then create a new query object to be used when querying the view:
Query.new()
Query query = new Query();
Once the view and query objects are available, the results of the server view
can be accessed using:
client.query(view, query)
UPDATE OPERATIONS
Parameters:
View view
Query query
Before accessing the view, a list of options can be set with the query object:
Parameters:
long casunique
String key
int offset
int default
DZone, Inc.
RUBY
CONNECTING TO COUCHBASE
www.dzone.com
Couchbase API
Hash Parameters:
:hostname
:bucket
:password
:nodelist
client = Couchbase.bucket
There is a shared single instance of the Couchbase connection object in the
Couchbase-model gem and can be used once connected, or when settings
are specified in /config/couchbase.yml, which connects on Rails startup.
Multi-Get Operations
val_array = client.get(keys)
val_hash = client.get(keys, :assemble_hash => true)
Add Operations
The add method adds a value to the database with the specified key,
but will fail (Couchbase::Error::KeyExists) if the key already exists in the
database.
client.add(key, value, options = {})
client.add[key, options = {}] = value
Replace Operations
:extended
:quiet
:ttl
[Fixnum, Integer] Get and Touch, :ttl will also reset the
TTL at the same time as the get (seconds)
:lock
:format
:assemble_hash
The replace method will replace an existing key with a new value but will fail
(Couchbase::Error::NotFound) if the key does not exist in the database.
client.replace(key, value, options = {})
client.replace[key, options = {}] = value
Set Operations
The set method stores a value to the database with a specified key,
overwriting existing content if it exists (see add/replace above).
options:
:ttl
:flags
:cas
:format
client.stats(:memory)
Parameters:
stat
To delete keys/values
client.delete(key)
Atomic Counters are only atomic per cluster, but are useful for many
different patterns and numeric data. They are unsigned positive integers.
You cannot use atomic operations on floats or objects or arrays.
READING DATA
The Get method is versatile, allowing for both optimistic and pessimistic
locking, multiple gets, and hash-like syntax.
DZone, Inc.
www.dzone.com
Couchbase API
options:
:startkey_docid
:endkey_docid
:initial
:inclusive_end
:extended
:limit
[Fixnum, Integer] Sets TTL, doesnt alter existing TTL, will only
be applied to a new item created via :create and/or :initial
:skip
:reduce
:group
:group_level
:stale
:body
:on_error
delta
:create
:ttl
Touch Operations
Reset the TTL expiration on one or more keys explicitly with the touch
command, can also be done with a get command.
client.touch(key, ttl)
client.touch({key1=> ttl1, key2 => ttl2})
Durability Requirements
Observe the state of the keys on all the nodes or using :replicated and
:persisted it allows to set up the waiting rule.
client.observe(*keys, options = {})
client.observe_and_wait(*keys, options = {})
Non-JSON Operations
.NET
client.prepend(key, value)
client.append(key, value)
CONNECTING TO COUCHBASE
QUERYING DATA
With Couchbase Server 2.0, you can create Indexes through Map/Reduce
functions. http://www.couchbase.com/docs/couchbase-manual-2.0/
couchbase-views.html
WRITING DATA
Based on the storemode, Store and ExecuteStore may be an Add, Replace
or Set operation. Execute APIs return the operation result.
Refer to the design_doc by name, the Views are an array of view names, as
well as functions defined by name.
Store Operations
object.Store(storemode, key, value)
object.Store(storemode, key, value, validfor)
object.Store(storemode, key, value, expiresat)
ddoc = client.design_docs[design_doc_name]
ddoc.views
#=> [view_name, view_name_2, ]
Use dot notation for accessing the view by your defined view name and
pass in options for query parameters:
ExecuteStore Operations
object.ExecuteStore(storemode,
object.ExecuteStore(storemode,
object.ExecuteStore(storemode,
object.ExecuteStore(storemode,
object.ExecuteStore(storemode,
object.ExecuteStore(storemode,
ReplicateTo)
options:
key,
key,
key,
key,
key,
key,
value)
value,
value,
value,
value,
value,
expiresat)
validfor)
ReplicateTo)
PersistTo)
PersistTo,
:include_docs
:descending
String key
:key
Object value
Value to be stored
StoreMode storemode
Timespan validfor
DateTime expiresat
:startkey
Enum PersistsTo
:endkey
Enum ReplicateTo
:keys
DZone, Inc.
Parameters:
www.dzone.com
Couchbase API
READING DATA
Get Operations
object.Get(key, expiry)
object.ExecuteGet(key, expiry)
object.ExecuteGet(key)
object.ExecuteGet(keyarray)
object.Get(key)
object.Get(keyarray)
object.GetWithCas(key)
Timespan validfor
DateTime expiresat
Object defaultvalue
offset
casunique
QUERYING DATA
Parameters:
String key
object expiry
With Couchbase Server 2.0, you can add views and query those views
using your applications.
Create a view object to be used when querying a view :
GetView(designName, viewName)
Parameters:
UPDATING DATA
String designName
String viewName
object.Append(key, value)
object.Append(key, casvalue, value)
object.ExecuteAppend(key, value)
object.ExecuteAppend(key, casvalue, value)
object.Cas(storemode, key, value)
object.Cas(storemode, key, value, casunique)
object.Cas(storemode, key, value, validfor, casunique)
object.ExecuteCas(storemode, key, value)
object.ExecuteCas(storemode, key, value, casunique)
object.ExecuteCas(storemode, key, value, expiresat, casunique)
object.ExecuteCas(storemode, key, value, validfor, casunique)
object.Decrement(key, defaultvalue, offset)
object.Decrement(key, defaultvalue, offset, casunique)
object.Decrement(key, defaultvalue, offset, expiresat, casunique)
object.Decrement(key, defaultvalue, offset, validfor, casunique)
object.Decrement(key, defaultvalue, offset, expiresat)
object.Decrement(key, defaultvalue, offset, validfor)
object.Decrement(key, defaultvalue, offset)
object.Decrement(key, defaultvalue, offset, casunique)
object.Decrement(key, defaultvalue, offset, validfor, casunique)
object.Decrement(key, defaultvalue, offset, expiresat)
object.Decrement(key, defaultvalue, offset, validfor)
object.ExecuteDecrement(key, defaultvalue, offset)
object.ExecuteDecrement(key, defaultvalue, offset, casunique)
object.ExecuteDecrement(key, defaultvalue, offset, expiresat,
casunique)
object.ExecuteDecrement(key, defaultvalue, offset, validfor,
casunique)
object.ExecuteDecrement(key, defaultvalue, offset, expiresat)
object.ExecuteDecrement(key, defaultvalue, offset, validfor)
object.ExecuteRemove(key)
object.Remove(key)
object.ExecuteIncrement(key, defaultvalue, offset)
object.ExecuteIncrement(key, defaultvalue, offset, casunique)
object.ExecuteIncrement(key, defaultvalue, offset, expiresat,
casunique)
object.ExecuteIncrement(key, defaultvalue, offset, validfor,
casunique)
object.ExecuteIncrement(key, defaultvalue, offset, expiresat)
object.ExecuteIncrement(key, defaultvalue, offset, validfor)
object.Increment(key, defaultvalue, offset)
object.Increment(key, defaultvalue, offset, casunique)
object.Increment(key, defaultvalue, offset, expiresat, casunique)
object.Increment(key, defaultvalue, offset, validfor, casunique)
object.Increment(key, defaultvalue, offset, expiresat)
object.Increment(key, defaultvalue, offset, validfor)
object.ExecutePrepend(key, value)
object.ExecutePrepend(key, casunique, value)
object.Prepend(key, value)
object.Prepend(key, casunique, value)
object.Touch(key, expiry)
If you iterate over a strongly typed view, each item is of the type you
specified. If you use the non-generic version, each item you enumerate over
will be of type IViewRow. IViewRow provides methods for accessing details
of the row that are not present when using strongly typed views.
To get the original document from Couchbase:
row.GetItem()
Parameters:
GetView(designName, viewName).Group(true)
String key
Object value
Value to be stored
StoreMode storemode
DZone, Inc.
www.dzone.com
Couchbase API
To limit the number of results to and order the results in descending order:
GetView(designName, viewName).Limit(5).Descending(true)
Finally, add a reference to the Couchbase .Net Client Library in your project.
Enjoy building your application using Couchbase Server!
Useful Links
To create a new MVC project using Couchbase Server, select File -> New
Project and then select Web -> ASP.Net MVC4 application under the Visual
C# project templates. Give your project a name and click OK to create the
solution.
RECOMMENDED BOOK
Here
Free PDF
C++
Sencha Touch
Clean Code
Git for the Enterprise
DZone, Inc.
150 Preston Executive Dr.
Suite 201
Cary, NC 27513
Copyright 2013 DZone, Inc. All rights reserved. No part of this publication may be reproduced, stored in a retrieval
system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior
written permission of the publisher.
888.678.0399
919.678.0300
Refcardz Feedback Welcome
$7.95
[email protected]
Sponsorship Opportunities
[email protected]
Version 1.0