mongodb and java 8 agenda
play

MongoDB and Java 8 Agenda Java8 Main Features MongoDB + Java8 Few - PowerPoint PPT Presentation

MongoDB and Java 8 Agenda Java8 Main Features MongoDB + Java8 Few Examples RX Driver 3 Java 8 MongoDB Java Driver is JAVA6+ Complaint Java 8 Features and Improvements Lambda Expressions New Date API Stream API Type


  1. MongoDB and Java 8

  2. Agenda Java8 Main Features MongoDB + Java8 Few Examples RX Driver 3

  3. Java 8

  4. MongoDB Java Driver is JAVA6+ Complaint

  5. Java 8 Features and Improvements • Lambda Expressions • New Date API • Stream API • Type Annotations • Compact Profiles • Security Enhancements • JavaFX Improvements • Nashorn JS Engine • Unicode Enhancements • Selector Provider (IO & NIO) • Concurrency • … http://www.oracle.com/technetwork/java/javase/8-whats-new-2157071.html 6

  6. Java 8 Features and Improvements • Lambda Expressions • Stream API • New Date API • Type Annotations • Compact Profiles • Security Enhancements • JavaFX Improvements • Nashorn JS Engine • Unicode Enhancements • Selector Provider (IO & NIO) • Concurrency • … http://www.oracle.com/technetwork/java/javase/8-whats-new-2157071.html 7

  7. Lambda Function • Anonymous Functions – Not bounded to an identifier – Inline definition – Passed as argument to higher-order functions • Functional Java FTW 8

  8. Lambda Function • Anonymous Functions – Not bounded to an identifier – Inline definition – Passed as argument to higher-order functions • Functional Java FTW … .map(line -> Arrays. asList(line.split(SEPARATOR SEPARATOR))) ))) … i -> document.put(headers.get(i), values.get(i) ) 9

  9. Stream API • Streams are free flowing sequence of elements try(BufferedReader reader = new try new BufferedReader BufferedReader(fr fr)){ )){ – Pipeline kind of processing return return reader reader.lines .lines() () • Starts with a source of data .skip(1) .skip(1) (Collections) .map(line .map( line -> { -> { – Processes elements of that Document document = new new Document(); Document(); data source in a parallelized fashion List<String> values = Arrays. asList(line.split(SEPARATOR SEPARATOR)); )); IntStream. range(0, Math.min(values.size(), headers.size())) – Intermediary processing steps .forEach(i -> document.put(headers.get(i), values.get(i) )); return document return document; • Generally implemented by lambdas }).collect(Collectors. toList()); – Terminator operators • Streams are immutable! 10

  10. Stream API public public List<Document> List<Document> readRecords readRecords(List<String> (List<String> headers headers) { ) { try try ( (FileReader FileReader fr fr = = new new FileReader FileReader(this this.source source); ); BufferedReader reader = new new BufferedReader BufferedReader(fr fr)) { )) { return return reader reader .lines() .skip(1) .map(line -> { Document document = new new Document(); Document(); List<String> values = Arrays. asList(line .split( SEPARATOR SEPARATOR)); )); IntStream. range(0, Math. min(values.size(), headers.size())) .forEach( i -> document.put(headers.get(i), values.get(i))); return document return document; }).collect(Collectors. toList()); } catch catch ( (IOException IOException e) { ) { throw throw new new UncheckedIOException UncheckedIOException(e); ); } 11 }

  11. New Date API InstantCodec instantCodec = new new InstantCodec InstantCodec(); (); Map<BsonType, Class<?>> replacements = new new HashMap HashMap<BsonType BsonType, Class<?>>(); , Class<?>>(); replacements.put(BsonType. DATE_TIME DATE_TIME, , Instant. Instant.class class); ); • There is no native support for new Date API Our Codec Codec API gives the possibility map this data • type ( Instant. Instant.class class ) to encode to a driver supported data type and decode back to the original data type 12

  12. New Date API public public class class InstantCodec InstantCodec implements implements Codec<Instant> { Codec<Instant> { public public void void encode( encode(BsonWriter BsonWriter writer writer, Instant , Instant value value, EncoderContext encoderContext) { //will store Instant has Epoch Milliseconds writer.writeDateTime(value.toEpochMilli()); } public public Class<Instant> Class<Instant> getEncoderClass getEncoderClass() { () { return Instant. return Instant.class class; } //return back on Instant.class public public Instant decode( Instant decode(BsonReader BsonReader reader reader, , DecoderContext DecoderContext decoderContext decoderContext) { ) { return return Instant. Instant. ofEpochMilli ofEpochMilli(reader reader.readDateTime .readDateTime()); ()); } http://mongodb.github.io/mongo-java-driver/3.0/bson/codecs/ } 13

  13. New Date API public public class class InstantCodec InstantCodec implements implements Codec<Instant> { Codec<Instant> { public public void void encode( encode(BsonWriter BsonWriter writer writer, Instant , Instant value value, EncoderContext encoderContext) { //will store Instant has Epoch Milliseconds writer.writeDateTime(value.toEpochMilli()); } public public Class<Instant> Class<Instant> getEncoderClass getEncoderClass() { () { return Instant. return Instant.class class; } //return back on Instant.class public public Instant decode( Instant decode(BsonReader BsonReader reader reader, , DecoderContext DecoderContext decoderContext decoderContext) { ) { return return Instant. Instant. ofEpochMilli ofEpochMilli(reader reader.readDateTime .readDateTime()); ()); } http://mongodb.github.io/mongo-java-driver/3.0/bson/codecs/ } 14

  14. New Date API InstantCodec instantCodec = new new InstantCodec InstantCodec(); (); Map<BsonType, Class<?>> replacements = new new HashMap HashMap<BsonType BsonType, Class<?>>(); , Class<?>>(); replacements.put(BsonType. DATE_TIME DATE_TIME, , Instant. Instant.class class); ); CodecRegistry cr = CodecRegistries. fromRegistries( CodecRegistries. fromCodecs(instantCodec), CodecRegistries. fromProviders(documentCodecProvider), MongoClient. getDefaultCodecRegistry()); // add the new code registry has option. MongoClientOptions option = MongoClientOptions. builder() .codecRegistry(cr).build(); mc = mc = new new MongoClient MongoClient("localhost:27017" "localhost:27017", , option option); ); collection collection = = mc mc.getDatabase .getDatabase("dates" "dates"). ).getCollection getCollection("sample" "sample"); ); Document doc = new new Document( Document("java8date" "java8date", , Instant. Instant. now now()); ()); collection.insertOne collection .insertOne(doc doc); ); 15

  15. Extra: RX Driver

  16. ReactiveX Observable Observer

  17. MongoDB Reactive Streams Driver • http:// mongodb.github.io/mongo-java-driver-rx / • Observer based rather than callback based! (Cold Observables) • Built upon the MongoDB Async Driver & RxJava 18

  18. Join the Java Courses

  19. https://university.mongodb.com/courses/M101J 20

  20. Join the Team Finance & People Operations Sales & Account Management Pre-Sales Engineering Engineering Marketing View all jobs and apply: http://grnh.se/pj10su

  21. Obrigado! Norberto Leite Technical Evangelist norberto@mongodb.com @nleite https://github.com/nleite/java8demo

  22. A SIMPLE GUIDE TO USING AKKA PERSISTENCE Joost Heijkoop

  23. WHAT WILL THIS BE ABOUT? Using Akka with Scala Event Sourcing How to add Akka Persistence How to do Serialization with Stamina

  24. WHO AM I? Joost Heijkoop I like to create/build/learn/share/teach stuff I'm a Software developer, Full Stack Developer, Back-end Developer, Consultant, ... I Work at Xebia Amsterdam.scala meetup (SUG) organisor Serial meetup / conference attendee

  25. BUILD STUFF!

  26. AKKA + SCALA Akka is an Actor Framework, similar to Erlang Akka is message driven, "extreme" OO Scala hybrid programming language on the JVM

  27. AKKA + SCALA class Parrot extends Actor { var count = 0 override def receive: Receive = { case message: String => { count = count + 1 println(s"$count: $message") } } } val parrot = system.actorOf(Parrot.props) parrot ! "Hello" parrot ! "Hello" 1: Hello 2: Hello

  28. WHAT IS EVENT SOURCING All truths spring from recording events The current state is the sum of all events Command -> Event -> Update Recording of delta instead of current state You can replay all of history CQRS (Command Query Responsibility Segregation) - Martin Fowler

  29. EVENT SOURCING WITH AKKA PERSISTENCE Get a command Create event Persist/record the event to Event Store Update the internal state

  30. EVENT SOURCING WITH AKKA PERSISTENCE final def persist[A](event: A)(handler: (A) => Unit): Unit override def receiveCommand: Receive = { case message: String => persist(Incremented(count + 1, message))(update) println(s"$count: $message") } def update: Receive = { case Incremented(newCount, message) => count = newCount }

  31. EVENT SOURCING WITH AKKA PERSISTENCE class Parrot extends PersistentActor { var count = 0 override def persistenceId = "parrot" override def receiveRecover: Receive = { case event: Incremented => update(event) } override def receiveCommand: Receive = { case message: String => persist(Incremented(count + 1, message))(update) println(s"$count: $message") } def update = { case Incremented(newCount, message) => count = newCount } }

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend