{"id":305,"date":"2020-07-22T10:00:35","date_gmt":"2020-07-22T17:00:35","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/azure-sql\/?p=305"},"modified":"2020-08-03T11:23:24","modified_gmt":"2020-08-03T18:23:24","slug":"architecting-rest-api-with-python-flask-and-azure-sql","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/azure-sql\/architecting-rest-api-with-python-flask-and-azure-sql\/","title":{"rendered":"Architecting REST API with Python, Flask and Azure SQL"},"content":{"rendered":"<p><img decoding=\"async\" class=\"aligncenter wp-image-307 size-large\" src=\"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2020\/07\/image-01-1024x802.png\" alt=\"REST API with AzureSQL\" width=\"640\" height=\"501\" srcset=\"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2020\/07\/image-01-1024x802.png 1024w, https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2020\/07\/image-01-300x235.png 300w, https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2020\/07\/image-01-768x602.png 768w, https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2020\/07\/image-01.png 1117w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/p>\n<p><a href=\"https:\/\/docs.microsoft.com\/en-us\/sql\/relational-databases\/json\/json-data-sql-server\">Azure SQL has native JSON<\/a>\u00a0support which is a key factor to simplify a lot \u2014 and make developer-friendly \u2014 the interaction between the database and any service that needs to handle data in even the most exotic way.<\/p>\n<p>As depicted in the image above, JSON can be passed as-is and with just one line of code can be sent to Azure SQL where it can processed and returned in a JSON format. I\u2019m using Python in this sample but really, no matter which language you are using for your project, the workflow is the same. And the result is that, as a developer, you won\u2019t see any recordset or anything that resemble a tabular structure in any way. Just JSON, that can easily be serialized and de-serialized into an object for maximum convenience and easiness of development. Or just kept as a JSON document if you prefer.<\/p>\n<p>All the heavy lifting, in fact, is done by Azure SQL for you thanks to functions like\u00a0<a href=\"https:\/\/docs.microsoft.com\/en-us\/sql\/t-sql\/functions\/json-value-transact-sql?view=sql-server-ver15\">JSON_VALUE<\/a>\u00a0and\u00a0<a href=\"https:\/\/docs.microsoft.com\/en-us\/sql\/relational-databases\/json\/format-query-results-as-json-with-for-json-sql-server?view=sql-server-ver15\">FOR JSON<\/a>.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-308 size-large\" src=\"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2020\/07\/image-02-1024x434.png\" alt=\"JSON in Azure SQL\" width=\"640\" height=\"271\" srcset=\"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2020\/07\/image-02-1024x434.png 1024w, https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2020\/07\/image-02-300x127.png 300w, https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2020\/07\/image-02-768x325.png 768w, https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2020\/07\/image-02-1536x651.png 1536w, https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2020\/07\/image-02.png 1895w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/p>\n<p>To make things even easier, Python has an amazing framework named Flask that make the creation of REST API pretty simple. But there\u2019s even more. Flask has an extension on its own to make that process even easier. You just have to create your class with a method for each verb you wan to support. Impressive, really. Here\u2019s the two libraries you have to use:<\/p>\n<p>You can find a full explanation along with the source code (hosted on GitHub) and al the details to deploy the solution in Azure. You\u2019ll be able to create and deploy your API in less than 5 minutes!<\/p>\n<p><a href=\"https:\/\/techcommunity.microsoft.com\/t5\/azure-sql-database\/building-rest-api-with-python-flask-and-azure-sql\/ba-p\/1056637\">Building REST API with Python, Flask and Azure SQL<\/a><\/p>\n<h2 id=\"why-i-should-use-a-relational-database-at-all\">Why I should use a Relational Database at all?<\/h2>\n<p>Interesting question. Answer is\u2026easy. Yeah, you didn\u2019t expect that right? Here it is: if you need to store JSON as is, with no or really minimal need to query data stored therein and no need to make complex partial updates to the stored documents, then a document database could be a great choice. If you need anything else, like ability to execute non-trivial queries on data, process aggregates, have transaction consistency, fine-grained security and the need to validate ingested data against a schema, then a relational database should be your choice.<\/p>\n<p>If you\u2019re thinking that you don\u2019t need a schema (and this is the main reason why you\u2019re choosing a NoSQL database), since something like a \u201cschema-less\u201d is a better choice as it will give your much more flexibility and freedom, well\u2026.go and read this:<\/p>\n<p><a href=\"https:\/\/www.martinfowler.com\/articles\/schemaless\/#implicit-schema\">Schemaless Data Structures<\/a><\/p>\n<blockquote><p>The entire schema-less idea is like a\u00a0<em><a href=\"https:\/\/en.wikipedia.org\/wiki\/Leprechaun\">Leprechaun<\/a><\/em>. No, it won\u2019t give you the gold at the end of the rainbow: it just doesn\u2019t exists!<\/p><\/blockquote>\n<p>By using Azure SQL you have even more reasons why you\u00a0<strong>should <\/strong>use it: it is a modern, scalable, (post-)relational database, where you can find an amazing number of features that you\u2019ll need to find somewhere else otherwise. This would mean to use many different data management solution (Document, Graph, Key-Value store)\u2026and then having to deal with one of the most difficult problem to solve. Data Integration. If it is absolutely true that Naming Things and Cache Invalidation are the two hardest problem in Computer Science:<\/p>\n<blockquote><p>There are only\u00a0<a href=\"http:\/\/blog.davidemauri.it\/2020\/01\/07\/building-rest-api-with-python-flask-and-azure-sql\/(https:\/\/martinfowler.com\/bliki\/TwoHardThings.html)\">two hard things<\/a>\u00a0in Computer Science: cache invalidation and naming things (Phil Karlton)<\/p><\/blockquote>\n<p>I would easily say that Data Integration comes third. You want to avoid it if possible. And Azure SQL has many features to help you on that. And no, you don\u2019t have to worry about scalability. It scales pretty well. Up to 100TB and hundreds of thousands (at least) of transaction per seconds. Read more here:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/azure-sql\/10-reasons-to-use-azure-sql-in-your-next-project\/\">10 Reasons why Azure SQL is the Best Database for Developers<\/a><\/p>\n<h2 id=\"one-last-note\">One last note<\/h2>\n<p>Let me do the math for you here. 10 thousands transaction, let\u2019s say inserts, per second are 864 million rows per day. If you have a very small payload, say 100 bytes only, it will mean 80GB per day or 28 TB year. You can easily handle this load with an Azure SQL Hyperscale 8vCores. Probably way less cores than your desktop or laptop.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Creating REST API with Azure SQL is never been easier thanks to its native JSON support. Let&#8217;s see how we can use Python and Flask to create an elegant and modern solution to serve REST API to the world<\/p>\n","protected":false},"author":24720,"featured_media":307,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1,411],"tags":[409,408,29,407,410],"class_list":["post-305","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-azure-sql","category-python","tag-api","tag-flask","tag-json","tag-python","tag-rest"],"acf":[],"blog_post_summary":"<p>Creating REST API with Azure SQL is never been easier thanks to its native JSON support. Let&#8217;s see how we can use Python and Flask to create an elegant and modern solution to serve REST API to the world<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/posts\/305","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/users\/24720"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/comments?post=305"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/posts\/305\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/media\/307"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/media?parent=305"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/categories?post=305"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/tags?post=305"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}