Couchbase-sdk-net user manual 1.2-Chapter 1. Departure

Source: Internet
Author: User
Tags couchbase
Document directory
  • 1.3.1 create a project
  • 1.3.2 add Configuration
  • 1.3.3 client example
  • 1.3.4 CRUD operation
  • 1.3.5. stored as Json text
  • 1.3.6. CouchbaseClient JSON Extension Method
Chapter 1. Departure 1.1. Get the server program

Download and install the Couchbase server. After installation, return to here and continue.

1.2. Obtain the client class library

Both methods can be used:

1. Download the class library directly.

2. Run the NuGet Package Manger command:

PM> Install-Package CouchbaseNetClient

1.3. Trial 1.3.1 Project Creation

Create a console program using visual studio and add Couchbase. dll, Enyim. Memcached. dll,

References to these assembly Newtonsoft. Json. dll and RestSharp. dll (these dynamic link libraries can be found in the downloaded class library package)

1.3.2 add Configuration

You can use the programming method or the couchbase configuration section to configure it in the app. config file. Flexible updates to the app. config file are preferred.

Configure your app. config as follows:

<? Xml version = "1.0"?>

<Configuration>

<ConfigSections>

<Section name = "couchbase" type = "Couchbase. Configuration. CouchbaseClientSection, Couchbase"/>

</ConfigSections>

<Couchbase>

<Servers bucket = "default" bucketPassword = "">

<Add uri = "http: // 192.168.0.2: 8091/pools"/>

<Add uri = "http: // 192.168.0.3: 8091/pools"/>

</Servers>

</Couchbase>

</Configuration>

 

The above multiple URIs in the Servers list are the cluster configurations for the client to obtain information. If you are running on the local machine, you only need to set a 127.0.0.1 configuration.

The default Couchbase Server installation is to create a bucket named default without a password. Therefore, the attribute of the bucket password is optional. If you have created an authenticated bucket, you must configure that value in the preceding settings.

Warning:

Some TCP/IP ports are allocated by default for system communication in Windows. For more information about this issue, including how to adjust the configuration and add available ports, see Avoiding TCP/IP Port Exhaustion.

1.3.3 client example

Add the following statement to the Program. cs file:

Using Couchbase;

Using Enyim. Caching. Memcached;

Using Newtonsoft. Json;

The Couchbase namespace contains the client and configuration classes we will use.

Enyim. Caching. Memcached includes infrastructure support. Remember that Couchbase supports the Memcached protocol, so you can use the key/Value operation method of the popular Enyim Memcached client.

Next, create a client instance in the Main method.

Var client = new CouchbaseClient ();

 

In this exercise, this is a bad way to create a client. This client will create a connection pool and create a thread to obtain cluster configuration. Therefore, the best way is to create only one client instance for each application domain and each bucket. For this purpose, you can create a static attribute in a class.

Public class CouchbaseManager

{

Private readonly static CouchbaseClient _ instance;

Static CouchbaseManager ()

{

_ Instance = new CouchbaseClient ();

}

Public static CouchbaseClient Instance {get {return _ instance ;}}

}

1.3.4 CRUD operation

On the. Net client, the main crup api uses the standard Key/Value storage method. You must provide a key and value to create or update the text. Text Retrieval and removal are performed based on the provided key. For example:

If you select a bucket named "beer-sample" when installing the Couchbase Server and setting the cluster, you can find a text key in the bucket as "new_holland_brewing_company-sundog ."

{

"Name": "Sundog ",

"Abv": 5.25,

"Ibu": 0,

"Srm": 0,

"Upc": 0,

"Type": "beer ",

"Brewery_id": "new_holand_brewing_company ",

"Updated": "20:00:20 ",

"Description": "Sundog is an amber ale as deep as the copper glow of a Lake Michigan sunset. its biscuit malt give Sundog a toasty "style": "American-Style Amber/Red Ale ",

"Category": "North American Ale"

}

You can call the get method to obtain the content of the text.

Var savedBeer = client. Get ("new_holland_brewing_company-sundog ");

If you add a line of code to print the savedBeer to the console, you can see that it contains a json string:

Var savedBeer = client. Get ("new_holland_brewing_company-sundog ");

Console. WriteLine (savedBeer );

In this example, savedBeer is an Object type. If you want to get a string type Object, you can use the get

Var savedBeer = client. Get <string> ("new_holland_brewing_company-sundog ");

 

To demonstrate how to add text, first create a Json string.

Var newBeer =

@"{

"" Name "": "Old Yankee Ale "",

"" Abv "": 5.00,

"" Ibu "": 0,

"" Srm ": 0,

"" Upc "": 0,

"" Type ":" "beer "",

"" Brewery_id "": "cottrell_brewing "",

"" Updated "": "" 20:00:20 "",

"" Description "": "" A medium-bodied Amber Ale "",

"" Style ":" "American-Style Amber "",

"" Category "": "" North American Ale ""

}";

Here we create a simple key. The key name is connected by the beer factory name and the beer name with a broken number. The blank space is replaced with a line break, and the text is all in lower case. The key generation rules must remain unchanged. If you use a key to retrieve data, you must be able to predict which key to use.

Var key = "cottrell_brewing-old_yankee_ale ";

With this new key, json text can be easily saved.

Var result = client. Store (StoreMode. Add, key, newBeer );

 

Three parameters are used for saving. The first parameter is the storage mode, which is an enumeration. Add indicates adding a new key, and Replace indicates updating an existing key, set indicates that if the key does not exist, it is added or updated if it exists. If the key already exists, add will fail. If the key does not exist, the Replace operation will fail. The second and third parameters are key and value, respectively.

The return value is of the bool type, indicating whether the operation is successful or failed.

 

The delete operation uses the key to call the Remove Method. Similar to other methods, the Remove operation returns a return value of the bool type, indicating whether the operation is successful.

Var result = client. Remove ("cottrell_brewing-old_yankee_ale ");

 

1.3.5. stored as Json text

Strings stored in Json format are relatively simple and direct operations, but in fact, we often have requirements for objects in the storage field, more clearly, we have a lot of requirements for objects that directly store data. This. Net client Class Library supports storing serializable objects. Of course, these serializable objects will be used as Joson textBinary attachmentSave. Its impact is that the attachments are not indexed when they are viewed. A better solution is to serialize the data into a Json string before saving the data; extract the data, and then deserialize the Json string into an object.

 

1.3.6. CouchbaseClient JSON Extension Method

To simply read and write JSON, The CouchbaseClientExtensions class provides two methods in the Couchbase. Extensions namespace: StoreJson and GetJson. Both methods depend on the open-source class library Newtonsoft. Json library (already in the compressed package of the Couchbase. NET class library ). These two methods are simple rewriting of the get and store methods. Currently, complicated CAS and TTL operations are not supported.

Create a POCO class Beer and add a declaration.

Using Newtonsoft. Json;

 

Public class Beer

{

[JsonProperty ("name")]

Public string Name {get; set ;}

[JsonProperty ("abv")]

Public float ABV {get; set ;}

[JsonProperty ("type")]

Public string Type

{

Get {return "beer ";}

}

[JsonProperty ("brewery_id")]

Public string BreweryId {get; set ;}

[JsonProperty ("style")]

Public string Style {get; set ;}

[JsonProperty ("category")]

Public string Category {get; set ;}

}

Use a new instance:

Var newBeer = new Beer

{

Name = "Old Yankee Ale ",

ABV = 5.00f,

BreweryId = "cottrell_brewing ",

Style = "American-Style Amber ",

Category = "North American Ale"

};

 

Storage:

Var result = client. StoreJson (StoreMode. Set, key, newBeer );

Search:

 

Var savedBeer = client. GetJson <Beer> (key );

In Program, you can:

Class Program

{

Static void Main (string [] args)

{

Var client = new CouchbaseClient ();

Var key = "cottrell_brewing-old_yankee_ale ";

Var newBeer = new Beer

{

Name = "Old Yankee Ale ",

ABV = 5.00f,

BreweryId = "cottrell_brewing ",

Style = "American-Style Amber ",

Category = "North American Ale"

};

Var result = client. StoreJson (StoreMode. Set, key, newBeer );

If (result)

{

Var savedBeer = client. GetJson <Beer> (key );

Console. WriteLine ("Found beer:" + savedBeer. Name );

}

}

}

Complete example program, need to install vs2012

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.