Elasticsearch5.3 JAVA代码添加数据 Demo

Elasticsearch5.3 JAVA 添加数据 数据是从数据库查出来的!当然也可以写假的测试数据。

小弟写的不好!望见谅!我最近也是正在学习Elasticsearch,从来没有接触过,第一次接触!没有什么经验!

代码有的是从网上找的有的是小弟自己写的!希望对您有那么一点的启发!

package com.vdyoo.paas.answerservice.search.es.escreateindex;

import com.google.gson.Gson;
import com.vdyoo.paas.answerservice.exam.model.ClasssStudentExam;
import com.vdyoo.paas.answerservice.exam.service.ClasssStudentExamService;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.jsmth.spring.test.BaseTest;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by 王启才 on 2017/6/25.
 */
@Controller
public class InsertData extends BaseTest {
    @Resource
    ClasssStudentExamService classsStudentExamService;
    TransportClient client;

    @Before
    public void setup() throws UnknownHostException {
        System.out.println("create TransportClient...");
        client = new PreBuiltTransportClient(Settings.EMPTY)
                .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(""), 9300))
                .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(""), 9300))
                .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(""), 9300));
    }

    @After
    public void clearup() {
        System.out.println("close TransportClient...");
        if (client != null) {
            client.close();
        }
    }

    @Test
    public void doIndex() throws IOException {
        Gson gson = new Gson();
        //查询学生答题的基础数据数据
        List classsStudentExamServiceAll = classsStudentExamService.findAll();
        for (ClasssStudentExam classsStudentExam:classsStudentExamServiceAll) {
            System.out.println("===========学生基础数据===id:  "+classsStudentExam.getId()+"    =====ClasssStudentExam.id");
            System.out.println("===========学生基础数据===班级:  "+classsStudentExam.getClasss()+"    =====ClasssStudentExam.Classs");
            System.out.println("===========学生基础数据===课程班级:  "+classsStudentExam.getCourseClass()+"   =====ClasssStudentExam.CourseClass");
            System.out.println("===========学生基础数据===CourseClassExam(教学班):  "+classsStudentExam.getCourseClassExam()+"    =====ClasssStudentExam.CourseClassExam");
            System.out.println("===========学生基础数据===题号:  "+classsStudentExam.getExamnum()+"   =====ClasssStudentExam.Examnum");
            System.out.println("===========学生基础数据===是否Ok:  "+classsStudentExam.getOk()+"    =====ClasssStudentExam.ok");
            System.out.println("===========学生基础数据===教室编号:  "+classsStudentExam.getRoom()+"    =====ClasssStudentExam.room");
            System.out.println("===========学生基础数据===得分:  "+classsStudentExam.getScore()+"    =====ClasssStudentExam.score");
            System.out.println("===========学生基础数据===学生编号:  "+classsStudentExam.getStudent()+"    =====ClasssStudentExam.student");
            System.out.println("===========学生基础数据===更新时间:  "+classsStudentExam.getUpdatetime()+"    =====ClasssStudentExam.Updatetime");
            System.out.println("===========学生基础数据===创建时间:  "+classsStudentExam.getCreatetime()+"   =====ClasssStudentExam.Createtime");
            System.out.println("===========学生基础数据===答案:  "+classsStudentExam.getAnswer()+"     =====ClasssStudentExam.answer");
            System.out.println("===========学生基础数据===教学计划:  "+classsStudentExam.getCourseplan()+"   =====ClasssStudentExam.coursePlan");
            Map map = new HashMap();

            map.put("id", classsStudentExam.getId());
            map.put("Classs", classsStudentExam.getClasss());
            map.put("CourseClass", classsStudentExam.getCourseClass());
            map.put("CourseClassExam",classsStudentExam.getCourseClassExam());
            map.put("Examnum",classsStudentExam.getExamnum());
            map.put("Ok",classsStudentExam.getOk());
            map.put("Room",classsStudentExam.getRoom());
            map.put("Score",classsStudentExam.getScore());
            map.put("Student",classsStudentExam.getStudent());
            map.put("Updatetime",classsStudentExam.getUpdatetime());
            map.put("Createtime",classsStudentExam.getCreatetime());
            map.put("Answer",classsStudentExam.getAnswer());
            map.put("CourseplanId",classsStudentExam.getCourseplan());
            String json = gson.toJson(map);
            System.out.println(json);
            //索引名称 和 类型
            IndexRequestBuilder indexRequestBuilder = client.prepareIndex("classssstudentexamtest1234", "answer", classsStudentExam.getId().toString()).setSource(json);
            IndexResponse response = indexRequestBuilder.get();
            String _index = response.getIndex();
            System.out.println(_index);
            // Type name
            String _type = response.getType();
            // Document ID (generated or not)
            String _id = response.getId();
            // Version (if it's the first time you index this document, you will get: 1)
            long _version = response.getVersion();
            // status has stored current instance statement.
            RestStatus status = response.status();

        }
    }
}


你可能感兴趣的:(Elasticsearch5.3 JAVA代码添加数据 Demo)