網(wǎng)站開發(fā) 系統(tǒng)需求文檔專業(yè)制作網(wǎng)頁的公司
最近在學(xué)習(xí)es的理論知識(shí)以及實(shí)際操作,隨時(shí)更新~
概要:首先你得有1w條數(shù)據(jù)的json,然后用java讀取json文件導(dǎo)入
一. 創(chuàng)建Json數(shù)據(jù)
首先我生成1.5w條數(shù)據(jù),是為了實(shí)踐分頁查詢,用from-size和scroll翻頁去實(shí)踐
生成四個(gè)字段,name、age、sex、telephone
代碼如下:可直接復(fù)制粘貼用
package es;import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;/*** 生成1.5W條json數(shù)據(jù)*/
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 批量導(dǎo)入數(shù)據(jù)
public class EsConnectionExample { public static void main(String[] args) throws IOException { // 創(chuàng)建客戶端 RestHighLevelClient client = new RestHighLevelClient( RestClient.builder( new HttpHost("ip", port, "http"))); // 修改為你的ES地址和端口 //bulk 導(dǎo)入 //這塊改成你的文件的地址 try (BufferedReader reader = new BufferedReader(new FileReader("user.json"))) { String line; // 構(gòu)造 BulkRequest 對(duì)象并添加要導(dǎo)入的文檔 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); } // 發(fā)送 BulkRequest 請(qǐng)求 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 { // 關(guān)閉 ElasticSearch 客戶端連接 client.close(); } } }
此時(shí)已經(jīng)插入了