微服务框架 SpringCloud微服务架构 21 RestClient 操作文档 21.1 新增文档
微服务框架
【SpringCloud+RabbitMQ+Docker+Redis+搜索+分布式,系统详解springcloud微服务技术栈课程|黑马程序员Java微服务】
SpringCloud微服务架构
文章目录
- 微服务框架
- SpringCloud微服务架构
- 21 RestClient 操作文档
- 21.1 新增文档
- 21.1.1 新增文档
21 RestClient 操作文档
21.1 新增文档
21.1.1 新增文档
利用JavaRestClient实现文档的CRUD
去数据库查询酒店数据,导入到hotel索引库,实现酒店数据的CRUD。
基本步骤如下:
-
初始化JavaRestClient
-
利用JavaRestClient新增酒店数据
-
利用JavaRestClient根据id查询酒店数据
-
利用JavaRestClient删除酒店数据
-
利用JavaRestClient修改酒店数据
【初始化就可以跳过了】
新建一个测试类,实现文档相关操作,并且完成JavaRestClient的初始化
步骤2:添加酒店数据到索引库
先查询酒店数据,然后给这条数据创建倒排索引,即可完成添加:
直接试试
先弄一个和索引库结构一致的实体类
package cn.itcast.hotel.pojo;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* ClassName: HotelDoc
* date: 2022/11/1 10:51
*
* @author DingJiaxiong
*/
@Data
@NoArgsConstructor
public class HotelDoc {
private Long id;
private String name;
private String address;
private Integer price;
private Integer score;
private String brand;
private String city;
private String starName;
private String business;
private String location;
private String pic;
public HotelDoc(Hotel hotel) {
this.id = hotel.getId();
this.name = hotel.getName();
this.address = hotel.getAddress();
this.price = hotel.getPrice();
this.score = hotel.getScore();
this.brand = hotel.getBrand();
this.city = hotel.getCity();
this.starName = hotel.getStarName();
this.business = hotel.getBusiness();
this.location = hotel.getLatitude() + ", " + hotel.getLongitude();
this.pic = hotel.getPic();
}
}
业务层接口
package cn.itcast.hotel.service;
import cn.itcast.hotel.pojo.Hotel;
import com.baomidou.mybatisplus.extension.service.IService;
public interface IHotelService extends IService<Hotel> {
}
来一个新的测试类
package cn.itcast.hotel;
import cn.itcast.hotel.pojo.Hotel;
import cn.itcast.hotel.pojo.HotelDoc;
import cn.itcast.hotel.service.IHotelService;
import cn.itcast.hotel.service.impl.HotelService;
import com.alibaba.fastjson.JSON;
import org.apache.http.HttpHost;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.IOException;
/**
* ClassName: HotelDocumentTest
* date: 2022/11/1 10:45
*
* @author DingJiaxiong
*/
@SpringBootTest
public class HotelDocumentTest {
@Autowired
private IHotelService hotelService;
private RestHighLevelClient client;
// 添加文档
@Test
void testAddDocument() throws IOException {
//根据id 查询酒店数据
Hotel hotel = hotelService.getById(61083L);
//转换为文档类型
HotelDoc hotelDoc = new HotelDoc(hotel);
//1. 准备Request 对象
IndexRequest request = new IndexRequest("hotel").id(hotel.getId().toString());
//2. 准备Json 文档
request.source(JSON.toJSONString(hotelDoc), XContentType.JSON);
//3. 发送请求
client.index(request , RequestOptions.DEFAULT);
}
@BeforeEach
void setUp() { //在每个测试方法开始前都完成初始化工程
this.client = new RestHighLevelClient(RestClient.builder(
HttpHost.create("http://1.13.4.44:9200")
));
}
@AfterEach
void tearDown() throws IOException { //每次都自动销毁
this.client.close();
}
}
直接运行测试方法
我giao, 我红了,换一个驱动
再试一次
又红了,我再看看
行吧,数据库密码写错了…
再试一次,又红了…url 错了,我真的会喊救命
再试一次
绿了
kibana 验证一下
GET /hotel/_doc/61083
OK, 没毛病
【这就是添加文档】