SlideShare ist ein Scribd-Unternehmen logo
1 von 14
sunscrapers.comYour favored tech partner.
PostgreSQL and JSON
with Python
Przemek Lewandowski
16.06.2016
sunscrapers.comYour favored tech partner.
1. Whys
2. PostgreSQL types
3. HStore vs JSON vs JSONB
4. SQLAlchemy
5. Django
Overview
sunscrapers.comYour favored tech partner.
● Schema-less data
● Schema-unknown data
● Document storage
● User defined schema
Why?
sunscrapers.comYour favored tech partner.
● XML (since 8.2)
● HStore (since 8.2)
● JSON (since 9.2)
● JSONB (since 9.4)
PostgreSQL types
sunscrapers.comYour favored tech partner.
● Only simple key/value pairs
● No nesting
● Strings only
● Indexing
● Many functions and operators
HStore
SELECT 'user => "fred", id => 1'::hstore -> 'user' AS user;
user
------
fred
sunscrapers.comYour favored tech partner.
● Simple validation on input
● Stored as text (like XML)
● Preserves key order and duplicates
● Indexes (only expression index)
JSON
SELECT '[{"a": {"b":{"c": "foo"}}}'::json#>'{a,b}';
?column?
-------------
{"c": "foo"}
sunscrapers.comYour favored tech partner.
● Full JSON implementation
● Binary storage
● No key order or duplicate preservation
● Fast access operations
● Indexing
● No date type!
JSONB
SELECT '{"a": {"b": 2, "c": 3}}'::jsonb @> '{"a": {"c": 3}}'::jsonb;
sunscrapers.comYour favored tech partner.
SELECT feed_data.feed_id AS feed_data_feed_id,
feed_data.feed AS feed_data_feed,
feed_data.data AS feed_data_data
FROM Feed_data
WHERE CAST(feed_data.data ->> 'DISSEMINATION_ID' AS INTEGER) <= 90000
AND (feed_data.data ->> 'ACTION') = 'NEW'
ORDER BY feed_data.data -> 'PRICE_NOTATION' DESC
LIMIT 100 OFFSET 0
Query example
sunscrapers.comYour favored tech partner.
SQLAlchemy
● PostgreSQL dialect
● Close to database
● HStore, JSON, JSONB data types and much more
sunscrapers.comYour favored tech partner.
from sqlalchemy.dialects.postgresql import JSONB
# …
class FeedData(db.Model):
feed_id = Column(String(), primary_key=True)
feed = Column(Enum(*feed_urls.keys(), name='feeds_enum'), nullable=False)
data = Column(MutableDict.as_mutable(JSONB), nullable=False)
def __init__(self, feed_id, feed, data):
self.feed_id = feed_id
self.feed = feed
self.data = data
def as_dict(self):
data = {field: getattr(self, field) for field in ['feed_id', 'feed']}
data.update(self.data)
return data
SQLAlchemy ORM example I
sunscrapers.comYour favored tech partner.
def filter_by_date(db_field, dt_from, dt_to):
return and_(
db_field.cast(Date) >= dt_from,
db_field.cast(Date) <= dt_to
)
SQLAlchemy ORM example II
sunscrapers.comYour favored tech partner.
Django ORM
● Django introduced PostgreSQL specific model fields
● HStoreField since 1.8
● JSONField since 1.9 (uses JSONB)
● Support for migrations
● Third party libs like: django-hstore
sunscrapers.comYour favored tech partner.
More info
● https://www.pgcon.org/2014/schedule/events/684.en.html
● https://www.postgresql.org/docs/9.4/static/datatype-json.html
● https://docs.djangoproject.com/en/1.9/ref/contrib/postgres/fields/
● http://docs.sqlalchemy.org/en/latest/dialects/postgresql.html
sunscrapers.comYour favored tech partner.
Thanks!
Questions?

Weitere ähnliche Inhalte

Was ist angesagt?

2011 mongo FR - scaling with mongodb
2011 mongo FR - scaling with mongodb2011 mongo FR - scaling with mongodb
2011 mongo FR - scaling with mongodbantoinegirbal
 
Fall 2016 ats summit - Parent & Origin Selection
Fall 2016 ats summit  - Parent & Origin SelectionFall 2016 ats summit  - Parent & Origin Selection
Fall 2016 ats summit - Parent & Origin SelectionThomas Jackson
 
Mongo db php_shaken_not_stirred_joomlafrappe
Mongo db php_shaken_not_stirred_joomlafrappeMongo db php_shaken_not_stirred_joomlafrappe
Mongo db php_shaken_not_stirred_joomlafrappeSpyros Passas
 
PostgreSQL and Sphinx pgcon 2013
PostgreSQL and Sphinx   pgcon 2013PostgreSQL and Sphinx   pgcon 2013
PostgreSQL and Sphinx pgcon 2013Emanuel Calvo
 
Materialized views in PostgreSQL
Materialized views in PostgreSQLMaterialized views in PostgreSQL
Materialized views in PostgreSQLAshutosh Bapat
 
Elasticsearch 101 - Cluster setup and tuning
Elasticsearch 101 - Cluster setup and tuningElasticsearch 101 - Cluster setup and tuning
Elasticsearch 101 - Cluster setup and tuningPetar Djekic
 
2011 Mongo FR - Indexing in MongoDB
2011 Mongo FR - Indexing in MongoDB2011 Mongo FR - Indexing in MongoDB
2011 Mongo FR - Indexing in MongoDBantoinegirbal
 
Hands On Spring Data
Hands On Spring DataHands On Spring Data
Hands On Spring DataEric Bottard
 
PostgreSQL FTS Solutions FOSDEM 2013 - PGDAY
PostgreSQL FTS Solutions FOSDEM 2013 - PGDAYPostgreSQL FTS Solutions FOSDEM 2013 - PGDAY
PostgreSQL FTS Solutions FOSDEM 2013 - PGDAYEmanuel Calvo
 
10 Key MongoDB Performance Indicators
10 Key MongoDB Performance Indicators  10 Key MongoDB Performance Indicators
10 Key MongoDB Performance Indicators iammutex
 
Mongo Presentation by Metatagg Solutions
Mongo Presentation by Metatagg SolutionsMongo Presentation by Metatagg Solutions
Mongo Presentation by Metatagg SolutionsMetatagg Solutions
 
Terms of endearment - the ElasticSearch Query DSL explained
Terms of endearment - the ElasticSearch Query DSL explainedTerms of endearment - the ElasticSearch Query DSL explained
Terms of endearment - the ElasticSearch Query DSL explainedclintongormley
 
Retrieving Information From Solr
Retrieving Information From SolrRetrieving Information From Solr
Retrieving Information From SolrRamzi Alqrainy
 
Elastic Search
Elastic SearchElastic Search
Elastic SearchNavule Rao
 
PhpstudyTokyo MongoDB PHP CakePHP
PhpstudyTokyo MongoDB PHP CakePHPPhpstudyTokyo MongoDB PHP CakePHP
PhpstudyTokyo MongoDB PHP CakePHPichikaway
 

Was ist angesagt? (20)

Python and MongoDB
Python and MongoDBPython and MongoDB
Python and MongoDB
 
9.4json
9.4json9.4json
9.4json
 
2011 mongo FR - scaling with mongodb
2011 mongo FR - scaling with mongodb2011 mongo FR - scaling with mongodb
2011 mongo FR - scaling with mongodb
 
Fall 2016 ats summit - Parent & Origin Selection
Fall 2016 ats summit  - Parent & Origin SelectionFall 2016 ats summit  - Parent & Origin Selection
Fall 2016 ats summit - Parent & Origin Selection
 
Mondodb
MondodbMondodb
Mondodb
 
Mongo db php_shaken_not_stirred_joomlafrappe
Mongo db php_shaken_not_stirred_joomlafrappeMongo db php_shaken_not_stirred_joomlafrappe
Mongo db php_shaken_not_stirred_joomlafrappe
 
PostgreSQL and Sphinx pgcon 2013
PostgreSQL and Sphinx   pgcon 2013PostgreSQL and Sphinx   pgcon 2013
PostgreSQL and Sphinx pgcon 2013
 
Materialized views in PostgreSQL
Materialized views in PostgreSQLMaterialized views in PostgreSQL
Materialized views in PostgreSQL
 
Elasticsearch 101 - Cluster setup and tuning
Elasticsearch 101 - Cluster setup and tuningElasticsearch 101 - Cluster setup and tuning
Elasticsearch 101 - Cluster setup and tuning
 
2011 Mongo FR - Indexing in MongoDB
2011 Mongo FR - Indexing in MongoDB2011 Mongo FR - Indexing in MongoDB
2011 Mongo FR - Indexing in MongoDB
 
Hands On Spring Data
Hands On Spring DataHands On Spring Data
Hands On Spring Data
 
PostgreSQL FTS Solutions FOSDEM 2013 - PGDAY
PostgreSQL FTS Solutions FOSDEM 2013 - PGDAYPostgreSQL FTS Solutions FOSDEM 2013 - PGDAY
PostgreSQL FTS Solutions FOSDEM 2013 - PGDAY
 
MongoDB - Ekino PHP
MongoDB - Ekino PHPMongoDB - Ekino PHP
MongoDB - Ekino PHP
 
10 Key MongoDB Performance Indicators
10 Key MongoDB Performance Indicators  10 Key MongoDB Performance Indicators
10 Key MongoDB Performance Indicators
 
Building a Search Engine Using Lucene
Building a Search Engine Using LuceneBuilding a Search Engine Using Lucene
Building a Search Engine Using Lucene
 
Mongo Presentation by Metatagg Solutions
Mongo Presentation by Metatagg SolutionsMongo Presentation by Metatagg Solutions
Mongo Presentation by Metatagg Solutions
 
Terms of endearment - the ElasticSearch Query DSL explained
Terms of endearment - the ElasticSearch Query DSL explainedTerms of endearment - the ElasticSearch Query DSL explained
Terms of endearment - the ElasticSearch Query DSL explained
 
Retrieving Information From Solr
Retrieving Information From SolrRetrieving Information From Solr
Retrieving Information From Solr
 
Elastic Search
Elastic SearchElastic Search
Elastic Search
 
PhpstudyTokyo MongoDB PHP CakePHP
PhpstudyTokyo MongoDB PHP CakePHPPhpstudyTokyo MongoDB PHP CakePHP
PhpstudyTokyo MongoDB PHP CakePHP
 

Andere mochten auch

Writing MySQL User-defined Functions in JavaScript
Writing MySQL User-defined Functions in JavaScriptWriting MySQL User-defined Functions in JavaScript
Writing MySQL User-defined Functions in JavaScriptRoland Bouman
 
Hcad competencies booklet (2) (1)
Hcad competencies booklet (2) (1)Hcad competencies booklet (2) (1)
Hcad competencies booklet (2) (1)Ngonde
 
Creating value for customers - understanding context
Creating value for customers - understanding contextCreating value for customers - understanding context
Creating value for customers - understanding contextSunscrapers
 
Foundations of Foundation 6 - Jakub Włodaczyk (pl)
Foundations of Foundation 6 - Jakub Włodaczyk (pl)Foundations of Foundation 6 - Jakub Włodaczyk (pl)
Foundations of Foundation 6 - Jakub Włodaczyk (pl)Sunscrapers
 
DVCS Workflows for Teams - Bartek Rychlicki
DVCS Workflows for Teams - Bartek RychlickiDVCS Workflows for Teams - Bartek Rychlicki
DVCS Workflows for Teams - Bartek RychlickiSunscrapers
 
How to justify your recommendation - Łukasz Karwacki
How to justify your recommendation - Łukasz KarwackiHow to justify your recommendation - Łukasz Karwacki
How to justify your recommendation - Łukasz KarwackiSunscrapers
 
Django ORM - Marcin Markiewicz
Django ORM - Marcin Markiewicz Django ORM - Marcin Markiewicz
Django ORM - Marcin Markiewicz Sunscrapers
 
Py2 - Py3 migration - Krzysztof Skarupa
Py2  - Py3 migration - Krzysztof SkarupaPy2  - Py3 migration - Krzysztof Skarupa
Py2 - Py3 migration - Krzysztof SkarupaSunscrapers
 
Jaka przyszłość czeka polskich programistów?
Jaka przyszłość czeka polskich programistów?Jaka przyszłość czeka polskich programistów?
Jaka przyszłość czeka polskich programistów?Sunscrapers
 
Introduction to ReactJS - Comparison to AngularJS 2 - Robert Piękoś (pl)
Introduction to ReactJS - Comparison to AngularJS 2 - Robert Piękoś (pl)Introduction to ReactJS - Comparison to AngularJS 2 - Robert Piękoś (pl)
Introduction to ReactJS - Comparison to AngularJS 2 - Robert Piękoś (pl)Sunscrapers
 
Reactive programming
Reactive programmingReactive programming
Reactive programmingSunscrapers
 
Geek Sync | Using PowerShell with Python and SQL Server
Geek Sync | Using PowerShell with Python and SQL ServerGeek Sync | Using PowerShell with Python and SQL Server
Geek Sync | Using PowerShell with Python and SQL ServerIDERA Software
 
LinkedIn TBC JavaScript 100: Functions
 LinkedIn TBC JavaScript 100: Functions LinkedIn TBC JavaScript 100: Functions
LinkedIn TBC JavaScript 100: FunctionsAdam Crabtree
 

Andere mochten auch (14)

Writing MySQL User-defined Functions in JavaScript
Writing MySQL User-defined Functions in JavaScriptWriting MySQL User-defined Functions in JavaScript
Writing MySQL User-defined Functions in JavaScript
 
Hcad competencies booklet (2) (1)
Hcad competencies booklet (2) (1)Hcad competencies booklet (2) (1)
Hcad competencies booklet (2) (1)
 
Creating value for customers - understanding context
Creating value for customers - understanding contextCreating value for customers - understanding context
Creating value for customers - understanding context
 
Foundations of Foundation 6 - Jakub Włodaczyk (pl)
Foundations of Foundation 6 - Jakub Włodaczyk (pl)Foundations of Foundation 6 - Jakub Włodaczyk (pl)
Foundations of Foundation 6 - Jakub Włodaczyk (pl)
 
DVCS Workflows for Teams - Bartek Rychlicki
DVCS Workflows for Teams - Bartek RychlickiDVCS Workflows for Teams - Bartek Rychlicki
DVCS Workflows for Teams - Bartek Rychlicki
 
How to justify your recommendation - Łukasz Karwacki
How to justify your recommendation - Łukasz KarwackiHow to justify your recommendation - Łukasz Karwacki
How to justify your recommendation - Łukasz Karwacki
 
Django ORM - Marcin Markiewicz
Django ORM - Marcin Markiewicz Django ORM - Marcin Markiewicz
Django ORM - Marcin Markiewicz
 
Py2 - Py3 migration - Krzysztof Skarupa
Py2  - Py3 migration - Krzysztof SkarupaPy2  - Py3 migration - Krzysztof Skarupa
Py2 - Py3 migration - Krzysztof Skarupa
 
Jaka przyszłość czeka polskich programistów?
Jaka przyszłość czeka polskich programistów?Jaka przyszłość czeka polskich programistów?
Jaka przyszłość czeka polskich programistów?
 
Interruptions
InterruptionsInterruptions
Interruptions
 
Introduction to ReactJS - Comparison to AngularJS 2 - Robert Piękoś (pl)
Introduction to ReactJS - Comparison to AngularJS 2 - Robert Piękoś (pl)Introduction to ReactJS - Comparison to AngularJS 2 - Robert Piękoś (pl)
Introduction to ReactJS - Comparison to AngularJS 2 - Robert Piękoś (pl)
 
Reactive programming
Reactive programmingReactive programming
Reactive programming
 
Geek Sync | Using PowerShell with Python and SQL Server
Geek Sync | Using PowerShell with Python and SQL ServerGeek Sync | Using PowerShell with Python and SQL Server
Geek Sync | Using PowerShell with Python and SQL Server
 
LinkedIn TBC JavaScript 100: Functions
 LinkedIn TBC JavaScript 100: Functions LinkedIn TBC JavaScript 100: Functions
LinkedIn TBC JavaScript 100: Functions
 

Ähnlich wie PostgreSQL and JSON with Python - Przemek Lewandowski

Postgres vs Mongo / Олег Бартунов (Postgres Professional)
Postgres vs Mongo / Олег Бартунов (Postgres Professional)Postgres vs Mongo / Олег Бартунов (Postgres Professional)
Postgres vs Mongo / Олег Бартунов (Postgres Professional)Ontico
 
Working with JSON Data in PostgreSQL vs. MongoDB
Working with JSON Data in PostgreSQL vs. MongoDBWorking with JSON Data in PostgreSQL vs. MongoDB
Working with JSON Data in PostgreSQL vs. MongoDBScaleGrid.io
 
Webscale PostgreSQL - JSONB and Horizontal Scaling Strategies
Webscale PostgreSQL - JSONB and Horizontal Scaling StrategiesWebscale PostgreSQL - JSONB and Horizontal Scaling Strategies
Webscale PostgreSQL - JSONB and Horizontal Scaling StrategiesJonathan Katz
 
Spark & Cassandra - DevFest Córdoba
Spark & Cassandra - DevFest CórdobaSpark & Cassandra - DevFest Córdoba
Spark & Cassandra - DevFest CórdobaJose Mº Muñoz
 
Database madness with_mongoengine_and_sql_alchemy
Database madness with_mongoengine_and_sql_alchemyDatabase madness with_mongoengine_and_sql_alchemy
Database madness with_mongoengine_and_sql_alchemyJaime Buelta
 
Introducing Apache Spark's Data Frames and Dataset APIs workshop series
Introducing Apache Spark's Data Frames and Dataset APIs workshop seriesIntroducing Apache Spark's Data Frames and Dataset APIs workshop series
Introducing Apache Spark's Data Frames and Dataset APIs workshop seriesHolden Karau
 
JSON Processing in the Database using PostgreSQL 9.4 :: Data Wranglers DC :: ...
JSON Processing in the Database using PostgreSQL 9.4 :: Data Wranglers DC :: ...JSON Processing in the Database using PostgreSQL 9.4 :: Data Wranglers DC :: ...
JSON Processing in the Database using PostgreSQL 9.4 :: Data Wranglers DC :: ...Ryan B Harvey, CSDP, CSM
 
Postgresql Database Administration- Day3
Postgresql Database Administration- Day3Postgresql Database Administration- Day3
Postgresql Database Administration- Day3PoguttuezhiniVP
 
PostgreSQL's Secret NoSQL Superpowers
PostgreSQL's Secret NoSQL SuperpowersPostgreSQL's Secret NoSQL Superpowers
PostgreSQL's Secret NoSQL SuperpowersAmanda Gilmore
 
Searching for AI - Leveraging Solr for classic Artificial Intelligence tasks
Searching for AI - Leveraging Solr for classic Artificial Intelligence tasksSearching for AI - Leveraging Solr for classic Artificial Intelligence tasks
Searching for AI - Leveraging Solr for classic Artificial Intelligence tasksAlexandre Rafalovitch
 
Speed Up Your APEX Apps with JSON and Handlebars
Speed Up Your APEX Apps with JSON and HandlebarsSpeed Up Your APEX Apps with JSON and Handlebars
Speed Up Your APEX Apps with JSON and HandlebarsMarko Gorički
 
Introduction to Spark Datasets - Functional and relational together at last
Introduction to Spark Datasets - Functional and relational together at lastIntroduction to Spark Datasets - Functional and relational together at last
Introduction to Spark Datasets - Functional and relational together at lastHolden Karau
 
Apache Drill @ PJUG, Jan 15, 2013
Apache Drill @ PJUG, Jan 15, 2013Apache Drill @ PJUG, Jan 15, 2013
Apache Drill @ PJUG, Jan 15, 2013Gera Shegalov
 
CREATE INDEX … USING VODKA. VODKA CONNECTING INDEXES, Олег Бартунов, Александ...
CREATE INDEX … USING VODKA. VODKA CONNECTING INDEXES, Олег Бартунов, Александ...CREATE INDEX … USING VODKA. VODKA CONNECTING INDEXES, Олег Бартунов, Александ...
CREATE INDEX … USING VODKA. VODKA CONNECTING INDEXES, Олег Бартунов, Александ...Ontico
 
Google apps script database abstraction exposed version
Google apps script database abstraction   exposed versionGoogle apps script database abstraction   exposed version
Google apps script database abstraction exposed versionBruce McPherson
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDBantoinegirbal
 
2011 Mongo FR - MongoDB introduction
2011 Mongo FR - MongoDB introduction2011 Mongo FR - MongoDB introduction
2011 Mongo FR - MongoDB introductionantoinegirbal
 

Ähnlich wie PostgreSQL and JSON with Python - Przemek Lewandowski (20)

Postgres vs Mongo / Олег Бартунов (Postgres Professional)
Postgres vs Mongo / Олег Бартунов (Postgres Professional)Postgres vs Mongo / Олег Бартунов (Postgres Professional)
Postgres vs Mongo / Олег Бартунов (Postgres Professional)
 
Working with JSON Data in PostgreSQL vs. MongoDB
Working with JSON Data in PostgreSQL vs. MongoDBWorking with JSON Data in PostgreSQL vs. MongoDB
Working with JSON Data in PostgreSQL vs. MongoDB
 
Webscale PostgreSQL - JSONB and Horizontal Scaling Strategies
Webscale PostgreSQL - JSONB and Horizontal Scaling StrategiesWebscale PostgreSQL - JSONB and Horizontal Scaling Strategies
Webscale PostgreSQL - JSONB and Horizontal Scaling Strategies
 
Spark & Cassandra - DevFest Córdoba
Spark & Cassandra - DevFest CórdobaSpark & Cassandra - DevFest Córdoba
Spark & Cassandra - DevFest Córdoba
 
Database madness with_mongoengine_and_sql_alchemy
Database madness with_mongoengine_and_sql_alchemyDatabase madness with_mongoengine_and_sql_alchemy
Database madness with_mongoengine_and_sql_alchemy
 
Introducing Apache Spark's Data Frames and Dataset APIs workshop series
Introducing Apache Spark's Data Frames and Dataset APIs workshop seriesIntroducing Apache Spark's Data Frames and Dataset APIs workshop series
Introducing Apache Spark's Data Frames and Dataset APIs workshop series
 
Jquery 4
Jquery 4Jquery 4
Jquery 4
 
JSON Processing in the Database using PostgreSQL 9.4 :: Data Wranglers DC :: ...
JSON Processing in the Database using PostgreSQL 9.4 :: Data Wranglers DC :: ...JSON Processing in the Database using PostgreSQL 9.4 :: Data Wranglers DC :: ...
JSON Processing in the Database using PostgreSQL 9.4 :: Data Wranglers DC :: ...
 
Postgresql Database Administration- Day3
Postgresql Database Administration- Day3Postgresql Database Administration- Day3
Postgresql Database Administration- Day3
 
PostgreSQL's Secret NoSQL Superpowers
PostgreSQL's Secret NoSQL SuperpowersPostgreSQL's Secret NoSQL Superpowers
PostgreSQL's Secret NoSQL Superpowers
 
Searching for AI - Leveraging Solr for classic Artificial Intelligence tasks
Searching for AI - Leveraging Solr for classic Artificial Intelligence tasksSearching for AI - Leveraging Solr for classic Artificial Intelligence tasks
Searching for AI - Leveraging Solr for classic Artificial Intelligence tasks
 
Speed Up Your APEX Apps with JSON and Handlebars
Speed Up Your APEX Apps with JSON and HandlebarsSpeed Up Your APEX Apps with JSON and Handlebars
Speed Up Your APEX Apps with JSON and Handlebars
 
Introduction to Spark Datasets - Functional and relational together at last
Introduction to Spark Datasets - Functional and relational together at lastIntroduction to Spark Datasets - Functional and relational together at last
Introduction to Spark Datasets - Functional and relational together at last
 
Apache Drill @ PJUG, Jan 15, 2013
Apache Drill @ PJUG, Jan 15, 2013Apache Drill @ PJUG, Jan 15, 2013
Apache Drill @ PJUG, Jan 15, 2013
 
CREATE INDEX … USING VODKA. VODKA CONNECTING INDEXES, Олег Бартунов, Александ...
CREATE INDEX … USING VODKA. VODKA CONNECTING INDEXES, Олег Бартунов, Александ...CREATE INDEX … USING VODKA. VODKA CONNECTING INDEXES, Олег Бартунов, Александ...
CREATE INDEX … USING VODKA. VODKA CONNECTING INDEXES, Олег Бартунов, Александ...
 
Oracle adapters for Ruby ORMs
Oracle adapters for Ruby ORMsOracle adapters for Ruby ORMs
Oracle adapters for Ruby ORMs
 
Google apps script database abstraction exposed version
Google apps script database abstraction   exposed versionGoogle apps script database abstraction   exposed version
Google apps script database abstraction exposed version
 
An Introduction to Postgresql
An Introduction to PostgresqlAn Introduction to Postgresql
An Introduction to Postgresql
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
 
2011 Mongo FR - MongoDB introduction
2011 Mongo FR - MongoDB introduction2011 Mongo FR - MongoDB introduction
2011 Mongo FR - MongoDB introduction
 

Kürzlich hochgeladen

VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnAmarnathKambale
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...masabamasaba
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionOnePlan Solutions
 
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...Nitya salvi
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesVictorSzoltysek
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024Mind IT Systems
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdfPearlKirahMaeRagusta1
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrainmasabamasaba
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...Shane Coughlan
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...masabamasaba
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrandmasabamasaba
 
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...masabamasaba
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
SHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions PresentationSHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions PresentationShrmpro
 
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfayushiqss
 

Kürzlich hochgeladen (20)

VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
SHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions PresentationSHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions Presentation
 
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
 

PostgreSQL and JSON with Python - Przemek Lewandowski

  • 1. sunscrapers.comYour favored tech partner. PostgreSQL and JSON with Python Przemek Lewandowski 16.06.2016
  • 2. sunscrapers.comYour favored tech partner. 1. Whys 2. PostgreSQL types 3. HStore vs JSON vs JSONB 4. SQLAlchemy 5. Django Overview
  • 3. sunscrapers.comYour favored tech partner. ● Schema-less data ● Schema-unknown data ● Document storage ● User defined schema Why?
  • 4. sunscrapers.comYour favored tech partner. ● XML (since 8.2) ● HStore (since 8.2) ● JSON (since 9.2) ● JSONB (since 9.4) PostgreSQL types
  • 5. sunscrapers.comYour favored tech partner. ● Only simple key/value pairs ● No nesting ● Strings only ● Indexing ● Many functions and operators HStore SELECT 'user => "fred", id => 1'::hstore -> 'user' AS user; user ------ fred
  • 6. sunscrapers.comYour favored tech partner. ● Simple validation on input ● Stored as text (like XML) ● Preserves key order and duplicates ● Indexes (only expression index) JSON SELECT '[{"a": {"b":{"c": "foo"}}}'::json#>'{a,b}'; ?column? ------------- {"c": "foo"}
  • 7. sunscrapers.comYour favored tech partner. ● Full JSON implementation ● Binary storage ● No key order or duplicate preservation ● Fast access operations ● Indexing ● No date type! JSONB SELECT '{"a": {"b": 2, "c": 3}}'::jsonb @> '{"a": {"c": 3}}'::jsonb;
  • 8. sunscrapers.comYour favored tech partner. SELECT feed_data.feed_id AS feed_data_feed_id, feed_data.feed AS feed_data_feed, feed_data.data AS feed_data_data FROM Feed_data WHERE CAST(feed_data.data ->> 'DISSEMINATION_ID' AS INTEGER) <= 90000 AND (feed_data.data ->> 'ACTION') = 'NEW' ORDER BY feed_data.data -> 'PRICE_NOTATION' DESC LIMIT 100 OFFSET 0 Query example
  • 9. sunscrapers.comYour favored tech partner. SQLAlchemy ● PostgreSQL dialect ● Close to database ● HStore, JSON, JSONB data types and much more
  • 10. sunscrapers.comYour favored tech partner. from sqlalchemy.dialects.postgresql import JSONB # … class FeedData(db.Model): feed_id = Column(String(), primary_key=True) feed = Column(Enum(*feed_urls.keys(), name='feeds_enum'), nullable=False) data = Column(MutableDict.as_mutable(JSONB), nullable=False) def __init__(self, feed_id, feed, data): self.feed_id = feed_id self.feed = feed self.data = data def as_dict(self): data = {field: getattr(self, field) for field in ['feed_id', 'feed']} data.update(self.data) return data SQLAlchemy ORM example I
  • 11. sunscrapers.comYour favored tech partner. def filter_by_date(db_field, dt_from, dt_to): return and_( db_field.cast(Date) >= dt_from, db_field.cast(Date) <= dt_to ) SQLAlchemy ORM example II
  • 12. sunscrapers.comYour favored tech partner. Django ORM ● Django introduced PostgreSQL specific model fields ● HStoreField since 1.8 ● JSONField since 1.9 (uses JSONB) ● Support for migrations ● Third party libs like: django-hstore
  • 13. sunscrapers.comYour favored tech partner. More info ● https://www.pgcon.org/2014/schedule/events/684.en.html ● https://www.postgresql.org/docs/9.4/static/datatype-json.html ● https://docs.djangoproject.com/en/1.9/ref/contrib/postgres/fields/ ● http://docs.sqlalchemy.org/en/latest/dialects/postgresql.html
  • 14. sunscrapers.comYour favored tech partner. Thanks! Questions?