手机网站建设论文为什么中国禁止谷歌浏览器
SpringCloud 大型系列课程正在制作中,欢迎大家关注与提意见。
程序员每天的CV 与 板砖,也要知其所以然,本系列课程可以帮助初学者学习 SpringBooot 项目开发 与 SpringCloud 微服务系列项目开发
elasticsearch是一款非常强大的开源搜索引擎,具备非常多强大功能,可以帮助我们从海量数据中快速找到需要的内容。
本项目数据库使用的是 MySql ,查询数据使用的是 ElasticSearch
1 项目准备
SpringBoot RabbitMQ 延时队列取消订单【SpringBoot系列14】 本文章 基于这个项目来开发
本文章是系列文章 ,每节文章都有对应的代码,每节的源码都是在上一节的基础上配置而来,对应的视频讲解课程正在火速录制中。
1 项目依赖添加
首先是你的开发环境 以及服务器要安装es,我这里是使用 docker 来安装的 docker-compose安装elasticsearch及kibana
项目 pom.xm 中添加依赖
<dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-high-level-client</artifactId></dependency><dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-client</artifactId></dependency><dependency><groupId>org.elasticsearch</groupId><artifactId>elasticsearch</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><optional>true</optional></dependency><!--测试使用--><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13.2</version><scope>test</scope></dependency>
application.yml 中添加 es 的连接地址
elasticsearch:host: 127.0.0.1port: 9200
2 索引管理 - 相当数据库中的表
索引就像是数据库或者数据库中的表,我们平时是不会是通过java代码频繁的去创建修改删除数据库或者表的相关信息,我们只会针对数据做CRUD的操作。
kibana 提供了便捷的控制台开发工具
所以在使用 ElasticSearch 时,需要先使用 控制台来创建索引库,就好比你在操作数据库时,要先创建数据库与表 .
索引(Index),就是相同类型的文档的集合。
例如:
- 所有用户文档,就可以组织在一起,称为用户的索引;
- 所有商品的文档,可以组织在一起,称为商品的索引;
- 所有订单的文档,可以组织在一起,称为订单的索引;
因此,我们可以把索引当做是数据库中的表。
2.1 mysql与elasticsearch
2.2 创建索引库
如本项目要将订单数据保存到ES中,所以这里要创建订单的索引库,创建索引库和映射的基本语法如下
PUT /order
{"mappings": {"properties": {"id": {"type": "long"},"userId": {"type": "long"},"goodsId": {"type": "long"},"deliveryAddrId": {"type": "integer"},"sn": {"type": "long"},"goodsName": {"type": "text","analyzer": "ik_max_word","search_analyzer": "ik_smart"},"goodsPrice": {"type": "double"},"goodsCount": {"type": "integer"},"orderChannel": {"type": "integer"},"status": {"type": "integer"},"payDate": {"type": "date","format": "yyyy-MM-dd HH:mm:ss"},"createDate": {"type": "date","format": "yyyy-MM-dd HH:mm:ss"}}}
}
- type:字段数据类型,常见的简单类型有:
- 字符串:text(可分词的文本)、keyword(精确值,例如:品牌、国家、ip地址)
- 数值:long、integer、short、byte、double、float、
- 布尔:boolean
- 日期:date
- 对象:object
- index:是否创建索引,默认为true
- analyzer:使用哪种分词器
- properties:该字段的子字段
然后查询一下索引库
#查询
GET /order
删除索引库
#删除
DELETE /order
3 文档操作
这是现在数据库中订单表的数据
3.1 保存一条数据
import com.alibaba.fastjson.JSON;
import com.biglead.demo.pojo.Order;
import com.biglead.demo.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;import javax.annotation.Resource;
import java.io.IOException;@SpringBootTest
@RunWith(SpringRunner.class)
@Slf4j
public class ESDocumentTests {@ResourceRestHighLevelClient restHighLevelClient;@ResourceOrderService orderService;/*** 增加文档信息*/@Testpublic void addDocument() throws IOException {// 查询订单信息Order order = orderService.getOrderDetail(83L);// 将对象转为jsonString data = JSON.toJSONString(order);// 创建索引请求对象// 参数一 索引库名 参数二文档名称IndexRequest indexRequest = new IndexRequest("order").id(order.getId() + "");// 准备JSON文档indexRequest.source(data, XContentType.JSON);// 执行增加文档IndexResponse response = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);log.info("创建状态:{}", response.status());}}
我这里直接将 Order 实体的数据同步到了 ES中,这就要求对数据类型以及字段的完全匹配
@TableName("t_order")
@Data
public class Order implements Serializable {private static final long serialVersionUID = 1L;/** 订单ID **/@TableId(value = "id", type = IdType.AUTO)private Long id;/** 用户ID **/private Long userId;/** 商品订单号 **/private Long sn;/** 商品ID **/private Long goodsId;/** 收获地址ID **/private Long deliveryAddrId;/** 商品名字 **/private String goodsName;/** 商品数量 **/private Integer goodsCount;/** 商品价格 **/private BigDecimal goodsPrice;/** 1 pc,2 android, 3 ios **/private Integer orderChannel;/** 订单状态,0 新建未支付,1已支付,2已发货,3已收货,4已退货,5已完成 ,6已取消**/private Integer status;/** 订单创建时间 **/@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")private Date createDate;/** 支付时间 **/@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")private Date payDate;}
比如我这里的 Order 的 id 是 long 类型,如果ES 中对应的id 字段长度我定义为 integer 类型,同步时就会出错,因为长度不一样
3.2 查询上述数据
/*** 获取文档信息*/@Testpublic void getDocument() throws IOException {// 创建获取请求对象GetRequest getRequest = new GetRequest("order", "83");GetResponse response = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT);System.out.println(response.getSourceAsString());}
3.3 修改
/*** 更新文档信息*/@Testpublic void updateDocument() throws IOException {// 设置商品更新信息Order goods = new Order();goods.setGoodsName("Apple iPhone 苹果手机");goods.setGoodsPrice(new BigDecimal("345"));// 将对象转为jsonString data = JSON.toJSONString(goods);// 创建索引请求对象UpdateRequest updateRequest = new UpdateRequest("order", "83");// 设置更新文档内容updateRequest.doc(data, XContentType.JSON);// 执行更新文档UpdateResponse response = restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);log.info("创建状态:{}", response.status());}
3.4 删除
/*** 删除文档信息*/@Testpublic void deleteDocument() throws IOException {// 创建删除请求对象DeleteRequest deleteRequest = new DeleteRequest("order", "1");// 执行删除文档DeleteResponse response = restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);log.info("删除状态:{}", response.status());}
3.5 批量插入数据
@Testpublic void testBulkRequest() throws IOException {// 批量查询订单数据List<Order> orderList = orderService.alllist();// 1.创建RequestBulkRequest request = new BulkRequest();// 2.准备参数,添加多个新增的Requestfor (Order order : orderList) {// 创建新增文档的Request对象request.add(new IndexRequest("order").id(order.getId().toString()).source(JSON.toJSONString(order), XContentType.JSON));}// 3.发送请求BulkResponse bulk = restHighLevelClient.bulk(request, RequestOptions.DEFAULT);log.info("执行状态:{}", bulk.status());}
项目源码在这里 :https://gitee.com/android.long/spring-boot-study/tree/master/biglead-api-12-es
有兴趣可以关注一下公众号:biglead
- 创建SpringBoot基础项目
- SpringBoot项目集成mybatis
- SpringBoot 集成 Druid 数据源【SpringBoot系列3】
- SpringBoot MyBatis 实现分页查询数据【SpringBoot系列4】
- SpringBoot MyBatis-Plus 集成 【SpringBoot系列5】
- SpringBoot mybatis-plus-generator 代码生成器 【SpringBoot系列6】
- SpringBoot MyBatis-Plus 分页查询 【SpringBoot系列7】
- SpringBoot 集成Redis缓存 以及实现基本的数据缓存【SpringBoot系列8】
- SpringBoot 整合 Spring Security 实现安全认证【SpringBoot系列9】
- SpringBoot Security认证 Redis缓存用户信息【SpringBoot系列10】
- SpringBoot 整合 RabbitMQ 消息队列【SpringBoot系列11】
- SpringBoot 结合RabbitMQ与Redis实现商品的并发下单【SpringBoot系列12】
- SpringBoot 雪花算法生成商品订单号【SpringBoot系列13】
- SpringBoot RabbitMQ 延时队列取消订单【SpringBoot系列14】
- SpringBoot RabbitMQ 商品秒杀【SpringBoot系列15】