完成基本的配置以后,下面使用java client api进行简单的开发测试。
官方教程:噗嗤
1. Maven 依赖
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.tom.es</groupId> <artifactId>ElasticSearch0100</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>ElasticSearch0100</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <spring.version>4.1.0.RELEASE</spring.version> <slf4j.version>1.5.10</slf4j.version> <slf4j-log4j12.version>1.6.1</slf4j-log4j12.version> <java.version>1.8</java.version> <junit.version>4.12</junit.version> <org.aspectj-version>1.8.1</org.aspectj-version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <!-- ES dependency for query builder --> <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>5.4.2</version> </dependency> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>transport</artifactId> <version>5.4.3</version> </dependency> <!-- slf4j-log --> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-to-slf4j</artifactId> <version>2.8.2</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.24</version> <scope>test</scope> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.24</version> </dependency> <!-- add the x-pack jar as a dependency --> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>x-pack-transport</artifactId> <version>5.4.3</version> </dependency> </dependencies> </project>
官方那个repository好像不能用,所以我只加了依赖,在未启用x-pack的elasticsearch上,使用上面那个transport依赖即可,但是启用x-pack安全认证之后,需要使用x-pack-transport的依赖。
注意要添加下面的repository , 不然pom报错,找不到x-pack-transport
<!-- add the elasticsearch repo --> <repository> <id>elasticsearch-releases</id> <url>https://artifacts.elastic.co/maven</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository>
2. 示例代码
执行输入如下:package com.tom; import java.io.IOException; import java.net.InetSocketAddress; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.InetSocketTransportAddress; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.xpack.client.PreBuiltXPackTransportClient; import org.junit.After; import org.junit.Before; import org.junit.Test; public class TestXPackTransportClient { TransportClient client = null; @SuppressWarnings("resource") @Before public void before() { client = new PreBuiltXPackTransportClient(Settings.builder().put("cluster.name", "test_es")//集群名称 .put("node.name", "node-115")//加一个节点 .put("client.transport.sniff", true)//自动探测 .put("xpack.security.user", "elastic:密码")//xpack的用户 .build()) .addTransportAddress(new InetSocketTransportAddress(new InetSocketAddress("ip或者域名", 9300)));//地址1,可以加多个 } @After public void after() { if (client != null) { client.close(); } } @Test public void testIndex() throws IOException { XContentBuilder contentBuilder = XContentFactory.jsonBuilder().startObject(); contentBuilder.field("id", "2017_06_01"); contentBuilder.field("name", "KingKong"); contentBuilder.field("director", "tom"); contentBuilder.field("year", "2017"); String json = contentBuilder.endObject().string(); IndexResponse resp = client.prepareIndex("movies", "film").setSource(json, XContentType.JSON).get(); System.out.println(resp.toString()); } }
命令行查一下:OK的
ElasticSearch5.4.3 环境搭建 2017 (5-Java Client Security Api x-pack)
最新推荐文章于 2021-01-25 11:40:04 发布