postgrtesql as a nosql document store - the json/jsonb data type

24
Postgres – There is JavaScript in my SQL!

Upload: jumping-bean

Post on 15-Apr-2017

254 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Postgrtesql as a NoSQL Document Store - The JSON/JSONB data type

Postgres – There is JavaScript in my SQL!

Page 2: Postgrtesql as a NoSQL Document Store - The JSON/JSONB data type

About Me

● Solutions integrator at Jumping Bean– Developer & Trainer– Social Media

● Twitter @mxc4● Twitter

@jumpingbeansa● LinkedIn

Page 3: Postgrtesql as a NoSQL Document Store - The JSON/JSONB data type

What is JSON?

● JSON -> JavaScript Object Notation,● Used to transfer data in JavaScript,● Light weight way to marshal JavaScript objects,● Widely used in REST services,● Used as generic data transfer object for

unstructured data

Page 4: Postgrtesql as a NoSQL Document Store - The JSON/JSONB data type

Why store JSON?

● Need to store semi-structured data ● Reduce time to market

– Ability to define data requirements “in-flight● Need to store data from REST API,● All the arguments for NoSQL

Page 5: Postgrtesql as a NoSQL Document Store - The JSON/JSONB data type

Why Use Postgres for NoSQL?

● Postgres is fast● Postgres is ACID compliant,● Best of both worlds – benefits of relational

database with those of NoSQL databases,● Already invested in Postgres – Why learn

something new?

Page 6: Postgrtesql as a NoSQL Document Store - The JSON/JSONB data type

JSON/JSONB Data Type● Core type● Two data types

– JSON – 9.3● Stores exact copy of input text but must be valid JSON

– JSONB – 9.4● Stores a binary representation of the original input

● Encoding issues:– JSON spec uses UTF-8 encoding,– Database may use different encoding may be incompatibilities

Page 7: Postgrtesql as a NoSQL Document Store - The JSON/JSONB data type

JSON/JSONB

● Pros– JSON

● Ensures its valid JSON,● Copy of original, preserves

white space as well as order of keys and duplicate keys

– JSONB● Faster to process,● Supports indexing,● Tokenised BTree●

● Cons– JSON

● Must be parsed when used in processing functions,

– JSONB● Duplicate keys and

unnecessary white space discarded

Page 8: Postgrtesql as a NoSQL Document Store - The JSON/JSONB data type

Three Types of Functions

● Operators,– Functions to extract elements from JSON

● Creation functions,– Create JSON from database rows, arrays

● Processing functions– Typically to convert from JSON to records or

recordset

Page 9: Postgrtesql as a NoSQL Document Store - The JSON/JSONB data type

JSON/JSONB Operators

Page 10: Postgrtesql as a NoSQL Document Store - The JSON/JSONB data type

JSON/JSONB Operators

Page 11: Postgrtesql as a NoSQL Document Store - The JSON/JSONB data type

JSON/JSONB Operators

Page 12: Postgrtesql as a NoSQL Document Store - The JSON/JSONB data type

Demo Querying Twitter Users

Page 13: Postgrtesql as a NoSQL Document Store - The JSON/JSONB data type

JSON Creation Functions

Page 14: Postgrtesql as a NoSQL Document Store - The JSON/JSONB data type

JSON Processing Functions

Page 15: Postgrtesql as a NoSQL Document Store - The JSON/JSONB data type

JSON Processing Functions

Page 16: Postgrtesql as a NoSQL Document Store - The JSON/JSONB data type

JSON Processing Functions

Page 17: Postgrtesql as a NoSQL Document Store - The JSON/JSONB data type

JSON Processing Functions

Page 18: Postgrtesql as a NoSQL Document Store - The JSON/JSONB data type

Creating Index● Can create GIN (Generalised inverted index) index on JSONB,● Allows use of @ contains operator ,● E.G

– Create index idx01 on docs using GIN(body);● Can create index on specific fields with

– create index inx02 on docs ((doc >'col1')); – Text type index→● Text based index,● Select tuser 'location' from followers where tuser >'location' = “South Africa”→ →

– create index inx03 on docs((doc 'col1')); – JSON type index→● Index on JSON data type● select tuser->'location' from followers where tuser->'location' = '"South Africa"'::jsonb

Page 19: Postgrtesql as a NoSQL Document Store - The JSON/JSONB data type

JavaScript Functions

Page 20: Postgrtesql as a NoSQL Document Store - The JSON/JSONB data type

JavaScript Postgres Functions

● Postgres functions can be written in JavaScript,– Install PLv8 extensions

● “apt-get install postgresql-9.4-plv8”

– Enable extension with:● “Create extensions plv8”

Page 21: Postgrtesql as a NoSQL Document Store - The JSON/JSONB data type

PL/v8● Plv8.execute(sql,[args]);● Plv8.prepare(sql,[args]);

– var stmt = plv8.prepare('Select * from tbl where col1=$1',['text']);– var rows = stmt.execute('foss'); (returns a cursor);– stmt.free(); _> frees the prepared statement

● Cursors operations– fetch([nrows])– move([nrows])– close()

Page 22: Postgrtesql as a NoSQL Document Store - The JSON/JSONB data type

Plv8

● Plv8.return_next() → stores records in internal tuple store,

→ content of store return at end of function● Can also return an array of JSON matching

expected rowtype,● Plv8.elog([level],message[,msg2,...]);

Page 23: Postgrtesql as a NoSQL Document Store - The JSON/JSONB data type

Plv8 Function Example