<!--ELK  -->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>5.1.1</version>
<exclusions>
<!--<exclusion>
<artifactId>transport-netty4-client</artifactId>
<groupId>org.elasticsearch.plugin</groupId>
</exclusion>-->
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.6.2</version>
</dependency> <!-- springmvc json -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.8</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.8</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.8.5</version>
</dependency>


package com.sxis.util;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException; import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.action.admin.cluster.state.ClusterStateResponse;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.sort.SortOrder;
import org.elasticsearch.transport.client.PreBuiltTransportClient; /**
* 【描述】: ELK java API 接口 ,包括查询,删除,插入等待
* 【步骤】:
* @param
* @return
* @throws
* @author Allen
* @date 2017/7/4 9:47
*/
public class ElasticsearchUtil {
private static TransportClient client;
private static String elasticIp = "192.168.211.50";
private static int elasticPort = 9300; /**
* 【描述】: 初始化ElasticSearch对象
* 【步骤】:
* @param
* @return
* @throws
* @author Allen
* @date 2017/7/4 15:19
*/
public static void init() throws UnknownHostException, InterruptedException, ExecutionException {
//设置ES实例的名称.put("client.transport.sniff", true) //自动嗅探整个集群的状态,把集群中其他ES节点的ip添加到本地的客户端列表中
Settings esSettings = Settings.builder().put("cluster.name", "elasticsearch").build();
client = new PreBuiltTransportClient(esSettings);//初始化client较老版本发生了变化,此方法有几个重载方法,初始化插件等。
//此步骤添加IP,至少一个,其实一个就够了,因为添加了自动嗅探配置
client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(elasticIp), elasticPort));
System.out.println("连接建立成功");
} /**
* 【描述】: 创建index,把其中的文档转化为json的格式存储
* 【步骤】:
* @param   <Node>:节点ip <port>:节点端口号,默认9200 <Index>:索引名 <Type>:索引类型 <ID>:操作对象的ID号
* @return
* @throws
* @author Allen
* @date 2017/7/5 9:42
*/
public static void createIndex() throws ElasticsearchException,IOException {
for (int i=300; i<=50000000;i++){
User user = new User();
user.setId(1);
user.setName("huang fox " + i);
user.setAge(i % 100);
IndexResponse indexResponse = null;
indexResponse = client.prepareIndex("users", "user",i+"").setSource(generateJson(user)).execute().actionGet();
System.out.println("responseIsCreated: "+indexResponse);
}
System.out.println("it is ok !");
} public static void query() throws Exception {
//term查询
//QueryBuilder queryBuilder = QueryBuilders.termQuery("age", 50) ; //年龄等于50
//range查询
QueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("age").gt(50); //年龄大于50
SearchResponse searchResponse = client.prepareSearch("users")
.setTypes("user")
.setQuery(rangeQueryBuilder) //query
.setPostFilter(QueryBuilders.rangeQuery( "age" ).from( 70 ).to( 80 )) // Filter
.addSort("age", SortOrder.DESC)
.setSize(120) // 不设置的话,默认取10条数据
.execute().actionGet();
SearchHits hits = searchResponse.getHits();
System.out.println("查到记录数:"+hits.getTotalHits());
SearchHit[] searchHists = hits.getHits();
if(searchHists.length>0){
for(SearchHit hit:searchHists){
String name = (String) hit.getSource().get("name");
Integer age = Integer.parseInt( hit.getSource().get("age").toString() );
System.out.format("name:%s ,age :%d \n",name ,age);
}
}
} /**
* 转换成json对象
*
* @param user
* @return
*/
private static String generateJson(User user) {
String json = "";
try {
XContentBuilder contentBuilder = XContentFactory.jsonBuilder().startObject();
contentBuilder.field("id", user.getId());
contentBuilder.field("name", user.getName());
contentBuilder.field("age", user.getAge());
json = contentBuilder.endObject().string();
} catch (IOException e) {
e.printStackTrace();
}
return json;
} /*
* Get index 获取文档相当于读取数据库的一行数据
*/
public static void getIndex(){
GetResponse getresponse = client.prepareGet("users", "user", "402").execute().actionGet();
System.out.println(getresponse.getSourceAsString());
} /*
*Delete index 相当于删除一行数据
*/
public static void delete(){
DeleteResponse deleteresponse = client.prepareDelete("users", "user","1").execute().actionGet();
System.out.println(deleteresponse.getVersion());
} /*
*Delete index 删除索引及该索引所管辖的记录
*/
public static void deleteIndex(){
//删除所有记录
DeleteIndexResponse deleteIndexResponse = client.admin().indices().prepareDelete("logs").execute().actionGet();
System.out.println(deleteIndexResponse.isAcknowledged());
} /**
* 【描述】: 获取到所有的索引
* 【步骤】:
* @param
* @return
* @throws
* @author Allen
* @date 2017/7/4 16:27
*/
public static void getAllIndex(){
ClusterStateResponse response = client.admin().cluster().prepareState().execute().actionGet();
//获取所有索引
String[] indexs=response.getState().getMetaData().getConcreteAllIndices();
for (String index : indexs) {
System.out.println( index + " delete" );//
}
} public void close(){
//on shutdown 断开集群
client.close();
} public static void main( String[] args ) {
try {
init();
//createIndex();
//getIndex();
//delete();
//deleteIndex();
//getAllIndex();
query();
} catch( Exception e ) {
e.printStackTrace();
}
} } class User{ private static final long serialVersionUID = 5290251719938640641L; private Integer id;
private String name;
private int age; public Integer getId() {
return id;
} public void setId( Integer id ) {
this.id = id;
} public String getName() {
return name;
} public void setName( String name ) {
this.name = name;
} public int getAge() {
return age;
} public void setAge( int age ) {
this.age = age;
}
}
 

最新文章

  1. 我理解的MVC
  2. 【转载】CentOS服务器配置VPN详解
  3. mac下获取应用签名
  4. 关于iOS10的允许访问用户数据产生的问题
  5. WebService核心文件【server-config.wsdd】详解及调用示例
  6. Perl5中19个最重要的文件系统工具
  7. 手把手教你如何搭建iOS项目基本框架
  8. 微软职位内部推荐-Senior Software Engineer -Web
  9. phpMyAdmin 中数据库替换问题
  10. 网站用户身份识别俩大招之cookie
  11. MySQL replace into (insert into 的增强版)
  12. Mysql 8.0 导入txt文件操作(课程实验)
  13. 【原创】uC/OS II 任务切换原理
  14. MySql cmd下的学习笔记 —— 有关常用函数的介绍(数学函数,聚合函数等等)
  15. Oracle分析函数-nulls first/nulls last
  16. AWT是Java最早出现的图形界面,但很快就被Swing所取代。
  17. 胸片和CT断层图像是怎么来的?
  18. SourceTree这是一个无效的源路径
  19. NSIS安装vcredist_64.exe
  20. [IIS] IIS网站对文件读写无权限的解决方案(Access等)

热门文章

  1. 让你分分钟了解Web接口测试
  2. hdu1251(统计难题)
  3. 20180531-Postman 常用测试结果验证及使用技巧
  4. ios一些问题
  5. centos迷你版,没有安装ifconfig命令
  6. c# 获取某个进程的CPU使用百分百(类似任务管理器中显示CPU)
  7. 002-ubuntu安装
  8. php 5.0 新字符串
  9. jQuery 是javascript的一个库(常用插件、处理器)
  10. python之路----包