当前位置: 首页 > news >正文

Es bulk批量导入数据(1w+以上)

最近在学习es的理论知识以及实际操作,随时更新~
概要:首先你得有1w条数据的json,然后用java读取json文件导入

一. 创建Json数据

首先我生成1.5w条数据,是为了实践分页查询,用from-size和scroll翻页去实践
生成四个字段,name、age、sex、telephone

代码如下:可直接复制粘贴用

package es;import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;/*** 生成1.5W条json数据*/
public class JsonGenerator {public static void main(String[] args) {int numberOfRecords = 15000;String outputFile = "user.json";try (FileWriter writer = new FileWriter(outputFile)) {for (int i = 0; i < numberOfRecords; i++) {writer.write(generateJsonRecord());if (i < numberOfRecords - 1) {writer.write(",");} else {writer.write(" ");}}System.out.println("Generated " + numberOfRecords + " records successfully.");} catch (IOException e) {e.printStackTrace();}}private static String generateJsonRecord() {String name = generateRandomName();int age = generateRandomAge();String sex = generateRandomSex();String telephone = generateRandomTelephone();return "{\"name\":\"" + name + "\",\"age\":\"" + age + "\",\"sex\":\"" + sex + "\",\"telephone\":\"" + telephone + "\"}";}private static String generateRandomName() {String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";StringBuilder sb = new StringBuilder();Random random = new Random();for (int i = 0; i < 5; i++) {sb.append(characters.charAt(random.nextInt(characters.length())));}return sb.toString();}private static int generateRandomAge() {Random random = new Random();return random.nextInt(65) + 18;  // Generate age between 18 and 65}private static String generateRandomSex() {Random random = new Random();return random.nextBoolean() ? "Male" : "Female";}private static String generateRandomTelephone() {StringBuilder sb = new StringBuilder();Random random = new Random();for (int i = 0; i < 10; i++) {sb.append(random.nextInt(10));  // Append random digit to the telephone number}sb.append("-");for (int i = 0; i < 3; i++) {sb.append(random.nextInt(10));  // Append random digit to the telephone number}return sb.toString();}
}

生成的文件在该位置上
在这里插入图片描述

二.bulk 批量导入数据

public class EsConnectionExample {                                   public static void main(String[] args) throws IOException {      // 创建客户端                                                                                                            RestHighLevelClient client = new RestHighLevelClient(                                                               RestClient.builder(                                                                                         new HttpHost("ip", port, "http"))); // 修改为你的ES地址和端口                                      //bulk 导入  //这块改成你的文件的地址                                                                                         try (BufferedReader reader = new BufferedReader(new FileReader("user.json"))) {                     String line;                                                                                    // 构造 BulkRequest 对象并添加要导入的文档                                                                   BulkRequest request = new BulkRequest();                                                        while ((line = reader.readLine()) != null) {                                                    XContentBuilder builder = XContentFactory.jsonBuilder()                                     .startObject()                                                                      .field("name", line)                                                                .field("age", line)                                                                 .field("sex" , line)                                                                .field("telephone", line)                                                           .endObject();   //这块改成你的索引名字                                                                    IndexRequest indexRequest = new IndexRequest("my_index")                                    .source(builder);                                                                   request.add(indexRequest);                                                                  }                                                                                               // 发送 BulkRequest 请求                                                                            BulkResponse response = client.bulk(request, RequestOptions.DEFAULT);                           if (response.hasFailures()) {                                                                   System.out.println("Failed to import documents.");                                          } else {                                                                                        System.out.println("Documents imported successfully!");                                     }                                                                                               } catch (IOException e) {                                                                           e.printStackTrace();                                                                            } finally {                                                                                         // 关闭 ElasticSearch 客户端连接                                                                       client.close();                                                                                 } }                                                                                                  }                                                                                                                                                                                                               

此时已经插入了
在这里插入图片描述

http://www.lryc.cn/news/285105.html

相关文章:

  • #laravel 通过手动安装依赖PHPExcel#
  • Webpack 基本使用 - 1
  • 要编译Android 12系统的开机Logo,你需要执行以下步骤:
  • 【JS逆向学习】36kr登陆逆向案例(webpack)
  • R语言的ggplot2绘制分组折线图?
  • [C#]winform部署官方yolov8-obb旋转框检测的onnx模型
  • Git中config配置
  • Java开发安全之:Unreleased Resource: Streams需确保流得到释放
  • 【C++】文件操作
  • 高效能方法 - 任务清单优先级
  • go 语言爬虫库goquery介绍
  • 解决 Navicat 在笔记本外接显示器分辨率自适应展示问题
  • 网络安全产品之认识入侵检测系统
  • 牛客周赛 Round 10 解题报告 | 珂学家 | 三分模板 + 计数DFS + 回文中心扩展
  • SpringBoot 更新业务场景下,如何区分null是清空属性值 还是null为vo属性默认值?
  • 【深度学习每日小知识】NLP 自然语言处理
  • 一文理解Python选择语句
  • MyBatis XML 映射文件中的 SQL 语句可以分为动态语句和静态语句
  • Flask用于生产环境
  • 程序员如何向上管理,升职加薪
  • Microsoft Word 删除空行
  • 基于一次应用卡死问题所做的前端性能评估与优化尝试
  • JVM(上)
  • 【js】js 异步机制详解 Generator / Async / Promise
  • 【动态规划】【数学】【C++算法】805 数组的均值分割
  • Django笔记(五):模型models
  • 一个golang小白使用vscode搭建Ununtu20.04下的go开发环境
  • 【复现】Hytec Inter HWL 2511 SS路由器RCE漏洞_25
  • Kafka系列(四)
  • 【Linux学习】进程信号