Scaling Scala to the database - Stefan Zeiger (Typesafe)

1,291 views

Published on

Presented at JAX London 2013

This session shows you how Slick, the Scala database library, can bring your data seamlessly into your Scala application and compile Scala collection operations to database code for execution on the database server. There will be plenty of live coding that highlights the latest features such as distributed queries, macro-based type providers, and the use of non-SQL data stores.

Published in: Technology, Education
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,291
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
29
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Scaling Scala to the database - Stefan Zeiger (Typesafe)

  1. 1. Scaling Scala to the Database Stefan Zeiger
  2. 2. 1 Overview / Key Concepts
  3. 3. WE WRITE SQL SO YOU DON'T HAVE TO h"p://toto.lib.unca.edu/findingaids/photo/na5onal_clima5c_data_center/NCDC_interior.htm   NOAA's  Na5onal  Clima5c  Data  Center  is  the  source  of  this  image  and  it  is  used  by  permission  
  4. 4. Write database code in Scala •  Instead of SQL, JPQL, Criteria API, etc. for { p <- persons } yield p.name select p.NAME from PERSON p 4  
  5. 5. (for { p <- persons.filter(_.age < 20) ++ persons.filter(_.age >= 50) if p.name.startsWith("A") } yield p).groupBy(_.age).map { case (age, ps) => (age, ps.length) } select x2.x3, count(1) from ( select * from ( select x4."NAME" as x5, x4."AGE" as x3 from "PERSON" x4 where x4."AGE" < 20 union all select x6."NAME" as x5, x6."AGE" as x3 from "PERSON" x6 where x6."AGE" >= 50 ) x7 where x7.x5 like 'A%' escape '^' ) x2 group by x2.x3 5  
  6. 6. Scala Language Integrated Connection Kit •  •  •  •  Database query and access library for Scala Successor of ScalaQuery Developed at Typesafe and EPFL Open Source 6  
  7. 7. Supported Databases •  •  •  •  •  •  •  PostgreSQL MySQL H2 Hsqldb Derby / JavaDB SQLite Access Closed-­‐Source  Slick  Extensions   (with  commercial  support  by   Typesafe):     •  Oracle   •  DB/2   •  SQL  Server   7  
  8. 8. Components •  •  •  •  •  Lifted Embedding Direct Embedding Plain SQL Session Management Schema Model 8  
  9. 9. 2 Compared to ORMs
  10. 10. Impedance Mismatch: Retrieval Colombian   French_Roast   Espresso   Colombian_Decaf   French_Roast_Decaf   select COF_NAME from COFFEES Espresso   Price:   Supplier:    9.99    The  High  Ground   select c.*, s.SUP_NAME from COFFEES c, SUPPLIERS s where c.COF_NAME = ? and c.SUP_ID = s.SUP_ID 10  
  11. 11. Impedance Mismatch: Retrieval def getAllCoffees(): Seq[Coffee] = …     def printLinks(s: Seq[Coffee]) {                                                              +  "  "  +  c.price ) for(c <- s) println(c.name } def printDetails(c: Coffee) { println(c.name) println("Price: " + c.price) println("Supplier: " + c.supplier.name)         } Colombian   French_Roast   Espresso   Colombian_Decaf   French_Roast_Decaf   Espresso   Price:   Supplier:  9.99    The  High  Ground   11  
  12. 12. O/R Mapper •  Mapping low-level programming (OOP) to high-level concepts (relational algebra) •  Not transparent (but pretends to be) 12  
  13. 13. Better Match: Functional Programming • Relation • Attribute • Tuple • Relation Value • Relation Variable case class Coffee(name: String, supplierId: Int, price: Double) val coffees = Set( Coffee("Colombian", 101, 7.99), Coffee("French_Roast", 49, 8.99), Coffee("Espresso", 150, 9.99) ) - mutable state in the DB 13  
  14. 14. Functional-Relational Mapping •  •  •  •  •  Embraces the relational model No impedance mismatch Composable Queries Explicit control over statement execution Stateless 14  
  15. 15. 3 Demo
  16. 16. 4 Under The Hood
  17. 17. APIs Direct Embedding Lifted Embedding Scala Compiler Scala AST Slick AST Slick Macros Query Compiler Slick AST Executor DB Result 17  
  18. 18. Lifted Embedding Query[ (Column[String], Column[String]), (String, String) ] TableQuery[Coffees] Coffees Suppliers ColumnExtensionMethods.< val q = for { c <- coffees if c.price < 9.0 s <- c.supplier ConstColumn(9.0) } yield (c.name, s.name) (Column[String], Column[String]) Seq[ (String, String) ] Column[Double] val result = q.run 18  
  19. 19. Direct Embedding (experimental) Queryable[ (String, String) ] Queryable[Coffee] Coffee Supplier Double.< val q = for { c <- coffees if c.price < 9.0 s <- c.supplier } yield (c.name, s.name) (String, String) Seq[ (String, String) ] 9.0: Double Double val result = q.run 19  
  20. 20. Query Compiler •  Immutable ASTs –  Types can be mutated until they are observed •  Immutable compiler state –  containing AST + phase output state •  Phases transform compiler state –  using mutable state locally •  Drivers provide their own compilers 20  
  21. 21. Compiler Phases: SQL Clean Up •  •  •  •  •  •  inline assignUniqueSymbols expandTables inferTypes createResultSetMapping forceOuterBinds Flatten Columns •  •  •  •  •  •  expandRefs replaceFieldSymbols rewritePaths relabelUnions pruneFields assignTypes SQL Shape •  •  •  •  •  resolveZipJoins convertToComprehensions fuseComprehensions fixRowNumberOrdering hoistClientOps Generate Code •  codeGen (driver-specific) 21  
  22. 22. Compiler Phases: MemoryDriver Clean Up •  •  •  •  •  •  inline assignUniqueSymbols expandTables inferTypes createResultSetMapping forceOuterBinds Flatten Columns •  •  •  •  •  •  expandRefs replaceFieldSymbols rewritePaths relabelUnions pruneFields assignTypes Prepare for Interpreter •  codeGen 22  
  23. 23. Compiler Phases: Scheduling Clean Up •  •  inline assignUniqueSymbols Clean Up II Distribute •  distribute (to other drivers' compilers) e.g. H2 •  •  •  •  •  •  expandRefs replaceFieldSymbols rewritePaths relabelUnions pruneFields assignTypes … Query Compiler Flatten Columns MySQL Query Compiler •  •  •  •  expandTables inferTypes createResultSetMapping forceOuterBinds Query Compiler Prepare for Interpreter •  codeGen 23  
  24. 24. 5 Outlook
  25. 25. Slick 2.0 •  •  •  •  •  Coming Q4 / 2013 Query scheduling API Improvements New driver and back-end architecture Generate Slick code from database schemas
  26. 26. Outlook •  Macro-based type providers –  Prototype based on type macros (topic/type-providers) –  Released version will use macro annotations –  Scala 2.12? •  Default database library for Play –  as part of the Typesafe Reactive Platform •  Focus on usability (API, docs, semantics, etc.)
  27. 27. slick.typesafe.com @StefanZeiger

×