• docker下elasticsearch开启账号密码验证与java集成elasticsearch


    第一步:找到elasticsearch容器,命令

    docker ps
    
    • 1

    第二步:进docker下部署的elasticsearch中,容器名为第一步中elasticsearch对应的容器名称,命令

    docker exec -it db-容器名 bash
    
    • 1

    第三步,修改elasticsearch配置文件,首先进入config目录,目录中有个名为elasticsearch.yml的配置文件,在配置文件中加上一行xpack.security.enabled: true,代表启用xpack进行账号密码验证,命令如下

    cd config
    vi elasticsearch.yml
    
    • 1
    • 2

    //在elasticsearch,yml加上xpack.security.enabled: true,然后:wq保存退出

    第四步:重启elasticsearch

    第五步:重启后执行第二步中的命令,进入容器中,在进入bin目录,执行以下命令

    docker exec -it db-容器名 bash
    cd bin
    ./elasticsearch -d
    
    • 1
    • 2
    • 3

    执行完以上命令后,稍等一会执行./elasticsearch-setup-passwords interactive,这时候就需要为一些默认账户设置密码,设置好之后妥善保存,设置完成之后重启elasticsearch,然后打开浏览器访问服务器9200端口,弹出账号密码输入框代表配置成功

    我们在java中使用elasticsearch时,核心在于RestHighLevelClient高级请求客户端

    首先新建RestClientConfig配置类

    package ktw.micro.service.framework.es;
    
    import org.elasticsearch.client.RestHighLevelClient;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.elasticsearch.client.ClientConfiguration;
    import org.springframework.data.elasticsearch.client.RestClients;
    import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration;
    
    /**
     * @ClassName: RestClientConfig
     * @Description:
     * @author: zhang zihao
     * @date: 2022/11/12  16:15
     */
    @Configuration
    public class RestClientConfig extends AbstractElasticsearchConfiguration {
        @Override
        public RestHighLevelClient elasticsearchClient() {
            return RestClients.create(ClientConfiguration.localhost()).rest();
        }
    
        // no special bean creation needed
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    然后在ElasticsearchUtil工具类中编写客户端生成方法

    public static RestHighLevelClient getClient(String ip, int port, String userName, String password) {
    		final ClientConfiguration clientConfiguration = ClientConfiguration.builder().connectedTo(ip + ":" + port)
    				.withBasicAuth(userName, password).build();
    		return RestClients.create(clientConfiguration).rest();
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5

    然后编写测试类

    public class AppTest {
    	public static void main(String[] args) throws IOException {
    		query("csyd2013");
    	}
    	public static void query(String indexName) throws IOException {
    		RestHighLevelClient client = ElasticsearchUtil.getClient("192.168.2.39", 9200,"elastic","123456");
    		Coordinate[] coordinates = new Coordinate[5];
    		// coordinates[0] = new Coordinate(111.471, 27.257);
    		// coordinates[1] = new Coordinate(111.495, 27.241);
    		// coordinates[2] = new Coordinate(111.476, 27.238);
    		// coordinates[3] = new Coordinate(111.471, 27.257);
    
    		coordinates[0] = new Coordinate(107, 29);
    		coordinates[1] = new Coordinate(115, 29);
    		coordinates[2] = new Coordinate(115, 23);
    		coordinates[3] = new Coordinate(107, 23);
    		coordinates[4] = new Coordinate(107, 29);
    
    		CoordinatesBuilder coordinatesBuilder = new CoordinatesBuilder().coordinates(coordinates).close();
    		PolygonBuilder polygonBuilder = new PolygonBuilder(coordinatesBuilder);
    		@SuppressWarnings("deprecation")
    		GeoShapeQueryBuilder query = QueryBuilders.geoIntersectionQuery("the_geom", polygonBuilder);
    		// GeoShapeQueryBuilder query =
    		// QueryBuilders.geoIntersectionQuery("the_geom",
    		// polygonBuilder.buildGeometry());
    		SearchRequest searchRequest = new SearchRequest(indexName);
    		SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    		sourceBuilder.query(query);
    		searchRequest.source(sourceBuilder);
    		SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
    
    		Date t1 = new Date();
    		int count = searchResponse.getHits().getHits().length;
    
    		// SearchHits histogram =
    		// searchResponse.setSize(count.intValue()).execute().actionGet().getHits().value;
    		Date t2 = new Date();
    		int i = 1;
    		for (SearchHit searchHit : searchResponse.getHits().getHits()) {
    
    			Map<String, Object> search = searchHit.getSourceAsMap();
    			i++;
    		}
    		Date t3 = new Date();
    		System.out.println(t2.getTime() - t1.getTime());
    
    		System.out.println(t3.getTime() - t2.getTime());
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
  • 相关阅读:
    网格数据生成函数meshgrid
    2-5基础配置-Win2003增加攻击面
    【板栗糖GIS】DOS—如何删除特定的文件夹
    响应式设计的实现方式
    笔尖笔帽检测3:Android实现笔尖笔帽检测算法(含源码 可是实时检测)
    消息队列-------Rabbitmq介绍和安装
    MyBatis:核心配置文件
    socket编程|TCP
    嵌入式开发:嵌入式基础知识——正确启动固件项目的 10 条建议
    第五十一章 开发自定义标签 - 使用%CSP.Rule方法
  • 原文地址:https://blog.csdn.net/qq_43582366/article/details/127869716