Introduction to
Bharat Singh
Trainee Software Consultant
Knoldus Software LLP.
Agenda
●
What is Couchbase
●
Why NoSQL ?
●
Features of Couchbase
●
Views in Couchbase
●
Basic CURD operations with fixed key
What is Couchbase
Couchbase Server is a high-performance NoSQL
distributed database with a flexible data model. It
scales on commodity hardware to support large
data sets with a high number of concurrent reads
and writes while maintaining low latency and
strong consistency.
Why NoSQL ?
– With relational technologies, many application developers find it difficult, or
even impossible, to get the dynamic scalability and level of scale they need
while also maintaining the performance users demand.
– Data is becoming easier to capture and access through third parties such as
Facebook. It’s not surprising that developers find increasing value in leveraging
this data to enrich existing applications and create new apps.
– Developers want a highly flexible solution that easily accommodates any new
type of data they choose to work with, and isn’t disrupted by content structure
changes from third-party data providers. Much of the new data is unstructured
and semi-structured, so developers also need a database that can efficiently
store it.
– NoSQL databases are built from the ground up to be distributed, scale-out
technologies and are therefore a better fit with the highly distributed nature of
the three-tier internet architecture.
Features of Couchbase
Multi-Dimensional Scaling
●
Couchbase has redefined the way enterprises scale distributed databases with the
option of Multi-Dimensional Scaling. It separates, isolates, and scales individual
services – query, index, and data – to improve application performance and increase
resource utilization. Couchbase Server 4.0 is the first and only distributed database
capable of scaling with the speed and precision required by enterprise applications
with variable workloads.
●
Multi-Dimensional Scaling enables enterprises to optimize hardware by allocating
resources based on the workload of a specific service, and to avoid resource
contention by performing queries, maintaining indexes, and writing data with different
nodes. It is inefficient to require participation from every node to perform a query or
maintain an index. Couchbase Server 4.0 solves this problem by scaling data
independent of queries and indexes.
Multidimensional scaling
Features of Couchbase
N1QL (read as 'Nickel')
●
N1QL is the first query language to leverage the complete flexibility of JSON and
the full power of SQL. While JSON benefits from SQL because it enables developers
to model and query data with relationships, SQL benefits from JSON because it
removes the “impedance mismatch” between the data model and the application model.
In fact, the data model is no longer limited to "single table" or "table per query"
impementations - related data can be embedded or referenced and the same data can be
queried in different ways.
●
N1QL enables developers to transition to a NoSQL database by leveraging existing
SQL and data modeling knowledge. It is now possible to create a data model that
combines both nested and related data while avoiding the need to create and maintain
complex query logic – for example, multiple queries are no longer required to get
related data. N1QL enables developers to buid applications that require ad-hoc access
to data with greater precision and intelligence.
Features of Couchbase
Global Secondary Indexing (GSI)
●
In addition to the existing MapReduce-based indexes known as views, Couchbase
Server provides a global secondary index (GSI). With GSI, you can access your
documents faster by using secondary index fields without the overhead of
MapReduce.
●
GSI provides fast access for N1QL queries. With the support of GSI, you can submit
queries that target specific lookups on secondary fields. For example, you can look
up customers by email address rather than relying on a customer key.
●
GSI provides the following benefits:
●
Improves index efficiency by avoiding gather-scatter and index working set
management
●
Improves application scalability by allowing indexes to partition and scale
independently
Features of Couchbase
Cross Datacenter Replication (XDCR) -
Filtering
●
Couchbase supports cross-data-center replication (XDCR), which provides live
replication of database contents of one Couchbase cluster to a geographically
remote cluster. Note that XDCR operates simultaneously with intracluster replication
(the copying of live documents to their inactive replica counterparts on other cluster
members), and all systems in an XDCR arrangement invisibly synchronize with one
another.
Views in Couchbase
●
Queries on Couchbase Server are performed via "views," Couchbase terminology
for indexes. Put another way, when you create an index, you're provided with a view
that serves as your mechanism for querying Couchbase data
●
To define a view, you build a specific kind of document called a design document.
The design document holds the JavaScript code that implements the mapreduce
operations that create the view's index.
●
The map function in a design document's mapreduce specification filters and
extracts information from the documents against which it executes. The result is a
set of key/value pairs that comprise the query-accelerating index.
The reduce function is optional. It is typically used to aggregate or sum the data
manipulated by the map operation. Code in the reduce function can be used to
implement operations that correspond roughly to SQL's ORDER BY, SORT, and
aggregation features.
Views in Couchbase
●
A design document can hold multiple views and the configuration applies to all views
in the document. If you update one index, all indexes in the document will be
updated.
●
Finally, Couchbase distinguishes between development and production views, and
the two are kept in separate namespaces. Development views can be modified;
production views cannot. The reason for the distinction arises from the fact that, if
you modify a view, all views in that design document will be invalidated, and all
indexes defined by mapreduce functions in the design document will be rebuilt.
Therefore, development views enable you to test your view code in a kind of
sandbox before deploying it into a production view.
Initialization Couchbase trial app
//Required imports
import com.couchbase.client.java.{Bucket, CouchbaseCluster}
//Create cluster and open bucket
val cluster = CouchbaseCluster.create(“127.0.0.1”)
val bucket = cluster.openBucket(“user”)
//case class to store data in bucket
case class User(name:String, email: String)
CURD Operations in Couchbase
Create record :
def insert(user: User): Option[User] = {
try {
val doc = JsonObject.fromJson(compact(render(decompose(user))))
val result = bucket.insert(JsonDocument.create("usr::" + user.email, (doc)))
val jsonAsString = result.content().toString()
val insertedData = parse(jsonAsString).extract[User]
Some(insertedData)
} catch {
case exception: Exception =>
None
}
}
CURD Operations in Couchbase
Update record
def update(user: User): Option[User] = {
try {
val doc = JsonObject.fromJson(compact(render(decompose(user))))
val result = bucket.upsert(JsonDocument.create("usr::" + user.email, (doc)))
val jsonAsString = result.content().toString()
val insertedData = parse(jsonAsString).extract[User]
Some(insertedData)
} catch {
case exception: Exception =>
None
}
}
CURD Operations in Couchbase
Retrieve record
def retrieveUser(email: String): Option[User] = {
try {
val result = bucket.get("usr::" + email)
Some(parse(result.content().toString()).extract[User])
} catch {
case exception: Exception =>
None
}
}
CURD Operations in Couchbase
Delete record
def deleteUser(email: String): Int = {
try {
val result = bucket.remove("usr::" + email)
1
} catch {
case exception: Exception =>
0
}
}
Thank You

Couchbase - Introduction

  • 1.
    Introduction to Bharat Singh TraineeSoftware Consultant Knoldus Software LLP.
  • 2.
    Agenda ● What is Couchbase ● WhyNoSQL ? ● Features of Couchbase ● Views in Couchbase ● Basic CURD operations with fixed key
  • 3.
    What is Couchbase CouchbaseServer is a high-performance NoSQL distributed database with a flexible data model. It scales on commodity hardware to support large data sets with a high number of concurrent reads and writes while maintaining low latency and strong consistency.
  • 4.
    Why NoSQL ? –With relational technologies, many application developers find it difficult, or even impossible, to get the dynamic scalability and level of scale they need while also maintaining the performance users demand. – Data is becoming easier to capture and access through third parties such as Facebook. It’s not surprising that developers find increasing value in leveraging this data to enrich existing applications and create new apps. – Developers want a highly flexible solution that easily accommodates any new type of data they choose to work with, and isn’t disrupted by content structure changes from third-party data providers. Much of the new data is unstructured and semi-structured, so developers also need a database that can efficiently store it. – NoSQL databases are built from the ground up to be distributed, scale-out technologies and are therefore a better fit with the highly distributed nature of the three-tier internet architecture.
  • 5.
    Features of Couchbase Multi-DimensionalScaling ● Couchbase has redefined the way enterprises scale distributed databases with the option of Multi-Dimensional Scaling. It separates, isolates, and scales individual services – query, index, and data – to improve application performance and increase resource utilization. Couchbase Server 4.0 is the first and only distributed database capable of scaling with the speed and precision required by enterprise applications with variable workloads. ● Multi-Dimensional Scaling enables enterprises to optimize hardware by allocating resources based on the workload of a specific service, and to avoid resource contention by performing queries, maintaining indexes, and writing data with different nodes. It is inefficient to require participation from every node to perform a query or maintain an index. Couchbase Server 4.0 solves this problem by scaling data independent of queries and indexes.
  • 6.
  • 7.
    Features of Couchbase N1QL(read as 'Nickel') ● N1QL is the first query language to leverage the complete flexibility of JSON and the full power of SQL. While JSON benefits from SQL because it enables developers to model and query data with relationships, SQL benefits from JSON because it removes the “impedance mismatch” between the data model and the application model. In fact, the data model is no longer limited to "single table" or "table per query" impementations - related data can be embedded or referenced and the same data can be queried in different ways. ● N1QL enables developers to transition to a NoSQL database by leveraging existing SQL and data modeling knowledge. It is now possible to create a data model that combines both nested and related data while avoiding the need to create and maintain complex query logic – for example, multiple queries are no longer required to get related data. N1QL enables developers to buid applications that require ad-hoc access to data with greater precision and intelligence.
  • 8.
    Features of Couchbase GlobalSecondary Indexing (GSI) ● In addition to the existing MapReduce-based indexes known as views, Couchbase Server provides a global secondary index (GSI). With GSI, you can access your documents faster by using secondary index fields without the overhead of MapReduce. ● GSI provides fast access for N1QL queries. With the support of GSI, you can submit queries that target specific lookups on secondary fields. For example, you can look up customers by email address rather than relying on a customer key. ● GSI provides the following benefits: ● Improves index efficiency by avoiding gather-scatter and index working set management ● Improves application scalability by allowing indexes to partition and scale independently
  • 10.
    Features of Couchbase CrossDatacenter Replication (XDCR) - Filtering ● Couchbase supports cross-data-center replication (XDCR), which provides live replication of database contents of one Couchbase cluster to a geographically remote cluster. Note that XDCR operates simultaneously with intracluster replication (the copying of live documents to their inactive replica counterparts on other cluster members), and all systems in an XDCR arrangement invisibly synchronize with one another.
  • 11.
    Views in Couchbase ● Querieson Couchbase Server are performed via "views," Couchbase terminology for indexes. Put another way, when you create an index, you're provided with a view that serves as your mechanism for querying Couchbase data ● To define a view, you build a specific kind of document called a design document. The design document holds the JavaScript code that implements the mapreduce operations that create the view's index. ● The map function in a design document's mapreduce specification filters and extracts information from the documents against which it executes. The result is a set of key/value pairs that comprise the query-accelerating index. The reduce function is optional. It is typically used to aggregate or sum the data manipulated by the map operation. Code in the reduce function can be used to implement operations that correspond roughly to SQL's ORDER BY, SORT, and aggregation features.
  • 12.
    Views in Couchbase ● Adesign document can hold multiple views and the configuration applies to all views in the document. If you update one index, all indexes in the document will be updated. ● Finally, Couchbase distinguishes between development and production views, and the two are kept in separate namespaces. Development views can be modified; production views cannot. The reason for the distinction arises from the fact that, if you modify a view, all views in that design document will be invalidated, and all indexes defined by mapreduce functions in the design document will be rebuilt. Therefore, development views enable you to test your view code in a kind of sandbox before deploying it into a production view.
  • 13.
    Initialization Couchbase trialapp //Required imports import com.couchbase.client.java.{Bucket, CouchbaseCluster} //Create cluster and open bucket val cluster = CouchbaseCluster.create(“127.0.0.1”) val bucket = cluster.openBucket(“user”) //case class to store data in bucket case class User(name:String, email: String)
  • 14.
    CURD Operations inCouchbase Create record : def insert(user: User): Option[User] = { try { val doc = JsonObject.fromJson(compact(render(decompose(user)))) val result = bucket.insert(JsonDocument.create("usr::" + user.email, (doc))) val jsonAsString = result.content().toString() val insertedData = parse(jsonAsString).extract[User] Some(insertedData) } catch { case exception: Exception => None } }
  • 15.
    CURD Operations inCouchbase Update record def update(user: User): Option[User] = { try { val doc = JsonObject.fromJson(compact(render(decompose(user)))) val result = bucket.upsert(JsonDocument.create("usr::" + user.email, (doc))) val jsonAsString = result.content().toString() val insertedData = parse(jsonAsString).extract[User] Some(insertedData) } catch { case exception: Exception => None } }
  • 16.
    CURD Operations inCouchbase Retrieve record def retrieveUser(email: String): Option[User] = { try { val result = bucket.get("usr::" + email) Some(parse(result.content().toString()).extract[User]) } catch { case exception: Exception => None } }
  • 17.
    CURD Operations inCouchbase Delete record def deleteUser(email: String): Int = { try { val result = bucket.remove("usr::" + email) 1 } catch { case exception: Exception => 0 } }
  • 18.