Series
This blog entry is part of a series called Stream Processing With Spring, Kafka, Spark and Cassandra.
- Part 1 - Overview
- Part 2 - Setting up Kafka
- Part 3 - Writing a Spring Boot Kafka Producer
- Part 4 - Consuming Kafka data with Spark Streaming and Output to Cassandra
- Part 5 - Displaying Cassandra Data With Spring Boot
Displaying Cassandra Data With Spring Boot
Now that we have our voting data in Cassandra let's write a simple Spring Boot project that simply gathers all the data from cassandra sorts them and displays to user.
Setting up a project
- Project SDK: Java 8
- Initializr Service URL: https://start.spring.io
- Next
- Name: boot-cassandra-data-show
- Type: Gradle Project
- Packaging: Jar
- Java Version: 1.8
- Language: Java
- Group: com.example
- Artifact: boot-cassandra-data-show
- Vesion: 0.0.1-SNAPSHOT
- Description: Spring Boot Display Cassandra Data
- Package: com.example
- Next
- Spring Boot Version: 1.3
- Core - Web
- Template Engines - Mustache
- Next
- Project name: boot-cassandra-data-show
- The rest is just fine ...
- Finish
- After creating project check sdk setting, it should be java 8
Cassandra dependencies
compile('com.datastax.cassandra:cassandra-driver-core:2.1.9')
Vote class
We'll use this class to map rows from cassandra.
package com.example; import java.io.Serializable; public class Vote implements Serializable { private String name; private Integer votes; public Vote(String name, Integer votes) { this.name = name; this.votes = votes; } public Vote() { } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getVotes() { return votes; } public void setVotes(Integer votes) { this.votes = votes; } }
application.properties
server.port = 8090 contactPoint = 127.0.0.1 keyspace = voting
CassandraSessionManager
This bean is used to setup connection towards Cassandra
package com.example; import com.datastax.driver.core.Cluster; import com.datastax.driver.core.Session; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; @Configuration public class CassandraSessionManager { private Session session; private Cluster cluster; @Value("${contactPoint}") private String contactPoint; @Value("${keyspace}") private String keyspace; public CassandraSessionManager() { } public Session getSession() { return session; } @PostConstruct public void initIt() { cluster = Cluster.builder().addContactPoint( contactPoint).build(); session = cluster.connect(keyspace); } @PreDestroy public void destroy() { if (session != null) { session.close(); } if (cluster != null) { cluster.close(); } } }
BootCassandraDataShowApplication
Automatically generated ...
package com.example; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class BootCassandraDataShowApplication { public static void main(String[] args) { SpringApplication.run( BootCassandraDataShowApplication.class, args); } }
AppBeans
Bean for holding configured objects.
package com.example; import com.datastax.driver.core.Session; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class AppBeans { @Bean public Session session() { return sessionManager().getSession(); } @Bean public CassandraSessionManager sessionManager() { return new CassandraSessionManager(); } }
Web Controller
package com.example; import com.datastax.driver.core.ResultSet; import com.datastax.driver.core.Row; import com.datastax.driver.core.Session; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import java.util.ArrayList; import java.util.Collections; import java.util.Map; @Configuration @Controller public class WelcomeController { @Autowired Session session; @RequestMapping("/") public String welcome(Map<String, Object> model) { final ResultSet rows = session.execute("SELECT * FROM votes"); ArrayListresults = new ArrayList<>(); for (Row row : rows.all()) { results.add(new Vote( row.getString("name"), row.getInt("votes") )); } Collections.sort(results, (a, b) -> b.getVotes().compareTo(a.getVotes())); model.put("results", results); return "welcome"; } }
Template to show the results
<!DOCTYPE html> <html lang="en"> <body> <h1>Voting results:</h1> <br/> {{#results}} <strong>{{this.name}}</strong> {{this.votes}} <br/> {{/results}} </body> </html>
That's all folks
Now this app might not seem as a lot, but there's a kafka cluster that receives messages comming in from a spring boot app that exposes REST interface. Messages that come in from kafka are then processed with Spark Streaming and then sent to Cassandra. There is another Spring Boot app that sorts and displays results to the users. This small tutorial covers most of the cool java/big data technologies now-days. Special thanks to the readers that went through all five parts of this tutorial ;)
7 comments:
Hi Mark - This tutorial is really awesome, really appreciate your time and effort. It can really powerful. thanks
Thank you for reading!
Hi Mark ,
This is really awesome and I have one suggestion . If we can make this app as some analytics like streaming or feeding the data from twitter or so and streaming through web and pushing on to react front end.
But this is really awesome tutorial to get to start with the all the stack.
Thanks,
Akhil.
The idea is great, I was also thinking about it for a while ... in the end decided to go for the simplest approach.
Interesting prototype. Do you mind share the sample code?
Thanks Marko for the brilliant work!
https://drive.google.com/open?id=0Bz9kDTTW0oRgWXdoTGFtM1dLelE
password: hello
Excellent & thanks Marko for the prompt reply!
Best wishes!
Post a Comment