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

Elasticsearch Java API之清空索引

2015-08-05 22:09 597 查看
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

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