ElasticSearch5.4.3 环境搭建 2017 (5-Java Client Security Api x-pack)

完成基本的配置以后,下面使用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的



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值