西二旗网站建设,wordpress底部跟随按钮怎么做,西安高校定制网站建设,wordpress news themeSpringCloud 大型系列课程正在制作中#xff0c;欢迎大家关注与提意见。 程序员每天的CV 与 板砖#xff0c;也要知其所以然#xff0c;本系列课程可以帮助初学者学习 SpringBooot 项目开发 与 SpringCloud 微服务系列项目开发 elasticsearch是一款非常强大的开源搜索引擎欢迎大家关注与提意见。 程序员每天的CV 与 板砖也要知其所以然本系列课程可以帮助初学者学习 SpringBooot 项目开发 与 SpringCloud 微服务系列项目开发 elasticsearch是一款非常强大的开源搜索引擎具备非常多强大功能可以帮助我们从海量数据中快速找到需要的内容。 本项目数据库使用的是 MySql ,查询数据使用的是 ElasticSearch 1 项目准备
SpringBoot RabbitMQ 延时队列取消订单【SpringBoot系列14】 本文章 基于这个项目来开发
本文章是系列文章 每节文章都有对应的代码每节的源码都是在上一节的基础上配置而来对应的视频讲解课程正在火速录制中。
1 项目依赖添加
首先是你的开发环境 以及服务器要安装es我这里是使用 docker 来安装的 docker-compose安装elasticsearch及kibana
项目 pom.xm 中添加依赖 dependencygroupIdorg.elasticsearch.client/groupIdartifactIdelasticsearch-rest-high-level-client/artifactId/dependencydependencygroupIdorg.elasticsearch.client/groupIdartifactIdelasticsearch-rest-client/artifactId/dependencydependencygroupIdorg.elasticsearch/groupIdartifactIdelasticsearch/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-configuration-processor/artifactIdoptionaltrue/optional/dependency!--测试使用--dependencygroupIdjunit/groupIdartifactIdjunit/artifactIdversion4.13.2/versionscopetest/scope/dependencyapplication.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是否创建索引默认为trueanalyzer使用哪种分词器properties该字段的子字段 然后查询一下索引库
#查询
GET /order删除索引库
#删除
DELETE /order3 文档操作
这是现在数据库中订单表的数据
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 GMT8)private Date createDate;/** 支付时间 **/JsonFormat(pattern yyyy-MM-dd HH:mm:ss, timezone GMT8)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 {// 批量查询订单数据ListOrder 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项目集成mybatisSpringBoot 集成 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】