Pivotal Cloud Platform Roadshow: Sign Up for Pivotal Web Services
eXtreme Tuesday Club at Pivotal Labs ft. Speemdnet / San Francisco - SEP 2015
-
Upload
speedment-inc -
Category
Software
-
view
748 -
download
0
Transcript of eXtreme Tuesday Club at Pivotal Labs ft. Speemdnet / San Francisco - SEP 2015
Wrap your Database in Java 8
Per-Åke MinborgCarina DreifeldtMalin WeissEmil Forslund
Speedment
Company• Company started 2010• Speedment customers:
Fintech, manufacturing, media, telecom, transport, retail, publishing and entertainment.
•Office Palo Alto, California Gothenburg, Sweden
Open Source Project• Apache 2, started 2015• Fully Java 8 compatible• www.speedment.org, GitHub
Bio Per-Åke Minborg• Master of Science Sweden• Inventor and Serial Entrepreneur• Java >15 years• Speaker at Meetups and Javaforums around
the globe, (Java-One 2015)• Java 8 and OpenSource blogger
Speedment Started With A Problem
Do You Recognize the following?• When coding it is complex to connect to a database…..
…then you use an ORM
Source: ZeroTurnaround
SOME PROS of Using an ORM:• You can work with a relational database as if it were object oriented.• Increased productivity.• Provides a certain degree of abstraction (you can replace your
DBMS).
SOME CONS of Using an ORM:• Slows down the application• You must still write SQL, HQL, et. al• You can not access all Java 8 features
How to skip these ORMs and work with Pure Java 8
• Lambda expressions• Stream API• Optional
Lambda Expressions• Anonymous methods can be expressed using the new ”->”
syntax
Deck deck = Deck.createShuffledDeck();List<Card> cards = deck.findWhere(card -> card.isHeart() && card.getValue() > 10);
Lambda Expressions• Any single-method interface can be implemented using lambda • can be annoted with @FunctionalInterface to show this
@FunctionalInterfaceinterface VectorOperation { Vector apply(Vector a, Vector b);}
VectorOperation plus = (a, b) -> new Vector( a.getX() + b.getX(), a.getY() + b.getY());
Lambda Expressions Functional pointers can also be used with the ”::” syntax
Set<Hero> warriors = typicalParty.where(Hero::isWarrior);…interface Hero { boolean isWarrior();}…class Party { private Set<Hero> heroes;
public Set<Hero> select(Predicate<Hero> selector) { … }}
Stream API• Streams are a modern way of working with groups of elements• Defining a stream is always an O(1)-operation• The elements are only traversed when used• Streams can often be executed in parallell
Stream API• Example: Get the average strength of all warriors with axes in the
party
Set<Hero> party = … ;int averageStrength = party.stream() .filter(hero -> Archtype.WARRIOR == hero.getArchtype()) .filter(hero -> Weapon.AXE == hero.getWeapon()) .mapToInt(Hero::getStrength) .average();
By replacing .stream() with .parallelStream() the operation can be performed using multiple processors
Optional• Null is often described as ”The Billion Dollar Mistake” of modern
programming languages• Using the Optional-interface we can now describe a value that might or
might not be present in a null-safe way
String getEmailAddress() { // Is always defined return this.emailAddress;}
Optional<String> getFirstname() { // Might not exist return Optional.ofNullable( this.firstname );}
Optional• You can work with Optionals in the same way you work with
streamsOptional<String> firstname = getFirstname() .filter(fn -> fn.length() > 3) .filter(fn -> fn.charAt(0) == 'A');
• When the value of an optional is retreived, you can handle every caseString absoluteFirstname = firstname.getOrDefault("Anonymous");
Speedment APIEmbrace Java 8 paradigm with stream(), filter(), sort(), limit() etc.
Forget about SQL, JQLFocus on Business Logic
Focus on what is fun
Develop a Database app within hours
No Query API
Stick to Java 8
Initialization
Speedment speedment = new HelloSpeedment().build();
Manager<Hare> hares = speedment.managerOf(Hare.class);
Persistence
Hare spire = hares.newInstance() .setName(“Spire") .setColor(“Blue") .setAge(1) .persist();
Querying
List<Hare> oldHares = hares.stream() .filter(h -> h.getAge() > 8) .collect(toList());
Optimised Predicate Look-up
Optional<Hare> spire= hares.stream() .filter(Hare.NAME.equal(“Spire")) .findAny();
Entities are Linked
Optional<Carrot> carrot = hares.stream() .filter(Hare.NAME.equal(“Spire”)) // Carrot is a foreign key. .flatMap(Hare::findCarrots) .findAny();
Multi-threading
hares.stream() .parallel() .filter(h -> humans.stream() .filter(Human.NAME.equal(h.getName())) .anyMatch() ).forEach(System.out::println);
Configuration• Groovy script like Gradle• Config file is placed relative to the POM
dbms { schema { table { name = ”hare"; column { name = "name"; } } }}
Ease of use:• Single Maven dependency in POM • Maven plugin for code generation/GUI• Maven targets• Works with Gradle• GUI to automatically derive the groovy config file from existing
databases
Conclusions• Code generation, No boiler plate code• Focus on the problem at hand!• Forget about the database• Use Java 8
Workshop: Social network We will focus on the server-side
MySQL database
Server application
Client GUI
http
Workshop: Social NetworkThe goal is to add features:
• Register• Login• Upload pictures
Workshop: Social Network Speedment: https://
github.com/speedment/speedment
https://github.com/Pyknic/SocialPhotoNetworkClient
Social Network Lab : https://github.com/Pyknic/SocialPhotoNetworkServer
Insane Mode...Want fast development + super duper fast execution ?
Speedment Enterprise Solution provides extreme performance
We are Where You are!• www.speedment.org
• GitHub: https://github.com/speedment/speedment
• Twitter: @Pminborg
• Mail: [email protected]
• Blog: minborgsjavapot.blogspot.com
• Silicon Valley