栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

Elasticsearch-02 Java Client测试

Elasticsearch-02 Java Client测试

前言

先上一个简单的Java客户端
后边再上springboot项目集成
这个没什么好说的,直接上代码了
不要问是怎么玩儿的,跟着官网教程 一步一步抄就好
官方参考代码
REST API
JAVA API
源码地址

pom.xml
        
            org.apache.logging.log4j
            log4j-api
            2.17.0
        

        
            com.alibaba
            fastjson
            1.2.73
        

        
            org.projectlombok
            lombok
            1.18.6
        

        
            co.elastic.clients
            elasticsearch-java
            7.17.0
        
        
            com.fasterxml.jackson.core
            jackson-databind
            2.12.3
        
java代码

对象实例

package com.deao.test;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Date;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class UserActionDTO {

    public Long userId;

    public Long schoolId;

    public String identifier;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    public Date timeActive;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    public Date timeLeave;

    public String phoneModel;

    public String accessPath;

    public String userType;

    public String url;

    public String version;

    public String sysVersion;

}

main方法

package com.deao.test;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch.core.SearchResponse;
import co.elastic.clients.elasticsearch.core.search.Hit;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import com.alibaba.fastjson.JSON;
import lombok.extern.log4j.Log4j2;
import org.apache.http.HttpHost;
import org.elasticsearch.client.HttpAsyncResponseConsumerFactory;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;

import java.io.IOException;

@Log4j2
public class RestClientTest717 {

    private static final RequestOptions COMMON_OPTIONS;

    static {
        RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();

        // 默认缓存限制为100MB,此处修改为30MB。
        builder.setHttpAsyncResponseConsumerFactory(
                new HttpAsyncResponseConsumerFactory
                        .HeapBufferedResponseConsumerFactory(30 * 1024 * 1024));
        COMMON_OPTIONS = builder.build();
    }

    public static void main(String[] args) {

        // Create the low-level client
        RestClient restClient = RestClient.builder(new HttpHost("192.168.2.18", 9200))
            .build();
        // Create the transport with a Jackson mapper
        ElasticsearchTransport transport = new RestClientTransport(
                restClient, new JacksonJsonpMapper());
        // And create the API client - Synchronous blocking client
        ElasticsearchClient client = new ElasticsearchClient(transport);

        try {
//            // 创建索引 products
//            CreateIndexResponse products = client.indices().create(c -> c.index("products"));
//            // 创建索引 test-index
//            CreateIndexResponse createResponse = client.indices().create(
//                    new CreateIndexRequest.Builder()
//                            .index("test-index")
//                            .aliases("foo", aliasBuilder -> aliasBuilder.isWriteIndex(true))
//                            .build()
//            );
            SearchResponse search = client.search(s -> s
                            .index("weiheng")
                            .query(q -> q
                                    .term(t -> t
                                            .field("userId")
                                            .value(v -> v.stringValue("1"))
                                    )),
                    UserActionDTO.class);

            for (Hit hit: search.hits().hits()) {
                UserActionDTO source = hit.source();
                System.out.println(JSON.toJSONString(source));
            }
            // 关闭客户端
            restClient.close();
        } catch (IOException ioException) {
            // 异常处理。
        }
    }
}
测试结果

读取索引 weiheng 里的数据内容
新增索引
kibana新增数据

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/736059.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号