Skip to content

Es personal #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .idea/libraries/Maven__com_alibaba_fastjson_1_2_62.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

124 changes: 124 additions & 0 deletions .idea/uiDesigner.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@
java 连接 elasticsearch5.x 集群及Java API代码

## package elasticsearch6.x
java 连接 elasticsearch6.x 集群及Java API代码
java 连接 elasticsearch6.x 集群及Java API代码

## package elasticsearchAction
elasticsearch 二次开发,包含增删改查底层操作
4 changes: 4 additions & 0 deletions elasticsearch5.x/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@

<groupId>com.elastic</groupId>
<artifactId>elasticsearch5.x</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>../elasticsearchAction</module>
</modules>


<dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ public class SpringDataClientBuild {
private String CLUSTER_HOSTNAME_PORT;

/**
* 在Spring中,bean可以被定义为两种模式:prototype(多例)和singleton(单例)
* 在Spring中,bean可以被定义为两种模式:prototype(原型)和singleton(单例)
* singleton(单例):只有一个共享的实例存在,所有对这个bean的请求都会返回这个唯一的实例。Spring bean 默认是单例模式.
* prototype(多例):对这个bean的每次请求都会创建一个新的bean实例,类似于new。
* prototype(原型):对这个bean的每次请求都会创建一个新的bean实例,类似于new。
*/
@Bean
@Scope("prototype")
Expand Down Expand Up @@ -78,4 +78,4 @@ public Client getClient() {
return client;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,4 @@ public void run() {
}

}


}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import org.apache.http.message.BasicHeader;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;

import java.util.List;
import java.util.stream.Collectors;
Expand All @@ -23,15 +22,16 @@ public class ClientBuilders {

/**
* 构建一个简单的RestClientBuilder方便测试
*
* @return
*/
public RestClientBuilder getSimpleClientBuilder(){
String [] ipHosts = CLUSTER_HOSTNAME_PORT.split(",");
public RestClientBuilder getSimpleClientBuilder() {
String[] ipHosts = CLUSTER_HOSTNAME_PORT.split(",");
List<HttpHost> httpHostsList = Stream.of(ipHosts)
.map(this::createHttpHost)
.collect(Collectors.toList());

HttpHost [] httpHosts = httpHostsList.toArray(new HttpHost[httpHostsList.size()]);
HttpHost[] httpHosts = httpHostsList.toArray(new HttpHost[httpHostsList.size()]);

RestClientBuilder builder = RestClient.builder(httpHosts);
return builder;
Expand All @@ -43,12 +43,13 @@ private HttpHost createHttpHost(String ip) {

/**
* 初始化 clientBuilder的详细说明
*
* @return
*/
public static RestClientBuilder getClientBulider() {


String [] hostNamesPort = CLUSTER_HOSTNAME_PORT.split(",");
String[] hostNamesPort = CLUSTER_HOSTNAME_PORT.split(",");

String host;
int port;
Expand Down
126 changes: 62 additions & 64 deletions elasticsearch6.x/src/main/java/com/elastic/utils/ConfigUtils.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.elastic.utils;


import com.elastic.exception.ESIoException;
import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
Expand All @@ -13,65 +10,66 @@
**/
public class ConfigUtils {

/**
* 配置文件
* */
private static String esConfigFileName = "elasticsearch.properties";

/**
* es集群名
* */
private static String esClusterName;
/**
* es集群ip地址
* */
private static String esClusterDiscoverHostName;
/**
* es集群是否加入嗅探功能
* */
private static String clientTransportSniff;

private static Properties properties = new Properties();
static{
try {
ClassLoader classLoader = ConfigUtils.class.getClassLoader();
InputStream resourceAsStream = classLoader.getResourceAsStream(esConfigFileName);
properties.load(resourceAsStream);
init();
}catch (IOException e){
e.printStackTrace();
}

}

private static void init() {

esClusterName = properties.getProperty("elastic.cluster.name");
esClusterDiscoverHostName = properties.getProperty("elastic.cluster.discover.hostname");
clientTransportSniff = properties.getProperty("elastic.cluster.clientTransportSniff");

if ("".equals(esClusterName)||"".equals(esClusterName)||"".equals(clientTransportSniff)){
throw new RuntimeException("elasticsearch 集群参数为空异常");
}
}

public static String getEsClusterName() {
return esClusterName;
}

public static String getEsClusterDiscoverHostName() {
return esClusterDiscoverHostName;
}

public static void setEsClusterDiscoverHostName(String esClusterDiscoverHostName) {
ConfigUtils.esClusterDiscoverHostName = esClusterDiscoverHostName;
}

public static String getClientTransportSniff() {
return clientTransportSniff;
}

public static void setClientTransportSniff(String clientTransportSniff) {
ConfigUtils.clientTransportSniff = clientTransportSniff;
}
/**
* 配置文件
*/
private static String esConfigFileName = "elasticsearch.properties";

/**
* es集群名
*/
private static String esClusterName;
/**
* es集群ip地址
*/
private static String esClusterDiscoverHostName;
/**
* es集群是否加入嗅探功能
*/
private static String clientTransportSniff;

private static Properties properties = new Properties();

static {
try {
ClassLoader classLoader = ConfigUtils.class.getClassLoader();
InputStream resourceAsStream = classLoader.getResourceAsStream(esConfigFileName);
properties.load(resourceAsStream);
init();
} catch (IOException e) {
e.printStackTrace();
}

}

private static void init() {

esClusterName = properties.getProperty("elastic.cluster.name");
esClusterDiscoverHostName = properties.getProperty("elastic.cluster.discover.hostname");
clientTransportSniff = properties.getProperty("elastic.cluster.clientTransportSniff");

if ("".equals(esClusterName) || "".equals(esClusterName) || "".equals(clientTransportSniff)) {
throw new RuntimeException("elasticsearch 集群参数为空异常");
}
}

public static String getEsClusterName() {
return esClusterName;
}

public static String getEsClusterDiscoverHostName() {
return esClusterDiscoverHostName;
}

public static void setEsClusterDiscoverHostName(String esClusterDiscoverHostName) {
ConfigUtils.esClusterDiscoverHostName = esClusterDiscoverHostName;
}

public static String getClientTransportSniff() {
return clientTransportSniff;
}

public static void setClientTransportSniff(String clientTransportSniff) {
ConfigUtils.clientTransportSniff = clientTransportSniff;
}
}
Loading