市面上常见的NOSQL解决方案
-
Redis -
Monogo -
ES
Redis
Redis是一款key-value存储结构的内存级NOSQL数据库
-
支持多种数据存储格式 -
支持持久化 -
支持集群
● Redis下载(Windows版本)
● Redis安装与启动(Windows版)
-
服务端启动命令
redis-server.exe redis.windows.conf
-
客户端端启动命令
redis-cli.exe
● RedisTemplate提供操作各种数据存储类型的接口API
SpringBoot整合Redis
1.导入Redis对应的Starter
2.配置
3.提供操作Redis接口对象RedisTemplateops*
:获取各种数据类型操作接口
● 客户端:RedisTemplate以对象作为key和value,内部对数据进行序列化
@Autowired
private RedisTemplate redisTemplate;
@Test
void set() {
ValueOperations ops = redisTemplate.opsForValue();
ops.set("age",41);
}
@Test
void get() {
ValueOperations ops = redisTemplate.opsForValue();
Object age = ops.get("age");
System.out.println(age);
}
● 客户端:StringRedisTemplate以字符串作为key和value,与Redis客户端操作等效
@SpringBootTest
public class StringRedisTemplateTest {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Test
void set() {
ValueOperations ops = stringRedisTemplate.opsForValue();
ops.set("testKey","testValue");
}
@Test
void get(){
ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
String name = ops.get("testKey");
System.out.println(name);
}
}
● 客户端选择Jedis
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
● 配置客户端及专用属性
spring:
redis:
host: localhost
port: 6379
client-type: lettuce
lettuce:
pool:
max-active: 16
jedis:
pool:
max-active: 16
● lettuce与jedis的区别
① jedis连接Redis服务器是直连模式,当多线程模式使用jedis会存在线程安全问题,解决方案可以通过配置连接池使每个连接专用,这样整体性能就大受影响。
② lettuce基于Netty框架进行与Redis服务器连接,底层设计中采用StatefulRedisConnection。StatefulRedisConnection自身是线程安全的,可以保障并发访问安全问题,所以一个连接可以被多线程复用。当然lettuce也支持多连接实例一起工作。
SpringBoot整合Redis客户端选择:
1.lettuce(默认)
2.jedis
Mongodb
Mongodb是一个开源、高性能、无模式的文档型数据库,NoSQL数据库产品中的一种,是最像关系型数据库的非关系型数据库
● 淘宝用户数据
● 游戏装备数据,游戏道具数据
● 直播数据,粉丝数据,打赏数据
● 物联网数据
-
存储位置:Mongodb -
特征:临时存储、修改频率飞速
● Windows版Mongodb下载
● Windows版Mongodb安装
-
解压缩后设置数据目录
● Windows版Mongodb启动
-
服务端启动命令
mogond --dbpath=..datadb
-
客户端端启动命令
mogo --host=127.0.0.1 --port=27017
-
步骤一:下载对应的dll文件(通过互联网搜索即可) -
步骤二:拷贝到windows安装路径下的system32目录中 -
步骤三:执行命令注册对应dll文件
regsvr32 vcruntime140_1.dll
-
新增
db.集合名称.insert/save/insertOne(文档)
-
修改
db.集合名称.remove(条件)
-
删除
db.集合名称.update(条件,{操作种类:{文档}})
1.基础查询
查询全部:db.集合.find();
查第一条:db.集合.findOne();
查询指定数量文档:db.集合.find().limit(10) //查10条文档
跳过指定数量文档:db.集合.find().skip(20) //跳过20条文档
统计:db.集合.cout()
排序:db.集合.sort({age:1}) //按年龄升序排列
投影:db.集合名称.find(条件,{name:1,age,1}) //仅保留name与age域
2.条件查询
基本格式: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
客户端读写Mongodb
@Autowired
private MongoTemplate mongoTemplate;
@Test
void contextLoads() {
Book book = new Book();
book.setId(2);
book.setName("springboot2");
book.setType("springboot2");
book.setDescription("springboot2");
mongoTemplate.save(book);
}
@Test
void find(){
List<Book> all = mongoTemplate.findAll(Book.class);
System.out.println(all);
}
Elasticsearch(ES)
● Elasticsearch 是一个分布式全文搜索引擎
● Windows版ES下载
● Windows版ES安装与启动
运行:elasticsearch.bat
● 创建文档
POST http://localhost:9200/books/_doc #使用系统生成id
POST http://localhost:9200/books/_create/1 #使用指定id
POST http://localhost:9200/books/_doc/1 #使用指定id,不存在创建,存在更新(版本递增)
{
"name":"springboot"
"type":"springboot"
"description":"springboot"
}
● 查询文档
GET http://localhost:9200/books/_doc/1 #查询单个文档
GET http://localhost:9200/books/_serch #查询全部文档
● 条件查询
GET http://localhost:9200/books/_serch?q=name:springboot
● 删除文档
DELETE http://localhost:9200/books/_doc/1
● 修改文档(全量修改)
PUT http://localhost:9200/books/_doc/1
{
"name":"springboot"
"type":"springboot"
"description":"springboot"
}
● 修改文档(部分修改)
POST http://localhost:9200/books/_update/1
{
"doc":{
"name":"springboot"
}
}
● 导入坐标
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
● 配置
elasticsearch:
rest:
uris: http://localhost:9200
● 客户端
@SpringBootTest
class Springboot18EsApplicationTests {
@Autowired
private ElasticsearchRestTemplate template;
}
● SpringBoot平台并没有跟随ES的更新速度进行同步更新,ES提供了High Level Client 操作ES
● 导入坐标
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
</dependency>
● 配置(无)
● 客户端
@Test
void testCreateIndex() throws IOException {
HttpHost host =HttpHost.create("http://localhost:9200");
RestClientBuilder builder = RestClient.builder(host);
client =new RestHighLevelClient(builder);
//客户端操作
CreateIndexRequest request =new CreateIndexRequest("books");
//获取操作索引的客户端对象,调用创建索引操作
client.indices().create(request, RequestOptions.DEFAULT);
//关闭客户端
client.close();
}
● 客户端改进
@SpringBootTest
class Springboot18EsApplicationTests {
private RestHighLevelClient client;
@BeforeEach
void setUP(){
this.client =new RestHighLevelClient( RestClient.builder(HttpHost.create("http://localhost:9200")));
}
@AfterEach
void tearDown() throws IOException {
this.client.close();
}
@Test
void testCreateIndex() throws IOException {
//客户端操作
CreateIndexRequest request =new CreateIndexRequest("books");
//获取操作索引的客户端对象,调用创建索引操作
client.indices().create(request, RequestOptions.DEFAULT);
}
}
● 创建索引
@Test
void testCreateIndexByIK() throws IOException {
CreateIndexRequest request = new CreateIndexRequest("books");
String json = "{n" +
" "mappings":{n" +
" "properties":{n" +
" "id":{n" +
" "type":"keyword"n" +
" },n" +
" "name":{n" +
" "type":"text",n" +
" "analyzer":"ik_max_word",n" +
" "copy_to":"all"n" +
" },n" +
" "type":{n" +
" "type":"keyword"n" +
" },n" +
" "description":{n" +
" "type":"text",n" +
" "analyzer":"ik_max_word",n" +
" "copy_to":"all"n" +
" },n" +
" "all":{n" +
" "type":"text",n" +
" "analyzer":"ik_max_word"n" +
" }n" +
" }n" +
" }n" +
"}";
//设置请求中的参数
request.source(json, XContentType.JSON);
client.indices().create(request, RequestOptions.DEFAULT);
}
● 添加文档
@Test
//添加文档
void testCreateDoc() throws IOException {
Book book = bookDao.selectById(5);
IndexRequest request = new IndexRequest("books").id(book.getId().toString());
String json = JSON.toJSONString(book);
request.source(json,XContentType.JSON);
client.index(request,RequestOptions.DEFAULT);
}
● 按id查询文档
@Test
//按id查询
void testGet() throws IOException {
GetRequest request = new GetRequest("books","5");
GetResponse response = client.get(request, RequestOptions.DEFAULT);
String json = response.getSourceAsString();
System.out.println(json);
}
● 按条件查询文档
@Test
//按条件查询
void testSearch() throws IOException {
SearchRequest request = new SearchRequest("books");
SearchSourceBuilder builder = new SearchSourceBuilder();
builder.query(QueryBuilders.termQuery("all","spring"));
request.source(builder);
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
SearchHits hits = response.getHits();
for (SearchHit hit : hits) {
String source = hit.getSourceAsString();
//System.out.println(source);
Book book = JSON.parseObject(source, Book.class);
System.out.println(book);
}
}
原文始发于微信公众号(itmkyuan):SpringBoot数据层(NOSQL)解决方案
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/39381.html