标签 elasticsearch 下的文章

maven shade解决storm Elasticsearch log4j jar包版本冲突

新建一个maven简单工程即可,该工程的目的是将es、log4j等jar包里的class文件重新打包,全部放入到一个jar包里,再放入的过程中将所有“org.apache.logging.log4j”开头的报名改为“my.elasticsearch.log4j”,也就相当于将import log4j的地方统统改了,这样就相当于第三方编写的log4j,pom.xml如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>my.elasticsearch.test</groupId>
	<artifactId>es-shaded</artifactId>
	<version>1.0-SNAPSHOT</version>
	<properties>
		<elasticsearch.version>5.2.2</elasticsearch.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.elasticsearch</groupId>
			<artifactId>elasticsearch</artifactId>
			<version>${elasticsearch.version}</version>
		</dependency>
		<dependency>
			<groupId>org.elasticsearch.client</groupId>
			<artifactId>transport</artifactId>
			<version>${elasticsearch.version}</version>
		</dependency>
		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-core</artifactId>
			<version>2.7</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>log4j-over-slf4j</artifactId>
			<version>1.7.7</version>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-shade-plugin</artifactId>
				<version>2.4.1</version>
				<executions>
					<execution>
						<phase>package</phase>
						<goals>
							<goal>shade</goal>
						</goals>
						<configuration>
							<relocations>
								<relocation>
									<pattern>org.apache.logging.log4j</pattern>
									<shadedPattern>my.elasticsearch.log4j</shadedPattern>
								</relocation>
							</relocations>
							<transformers>
								<transformer
									implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer" />
							</transformers>
						</configuration>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
</project>

运行mvn clean install
将打包好的jar包安装到本地,验证效果如下图:

jar包版本冲突解决

jar包版本冲突解决


jar包版本冲突

jar包版本冲突


最后在自己的工程里,引入新的jar包即可,不需要额外引入es和log4j的jar包了。

elasticsearch在window下的安装和java查询

下载elasticsearch的zip包,elasticsearch的版本是2.2.1
ps:elasticsearch的api随版本更新的速度快,这里边需要查看对应版本的api文档
解压后安装,elasticsearch的访问地址:
http://localhost:9200/

{
  "name" : "Venus",
  "cluster_name" : "elasticsearch",
  "version" : {
    "number" : "2.2.1",
    "build_hash" : "d045fc29d1932bce18b2e65ab8b297fbf6cd41a1",
    "build_timestamp" : "2016-03-09T09:38:54Z",
    "build_snapshot" : false,
    "lucene_version" : "5.4.1"
  },
  "tagline" : "You Know, for Search"
}

安装插件:
elasticsearch插件elasticsearch-head安装:
elasticsearch-head是一个elasticsearch的集群管理工具,它是完全由html5编写的独立网页程序,你可以通过插件把它集成到es。
在cmd进入elasticsearch安装的bin目录,执行下边的命令:

plugin install mobz/elasticsearch-head

网上查的命令是:

plugin -install mobz/elasticsearch-head

注意比较不同
安装完命令可以打开url:http://localhost:9200/_plugin/head/ 查看效果

elasticsearch-head

elasticsearch-head


bigdesk这个插件就不要安装了,github上的代码都是几年前的了
pojo类:

package com.rong360.elasticsearch;
public class User {
	private long id;
	private String name;
	private int age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
}

elasticsearch建立索引:

package com.rong360.elasticsearch;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHits;
public class ElasticSearchClient {
	private static Client client;
	public void init() {
		try {
			client = TransportClient.builder().build()
					.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
	}
	public void close() {
		client.close();
	}
	public void createIndex() {
		for (int i = 0; i < 1000; i++) {
			User user = new User();
			user.setId(new Long(i));
			user.setName("huang fox " + i);
			user.setAge(i % 100);
			client.prepareIndex("users", "user").setSource(generateJson(user)).execute().actionGet();
		}
	}
	private 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;
	}
	public static void search() {
		SearchResponse response = client.prepareSearch("users").setTypes("user")
				.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
				.setQuery(QueryBuilders.termQuery("name", "fox")) // Query
				.setPostFilter(QueryBuilders.rangeQuery("age").from(22).to(26))// Filter
				.setFrom(0).setSize(60).setExplain(true).execute().actionGet();
		SearchHits hits = response.getHits();
		System.out.println(hits.getTotalHits());
		for (int i = 0; i < hits.getHits().length; i++) {
			System.out.println(hits.getHits()[i].getSourceAsString());
		}
	}
	public static void main(String[] args) {
		long a = System.currentTimeMillis();
		ElasticSearchClient elasticSearchClient = new ElasticSearchClient();
		elasticSearchClient.init();
		System.out.println("监测的时间1:"+(System.currentTimeMillis()-a)/1000f+" 秒 ");
		elasticSearchClient.createIndex();	//创建索引
		System.out.println("监测的时间2:"+(System.currentTimeMillis()-a)/1000f+" 秒 ");
		search();	//查询
		System.out.println("监测的时间3:"+(System.currentTimeMillis()-a)/1000f+" 秒 ");
		elasticSearchClient.close();
	}
}
elasticsearch_demo

elasticsearch_demo