1. 程式人生 > >elasticsearch 過期資料自動刪除Java程式碼

elasticsearch 過期資料自動刪除Java程式碼

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();

		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