Elasticsearch Java访问

package com.es.api;

import com.alibaba.fastjson.JSONObject;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.net.InetAddress;
import java.util.Date;

import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;

public class EsHandle {

    private TransportClient client = null;

    public void init(){
        try {
            Settings settings = Settings.builder().put("cluster.name","estest").build();
            client = new PreBuiltTransportClient(settings).addTransportAddress(
                    new TransportAddress(InetAddress.getByName("192.168.59.132"), 9301));
        }
        catch (Exception e){
            System.out.println(e);
        }
    }

    //创建索引,写入数据测试
    public boolean createIndex(){
        try {
            IndexResponse response = client.prepareIndex("testapi","fultext","3").setSource(jsonBuilder().startObject().field("username", "sq").field("sex", "woman")
                    .field("birthday", new Date()).field("age", 22).field("message", "trying out Elasticsearch")
                    .endObject()).get();
            System.out.println(response.toString());
        }
        catch (Exception e){
            System.out.println(e);
        }
        return true;
    }

    //获取索引
    public String GetIndex(){
        GetResponse getResponse = client.prepareGet("testapi","fultext","3").get();
        return getResponse.getSourceAsString();
    }

    public void readFileLine(){

        try{
            File f= new File("F:/test/201909.json");
            BufferedReader br = new BufferedReader(new FileReader(f));

            String line = null;
            int i=1;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
                JSONObject jsonObject = JSONObject.parseObject(line);
                XContentBuilder builder = JsonXContent.contentBuilder().startObject().field("dataType",jsonObject.getString("dataType"))
                        .field("jdFrom",jsonObject.getString("jdFrom"))
                        .field("pubTime",jsonObject.getString("pubTime"))
                        .field("incName",jsonObject.getJSONObject("jdInc").getString("incName"))
                        .field("incScale",jsonObject.getJSONObject("jdInc").getString("incScale"))
                        .field("incType",jsonObject.getJSONObject("jdInc").getString("incType"))
                        .field("incIndustryList",jsonObject.getJSONObject("jdInc").getString("incIndustryList"))
                        .field("incLocation",jsonObject.getJSONObject("jdInc").getString("incLocation"))
                        .field("jobTypeList",jsonObject.getJSONObject("jdJob").getString("jobTypeList"))
                        .field("jobPosition",jsonObject.getJSONObject("jdJob").getString("jobPosition"))
                        .field("age",jsonObject.getJSONObject("jdJob").getString("age"))
                        .field("keyWordList",jsonObject.getJSONObject("others").getString("keyWordList"))
                        .field("isFullTime",jsonObject.getJSONObject("others").getString("isFullTime"))
                        .field("jobReport",jsonObject.getJSONObject("others").getString("jobReport"))
                        .field("status",jsonObject.getJSONObject("indexInfo").getString("status")).endObject();
//                XContentBuilder builder = JsonXContent.contentBuilder().value(line);//startObject(line);
//                XContentParser parser = (XContentParser) XContentFactory.xContent(XContentType.JSON).createParser(line.getBytes());
//                parser.close();
//                XContentBuilder builder = jsonBuilder().copyCurrentStructure(parser);
                IndexResponse response = client.prepareIndex("zhilian_jd","fultext",String.valueOf(i++)).setSource(builder).get();
                System.out.println(response.toString());
            }

            br.close();
        }
        catch (Exception e){
            System.out.println(e);
        }

    }

    public static void main(String[] args) {
        EsHandle eh = new EsHandle();
        eh.init();
        eh.readFileLine();
//        eh.createIndex();
//        System.out.println(eh.GetIndex());
    }
}

后面继续完善

©️2020 CSDN 皮肤主题: 像素格子 设计师:CSDN官方博客 返回首页