市面上常见的NoSQL解决方案
Redis
-
Redis是一款key-value存储结构的内存级NoSQL数据库
- 支持多种数据存储格式
- 支持持久化
- 支持集群
-
Redis下载( Windows版)
-
Redis安装与启动 (windows版)
-
windows解压安装或一键式安装
-
服务端启动命令
redis-server.exe redis.windows.conf
-
客户端启动命令
redis-cli.exe
-
Spring boot整合Redis
导入redis对应的starter
配置
提供操作Redis接口对象RedisTemplate
ops*: 获取各种数据类型操作接口
SpringBoot整合Redis客户端选择
lettuce (默认)
jedis
-
导入SpringBoot整合Redis坐标
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
-
配置Redis(采用默认配置)
spring: redis: host: localhost # 127.0.0.1 port: 6379
主机: localhost (默认)端口: 6379 (默认)
-
RedisTemplate提供操作各种数据存储类型的接口API
-
客户端: RedisTemplate
@SpringBootTest class Springboot16NosqlApplicationTests { @Test void set(@Autowired RedisTemplate redisTemplate) { ValueOperations ops = redisTemplate.opsForValue(); ops.set("testKey","testValue"); } @Test void get(@Autowired RedisTemplate redisTemplate) { ValueOperations ops = redisTemplate.opsForValue(); Object val = ops.get("testKey"); System.out.println(val); } }
-
客户端: RedisTemplate以对象作为key和value,内部对数据进行序列化
@SpringBootTest class Springboot16NosqlApplicationTests { @Test void set(@Autowired RedisTemplate redisTemplate) { ValueOperations ops = redisTemplate.opsForValue(); ops.set("testKey","testValue"); } @Test void get(@Autowired RedisTemplate redisTemplate) { ValueOperations ops = redisTemplate.opsForValue(); Object val = ops.get("testKey"); System.out.println(val); } }
-
客户端: StringRedisTemplate以字符串作为key和value,与Redis客户端操作等效 (常用)
@SpringBootTest class SpringbootNosqlApplicationTests { @Test void set(@Autowired StringRedisTemplate redisTemplate) { ValueOperations ops = redisTemplate.opsForValue(); ops .set("testKey","testValue"); } @Test void get(@Autowired StringRedisTemplate redisTemplate) { ValueOperations ops = redisTemplate.opsForValue(); Object val = ops.get("testKey"); System.out.println(val); } }
-
如果客户端需要选择:jedis
-
导入jedis坐标
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> </dependency>
-
配置客户端
spring: redis: host: localhost # 127.0.0.1 port: 6379 cliect-type: jedis #指定客户端类型(lettuce或jedis) #配置客户端专用属性 lettuce : pool: max-active: 16 jedis: pool: max-active: 16
-
-
lettcus与iedis区别
jedis连接Redis服务器是直连模式,当多线程模式下使用jedis会存在线程安全问题,解决方案可以通过配置连接池使每个连接专用,这样整体性能就大受影响。
lettcus基于Netty框架进行与Redis服务器连接,底层设计中采用StatefulRedisConnection。StatefulRedisConnection自身是线程安全的,可以保障并发访问安全问题,所以一个连接可以被多线程复用。当然lettcus也支持多连接实例一起工作。
Mongo
-
MongODB是一个开源、高性能、无模式的文档型数据库。NOSOL数据库产品中的一种,是最像关系型数据库的非关系型数据库
-
Windows版Mongo下载
-
Windows版Mongo安装
- 解压缩后设置数据目录
-
Windows版Mongo启动
-
服务端启动 (设置dbpath对应的数据存储的路径为上一层目录下的data目录下的db文件夹)
mongod --dbpath=..\data\db
-
客户端启动
mongo --host=127.0.0.1 --port=27017
-
-
Mongodb的基本操作
-
新增
db.集合名称.insert/save/insertone(文档)
-
修改
db.集合名称.remove(条件)
-
删除
db.集合名称.update(条件,{操作种类:{文档}})
-
基础查询
- 查询全部: db.集合.find(); - 查第一条: db.集合.findone() - 查询指定数量文档: db.集合.find().limit(10) //查10条文档 - 跳过指定数量文档: db.集合.find().skip(20) //跳过20条文档 - 统计: db.集合.count() - 排序: db.集合.sort({age:1}) //按age升序排序 - 投影: db.集合名称.find(条件,{name:1,age:1}) //仅保留name与age域
-
条件查询
- 基本格式: db.集合.find({条件}) - 模糊查询: db.集合.find({域名:/正则表达式/}) //等同SQL中的like,比like强大,可以执行正则所有规则 - 条件比较运算: db.集合.find({域名:{$gt:值}}) //等同SQL中的数值比较操作,例如:name>18 - 包含查询: db.集合.find({域名:{$in:[值1,值2]}}) //等同于SQL中的in - 条件连接查询: db.集合.find({$and:[{条件1},{条件2}]}) //等同于SQL中的and、or
-
Spring boot整合MOngoDB
导入Mongodb对应的starter
配置mongodb访问url
提供操作Mongodb接口对象MongoTemplate
-
导入Mongodb驱动
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency>
-
配置客户端
spring: data: mongodb : uri: mongodb://localhost/itheima
-
客户端读写Mongodb
@Test void testSave(@Autowired MongoTemplate mongoTemplate){ Book book = new Book(); book.setId(1); book.setType("springboot"); book.setName("springboot"); book.setDescription("springboot"); mongoTemplate.save(book); } @Test void testFind(@Autowired MongoTemplate mongoTemplate){ List<Book> all = mongoTemplate.findAll(Book.class); System.out.println(all); }
ES
-
Elasticsearch是一个分布式全文搜索引擎
-
Windows版ES下载
https://www.elastic.co/cn/downloads/elasticsearch -
Windows版ES安装与启动
运行 elasticsearch.bat
-
IK分词器
下载:https://github.com/medcl/elasticsearch-analysis-ik/releases -
创建/查询/删除索引
PUT http://localhost:9200/索引名称 GET http://localhost:9200/索引名称 DELETE http://localhost:9200/索引名称
-
创建索引并指定规则
{ "mappings":{ "properties":{ "id":{ "type":"keyword" }, "name":{ "type" :"text", "analyzer":"ik_max_word", "copy_to":"al1" }, "type" :{ "type" :"keyword" }, "description":{ "type" :"text", "analyzer":"ik_max_word", "copy_to":"al1" }, "al1":{ "type":"text", "analyzer":"ik_max_word" } } } }
Spring boot整合MOngoDB
-
导入坐标
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency>
-
配置
spring: elasticsearch: rest: uris: http://localhost:9200
-
客户端
@SpringBootTest class Springboot18EsApplicationTests{ @Autowired private ElasticsearchRestTemplate template; }
-
SpringBoot平台并没有跟随ES的更新速度进行同步更新,ES提供了HighLevelClient操作ES
-
导入坐标
<dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> </dependency>
配置(无)
-
客户端
@Test void test() throws IoException { HttpHost host = HttpHost.create("http://localhost:9200"); RestClientBuilder builder = Restclient.builder(host); RestHighLevelclient client = new RestHighLevelclient(builder); //客户端操作 CreateIndexRequest request = new CreateIndexRequest("books"); //获取操作索引的客户端对象,调用创建索引作 client.indices().create(request, RequestOptions.DEFAULT); //关闭客户端 client.close(); }
-
客户端改进
@SpringBootTest class Springboot18EsApplicationTests{ @Autowired private BookDao bookDao; @Autowired RestHighLevelclient client; @BeforeEach void setUp(){ this.client = new RestHighLevelclient(Restclient, builder(HttpHost.create("http://localhost:9208"))); } @AfterEach void tearDown()throws IOException { this.client.close(); } @Test void test()throws IoException { //客户端操作 CreateIndexRequest request = new CreateIndexRequest("books"); //获取操作索引的客户端对象,调用创建索引操作 client,indices().create(request, RequestOptions.DEFAULT); } }
-