Debian安装和使用Elasticsearch 8.9

命令行通过 .deb 包安装 Elasticsearch

创建一个新用户

bash 复制代码
adduser elastic --> rust
# 添加sudo权限
# https://phoenixnap.com/kb/how-to-create-sudo-user-on-ubuntu
usermod -aG sudo elastic
groups elastic

下载Elasticsearch v8.9.0 Debian 包

https://www.elastic.co/guide/en/elasticsearch/reference/current/deb.html#install-deb

Elasticsearch: 初学者指南

https://medium.com/@animeshblog/elasticsearch-the-beginners-cookbook-1cf30f98218

快速开始

主要参考:Verifying HTTPS with a certificate fingerprint

创建 Elasticsearch Java客户端

java 复制代码
package org.elastic.service.es;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.TransportUtils;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.elasticsearch.client.RestClient;

import javax.net.ssl.SSLContext;

/**
 * <a href="https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/connecting.html#_verifying_https_with_a_certificate_fingerprint">Verifying HTTPS with a certificate fingerprint</a>
 */
public class MEElastic {
    /**
     * The SHA-256 fingerprint of the certificate used to sign the Elasticsearch server certificate.
     * <p>
     * come from <a href="https://security.stackexchange.com/a/14345">What is the actual value of a certificate fingerprint?</a>
     */
    private static final String fingerprint = "2c895506b07083da7299656fa7fc4b433c3e26c03cf1a99eef23c537711b6e6e";


    private static final String esUsername = "elastic";

    private static final String esPassword = "k_*t_arOy7RF6EQVL-QA";

    private RestClient elasticsearchRestClient() {
        SSLContext sslContext = TransportUtils
                .sslContextFromCaFingerprint(fingerprint);
        BasicCredentialsProvider credsProv = new BasicCredentialsProvider();
        credsProv.setCredentials(
                AuthScope.ANY, new UsernamePasswordCredentials(esUsername, esPassword)
        );
        return RestClient
                .builder(new HttpHost("127.0.0.1", 9200, "https"))
                .setHttpClientConfigCallback(hc -> hc
                        .setSSLContext(sslContext)
                        .setDefaultCredentialsProvider(credsProv)
                        .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
                )
                .build();
    }

    public ElasticsearchClient elasticsearchClient() {
        // Create the transport and the API client
        ElasticsearchTransport transport = new RestClientTransport(elasticsearchRestClient(), new JacksonJsonpMapper());
        return new ElasticsearchClient(transport);
    }
}

测试ElasticsearchClient

java 复制代码
@Test
void elasticsearchClient() throws IOException {
    MEElastic meElastic = new MEElastic();
    ElasticsearchClient esClient = meElastic.elasticsearchClient();
    GetIndexResponse rsp = esClient.indices().get(request -> request.index("test"));
    System.out.println(rsp);
    esClient._transport().close();
}
相关推荐
雁于飞8 小时前
vscode中使用git、githup的基操
笔记·git·vscode·学习·elasticsearch·gitee·github
今生相伴99110 小时前
ELFK:企业级日志管理的完整解决方案——从入门到精通
运维·elk·elasticsearch
在未来等你17 小时前
Elasticsearch面试精讲 Day 15:索引别名与零停机更新
大数据·分布式·elasticsearch·搜索引擎·面试
在未来等你19 小时前
Elasticsearch面试精讲 Day 12:数据建模与字段类型选择
大数据·分布式·elasticsearch·搜索引擎·面试
在未来等你1 天前
Elasticsearch面试精讲 Day 14:数据写入与刷新机制
大数据·分布式·elasticsearch·搜索引擎·面试
phac1231 天前
git 如何直接拉去远程仓库的内容且忽略本地与远端不一致的commit
大数据·git·elasticsearch
在未来等你1 天前
Elasticsearch面试精讲 Day 11:索引模板与动态映射
大数据·分布式·elasticsearch·搜索引擎·面试
哥哥还在IT中1 天前
Elasticsearch优化从入门到精通
大数据·elasticsearch·搜索引擎
Elastic 中国社区官方博客1 天前
使用 cloud-native Elasticsearch 与 ECK 运行
大数据·数据库·elasticsearch·搜索引擎·kubernetes·k8s·全文检索
2301_781668611 天前
Elasticsearch 02
大数据·elasticsearch·搜索引擎