3. 向索引库中导入数据

2024-06-24 05:44
文章标签 数据 索引 导入 库中

本文主要是介绍3. 向索引库中导入数据,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1. 准备数据库对象

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName("tb_hotel")
public class Hotel {@TableId(type = IdType.INPUT)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 latitude;private String longitude;private String pic;
}

2. 准备文档对象

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;/*** 与索引库的doc保持一致*/
@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName("tb_hotel")
public class HotelDoc {@TableId(type = IdType.INPUT)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();//整合地理对象和es中的mappings保持一致this.location = hotel.getLatitude() + "," + hotel.getLongitude();this.pic = hotel.getPic();}
}

3. 向es中插入数据

//向es中插入数据@GetMapping("inserDoc")public void inserDoc() throws IOException {List<Hotel> hotels = hotelMapper.selectList(null);for (int i = 0; i < hotels.size(); i++) {//doc准备Hotel hotel = hotels.get(i);HotelDoc hotelDoc = new HotelDoc(hotel);//准备request对象IndexRequest request = new IndexRequest("hotel").id(hotel.getId().toString());//本人使用的是7.8.0版本,但是依赖用的是6.8.6,因此加入默认的type类型为:_doc,否则会插入失败request.type("_doc");//准备json文档request.source(JSON.toJSONString(hotelDoc),XContentType.JSON);//发送请求restHighLevelClient.index(request,RequestOptions.DEFAULT);}}

在这里插入图片描述

4. 根据文档id查询文档记录

@GetMapping("getDocById")public void getDocById() throws IOException {//1. 创建request对象GetRequest request = new GetRequest("hotel");request.id("1");//doc = 1request.type("_doc");//2.发送请求GetResponse response = restHighLevelClient.get(request, RequestOptions.DEFAULT);String json = response.getSourceAsString();System.out.println(json);}
2023-06-23 13:14:21.142  INFO 11932 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 1 ms
{"address":"柳州东路1号","brand":"如家","business":"弘阳商圈","city":"南京市浦口区","id":1,"location":"33.33,131.33","name":"如家","pic":"http://www.bai.com/images/rujia.png","price":189,"score":7,"starName":"二星级"}
Disconnected from the target VM, address: '127.0.0.1:64333', transport: 'socket'

5 根据文档id更新文档

忍不了,升级了依赖到7.8.0

// 根据id修改数据@GetMapping("updateDocById")public void updateDocById() throws IOException {//1. 创建request对象UpdateRequest request = new UpdateRequest("hotel","1");request.doc("name","jack","age",22);//2.发送请求UpdateResponse update = restHighLevelClient.update(request, RequestOptions.DEFAULT);System.out.println(update.getResult());}

更新结果

2024-06-23 13:31:42.279  INFO 13972 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2024-06-23 13:31:42.279  INFO 13972 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2024-06-23 13:31:42.281  INFO 13972 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 2 ms
UPDATED
2024-06-23 13:32:08.321 ERROR 13972 --- [rRegistryThread] c.xxl.job.core.util.XxlJobRemotingUtil   : Connection refused: connect

在这里插入图片描述

6 删除文档

//删除文档@GetMapping("delDocById")public void delDocById() throws IOException {//1. 创建request对象DeleteRequest request = new DeleteRequest("hotel","1");//2.发送请求DeleteResponse delete = restHighLevelClient.delete(request, RequestOptions.DEFAULT);System.out.println(delete.getResult());}

删除成功

2024-06-23 13:36:22.696  INFO 19580 --- [rRegistryThread] c.x.j.c.thread.ExecutorRegistryThread    : >>>>>>>>>>> xxl-job registry fail, registryParam:RegistryParam{registryGroup='EXECUTOR', registryKey='myXxlJob', registryValue='http://192.168.1.15:8088/'}, registryResult:ReturnT [code=500, msg=xxl-rpc remoting error(Connection refused: connect), for url : http://localhost:18080/xxl-job-admin/api/registry, content=null]
DELETED
2024-06-23 13:36:54.769 ERROR 19580 --- [rRegistryThread] c.xxl.job.core.util.XxlJobRemotingUtil   : Connection refused: connect

7 批量导入

//批量数据导入@GetMapping("batchInsert")public void batchInsert() throws IOException {List<Hotel> hotels = hotelMapper.selectList(null);BulkRequest request = new BulkRequest();for (int i = 0; i < hotels.size(); i++) {request.add(new IndexRequest("hotel").id(hotels.get(i).getId().toString()).source(JSON.toJSONString(hotels.get(i)),XContentType.JSON));}BulkResponse bulk = restHighLevelClient.bulk(request, RequestOptions.DEFAULT);System.out.println(bulk.status());System.out.println(bulk.getTook());}

插入成功

2024-06-23 13:45:46.866  WARN 13928 --- [nio-8080-exec-2] com.zaxxer.hikari.util.DriverDataSource  : Registered driver with driverClassName=com.mysql.jdbc.Driver was not found, trying direct instantiation.
2024-06-23 13:45:46.955  INFO 13928 --- [nio-8080-exec-2] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
OK
73ms
2024-06-23 13:45:58.035 ERROR 13928 --- [rRegistryThread] c.xxl.job.core.util.XxlJobRemotingUtil   : Connection refused: connect

在这里插入图片描述

这篇关于3. 向索引库中导入数据的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1089322

相关文章

SpringBoot分段处理List集合多线程批量插入数据方式

《SpringBoot分段处理List集合多线程批量插入数据方式》文章介绍如何处理大数据量List批量插入数据库的优化方案:通过拆分List并分配独立线程处理,结合Spring线程池与异步方法提升效率... 目录项目场景解决方案1.实体类2.Mapper3.spring容器注入线程池bejsan对象4.创建

PHP轻松处理千万行数据的方法详解

《PHP轻松处理千万行数据的方法详解》说到处理大数据集,PHP通常不是第一个想到的语言,但如果你曾经需要处理数百万行数据而不让服务器崩溃或内存耗尽,你就会知道PHP用对了工具有多强大,下面小编就... 目录问题的本质php 中的数据流处理:为什么必不可少生成器:内存高效的迭代方式流量控制:避免系统过载一次性

C#实现千万数据秒级导入的代码

《C#实现千万数据秒级导入的代码》在实际开发中excel导入很常见,现代社会中很容易遇到大数据处理业务,所以本文我就给大家分享一下千万数据秒级导入怎么实现,文中有详细的代码示例供大家参考,需要的朋友可... 目录前言一、数据存储二、处理逻辑优化前代码处理逻辑优化后的代码总结前言在实际开发中excel导入很

oracle 11g导入\导出(expdp impdp)之导入过程

《oracle11g导入导出(expdpimpdp)之导入过程》导出需使用SEC.DMP格式,无分号;建立expdir目录(E:/exp)并确保存在;导入在cmd下执行,需sys用户权限;若需修... 目录准备文件导入(impdp)1、建立directory2、导入语句 3、更改密码总结上一个环节,我们讲了

MyBatis-plus处理存储json数据过程

《MyBatis-plus处理存储json数据过程》文章介绍MyBatis-Plus3.4.21处理对象与集合的差异:对象可用内置Handler配合autoResultMap,集合需自定义处理器继承F... 目录1、如果是对象2、如果需要转换的是List集合总结对象和集合分两种情况处理,目前我用的MP的版本

GSON框架下将百度天气JSON数据转JavaBean

《GSON框架下将百度天气JSON数据转JavaBean》这篇文章主要为大家详细介绍了如何在GSON框架下实现将百度天气JSON数据转JavaBean,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录前言一、百度天气jsON1、请求参数2、返回参数3、属性映射二、GSON属性映射实战1、类对象映

C# LiteDB处理时间序列数据的高性能解决方案

《C#LiteDB处理时间序列数据的高性能解决方案》LiteDB作为.NET生态下的轻量级嵌入式NoSQL数据库,一直是时间序列处理的优选方案,本文将为大家大家简单介绍一下LiteDB处理时间序列数... 目录为什么选择LiteDB处理时间序列数据第一章:LiteDB时间序列数据模型设计1.1 核心设计原则

Java+AI驱动实现PDF文件数据提取与解析

《Java+AI驱动实现PDF文件数据提取与解析》本文将和大家分享一套基于AI的体检报告智能评估方案,详细介绍从PDF上传、内容提取到AI分析、数据存储的全流程自动化实现方法,感兴趣的可以了解下... 目录一、核心流程:从上传到评估的完整链路二、第一步:解析 PDF,提取体检报告内容1. 引入依赖2. 封装

MySQL中查询和展示LONGBLOB类型数据的技巧总结

《MySQL中查询和展示LONGBLOB类型数据的技巧总结》在MySQL中LONGBLOB是一种二进制大对象(BLOB)数据类型,用于存储大量的二进制数据,:本文主要介绍MySQL中查询和展示LO... 目录前言1. 查询 LONGBLOB 数据的大小2. 查询并展示 LONGBLOB 数据2.1 转换为十

使用SpringBoot+InfluxDB实现高效数据存储与查询

《使用SpringBoot+InfluxDB实现高效数据存储与查询》InfluxDB是一个开源的时间序列数据库,特别适合处理带有时间戳的监控数据、指标数据等,下面详细介绍如何在SpringBoot项目... 目录1、项目介绍2、 InfluxDB 介绍3、Spring Boot 配置 InfluxDB4、I