The most detailed big data ELK article series in the whole network! The new articles have listed the historical article directory to help you review the previous knowledge focus.
catalogue
1. Initialize client connection
3. Write code to add position data
4. Write test cases and add test methods
Elasticsearch programming

1, Add position data
1. Initialize client connection
- Build a client connection using RestHighLevelClient.
- RestClientBuilder is built based on RestClient.builder method
- Add nodes of ES with HttpHost
Reference code:
private RestHighLevelClient restHighLevelClient; private static final String JOB_IDX_NAME = "job_idx"; public JobFullTextServiceImpl() { restHighLevelClient = new RestHighLevelClient(RestClient.builder( new HttpHost("node1", 9200, "http") , new HttpHost("node2", 9200, "http") , new HttpHost("node3", 9200, "http") )); }
2. Close client connection
@Override public void close() { try { restHighLevelClient.close(); } catch (IOException e) { e.printStackTrace(); } }
3. Write code to add position data
Implementation steps:
- An IndexRequest object is built to describe the data requested by ES.
- Set the document ID.
- Use FastJSON to convert entity class objects to JSON.
- Use the IndexRequest.source method to set the document data and set the requested data to JSON format.
- Use ES High level client to call the index method to initiate a request to add a document to the index.
Reference code:
@Override public void add(JobDetail jobDetail) { // 1. Build an IndexRequest object to describe the data requested by ES. IndexRequest indexRequest = new IndexRequest(JOB_IDX_NAME); // 2. Set the document ID. indexRequest.id(jobDetail.getId() + ""); // 3. Build an entity class object and use FastJSON to convert the entity class object into JSON. String json = JSON.toJSONString(jobDetail); // 4. Use the IndexRequest.source method to set the request data. indexRequest.source(json); try { // 5. Use ES High level client to call the index method to initiate the request restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT); } catch (IOException e) { e.printStackTrace(); } System.out.println("Index created successfully!"); }
Common errors:
java.lang.IllegalArgumentException: The number of object passed must be even but was [1] at org.elasticsearch.action.index.IndexRequest.source(IndexRequest.java:474) at org.elasticsearch.action.index.IndexRequest.source(IndexRequest.java:461)
Reason: IndexRequest.source requested to pass an even number of parameters, but only one was passed
4. Write test cases and add test methods
- Create one in the test/java directory cn.it.elasticsearch.service package.
- Create a JobFullTextServiceTest class under cn.it.elasticsearch.service package.
- The JobFullTextService object is constructed in @BeforeTest, and the close method is called in @AfterTest to close the connection.
- Write test cases, build an entity class for testing, and test the add method.
Reference code:
public class JobFullTextServiceTest { private JobFullTextService jobFullTextService; @BeforeTest public void beforeTest() { jobFullTextService = new JobFullTextServiceImpl(); } @Test public void addTest() { // 1. Test new index documents jobFullTextService = new JobFullTextServiceImpl(); JobDetail jobDetail = new JobDetail(); jobDetail.setId(1); jobDetail.setArea("Jiangsu Province-Nanjing City"); jobDetail.setCmp("Elasticsearch university"); jobDetail.setEdu("Bachelor degree or above"); jobDetail.setExp("One year working experience"); jobDetail.setTitle("Big Data Engineer"); jobDetail.setJob_type("full-time"); jobDetail.setPv("1700 Browse times"); jobDetail.setJd("meeting Hadoop All right"); jobDetail.setSalary("5-9 thousand/month"); jobFullTextService.add(jobDetail); } @AfterTest public void afterTest() { jobFullTextService.close(); } }
- 📢 Blog home page: https://lansonli.blog.csdn.net
- 📢 Welcome to praise 👍 Collection ⭐ Leaving a message. 📝 Please correct any errors!
- 📢 This article was originally written by Lansonli and started on CSDN blog 🙉
- 📢 Big data series articles will be updated every day. When you stop to rest, don't forget that others are still running. I hope you will seize the time to study and make every effort to go to a better life ✨