在研究ELK日志收集系统时,顺便对于ElasticSearch的javaapi了解了下。
于是将自己写的测试代码上传做个备注。
Fileinfo.java
构造数据类,模拟的是文件的具体信息。
package com.elastic;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
public class FileInfo
{
// 文件名称
private String filename;
// 文件路径
private String filepath;
// 文件大小
private Long filesize;
// 作者
private String fileauthor;
// 最后更新日期
private Date filedate;
public FileInfo()
{
super();
}
public FileInfo(String filename, String filepath, Long filesize, String fileauthor)
{
super();
this.filename = filename;
this.filepath = filepath;
this.filesize = filesize;
this.fileauthor = fileauthor;
}
/**
* @return filename
*/
public String getFilename()
{
return filename;
}
/**
* @param filename 要设置的 filename
*/
public void setFilename(String filename)
{
this.filename = filename;
}
/**
* @return filepath
*/
public String getFilepath()
{
return filepath;
}
/**
* @param filepath 要设置的 filepath
*/
public void setFilepath(String filepath)
{
this.filepath = filepath;
}
/**
* @return filesize
*/
public Long getFilesize()
{
return filesize;
}
/**
* @param filesize 要设置的 filesize
*/
public void setFilesize(Long filesize)
{
this.filesize = filesize;
}
/**
* @return fileauthor
*/
public String getFileauthor()
{
return fileauthor;
}
/**
* @param fileauthor 要设置的 fileauthor
*/
public void setFileauthor(String fileauthor)
{
this.fileauthor = fileauthor;
}
/**
* @return filedate
*/
public Date getFiledate()
{
return filedate;
}
/**
* @param filedate 要设置的 filedate
*/
public void setFiledate(Date filedate)
{
this.filedate = filedate;
}
/**
*
* @return json对象(mapping)
*/
public String toJsonStr()
{
String jsonData = null;
try
{
XContentBuilder jsonBuild = XContentFactory.jsonBuilder()
.startObject()
.field("filename", filename)
.field("filepath", filepath)
.field("filesize", filesize)
.field("fileauthor", fileauthor)
.field("filedate", new Date(new File(filepath).lastModified()))
.endObject();
jsonData = jsonBuild.string();
}
catch (IOException e)
{
System.out.println("build Json String failed!");
}
return jsonData;
}
}
ESHandle.java
ES相关的方法接口
package com.elastic;
import java.util.List;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.deletebyquery.DeleteByQueryResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.node.Node;
import org.elasticsearch.node.NodeBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
public class ESHandler
{
public Client client;
// TransportClient方式
public ESHandler(String clusterName, String ip)
{
if (null == client)
{
client = new TransportClient(ImmutableSettings.settingsBuilder()
.put("client.transport.sniff", true)
.put("cluster.name", clusterName)
.build())
.addTransportAddress(new InetSocketTransportAddress(ip, 9300));
}
}
// Node方式,指定clusterName
public ESHandler(String clusterName)
{
if (null == client)
{
Node node = NodeBuilder.nodeBuilder().clusterName(clusterName).client(true).node();
client = node.client();
}
}
// 单条数据索引
public IndexResponse IndexSingleDoc(String indexName, String type, String id, String jsondata)
{
IndexRequestBuilder requestBuilder = null;
// 指定id,或者由ES随即生成ID
if (isStringEmptyOrNull(id))
{
requestBuilder = client.prepareIndex(indexName, type);
}
else
{
requestBuilder = client.prepareIndex(indexName, type, id);
}
return requestBuilder.setSource(jsondata).execute().actionGet();
}
// 单条数据获取
public GetResponse getSingleDoc(String indexName, String type, String id)
{
return client.prepareGet(indexName, type, id)
.execute()
.actionGet();
}
// 单条数据删除
public DeleteResponse deleteSingleDoc(String indexName, String type, String id)
{
return client.prepareDelete(indexName, type, id)
.setOperationThreaded(false)
.execute().actionGet();
}
// 通过Query删除数据
public DeleteByQueryResponse deleteByQuery(String indexName, QueryBuilder querybuilder)
{
return client.prepareDeleteByQuery(indexName)
.setQuery(querybuilder)
.execute()
.actionGet();
}
// 批量数据索引
public BulkResponse bulkIndex(String indexName, String type, List<String> jsondata)
{
BulkRequestBuilder bulkRequest = client.prepareBulk();
for(int i = 0; i < jsondata.size(); i++)
{
IndexRequestBuilder requestBuilder = client.prepareIndex(indexName, type).setRefresh(true);
bulkRequest.add(requestBuilder.setSource(jsondata.get(i)));
}
BulkResponse response = bulkRequest.execute().actionGet();
if (response.hasFailures())
{
System.out.println("Buld Index failed!Msg: " + response.buildFailureMessage());
}
return response;
}
// 处理搜索结果
public void dealWithResponse(SearchHits hits)
{
SearchHit[] searchHits = hits.getHits();
System.out.println(searchHits.length);
for(SearchHit hit:searchHits)
{
System.out.println((String)hit.getSource().get("type")
+ "|"
+ (String)hit.getSource().get("message"));
}
}
public void close()
{
client.close();
}
public static boolean isStringEmptyOrNull(String str)
{
if (null == str || "".equals(str))
{
return true;
}
return false;
}
}