Elasticsearch Java API之清空索引

package com.nerve.core.test;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;

import org.elasticsearch.action.admin.cluster.state.ClusterStateResponse;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.transport.InetSocketTransportAddress;



public class ClusterManager {

	private static Client javaClient = null;

	private static Properties properties = null;
	
	public static Client getJavaClient() {
		if (javaClient == null) {
			try {
				String urlstr = getIp();
				URL url = new URL(urlstr);
				javaClient = new TransportClient()
						.addTransportAddress(new InetSocketTransportAddress(url
								.getHost(), 9300));

			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return javaClient;
	}


	public static Properties getProperties() throws IOException {
		if (properties == null) {
			properties = new Properties();
			InputStream inputStream = ClusterManager.class
					.getResourceAsStream("/sysconfig.properties");
			System.out.println(inputStream);
			properties.load(inputStream);

		}
		return properties;
	}

	
	public static String getIp() {
		String es_cluster_master_ip = "http://10.0.0.113:9200";
		try {
			es_cluster_master_ip = getProperties().getProperty(
					"es_cluster_master_ip");
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println(es_cluster_master_ip);
		return es_cluster_master_ip;
	}

	
	public static void main(String[] args) throws Exception {
		Client client=getJavaClient();

		ClusterStateResponse response = client.admin().cluster()  
				.prepareState()  
				.execute().actionGet();
		//获取所有索引
		String[] indexs=response.getState().getMetaData().getConcreteAllIndices();
		for (String index : indexs) {
			System.out.println(index+" delete");//
			//清空所有索引。
			
			DeleteIndexResponse deleteIndexResponse = client.admin().indices()  
					.prepareDelete(index)  
					.execute().actionGet();  
			System.out.println(deleteIndexResponse.getHeaders());
			
		}

		//client.admin().cluster().
		//http://10.0.0.101:9200/_status
		//http://10.0.0.101:9200/_status
		//indices

		
		
	}
}

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值