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 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 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 searchIndexNameByPreffix(String preffix, TransportClient client, String expiryDate) {
		List deleteIndex = new ArrayList<>();

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

		Set 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;
	}

}


	4.0.0

	cn.bmkp
	esCleaner
	0.1
	jar

	esCleaner
	http://maven.apache.org

	
		UTF-8
	

	
		
			org.elasticsearch.client
			transport
			5.6.4
		

		
			org.projectlombok
			lombok
			1.16.18
			provided
		
		
			org.slf4j
			slf4j-log4j12
			1.7.2
		
	
	
		src/main/java
		src/test/java

		
			
				org.apache.maven.plugins
				maven-shade-plugin
				2.4.1
				
					
						package
						
							shade
						
						
							
								
									cn.bmkp.esCleaner.App
								
							
						
					
				
			

			
				org.codehaus.mojo
				exec-maven-plugin
				1.2.1
				
					
						
							exec
						
					
				
				
					java
					true
					false
					compile
					cn.bmkp.esCleaner.App
				
			

			
				org.apache.maven.plugins
				maven-compiler-plugin
				2.5.1
				
					1.8
					1.8
				
			
		
	


ss

你可能感兴趣的:(elasticsearch)