Hello friends!!! Here we are going to discuss Spring Boot with NoSQL, how Spring Boot provide the support for NoSQL technologies and how to use into Spring Boot Application. NoSQL is a non-relational database management systems, different from traditional relational database management systems in some significant ways.
NoSQL is nothing but it is database without support of SQL queries unlike traditional databases MySQL, DB2, Oracle etc. Spring Data provides additional projects that help you access a variety of NoSQL technologies including MongoDB, Neo4J, Elasticsearch, Solr, Redis, Gemfire, Couchbase and Cassandra. Spring Boot provides auto-configuration for Redis, MongoDB, Neo4j, Elasticsearch, Solr and Cassandra.
MongoDB is an open-source NoSQL document database that uses a JSON-like schema instead of traditional table-based relational data. Spring Boot offers several conveniences for working with MongoDB, including the spring-boot-starter-data-mongodb ‘Starter’. Spring Data includes repository support for MongoDB.
Spring Boot offers auto-configuration for Embedded Mongo. To use it in your Spring Boot application add a dependency on de.flapdoodle.embed:de.flapdoodle.embed.mongo. The port that Mongo will listen on can be configured using the spring.data.mongodb.port property. To use a randomly allocated free port use a value of zero. The MongoClient created by MongoAutoConfiguration will be automatically configured to use the randomly allocated port.
spring.data.mongodb.host=mongoserver spring.data.mongodb.port=27017
Spring Data Mongo provides a MongoTemplate class that is very similar in its design to Spring’s JdbcTemplate. As with JdbcTemplate Spring Boot auto-configures a bean for you to simply inject:
@Component public class DataBean { @Autowired MongoTemplate mongoTemplate; // other tasks... }
For MongoDB Full Example Click Here
Neo4j is an open-source NoSQL graph database that uses a rich data model of nodes related by first class relationships which is better suited for connected big data than traditional rdbms approaches. Spring Boot offers several conveniences for working with Neo4j, including the spring-boot-starter-data-neo4j ‘Starter’.
You can configure the user and credentials to use via the spring.data.neo4j.* properties:
spring.data.neo4j.uri=http://localhost:7474 spring.data.neo4j.username=neo4j spring.data.neo4j.password=secret
@Component public class DataBean{ @Autowired Neo4jTemplate neo4jTemplate; //Other tasks }
By default the instance will attempt to connect to a Neo4j server using localhost:7474
If you add org.neo4j:neo4j-ogm-embedded-driver to the dependencies of your application, Spring Boot will automatically configure an in-process embedded instance of Neo4j that will not persist any data when your application shuts down. You can explicitly disable that mode using spring.data.neo4j.embedded.enabled=false.
spring.data.neo4j.uri=file://opt/db/graph.db
Spring Data includes repository support for Neo4j. You can customize entity scanning locations using the @NodeEntityScan annotation.
@EnableNeo4jRepositories(basePackages = "com.dineshonjava.myapp.repository") @EnableTransactionManagement
package com.dineshonjava.myapp.domain; import org.springframework.data.domain.*; import org.springframework.data.repository.*; public interface UserRepository extends GraphRepository<User> { Page<User> findAll(Pageable pageable); User findByNameAndEamil(String name, String email); }
Redis is a cache, message broker and richly-featured key-value store. Spring Boot offers basic auto-configuration for the Jedis client library and abstractions on top of it provided by Spring Data Redis. There is a spring-boot-starter-data-redis ‘Starter’ for collecting the dependencies in a convenient way.
You can inject an auto-configured RedisConnectionFactory, StringRedisTemplate or vanilla RedisTemplate instance as you would any other Spring Bean. By default the instance will attempt to connect to a Redis server using localhost:6379.
@Component public class DataBean { @Autowired StringRedisTemplate template; // other tasks... }
Spring Data Gemfire provides convenient Spring-friendly tools for accessing the Pivotal Gemfire data management platform. There is a spring-boot-starter-data-gemfire ‘Starter’ for collecting the dependencies in a convenient way. There is currently no auto-configuration support for Gemfire, but you can enable Spring Data Repositories with a single annotation (@EnableGemfireRepositories).
Apache Solr is a search engine. Spring Boot offers basic auto-configuration for the Solr 5 client library and abstractions on top of it provided by Spring Data Solr. There is a spring-boot-starter-data-solr ‘Starter’ for collecting the dependencies in a convenient way.
You can inject an auto-configured SolrClient instance as you would any other Spring bean. By default the instance will attempt to connect to a server using localhost:8983/solr:
@Component public class DataBean { @Autowired SolrClient solr; // other tasks... }
Spring Data includes repository support for Apache Solr.
Elasticsearch is an open source, distributed, real-time search and analytics engine. Spring Boot offers basic auto-configuration for the Elasticsearch and abstractions on top of it provided by Spring Data Elasticsearch. There is a spring-boot-starter-data-elasticsearch ‘Starter’ for collecting the dependencies in a convenient way. Spring Boot also supports Jest.
You can inject an auto-configured ElasticsearchTemplate or Elasticsearch Client instance as you would any other Spring Bean. By default the instance will embed a local in-memory server (a Node in Elasticsearch terms) and use the current working directory as the home directory for the server.
you can switch to a remote server by setting spring.data.elasticsearch.cluster-nodes to a comma-separated ‘host:port’ list.
spring.data.elasticsearch.cluster-nodes=localhost:9300 @Component public class DataBean { @Autowired ElasticsearchTemplate template; //other tasks ... }
Spring Data includes repository support for Elasticsearch.
spring.elasticsearch.jest.uris=http://search.doj.com:9200 spring.elasticsearch.jest.read-timeout=10000 spring.elasticsearch.jest.username=user spring.elasticsearch.jest.password=secret
If you have Jest on the classpath, you can inject an auto-configured JestClient targeting localhost:9200 by default.
Cassandra is an open source, distributed database management system designed to handle large amounts of data across many commodity servers. Spring Boot offers auto-configuration for Cassandra and abstractions on top of it provided by Spring Data Cassandra. There is a spring-boot-starter-data-cassandra ‘Starter’ for collecting the dependencies in a convenient way.
You can inject an auto-configured CassandraTemplate or a Cassandra Session instance as you would with any other Spring Bean. The spring.data.cassandra.* properties can be used to customize the connection.
spring.data.cassandra.keyspace-name=mykeyspace spring.data.cassandra.contact-points=cassandrahost1,cassandrahost2 @Component public class DataBean { @Autowired CassandraTemplate template; //other tasks ... }
Couchbase is an open-source, distributed multi-model NoSQL document-oriented database that is optimized for interactive applications. Spring Boot offers auto-configuration for Couchbase and abstractions on top of it provided by Spring Data Couchbase. There is a spring-boot-starter-data-couchbase ‘Starter’ for collecting the dependencies in a convenient way.
The spring.couchbase.* properties can be used to customize the connection. Generally you will provide the bootstrap hosts, bucket name and password:
spring.couchbase.bootstrap-hosts=mydojhost-1,192.168.11.111 spring.couchbase.bucket.name=my-bucket spring.couchbase.bucket.password=secret
Spring Data includes repository support for Couchbase. ou can inject an auto-configured CouchbaseTemplate instance.
@Component public class DataBean { @Autowired CouchbaseTemplate template; // other tasks... }
Here we have discussed some detail about NoSQL database with Spring Boot. Spring Boot Provide major support and auto configuration for connect to NoSQl databases.
Happy Spring Boot Learning!!!
Reference
http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/
Spring Boot Related Topics
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…