您的位置:首页 > 编程语言 > Java开发

elasticsearch 过期数据自动删除Java代码

2018-03-14 18:22 597 查看
es中的索引名为index-yyy-MM-dd 的形式的时候,可以根据直接日期判断来直接删除过期的整个索引

请尊重知识产权,博客原文地址http://blog.csdn.net/qq1032355091/article/details/79558496
package cn.bmkp.esCleaner;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
import org.elasticsearch.action.admin.indices.stats.IndexStats;
import org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse;
import org.elasticsearch.client.IndicesAdminClient;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

import lombok.extern.log4j.Log4j;

@Log4j
public class App {
public static void main(String[] args) {
String expiryDate = getDate(45);
String indexPreffix = "order_process";
TransportClient client;
try {

client = getClient();
List<String> index = searchIndexNameByPreffix(indexPreffix, client, expiryDate);
delete(client, index);
} catch (UnknownHostException e1) {
e1.printStackTrace();
}

}

/**
* 删除整个索引
*
* @param client
* @param index
* @user jiangqiang
* @date 2018年3月14日下午5:01:02
*/
public static void delete(TransportClient client, List<String> index) {
IndicesAdminClient indicesAdminClient = client.admin().indices();
for (String s : index) {

DeleteIndexResponse response = indicesAdminClient.prepareDelete(s).execute().actionGet();
log.info("删除 索引 " + s);
log.info(response.isAcknowledged());
}
}

/**
* 根据索引前缀名搜索出要过期的索引名
*
* @param preffix
* @param client
* @param expiryDate 过期日期
* @return
* @user jiangqiang
* @date 2018年3月14日下午5:01:28
*/
public static List<String> searchIndexNameByPreffix(String preffix, TransportClient client, String expiryDate) {
List<String> deleteIndex = new ArrayList<>();

IndicesAdminClient indicesAdminClient = client.admin().indices();
IndicesStatsResponse response = indicesAdminClient.prepareStats(preffix + "-*").all().get();
Map<String, IndexStats> indices = response.getIndices();

Set<String> keySet = indices.keySet();
4000

for (String key : keySet) {
String d = key.substring(key.indexOf("-") + 1);
int c = d.compareTo(expiryDate);
if (c < 0) {
log.info(d);
deleteIndex.add(preffix + "-" + d);
}
}

return deleteIndex;

}

/**
* 获取客户端连接
*
* @return
* @throws UnknownHostException
* @user jiangqiang
* @date 2018年3月14日下午5:02:09
*/
public static TransportClient getClient() throws UnknownHostException {
// 设置集群名称
Settings settings = Settings.builder().put("cluster.name", "test-es").build();
TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("111.111.111.111"), 9300));
return client;
}

/**
* 获取多少天以前的日期
*
* @param num
* @return
* @user jiangqiang
* @date 2018年3月14日下午4:30:55
*/
public static String getDate(int num) {
Calendar c = Calendar.getInstance();
c.set(Calendar.DATE, c.get(Calendar.DATE) - num);
Date day = c.getTime();
String str = new SimpleDateFormat("yyyy-MM-dd").format(day);
log.info(str);
return str;
}

}

<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>cn.bmkp</groupId>
<artifactId>esCleaner</artifactId>
<version>0.1</version>
<packaging>jar</packaging>

<name>esCleaner</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>5.6.4</version>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.18</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.2</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<testSourceDirectory>src/test/java</testSourceDirectory>

<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer">
<mainClass>cn.bmkp.esCleaner.App</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>java</executable>
<includeProjectDependencies>true</includeProjectDependencies>
<includePluginDependencies>false</includePluginDependencies>
<classpathScope>compile</classpathScope>
<mainClass>cn.bmkp.esCleaner.App</mainClass>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

ss
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  elasticsearch