N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

78
N1QL and SDK Support for Java, .NET and Node.js Simon Baslé, Jeff Morris and Todd Greenstein Couchbase

Transcript of N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

Page 1: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

N1QL and SDK Support for Java, .NET and Node.js

Simon Baslé, Jeff Morris and Todd Greenstein Couchbase

Page 2: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

N1QL Primer

Page 3: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

©2015 Couchbase Inc. 3

N1QL Primer

3

NoSQL...

Page 4: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

©2015 Couchbase Inc. 4

N1QL Primer

4

NoSQLbut why not a

query language

?

Page 5: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

©2015 Couchbase Inc. 5

N1QL Primer

5

querying on

relational data=

SQL

Page 6: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

©2015 Couchbase Inc. 6

N1QL Primer

6

querying on

JSON=

N1QL

Page 7: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

©2015 Couchbase Inc. 7

N1QL Primer

7

SELECT fname, children FROM tutorial

Page 8: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

©2015 Couchbase Inc. 8

N1QL Primer

8

SELECT fname, children FROM tutorial

looks like SQL?

Page 9: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

©2015 Couchbase Inc. 9

N1QL Primer

9

SELECT fname, children FROM tutorial

WHERE EVERY child IN tutorial.children

SATISFIES child.age > 10 END

Page 10: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

©2015 Couchbase Inc. 10

N1QL Primer

10

cbq-enginethe N1QL service

Page 11: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

©2015 Couchbase Inc. 11

N1QL Primer

11

cbq-enginethe N1QL service

integrated in Couchbase 4.0, port 8093

Page 12: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

©2015 Couchbase Inc. 12

N1QL Primer

12

cbqcommand line client

Page 13: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

©2015 Couchbase Inc. 13

N1QL Primer

13

responses are

JSON

Page 14: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

©2015 Couchbase Inc. 14

N1QL Primer

14

{ "results": [ { "children": [ { "age": 17, "fname": "Abama", "gender": "m" }, { "age": 21, "fname": "Bebama", "gender": "m" } ], "fname": "Ian" } ]}

responses are

JSON

Page 15: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

©2015 Couchbase Inc. 15

N1QL Primer

15

responses have

MetaData

Page 16: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

©2015 Couchbase Inc. 16

N1QL Primer

16

"requestID": "19bb1a64-633a-417a-833c-97a8b4c48e7e", "signature": { "Greeting": "string" }, "results": [ {"Greeting": "Hello World" "status": "success", "metrics": { "elapsedTime": "7.48ms", "executionTime": "6.8ms", "resultCount": 1, "resultSize": 49 }}

responses have

MetaData

Page 17: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

Usage in the SDKs

Page 18: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

©2015 Couchbase Inc. 18

Usage in the SDKs

18

C / C++

Page 19: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

©2015 Couchbase Inc. 19

Usage in the SDKs

19

Page 20: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

©2015 Couchbase Inc. 20

Usage in the SDKs

20

API at the

Bucketlevel

Page 21: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

©2015 Couchbase Inc. 21

Usage in the SDKs

21

Statementin a

Querywith additional

Parameters

Page 22: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

©2015 Couchbase Inc. 22

Usage in the SDKs

22

Statements can have

Placeholders

Page 23: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

©2015 Couchbase Inc. 23

Usage in the SDKs

23

either

positional $1

Page 24: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

©2015 Couchbase Inc. 24

Usage in the SDKs

24

or

named$param

Page 25: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

©2015 Couchbase Inc. 25

Usage in the SDKs

25

Placeholder values

are passed in the query's parameters

Page 26: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

©2015 Couchbase Inc. 26

Usage in the SDKs

26

Statements can be

Prepared

Page 27: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

©2015 Couchbase Inc. 27

Usage in the SDKs

27

get a QueryPlanhold to it

then execute it

repeatedly

Page 28: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

NodeJS

N1QL in

Ottoman NodeJS ODM for Couchbase

Page 29: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

©2015 Couchbase Inc. 29

NodeJS

29

Class:N1qlQuery

Running N1QL Queries using the Node Client

Page 30: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

©2015 Couchbase Inc. 30

NodeJS

30

// Instantiate The Query API

var couchbase = require('couchbase');var myCluster = new couchbase.Cluster(‘localhost:8091”);var myBucket = myCluster.openBucket(‘travel-sample’);var myQuery = couchbase.N1qlQuery;

Page 31: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

©2015 Couchbase Inc. 31

NodeJS

31

function query(sql,done){ var queryToRun = myQuery.fromString(sql) .consistency(myQuery.Consistency.REQUEST_PLUS); myBucket.query(queryToRun,function(err,result){ if (err) { console.log("ERR:",err); done(err,null); return; } done(null,result); return; });}

Page 32: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

©2015 Couchbase Inc. 32

NodeJS

32

function query(sql,done){ var queryToRun = myQuery.fromString(sql) .consistency(myQuery.Consistency.REQUEST_PLUS); myBucket.query(queryToRun,function(err,result){ if (err) { console.log("ERR:",err); done(err,null); return; } done(null,result); return; });}

Page 33: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

©2015 Couchbase Inc. 33

NodeJS

33

function query(sql,done){ var queryToRun = myQuery.fromString(sql) .consistency(myQuery.Consistency.REQUEST_PLUS); myBucket.query(queryToRun,function(err,result){ if (err) { console.log("ERR:",err); done(err,null); return; } done(null,result); return; });}

Page 34: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

©2015 Couchbase Inc. 34

NodeJS

34

function query(sql,done){ var queryToRun = myQuery.fromString(sql) .consistency(myQuery.Consistency.REQUEST_PLUS); myBucket.query(queryToRun,function(err,result){ if (err) { console.log("ERR:",err); done(err,null); return; } done(null,result); return; });}

Page 35: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

©2015 Couchbase Inc. 35

NodeJS

35

function query(sql,done){ var queryToRun = myQuery.fromString(sql) .consistency(myQuery.Consistency.REQUEST_PLUS); myBucket.query(queryToRun,function(err,result){ if (err) { console.log("ERR:",err); done(err,null); return; } done(null,result); return; });}

Page 36: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

©2015 Couchbase Inc. 36

NodeJS

36

function query(sql,done){ var queryToRun = myQuery.fromString(sql) .consistency(myQuery.Consistency.REQUEST_PLUS); myBucket.query(queryToRun,function(err,result){ if (err) { console.log("ERR:",err); done(err,null); return; } done(null,result); return; });}

Page 37: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

©2015 Couchbase Inc. 37

NodeJS

37

function query(sql,done){ var queryToRun = myQuery.fromString(sql) .consistency(myQuery.Consistency.REQUEST_PLUS); myBucket.query(queryToRun,function(err,result){ if (err) { console.log("ERR:",err); done(err,null); return; } done(null,result); return; });}

Page 38: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

Demo

Page 39: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

Java

N1QL in

Page 40: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

©2015 Couchbase Inc. 40

Java

40

Statementor QueryPlan

Page 41: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

©2015 Couchbase Inc. 41

Java

41

[+ QueryParams ]

Page 42: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

©2015 Couchbase Inc. 42

Java

42

[+ JsonArray ]for placeholder values

Page 43: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

©2015 Couchbase Inc. 43

Java

43

Query=

Page 44: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

©2015 Couchbase Inc. 44

Java

44

Query.simple(...)

Page 45: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

©2015 Couchbase Inc. 45

Java

45

Query.parametrized(...)

Page 46: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

©2015 Couchbase Inc. 46

Java

46

Query.prepared(...)

Page 47: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

©2015 Couchbase Inc. 47

Java

47

the QueryResult

Page 48: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

©2015 Couchbase Inc. 48

Java

48

the QueryResultparseSuccess()

finalSuccess()

Page 49: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

©2015 Couchbase Inc. 49

Java

49

the QueryResultallRows()errors()info()

Page 50: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

©2015 Couchbase Inc. 50

Java

50

the QueryResultrequestId()

clientContextId()signature()

Page 51: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

©2015 Couchbase Inc. 51

Java

51

the QueryResult can be an

AsyncQueryResult

Page 52: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

©2015 Couchbase Inc. 52

Java

52

the DSLbuilding SELECT statements with code

Page 53: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

©2015 Couchbase Inc. 53

Java

53

import static Select.select;

import static Expression.*;

Page 54: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

©2015 Couchbase Inc. 54

Java

54

SELECT *FROM `bucket`

WHERE token > 100

Page 55: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

©2015 Couchbase Inc. 55

Java

55

select("*").from(i("bucket"))

.where(x("token").gt(100))

Page 56: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

©2015 Couchbase Inc. 56

Java

56

select("*").from(i("bucket"))

.where(x("token").gt(100))

Page 57: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

©2015 Couchbase Inc. 57

Java

57

select("*").from(i("bucket"))

.where(x("token").gt(100))

Page 58: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

©2015 Couchbase Inc. 58

Java

58

select("*").from(i("bucket"))

.where(x("token").gt(100))

Page 59: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

©2015 Couchbase Inc. 59

Java

59

select("*").from(i("bucket"))

.where(x("token").gt(100))

Page 60: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

©2015 Couchbase Inc. 60

Java

60

select("*").from(i("bucket"))

.where(x("token").gt(100))

Page 61: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

©2015 Couchbase Inc. 61

Java

61

select("*").from(i("bucket"))

.where(x("token").gt(100))

Page 62: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

Demo

Page 63: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

.Net

N1QL in

Page 64: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

©2015 Couchbase Inc. 64

.NET

64

[1]SDKand [2]Extension

Support

Page 65: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

©2015 Couchbase Inc. 65

.NET [1]

65

[1]QueryRequest

API in the .NET SDK

Page 66: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

©2015 Couchbase Inc. 66

.NET [1]

66

AD HOC queries

Via IBucket

Page 67: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

©2015 Couchbase Inc. 67

.NET [1]

67

Support for all N1QL Features

Page 68: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

©2015 Couchbase Inc. 68

.NET [1]

68

A Wrapper around the

REST API

Page 69: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

©2015 Couchbase Inc. 69

.NET [1]

69

But adds:

Type safety Intellisense Support

Fluent interface Data Mapper

Page 70: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

©2015 Couchbase Inc. 70

.NET [2]

70

[2] LINQProvider

Page 71: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

©2015 Couchbase Inc. 71

.NET [2]

71

Based off Re-Linq

[EF and NHibernate are too!]

Page 72: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

©2015 Couchbase Inc. 72

.NET [2]

72

E x t e n ds

The .NET SDK

Page 73: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

©2015 Couchbase Inc. 73

.NET [2]

73

Open Source[do contribute]

Page 74: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

©2015 Couchbase Inc. 74

.NET [2]

74

Provides a

SubsetOf the N1QL Language…

[currently, but that will change]

Page 75: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

©2015 Couchbase Inc. 75

.NET [2]

75

Also, Requires: Domain Model == JSON

structure

Page 76: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

Demo

Page 77: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

Q&A

Page 78: N1QL and SDK Support for Java, .NET, and Node.js: Couchbase Connect 2015

Thank you.