使用 Java 客户端通过 HTTPS 连接到 Easysearch

news2025/2/22 14:16:23

Easysearch 一直致力于提高易用性,这也是我们的核心宗旨,然而之前一直没有官方的 Java 客户端,也对用户使用造成了一些困扰,现在,我们正式发布了第一个 Java 客户端 Easysearch-client:1.0.1

这一里程碑式的更新为开发人员带来了前所未有的便利性,使得与 Easysearch 集群的交互变得更加简洁和直观。通过 Easysearch-client,开发者可以直接使用 Java 方法和数据结构来进行交互,而不再需要依赖于传统的 HTTP 方法和 JSON。
这一变化大大简化了操作流程,使得数据管理和索引更加高效。Java 客户端的功能范围包括处理数据操作,管理集群,包括查看和维护集群的健康状态,并对 Security 模块全面兼容。它提供了一系列 API,用于管理角色、用户、权限、角色映射和账户。
这意味着安全性和访问控制现在可以更加细粒度地管理,确保了数据的安全性和合规性。

在这篇博客中,你将学习如何配置 Easysearch-client 客户端以通过 HTTPS 连接到 Easysearch。为了演示目的,我将首先设置一个带有 SSL 证书的 Easysearch 服务器。如果你已经有一个在运行,你可以跳过这一步。
接下来,我将引导你完成在 Java 应用程序中配置和使用 Java 客户端的步骤。

设置 Easysearch 服务器

首先从极限科技官网下载最新的 Mac 版本。我使用的是 1.6.1 版本,这是我写这篇文章时的最新版本。

wget https://dl-global.infinilabs.com/easysearch/stable/easysearch-1.6.1-214-mac-amd64.zip

确保您的系统已经安装并设置了 java 环境变量,版本在 11 以上。

解压下载文件。

unzip easysearch-1.6.1-214-mac-amd64.zip -d easysearch-1.6.1

cd 到 easysearch-1.6.1 执行初始化脚本来生成证书并自动下载插件。

 bin/initialize.sh

脚本执行后会自动输出随机生成的 admin 用户密码。

启动 Easysearch

bin/easysearch

此时,您的服务器已经准备就绪。您可以查看 logs/initialize.log 里显示的 curl 命令来进行验证。

curl -ku admin:xxxxxxxxx https://localhost:9200

显示类似的输出响应

{
  "name" : "MacBook-Pro.local",
  "cluster_name" : "easysearch",
  "cluster_uuid" : "1gRYQ6ssTiKGqcyuEN0Dbg",
  "version" : {
    "distribution" : "easysearch",
    "number" : "1.6.1",
    "distributor" : "INFINI Labs",
    "build_hash" : "14846e460e9976ba6d68c80bb9eca52af1179dcf",
    "build_date" : "2023-10-19T14:43:02.636639Z",
    "build_snapshot" : false,
    "lucene_version" : "8.11.2",
    "minimum_wire_lucene_version" : "7.7.0",
    "minimum_lucene_index_compatibility_version" : "7.7.0"
  },
  "tagline" : "You Know, For Easy Search!"
}

下面我们来看如何设置和使用客户端。

设置 Java 客户端

Easysearch 的 Java 客户端可在 中央仓库:https://repo1.maven.org/maven2/ 上获得。将其作为依赖项添加到你的 Java 应用程序中。
对于 Gradle 构建系统,在项目的 build.gradle 文件中包含以下依赖项:

dependencies {
    implementation 'com.infinilabs:easysearch-client:1.0.1'
    implementation "org.apache.logging.log4j:log4j-api:2.19.0"
    implementation "org.apache.logging.log4j:log4j-core:2.19.0"
    implementation 'org.apache.httpcomponents:httpclient:4.5.10'
    implementation 'org.apache.httpcomponents:httpcore-nio:4.4.12'
    implementation 'org.apache.httpcomponents:httpasyncclient:4.1.4'
    implementation 'joda-time:joda-time:2.10.4'
    implementation ('org.apache.lucene:lucene-core:8.11.2') {
        exclude group: '*', module: '*'
    }
    implementation ('org.apache.lucene:lucene-analyzers-common:8.11.2') {
        exclude group: '*', module: '*'
    }
    implementation ('org.apache.lucene:lucene-backward-codecs:8.11.2') {
        exclude group: '*', module: '*'
    }
    implementation ('org.apache.lucene:lucene-grouping:8.11.2') {
        exclude group: '*', module: '*'
    }
    implementation ('org.apache.lucene:lucene-highlighter:8.11.2') {
        exclude group: '*', module: '*'
    }
    implementation ('org.apache.lucene:lucene-join:8.11.2') {
        exclude group: '*', module: '*'
    }
    implementation ('org.apache.lucene:lucene-memory:8.11.2') {
        exclude group: '*', module: '*'
    }
    implementation ('org.apache.lucene:lucene-misc:8.11.2') {
        exclude group: '*', module: '*'
    }
    implementation ('org.apache.lucene:lucene-queries:8.11.2') {
        exclude group: '*', module: '*'
    }
    implementation ('org.apache.lucene:lucene-queryparser:8.11.2') {
        exclude group: '*', module: '*'
    }
    implementation ('org.apache.lucene:lucene-sandbox:8.11.2') {
        exclude group: '*', module: '*'
    }
    implementation ('org.apache.lucene:lucene-spatial3d:8.11.2') {
        exclude group: '*', module: '*'
    }
    implementation ('org.apache.lucene:lucene-suggest:8.11.2') {
        exclude group: '*', module: '*'
    }

  ......
}

对于 Maven 构建系统,在项目的 pom.xml 文件中包含以下依赖项:

<dependencies>
    <dependency>
        <groupId>com.infinilabs</groupId>
        <artifactId>easysearch-client</artifactId>
        <version>1.0.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.10</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpcore-nio</artifactId>
        <version>4.4.12</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpasyncclient</artifactId>
        <version>4.1.4</version>
    </dependency>
    <dependency>
        <groupId>joda-time</groupId>
        <artifactId>joda-time</artifactId>
        <version>2.10.4</version>
    </dependency>
    <dependency>
        <groupId>org.apache.lucene</groupId>
        <artifactId>lucene-core</artifactId>
        <version>8.11.2</version>
        <scope>compile</scope>
        <exclusions>
            <exclusion>
                <artifactId>*</artifactId>
                <groupId>*</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.apache.lucene</groupId>
        <artifactId>lucene-analyzers-common</artifactId>
        <version>8.11.2</version>
        <scope>compile</scope>
        <exclusions>
            <exclusion>
                <artifactId>*</artifactId>
                <groupId>*</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.apache.lucene</groupId>
        <artifactId>lucene-backward-codecs</artifactId>
        <version>8.11.2</version>
        <scope>compile</scope>
        <exclusions>
            <exclusion>
                <artifactId>*</artifactId>
                <groupId>*</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.apache.lucene</groupId>
        <artifactId>lucene-grouping</artifactId>
        <version>8.11.2</version>
        <scope>compile</scope>
        <exclusions>
            <exclusion>
                <artifactId>*</artifactId>
                <groupId>*</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.apache.lucene</groupId>
        <artifactId>lucene-highlighter</artifactId>
        <version>8.11.2</version>
        <scope>compile</scope>
        <exclusions>
            <exclusion>
                <artifactId>*</artifactId>
                <groupId>*</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.apache.lucene</groupId>
        <artifactId>lucene-join</artifactId>
        <version>8.11.2</version>
        <scope>compile</scope>
        <exclusions>
            <exclusion>
                <artifactId>*</artifactId>
                <groupId>*</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.apache.lucene</groupId>
        <artifactId>lucene-memory</artifactId>
        <version>8.11.2</version>
        <scope>compile</scope>
        <exclusions>
            <exclusion>
                <artifactId>*</artifactId>
                <groupId>*</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.apache.lucene</groupId>
        <artifactId>lucene-misc</artifactId>
        <version>8.11.2</version>
        <scope>compile</scope>
        <exclusions>
            <exclusion>
                <artifactId>*</artifactId>
                <groupId>*</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.apache.lucene</groupId>
        <artifactId>lucene-queries</artifactId>
        <version>8.11.2</version>
        <scope>compile</scope>
        <exclusions>
            <exclusion>
                <artifactId>*</artifactId>
                <groupId>*</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.apache.lucene</groupId>
        <artifactId>lucene-queryparser</artifactId>
        <version>8.11.2</version>
        <scope>compile</scope>
        <exclusions>
            <exclusion>
                <artifactId>*</artifactId>
                <groupId>*</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.apache.lucene</groupId>
        <artifactId>lucene-sandbox</artifactId>
        <version>8.11.2</version>
        <scope>compile</scope>
        <exclusions>
            <exclusion>
                <artifactId>*</artifactId>
                <groupId>*</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.apache.lucene</groupId>
        <artifactId>lucene-spatial3d</artifactId>
        <version>8.11.2</version>
        <scope>compile</scope>
        <exclusions>
            <exclusion>
                <artifactId>*</artifactId>
                <groupId>*</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.apache.lucene</groupId>
        <artifactId>lucene-suggest</artifactId>
        <version>8.11.2</version>
        <scope>compile</scope>
        <exclusions>
            <exclusion>
                <artifactId>*</artifactId>
                <groupId>*</groupId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

接下来,在你的 Java 应用程序中创建一个 client 实例,并使用它在 Easysearch 中创建索引并插入一些数据。但在此之前,为了使其工作,你需要将签署服务器证书的根机构证书添加到你的应用程序信任库中。让我们看看如何配置 Java 应用程序的信任库。

为了使用 java client,你需要将根 CA 证书 ca.crt 添加到应用程序信任库中。这告诉你的 Java 应用程序信任由此根机构签署的任何证书。easysearch-1.6.1/config/ 目录下已经生成了 ca.crt 文件。你可以将其添加到自定义信任库中,并在 Java 应用程序中使用该自定义信任库。

使用 Java keytool 创建一个自定义信任库并导入证书。keytool 不理解 .pem 格式,所以你需要首先使用 openssl 加密库将证书转换为 .der 格式,然后使用 Java keytool 将其添加到自定义信任库中。假设您的操作系统已经预装了 openssl。

第 1 步:将 CA 证书从 .pem 格式转换为 .der 格式。

openssl x509 -in easysearch-1.6.1/config/ca.crt -inform pem -out ca.der --outform der

第 2 步:创建自定义信任库并添加 ca.der 证书。
将 ca 证书添加到应用程序信任库中,表示应用程序信任由此 CA 签署的任何证书。

keytool -import -file ca.der -alias easysearch -keystore myTrustStore

过程中会提示您输入密钥库口令: 我为了测试用输入的 123456。

通过列出信任库中的证书来确认操作成功,这里的 123456 是我上面设置的密码,会显示出 easysearch 证书。

keytool -keystore myTrustStore -storepass 123456 -list

第 3 步:在 Java 应用程序代码中设置指向自定义信任库的系统属性,并连接集群,创建索引,插入数据。
可以通过设置系统属性,以指定 SSL/TLS 通信时使用的信任库:

 System.setProperty("javax.net.ssl.trustStore", "/full/path/to/myTrustStore");
System.setProperty("javax.net.ssl.trustStorePassword", "123456");

HttpHost[] httpHostArray = new HttpHost[1];
// infini.cloud 和 CN=infini.cloud 保持一致
httpHostArray[0] = new HttpHost("infini.cloud", 9200, "https");
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("admin", "1933791fb2b9f6c6146d"));

RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(httpHostArray)
    .setHttpClientConfigCallback((HttpAsyncClientBuilder httpAsyncClientBuilder) -> {
      httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
      return httpAsyncClientBuilder;
    }));

CreateIndexRequest createIndexRequest = new CreateIndexRequest("test-index");
createIndexRequest.settings(Settings.builder()
    .put("index.number_of_shards", 1)
    .put("index.number_of_replicas", 1)
);

//Create index
client.indices().create(createIndexRequest, RequestOptions.DEFAULT);

// Bulk
BulkRequest bulkRequest = new BulkRequest();
for (int i = 0; i < 10; i++) {
  IndexRequest indexRequest = new IndexRequest("test-index")
      .id(Integer.toString(i))
      .source("{\"field1\":\"value" + i + "\"}", XContentType.JSON);
  bulkRequest.add(indexRequest);
}

BulkResponse bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT);
System.out.println(Strings.toString(bulkResponse));

信任已签署 Easysearch 正在使用的证书的 CA 的示例,当 CA 证书以 PEM 编码文件的形式可用时:

Path caCertificatePath = Paths.get("/easysearch-test/easysearch-1.6.1/config/ca.crt");
CertificateFactory factory = CertificateFactory.getInstance("X.509");
Certificate trustedCa;
try (InputStream is = Files.newInputStream(caCertificatePath)) {
  trustedCa = factory.generateCertificate(is);
}
KeyStore trustStore = KeyStore.getInstance("pkcs12");
trustStore.load(null, null);
trustStore.setCertificateEntry("ca", trustedCa);
SSLContextBuilder sslContextBuilder = SSLContexts.custom()
    .loadTrustMaterial(trustStore, null);
final SSLContext sslContext = sslContextBuilder.build();


HttpHost[] httpHostArray = new HttpHost[1];
httpHostArray[0] = new HttpHost("infini.cloud", 9200, "https");
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("admin", "1933791fb2b9f6c6146d"));

RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(httpHostArray)
    .setHttpClientConfigCallback((HttpAsyncClientBuilder httpAsyncClientBuilder) -> {
      httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
      httpAsyncClientBuilder.setSSLContext(sslContext);
      return httpAsyncClientBuilder;
    }));

现在,您已经成功设置了 Java 客户端,并以安全的 HTTPS 通道连接到了 Easysearch 集群。除此之外,Java 客户端还具备强大的权限控制管理 API,具体请参考我们的官网文档:https://www.infinilabs.com/docs/latest/easysearch/references/client/security/

关于 Easysearch

about easysearch

INFINI Easysearch 是一个分布式的近实时搜索与分析引擎,核心引擎基于开源的 Apache Lucene。Easysearch 的目标是提供一个轻量级的 Elasticsearch 可替代版本,并继续完善和支持更多的企业级功能。 与 Elasticsearch 相比,Easysearch 更关注在搜索业务场景的优化和继续保持其产品的简洁与易用性。

官网文档:https://www.infinilabs.com/docs/latest/easysearch

下载地址:https://www.infinilabs.com/download

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1256413.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

C语言--利用选择法对数组中的10个整数按由小到大排序

一.选择法排序介绍&#x1f357; 所谓选择法就是先将10个数中最小的数字与arr[0]交换&#xff0c;再将arr[1]-arr[9]中最小的数字与arr[1]进行交换....每一次比较&#xff0c;找出一个未经排序的数中最小的一个。总共比较9轮。 下面以5个数字为例说明选择法的步骤。 二.完整代码…

【高级网络程序设计】Week3-2 Servlet

一、 What are servlets? 1. 定义 &#xff08;1&#xff09;Servlets are Java’s answer to CGI&#xff1a; programs that run on a web server acting as middle layer between HTTP request and databases or other applications.Used for client requests that cann…

Rust的异步编程与Futures

欢迎关注我的公众号lincyang新自媒体&#xff0c;回复关键字【程序员经典书单】&#xff0c;领取程序员的100本经典书单 大家好&#xff01;我是lincyang。 今天&#xff0c;我们来探讨Rust中的异步编程和Futures。Rust的异步编程是一个强大的特性&#xff0c;它允许开发者编写…

FloodFill

"绝境之中才窥见&#xff0c;Winner&#xff0c;Winner" FloodFill算法简介: floodfill又翻译成漫水填充。我们可以将下面的矩阵理解为一片具有一定高度的坡地&#xff0c;此时突发洪水&#xff0c;洪水会将高度<0的地方填满。 话句话来说&#xff0c;Fl…

NX二次开发UF_CURVE_ask_trim 函数介绍

文章作者&#xff1a;里海 来源网站&#xff1a;https://blog.csdn.net/WangPaiFeiXingYuan UF_CURVE_ask_trim Defined in: uf_curve.h int UF_CURVE_ask_trim(tag_t trim_feature, UF_CURVE_trim_p_t trim_info ) overview 概述 Retrieve the current parameters of an a…

Java + openCV更换证件照背景色

最近在小红书上看到很多更换证件照背景色的需求&#xff0c;联想到以前自己也更换过证件照背景色而且还是付费的&#xff0c;碰巧最近在看一本书《JavaOpenCV高效入门》&#xff0c;于是查找资料&#xff0c;找到了通过技术解决这个需求的办法。 先看效果图&#xff08;图片来自…

OpenCV入门11——图像的分割与修复

文章目录 图像分割的基本概念实战-分水岭法(一)实战-分水岭法(二)GrabCut基本原理实战-GrabCut主体程序的实现实战-GrabCut鼠标事件的处理实战-调用GrabCut实现图像分割meanshift图像分割视频前后景分离其它对视频前后影分离的方法图像修复 图像分割是计算机视觉中的一个重要领…

window环境搭建StarRocksFE节点

StarRocks部署–源码编译 前言 ​ 注意:本文借用了一些其他文章的一些截图&#xff0c;同时自己做了具体的编译步骤&#xff0c;添加了一些新的内容 ​ 目标&#xff1a; 编译StarRocks2.5.13版本FE节点代码&#xff0c;在本地window环境运行&#xff0c;可以访问到8030界面…

频剪辑软件Corel VideoStudio 会声会影2024最新7大新全新功能解析

我很喜欢视频剪辑软件Corel VideoStudio 会声会影2024&#xff0c;因为它使用起来很有趣。它很容易使用&#xff0c;但仍然给你很多功能和力量。视频剪辑软件Corel VideoStudio 会声会影2023让我与世界分享我的想法&#xff01;“这个产品的功能非常多&#xff0c;我几乎没有触…

【数据中台】开源项目(2)-Wormhole流式处理平台

Wormhole 是一个一站式流式处理云平台解决方案&#xff08;SPaaS - Stream Processing as a Service&#xff09;。 Wormhole 面向大数据流式处理项目的开发管理运维人员&#xff0c;致力于提供统一抽象的概念体系&#xff0c;直观可视化的操作界面&#xff0c;简单流畅的配置管…

《已解决: ImportError: Keras requires TensorFlow 2.2 or higher 问题》

&#x1f337;&#x1f341; 博主猫头虎&#xff08;&#x1f405;&#x1f43e;&#xff09;带您 Go to New World✨&#x1f341; &#x1f984; 博客首页: &#x1f405;&#x1f43e;猫头虎的博客&#x1f390;《面试题大全专栏》 &#x1f995; 文章图文并茂&#x1f996…

vscode 使用git提交前端代码

1、项目初始化git 如果是从其他地方拉的代码&#xff0c;把.git文件删除&#xff0c;再重新初始化。 2、提交代码 2.1、提交本地库 2.2、提交远程仓库 2.2.1、创建远程仓库 2.2.2、提交远程仓库–master分支 在本地添加远程仓库&#xff0c;设置别名为origin git remote…

首届教师案例教学竞赛一等奖作品上线至和鲸社区,快来学习!

细心的朋友可能已经发现&#xff0c;近期和鲸社区的频道页上线了一个新专区——“优秀参赛作品专区”。 图.和鲸社区频道页 迄今为止&#xff0c;和鲸参与/支持了 500 多场专业数据科学竞赛&#xff0c;包括面向气象、金融、医学、海洋等不同领域的&#xff0c;面向从业者、科学…

为你的项目加上微信登录(个人开发)

当我们开发个人项目的时候&#xff0c;为了用户登录的便捷性&#xff0c;经常会给我们的项目加上一些除了注册之外的方式&#xff0c;其中最常见的就是微信登录&#xff0c;但作为个人开发者&#xff0c;是无法使用微信的授权登录的&#xff0c;但是通过微信公众号可以获得同样…

【nginx】 实现限流

这里写自定义目录标题 前言正文nginx实现限流并发限制限制单IP并发数量限制单主机服务并发数量 速率限制限流效果 注意疑问参考链接 小结 前言 好久不见&#xff0c;还算为时不晚。最近一个月经历了工作的调整&#xff0c;技术栈从Java转向了Go和Python, 工作显得更忙了些&…

JavaScript基础—for语句、循环嵌套、数组、冒泡排序、综合案例—根据数据生成柱形图

版本说明 当前版本号[20231126]。 版本修改说明20231126初版 目录 文章目录 版本说明目录JavaScript 基础第三天笔记for 语句for语句的基本使用循环嵌套倒三角九九乘法表 数组数组是什么&#xff1f;数组的基本使用定义数组和数组单元访问数组和数组索引数据单元值类型数组长…

测试工程师必学看系列之Jmeter_性能测试:性能测试的流程和术语

性能测试的流程 一、准备工作 1、系统基础功能验证 一般情况下&#xff0c;只有在系统基础功能测试验证完成、系统趋于稳定的情况下&#xff0c;才会进行性能测试&#xff0c;否则性能测试是无意义的。2、测试团队组建 根据该项目的具体情况&#xff0c;组建一个几人的性能测试…

Linux面试题(三)

目录 34、du 和 df 的定义&#xff0c;以及区别&#xff1f; 35、awk 详解。 36、当你需要给命令绑定一个宏或者按键的时候&#xff0c;应该怎么做呢&#xff1f; 37、如果一个 linux 新手想要知道当前系统支持的所有命令的列表&#xff0c;他需要怎么做&#xff1f; 38、…

23年几个能打的UE4游戏技术选型

近期发现很多的精力放在游戏的整体技术选型以及产生的结果上面&#xff0c;所以回顾下几个游戏的选型和结果&#xff1b; 这里一个是自己玩游戏的画面流畅度的直接感受&#xff0c;以及一直非常喜爱的评测“数毛社”&#xff0c;digital foundry&#xff1b; 23年目前来看&…

【NeRF】3、MobileR2L | 移动端实时的神经光场(CVPR2023)

论文&#xff1a;Real-Time Neural Light Field on Mobile Devices 代码&#xff1a;https://github.com/snap-research/MobileR2L 出处&#xff1a;CVPR2023 贡献&#xff1a; 设计了一套移动端实时的 R2L 网络结构 MobileR2L&#xff0c;在 iphone13 上渲染一张 1008x756…