ElasticSearch介绍及基本API

1. ES基本介绍

https://blog.csdn.net/achuo/article/details/87865141Elasticsearch学习,请先看这一篇!内容比较完备,推荐阅读

2. API操作练习

 <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.12version>
            <scope>testscope>
        dependency>
        <dependency>
            <groupId>org.elasticsearch.clientgroupId>
            <artifactId>transportartifactId>
            <version>${elasticsearch.version}version>
        dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.coregroupId>
            <artifactId>jackson-coreartifactId>
            <version>2.9.0version>
        dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.coregroupId>
            <artifactId>jackson-databindartifactId>
            <version>2.9.0version>
        dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.coregroupId>
            <artifactId>jackson-annotationsartifactId>
            <version>2.9.0version>
        dependency>
import org.elasticsearch.action.bulk.BulkItemResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.get.MultiGetItemResponse;
import org.elasticsearch.action.get.MultiGetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
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.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author YDAlex
 * @version 1.0
 * @description 用javaAPI操作ES集群练习
 * @data 2019/11/20.
 */
public class EsJavaClient
{

    private static final String INDEX = "javaindex";
    private static final String TYPE = "javatype";
    private static  TransportClient client;

    @Before
    public void init() throws UnknownHostException {
        Settings settings = Settings.builder()
                .put("cluster.name","MyES")
                .put("client.transport.sniff", "true")//增加自动嗅探配置
                .build();

        client = new PreBuiltTransportClient(settings);
        client.addTransportAddress(new TransportAddress(InetAddress.getByName("hadoop"), 9300));

        System.out.println(client.toString());
    }
    @Test
    public void createIndex() {
        client.admin().indices().prepareCreate(INDEX).get();
        System.out.println("创建Index成功");
    }

    @Test
    public void deleteIndex() {
        client.admin().indices().prepareDelete(INDEX).get();
        System.out.println("删除Index成功");
    }

    //不推荐使用
    @Test
    public void createDoc() {
        String json = "{\"name\":\"GPY\"}";

        IndexResponse response = client.prepareIndex(INDEX, TYPE, "100")
                .setSource(json, XContentType.JSON)
                .get();
        System.out.println("返回的结果是:"+response);
    }

    //推荐使用
    @Test
    public void createDoc01() {
        Map<String, Object> json = new HashMap<>();
        json.put("name", "GPY");
        json.put("message", "trying out Elasticsearch");

        IndexResponse response = client.prepareIndex(INDEX, TYPE, "101").setSource(json).get();
        System.out.println(response.getVersion());
    }


    //推荐使用
    @Test
    public void createDoc02() throws IOException {

        XContentBuilder builder = XContentFactory.jsonBuilder()
                .startObject()
                .field("user", "GPY")
                .field("postDate", new Date())
                .field("message", "trying out Elasticsearch")
                .endObject();

        IndexResponse response = client.prepareIndex(INDEX, TYPE, "102").setSource(builder).get();
        System.out.println(response.getVersion());
    }

    @Test
    public void getDoc() {
        GetResponse response = client.prepareGet(INDEX, TYPE, "100").get();
        System.out.println(response.getSourceAsString());
    }

    @Test
    public void getDocsByIds() {

        MultiGetResponse responses = client.prepareMultiGet()
                .add(INDEX, TYPE,"100")
                .add(INDEX, TYPE, "101", "102", "1000")
                .get();

        for (MultiGetItemResponse response : responses) {
            GetResponse res = response.getResponse();
            if (res.isExists()) {
                System.out.println(res);
            } else {
                System.out.println("没有这条数据");
            }

        }
    }

    @Test
    public void testBatchDealWith(){
        //需求使用批处理技术,操作es索引之javaindex
        //新增索引name:beam,author:haha,version:4.4.4
        //删除索引id:100
        //修改索引id为102的信息,message:has changed

        BulkItemResponse []itms =  client.prepareBulk()
                .add(new IndexRequest(INDEX,TYPE).source("name","beam","author","haha","version","4.4.4"))
                .add(new DeleteRequest(INDEX,TYPE,"100"))
                .add(new UpdateRequest(INDEX,TYPE,"102").doc("message","has changed"))
                .get().getItems();

        for (BulkItemResponse itm: itms) {
            itm.isFailed();
        }


    }


    @After
    public void close(){
        client.close();
    }
}

你可能感兴趣的:(大数据学习)