developing couchbase part iii advanced app dev

36
1 1 Developing with Couchbase Part III: Using the Client Libraries John Zablocki Developer Advocate @codevoyeur

Upload: couchbase

Post on 19-Jul-2015

2.098 views

Category:

Entertainment & Humor


8 download

TRANSCRIPT

11

Developing with Couchbase Part III:Using the Client Libraries

John ZablockiDeveloper Advocate

@codevoyeur

{ " _id" : "John Zablocki”, "Title" : "Developer Advocate", "Company" : "Couchbase", "Books" : [ { "Title" : "Orchard CMS", "Publisher" : "O'Reilly" } ], "Twitter" : "@CodeVoyeur", "Email" : "[email protected]", "Meetup" : "Beantown ALT.NET“, "Code" : [

"https://github.com/jzablocki", http://bitbucket.com/johnzablockis

], "Blogs" : [

"http://dllhel.net", "http://blog.couchbase.com"

]}

About Me

● Documents, Objects and Schemas● View Review● .NET Client API● Demo: TapMap● Q & A

Agenda

4

Preamble

All examples are in C# using the Couchbase .NET Client Library 1.2 (Developer Preview)

Download at couchbase.com/develop/net/next

Here's how to connect to Couchbase Server:

var config = new CouchbaseClientConfiguration();config.Urls.Add(new Uri(http://192.168.1.2:/8091/pools/default));config.Urls.Add(new Uri(http://192.168.1.3:/8091/pools/default));config.Bucket = “protected”;config.BucketPassword = “s3cr3t”;

var client = new CouchbaseClient(config);

View Review

Documents, Objectsand Schemas

View Review .NET View API

Demo: TapMap

Q & A

6

Remember when we said that Couchbase documents were schema-less?

7

Well, it’s not quite that simple…

8

Recall those beer documents…

{"_id": "beer_abbey_ale","type": "beer","name": "Abbey Ale","brewery": "brewery_Brewery_Ommegang","abv": 7.1,"style": "Ale"

}A schema

9

Where did it come from?

10

A C# Class

public class Beer { public string Id { get; set; } public string Name { get; set; }

public double ABV { get; set; }

public override string Type { get { return "beer"; } }}

A schema

11

Your object model often becomes your schema, but how?

12

A C# Class

[Serializable]public class Beer{ [JsonProperty(PropertyName = "_id")] public string Id { get; set; } [JsonProperty("name")] public string Name { get; set; }

[JsonProperty("abv")] public double ABV { get; set; }

[JsonProperty(“type")] public override string Type { get { return "beer"; } }}

A schema mapping

13

Storing your model in C#

var key = “beer_Three_Philosophers”;var getResult = client.GetWithCas(key);

var beer = getResult.Result;beer.Name = “Three Philosophers”;

var json = JsonConvert.Serialize(beer);var casResult = client.Cas(StoreMode.Set, key, json, getResult.Cas);

var message = casResult.Result ? “Success” : “Failure”; Console.WriteLine(message);

Schemafication

Yum!

View Review

Documents, Objectsand Schemas

View Review .NET View API

Demo: TapMap

Qestions

15

These Couchbase guys really like their beer…

16

Yeah, we do…

17

It’s a much more interesting data source than Northwind, no?

18

Beer Documents

{"_id": "beer_abbey_ale","_rev": "1-00003e56befe4b4f0000005600000112","$flags": 0,"$expiration": 0,"type": "beer","name": "Abbey Ale","brewery": "brewery_brewery_ommegang","abv": 7.1

}

Unique key

Document type

Awesome beer

19

Let’s create an index on beer name…

_design/beers/_view/by_name

// map functionfunction (doc) { if (doc.type == "beer“ && beer.name) { emit(doc.name, null); }}

The results…[ {"id":"beer_abbey_ale","key":"Abbey Ale","value":null}, {"id":"beer_american_ale","key":"American Ale","value":null}, {"id":"beer_harpoon_ipa","key":"Harpoon IPA","value":null}, {"id":"beer_long_trail_ale","key":"Long Trail Ale","value":null}, {"id":"beer_old_yankee_ale","key":"Old Yankee Ale","value":null}, {"id":"beer_three_philosophers","key":"Three Philosophers","value":null} ]

.NET View API

Documents, Objectsand Schemas

View Review .NET View API

Demo: TapMap

Qestions

21

Calling the view from .NET

via .NET

var view = client.GetView("beers", "by_name");

foreach(var item in view) { var beer = client.Get<Beer>(item.ItemId); Console.WriteLine(beer.Name);}

Design doc

View name

Document key

22

Finding a beer by name…

_design/beers/_view/by_name?key=“Abbey Ale”

via .NET

var view = client.GetView("beers", "by_name") .Key(“Abbey Ale”) .FirstOrDefault();

var beer = client.Get<Beer>(item.ItemId);

Exact matc

h

23

Finding a range of beers by name…

_design/beers/_view/by_name?startkey=“A”&endkey=“D”

var view = client.GetView("beers", "by_name").StartKey(“A”).EndKey(“D”);

foreach(var item in view) { var beer = client.Get<Beer>(item.ItemId); Console.WriteLine(beer.Name);}

24

Limiting and skipping beers…

_design/beers/_view/by_name?limit=10&skip=2

var view = client.GetView("beers", "by_name").Limit(5).Skip(5);

foreach(var item in view) { var beer = client.Get<Beer>(item.ItemId); Console.WriteLine(beer.Name);}

25

Finding a strong ale…

The view

function (doc) { if (doc.type == "beer" && doc.style && doc.abv) { emit([doc.style, doc.abv], null); }}

26

Finding a strong ale…

C# Example

var view = client.GetView("beers", "by_style_and_abv")

.StartKey(new object[] { “Ale”, 6.0 })

.EndKey(new object[] { “Ale”, 10 });

foreach(var item in view) { var beer = client.Get<Beer>(item.ItemId); Console.WriteLine(beer.Name);}

27

Finding breweries by province

The brewery document

{"_id": "brewery_cottrell_brewing","_rev": "1-00003e56c0e830c80000005900000112","$flags": 0,"$expiration": 0,"type": "brewery","name": "Cottrell Brewing","city": "Pawcatuck","province": "Connecticut"

}

28

Finding breweries by province

The map function

function (doc) { if (doc.type == "brewery" && doc.province) { emit(doc.province, doc.name); }}

Map/Reduce – Map Results

• The conceptual output of the map function is

[

{"key":"Connecticut", "value":"Cottrell Brewing"}, {"key":"Connecticut","value":"Thomas Hooker Brewing Co."},

{"key":"Massachusetts","value":"Harpoon Brewing Company"}, {"key":"New York","value":"Brewery Ommegang"},

{"key":"Vermont","value":"Long Trail Brewing Company"}

]

Map/Reduce – Reduce Function

• A reduce function to count breweries by province– Or simply use the build in _count function

function (keys, values) { return values.length;}

• The conceptual input to the reduce function is:[ {"keys":["Connecticut", "Connecticut"],

"values": ["Cottrell Brewing", "Thomas Hooker Brewing Co."]}, {"keys":["Massachusetts"],"values":["Harpoon Brewing Co."]}, {"keys":["New York"],"values":["Brewery Ommegang"]},

{"keys":["Vermont"],"values":["Long Trail Brewing Company“]} ]

Map/Reduce – Reduce Results

• The conceptual result of the reduce is

{"key":"Connecticut","value":2}, {"key":"Massachusetts","value":1}, {"key":"New York","value":1}, {"key":"Vermont","value":1}

Map/Reduce .NET Code

_design/beers/_view/by_name?limit=10&skip=2

var view = client.GetView(“breweries", "by_province")

.Group(true);

foreach(var item in view) {

Console.WriteLine(item.ItemId);

}

Demo:TapMap

Documents, Objectsand Schemas

View Review .NET View API

Demo: TapMap

Qestions

TapMap

TapMap Info

• Written in C#, with ASP.NET MVC 4

• Code available at: http://github.com/couchbaselabs/TapMap

• Demonstrates common web app behaviors using Couchbase Server 2.0– Find a user in the database by username and password

– When creating a new user, verify a username and email are unique

– Filter a select list data source from the value of another

– Show data items on a map

• Demonstrates using a client library with common architectural patterns– Working with a Repository of T

– Creating a domain models• Document mapping

• Model inheritance

• Model composition

– Managing the client resources

Q&A

Documents, Objectsand Schemas

View Review .NET View API

Demo: TapMap

Q&A