NoSQL's biggest lie: SQL never went away - Martin Esmann

82
NoSQL's biggest lie: SQL never went away! Martin Esmann Developer Advocate, Couchbase 1

Transcript of NoSQL's biggest lie: SQL never went away - Martin Esmann

Page 1: NoSQL's biggest lie: SQL never went away - Martin Esmann

NoSQL's biggest lie: SQL never went away!Martin EsmannDeveloper Advocate, Couchbase

1

Page 2: NoSQL's biggest lie: SQL never went away - Martin Esmann

Let’s  do  something  unspectacular

2

Page 3: NoSQL's biggest lie: SQL never went away - Martin Esmann

©2014  Couchbase  Inc.

A  query

3

SELECT * FROM `travel-sample` WHERE type = ‘airline’ AND country = ‘United Kingdom’;

Page 4: NoSQL's biggest lie: SQL never went away - Martin Esmann

©2014  Couchbase  Inc.

A  query

4

Page 5: NoSQL's biggest lie: SQL never went away - Martin Esmann

Did  I  say  unspectacular?

5

Page 6: NoSQL's biggest lie: SQL never went away - Martin Esmann

©2014  Couchbase  Inc.

That  was  JSON

6

Page 7: NoSQL's biggest lie: SQL never went away - Martin Esmann

So,  what  did  we  do?

7

Page 8: NoSQL's biggest lie: SQL never went away - Martin Esmann

©2014  Couchbase  Inc.

We  put  the  SQL  back  into  NoSQL

8

Page 9: NoSQL's biggest lie: SQL never went away - Martin Esmann

But  first,  let’s  take  a  step  back

9

Page 10: NoSQL's biggest lie: SQL never went away - Martin Esmann

©2014  Couchbase  Inc.

NoSQL?

10

Polyglot  persistence  is  “…using  multiple  data  

storage  technologies,   chosen  based  upon  the  

way  data  is  being  used  by  individual  

applications.  Why  store  binary  images  in  

relational  database,  when  there  are  better  

storage  systems?”  

Martin  Fowler  and  Pramod Sadalage

Page 11: NoSQL's biggest lie: SQL never went away - Martin Esmann

©2014  Couchbase  Inc.

Types  of  NoSQL  database

11

By  Schumi4ever  (Own  work)  [CC  BY-­‐SA  3.0  (http://creativecommons.org/licenses/by-­‐sa/3.0)],  via  Wikimedia  Commons

Page 12: NoSQL's biggest lie: SQL never went away - Martin Esmann

©2014  Couchbase  Inc.

Key  value

12

Email: [email protected]:email: {

“personal”: “[email protected]”,“work”: “[email protected]

}

Page 13: NoSQL's biggest lie: SQL never went away - Martin Esmann

©2014  Couchbase  Inc.

London

[email protected]@[email protected]@[email protected]@couchbase.com

Document

13

Developer Advocacy

[email protected]@[email protected]@[email protected]@[email protected]@couchbase.com

[email protected]:

{"city": ”Copenhagen","glasses": false,"team": "Developer Advocacy","music": ”Electronic!"

}

Page 14: NoSQL's biggest lie: SQL never went away - Martin Esmann

©2014  Couchbase  Inc.

Document

14

London

[email protected]@[email protected]

Developer Advocacy

[email protected]@[email protected]

London and Developer Advocacy

[email protected]@[email protected]

Page 15: NoSQL's biggest lie: SQL never went away - Martin Esmann

©2014  Couchbase  Inc.

And  the  others

15

Page 16: NoSQL's biggest lie: SQL never went away - Martin Esmann

Context  is  all

16

Page 17: NoSQL's biggest lie: SQL never went away - Martin Esmann

©2014  Couchbase  Inc.

There's  always  a  trade-­‐off

17

• Offload   from  some  other  data  store  (i.e.  caching)• Computation  offload• Speed• Scalability• Availability• Flexibility  in  what  you  store• Query  flexibility

Page 18: NoSQL's biggest lie: SQL never went away - Martin Esmann

Where  Couchbase comes  in

18

Page 19: NoSQL's biggest lie: SQL never went away - Martin Esmann

©2014  Couchbase  Inc.

Couchbase Server  4.0

19

High  availability  cache

Key-­‐value  store

Document  database N1QL SQL-­‐like  query  

for  JSON

Page 20: NoSQL's biggest lie: SQL never went away - Martin Esmann

N1QL:  SQL  for  JSON

20

Page 21: NoSQL's biggest lie: SQL never went away - Martin Esmann

©2014  Couchbase  Inc.

A  user  profile  as  JSON

21

Page 22: NoSQL's biggest lie: SQL never went away - Martin Esmann

How  do  we  query  that?

22

Page 23: NoSQL's biggest lie: SQL never went away - Martin Esmann

©2014  Couchbase  Inc.

Querying  the  JSON  profile

23

• Look-­‐up  documents:   i.e.  manual  secondary  indexing   (2i)• Couchbase views:  i.e.  automated  secondary  indexing   (2i)• N1QL

Page 24: NoSQL's biggest lie: SQL never went away - Martin Esmann

©2014  Couchbase  Inc.

Manual  2i

24

Page 25: NoSQL's biggest lie: SQL never went away - Martin Esmann

©2014  Couchbase  Inc.

Automatic  2i:  views

25

Page 26: NoSQL's biggest lie: SQL never went away - Martin Esmann

©2014  Couchbase  Inc.

N1QL

26

Page 27: NoSQL's biggest lie: SQL never went away - Martin Esmann

©2014  Couchbase  Inc.

SELECT

27

SELECT  ...

Page 28: NoSQL's biggest lie: SQL never went away - Martin Esmann

©2014  Couchbase  Inc.

SELECT

28

SELECT 1 + 1;

{        "requestID":   "3ccebac7-­‐341a-­‐4c31-­‐a2c5-­‐b46aaed54356",      "signature":  {                

"$1":  "number"        },        "results":  [              

{                        "$1":  2                }        

],        "status":   "success",        "metrics":  {                

"elapsedTime":  "31.826219ms",                "executionTime":  "29.800616ms",                "resultCount":   1,                "resultSize":  31        

}}

Page 29: NoSQL's biggest lie: SQL never went away - Martin Esmann

©2014  Couchbase  Inc.

SELECT  COUNT

29

SELECT COUNT(*) FROM `default`WHERE office = "London";

{"requestID": "6e733000-ac83-44ba-95a7-9b012e9c553d","signature": {

"$1": "number"},"results": [

{"$1": 6

}],"status": "success","metrics": {

"elapsedTime": "18.603124ms","executionTime": "18.327696ms","resultCount": 1,"resultSize": 31

}}

Page 30: NoSQL's biggest lie: SQL never went away - Martin Esmann

©2014  Couchbase  Inc.

SELECT email FROM `default`WHERE office = "London";

SELECT

30

Page 31: NoSQL's biggest lie: SQL never went away - Martin Esmann

©2014  Couchbase  Inc.

SELECT email FROM `default`WHERE office = "London"AND team = "Developer Advocacy";

SELECT

31

Page 32: NoSQL's biggest lie: SQL never went away - Martin Esmann

©2014  Couchbase  Inc.

SELECT email FROM `default`WHERE office = "London"AND team != "Developer Advocacy";

SELECT

32

Page 33: NoSQL's biggest lie: SQL never went away - Martin Esmann

But  this  is  JSON

33

Page 34: NoSQL's biggest lie: SQL never went away - Martin Esmann

©2014  Couchbase  Inc.

SELECT conferences[0].nameAS event_name FROM `default`;

ARRAY  ELEMENTS

34

Page 35: NoSQL's biggest lie: SQL never went away - Martin Esmann

©2014  Couchbase  Inc.

SELECT DISTINCT conferences[0].nameAS event_name FROM `default`;

DISTINCT  ARRAY  ELEMENTS

35

Page 36: NoSQL's biggest lie: SQL never went away - Martin Esmann

©2014  Couchbase  Inc.

SELECT DISTINCT conferences[0].nameAS event_name FROM `default`WHERE conferencesIS NOT MISSING;

REMOVE  MISSING  ITEMS

36

Page 37: NoSQL's biggest lie: SQL never went away - Martin Esmann

©2014  Couchbase  Inc.

SELECT email AS person,conferences[0].name AS event FROM `default`WHERE ANY event in conferences SATISFIES event.name = "Droidcon Sweden" END;

WHO  IS  GOING  TO  DROIDCON  SWEDEN?

37

Page 38: NoSQL's biggest lie: SQL never went away - Martin Esmann

What's  going  on  underneath?

38

Page 39: NoSQL's biggest lie: SQL never went away - Martin Esmann

©2014  Couchbase  Inc.

EXPLAIN SELECT email AS person,conferences[0].name AS event FROM `default`WHERE ANY event in conferences SATISFIES event.name = "Droidcon Sweden" END;

EXPLAIN

39

Page 40: NoSQL's biggest lie: SQL never went away - Martin Esmann

Is  N1QL  read-­‐only?

40

Page 41: NoSQL's biggest lie: SQL never went away - Martin Esmann

©2014  Couchbase  Inc.

Updating  and  deleting

41

• DELETE:  provide  the  key  to  delete  the  document• INSERT:  provide  a  key  and  some  JSON  to  create  a  new  document• UPSERT:  as  INSERT  but  will  overwrite  existing  docs• UPDATE:  change  individual  values  inside  existing  docs

Page 42: NoSQL's biggest lie: SQL never went away - Martin Esmann

©2014  Couchbase  Inc.

UPDATE

42

Page 43: NoSQL's biggest lie: SQL never went away - Martin Esmann

©2014  Couchbase  Inc.

DELETE

43

Page 44: NoSQL's biggest lie: SQL never went away - Martin Esmann

A  larger  data-­‐set:  travel-­‐sample

44

Page 45: NoSQL's biggest lie: SQL never went away - Martin Esmann

©2014  Couchbase  Inc.

TRAVEL  SAMPLE  DATA

45

Page 46: NoSQL's biggest lie: SQL never went away - Martin Esmann

©2014  Couchbase  Inc.

travel-­‐sample

46

Page 47: NoSQL's biggest lie: SQL never went away - Martin Esmann

©2014  Couchbase  Inc.

CREATE  PRIMARY  INDEX

47

CREATE PRIMARY INDEXON `travel-sample`USING GSI;

Page 48: NoSQL's biggest lie: SQL never went away - Martin Esmann

©2014  Couchbase  Inc.

SELECT

48

SELECT * FROM `travel-sample`WHERE type = "airline";

Page 49: NoSQL's biggest lie: SQL never went away - Martin Esmann

©2014  Couchbase  Inc.

SELECT

49

Page 50: NoSQL's biggest lie: SQL never went away - Martin Esmann

©2014  Couchbase  Inc.

SELECT

50

SELECT * FROM `travel-sample`WHERE type = "airline"AND country = "United States";

Page 51: NoSQL's biggest lie: SQL never went away - Martin Esmann

©2014  Couchbase  Inc.

SELECT

51

Page 52: NoSQL's biggest lie: SQL never went away - Martin Esmann

Indexes

52

Page 53: NoSQL's biggest lie: SQL never went away - Martin Esmann

©2014  Couchbase  Inc.

CREATE  INDEX

53

CREATE INDEX airline ON `travel-sample`(type)WHERE type = "airline"USING GSI;

Page 54: NoSQL's biggest lie: SQL never went away - Martin Esmann

JOINs

54

Page 55: NoSQL's biggest lie: SQL never went away - Martin Esmann

©2014  Couchbase  Inc.

JOINs

55

• Retrieve  data  from  two  documents  in  a  single  SELECT• Join  within  a  keyspace/bucket• Join  across  keyspaces/buckets

Page 56: NoSQL's biggest lie: SQL never went away - Martin Esmann

©2014  Couchbase  Inc.

A  SIMPLE  JOIN

56

SELECT * FROM `travel-sample` r JOIN `travel-sample` a ON KEYS r.airlineidWHERE r.sourceairport="LHR" AND r.destinationairport = "SFO";

Page 57: NoSQL's biggest lie: SQL never went away - Martin Esmann

©2014  Couchbase  Inc.

WHO  FLIES  LHR-­‐>SFO?

57

SELECT DISTINCT a.name FROM `travel-sample` r JOIN `travel-sample` a ON KEYS r.airlineidWHERE r.sourceairport="LHR" AND r.destinationairport = "SFO";

Page 58: NoSQL's biggest lie: SQL never went away - Martin Esmann

©2014  Couchbase  Inc.

UNNEST

58

• Breaks  out  nested  JSON  from  the  results

Page 59: NoSQL's biggest lie: SQL never went away - Martin Esmann

©2014  Couchbase  Inc.

SOMETHING  USEFUL

59

SELECT a.name, s.flight, s.utc, r.sourceairport, r.destinationairport, r.equipmentFROM `travel-sample` r UNNEST r.schedule sJOIN `travel-sample` a ON KEYS r.airlineidWHERE r.sourceairport="LHR" AND r.destinationairport = "SFO"AND s.day=1ORDER BY s.utc;

Page 60: NoSQL's biggest lie: SQL never went away - Martin Esmann

N1QL  vs  View’s

60

Page 61: NoSQL's biggest lie: SQL never went away - Martin Esmann

©2014  Couchbase  Inc.

N1QL  and  views

61

N1QL Views

Ad-hoc querying Predictable queries

JSON in and JSON out Number crunching

Large growth clusters Multi-dimensional/geospatialqueries

Page 62: NoSQL's biggest lie: SQL never went away - Martin Esmann

Multi-­‐dimensional  scaling

62

Page 63: NoSQL's biggest lie: SQL never went away - Martin Esmann

©2014  Couchbase  Inc.

Scaling  out

63

Horizontal  scaling▪ Partitions  a  dataset  onto  one  or  more  homogenous  nodes▪ Each  node  runs  the  same  mixed  workloads▪ Re-­‐partition  dataset  with  additional  hardware  capacity

Page 64: NoSQL's biggest lie: SQL never went away - Martin Esmann

©2014  Couchbase  Inc.

What  is  Multi-­‐Dimensional  Scalability?  MDS  is  the  architecture   that  enables  independent   scaling  of  data,  query,  and  indexing  workloads.

Multi-­‐dimensional  scaling

64

Page 65: NoSQL's biggest lie: SQL never went away - Martin Esmann

©2014  Couchbase  Inc.

Multi-­‐dimensional  scaling

65

Isolated  Service  for  minimized  interference▪ Independent  “zones”  for  query,  index,  and  data  services

Minimize  indexing  and  query  overhead  on  core  key-­‐value  operations.

Page 66: NoSQL's biggest lie: SQL never went away - Martin Esmann

©2014  Couchbase  Inc.

Multi-­‐dimensional  scaling

66

Independent   scalability  for  the  best  computational   capacity  per  service

Heavier  indexing  (index  more  fields):  scale  up  index  service  nodes.

More  RAM  for  query  processing:scale  up  query  service  nodes.

Page 67: NoSQL's biggest lie: SQL never went away - Martin Esmann

Geospatial  querying

67

Page 68: NoSQL's biggest lie: SQL never went away - Martin Esmann

©2014  Couchbase  Inc.

Geospatial  views

68

• Experimental  in  3.0,  now  GA  in  4.0• Performance  and  stability  improvements• GeoJSON output• Bounding-­‐box   and  range  queries  on  

multiple  dimensions

Page 69: NoSQL's biggest lie: SQL never went away - Martin Esmann

What  else  is  in  Couchbase Server  4.0?

69

Page 70: NoSQL's biggest lie: SQL never went away - Martin Esmann

©2014  Couchbase  Inc.

Other  new  things  in  Couchbase Server  4.0

70

• ForestDB:  GSIs  are  stored  with  a  new  storage  engine• Filtered  XDCR:  more  efficient  cross-­‐data  centre  replication• Security:  LDAP,  admin  auditing

Page 71: NoSQL's biggest lie: SQL never went away - Martin Esmann

Developers?SDK  support  for  N1QL!

71

Page 72: NoSQL's biggest lie: SQL never went away - Martin Esmann

©2014  Couchbase  Inc.

.NET SDK: Connection

72

//  Option  1:  Create  Cluster  and  open  bucket  var cluster  =  new Cluster(config);var bucket  =  cluster.OpenBucket(bucketName);

//  Option  2:  ClusterHelper and  open  bucket  (Singleton,  thread  safe  bucket  instanceClusterHelper.Initialize(config);var cluster  =  ClusterHelper.Get();var bucket  =  ClusterHelper.GetBucket(bucketName);

//  Close  connectioncluster.CloseBucket(bucket);cluster.Dispose();

Page 73: NoSQL's biggest lie: SQL never went away - Martin Esmann

©2014  Couchbase  Inc.

.NET SDK: Client Configuration

73

//  Cluster  Configurationvar config =  new ClientConfiguration

{Servers  =  new List<Uri>  {  new Uri("http://10.211.55.2:8091/pools"),},UseSsl =  false,BucketConfigs =  new Dictionary<string,  BucketConfiguration>

{{

"travel-­‐sample",  new BucketConfiguration{

BucketName =  "travel-­‐sample",UseSsl =  false,Password  =  "",PoolConfiguration =  new PoolConfiguration

{MaxSize =  10,MinSize =  5

}}

}}

};

Page 74: NoSQL's biggest lie: SQL never went away - Martin Esmann

©2014  Couchbase  Inc.

.NET SDK: Basic Operations

74

//  Create   document   (dynamic)var doc  =  new Document<dynamic>{

Id  =  "doc1",Content   =  new{

Id  =  "doc1",Title =  "My Document",Type =  "basic",Pages   =  3

}};

//  update   or  create   documentvar upsertResult =  await bucket.UpsertAsync<dynamic>(doc);

//  Get  documentvar getResult =  bucket.GetDocument<dynamic>("key1");

//  Delete   documentvar deleteResult =  await bucket.RemoveAsync<dynamic>(doc);

//  Check   if  doc/key   exsistsvar exsists =  await bucket.ExistsAsync("key1");

Page 75: NoSQL's biggest lie: SQL never went away - Martin Esmann

©2014  Couchbase  Inc.

.NET SDK: N1QL Query (raw)

75

//  Create  Query  Request.  Raw  API  requestvar query  =  QueryRequest.Create("SELECT  COUNT(*)  FROM  `travel-­‐sample`  WHERE  type  =  'airline'");

//  Execute  Query.  var response  =  await bucket.QueryAsync<dynamic>(query);

//  Convert  result  to  string  for  easy  console  print  out.var result  =  JsonConvert.SerializeObject(response,  Formatting.Indented);

Page 76: NoSQL's biggest lie: SQL never went away - Martin Esmann

©2014  Couchbase  Inc.

.NET SDK: N1QL Query (Linq2Couchbase)

76

//  Lambda  syntax

var result  =  ClusterHelper.GetBucket("beer-­‐sample").Queryable<Beer>().Where(a  =>  a.Type ==  "beer").Select(a  =>  a).Join(

ClusterHelper.GetBucket("beer-­‐sample").Queryable<Brewery>().Where(airline  =>  airline.Type ==  "brewery"),innerKey =>  innerKey.BreweryId,outerKey =>  N1Ql.Key(  outerKey),  (inner,outer)  =>  new {  Inner  =    inner,  Outer  =  outer})

.Take(1)

.ToList();

Page 77: NoSQL's biggest lie: SQL never went away - Martin Esmann

©2014  Couchbase  Inc.

.NET SDK: N1QL Query (Linq2Couchbase)

77

//  Query  syntax

var query  =  from beer  in QueryFactory.Queryable<Beer>(mockBucket.Object)join breweryGroup in QueryFactory.Queryable<Brewery>(mockBucket.Object)on beer.BreweryId equals N1Ql.Key(breweryGroup)  into bgfrom brewery  in bg.DefaultIfEmpty()select new {  beer.Name,  beer.Abv,  BreweryName =  brewery.Name };

Page 78: NoSQL's biggest lie: SQL never went away - Martin Esmann

https://github.com/couchbaselabs/N1QL-­‐Intro-­‐dotNET-­‐Supplement-­‐Demo-­‐code

Page 79: NoSQL's biggest lie: SQL never went away - Martin Esmann

Next Steps

Page 80: NoSQL's biggest lie: SQL never went away - Martin Esmann

Couchbase Developer Portal

developer.couchbase.com

80

Page 81: NoSQL's biggest lie: SQL never went away - Martin Esmann

Forums

http://forums.couchbase.com

81

Page 82: NoSQL's biggest lie: SQL never went away - Martin Esmann

Thanks for listening!