Maven搭建Elasticsearch开发环境

1.首先创建Maven工程时引入的setting.xml配置中远程仓库设置

注:还有一个本地仓库的配置(

如果你在本机有仓库的话我的本地仓库:<localRepository>D:/Apache items/Repository</localRepository>)

    <mirror>
      <id>mirrorId</id>
      <mirrorOf>repositoryId</mirrorOf>
      <name>Human Readable Name for this Mirror.</name>
      <url>http://repo1.maven.org/maven2/</url>//仓库也可以选其他的如阿里的
    </mirror>

2.然后就是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>com.xxxx</groupId>                      
<artifactId>Elasticsearch</artifactId> <!--一般为工程名称-->
<version>1.0</version>              
<packaging>jar</packaging>             <!--工程打成jar包-->  


<name>Elasticsearch</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>5.6.3</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>5.6.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.netty/netty-transport -->
<!-- 引入之后PreBuiltTransportClient会引入 -->
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-transport</artifactId>
<version>4.1.13.Final</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.9.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.github.richardwilly98.elasticsearch/elasticsearch-river-mongodb -->
<!-- 引入连接 mongo数据库的相关jar -->
<dependency>
<groupId>com.github.richardwilly98.elasticsearch</groupId>
<artifactId>elasticsearch-river-mongodb</artifactId>
<version>2.0.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>


</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<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>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
</transformers>
</configuration>
</execution>
</executions>
</plugin>


</plugins>
</build>
<repositories>
<!-- lucene下载地址 -->
<repository>
<id>elastic-lucene-snapshots</id>
<name>Elastic Lucene Snapshots</name>
<url>http://s3.amazonaws.com/download.elasticsearch.org/lucenesnapshots/00142c9</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>sonatype-releases</id>
<name>Sonatype Releases Repository</name>
<url>https://oss.sonatype.org/content/repositories/releases</url>
</repository>
<repository>
<id>sonatype-releases</id>
<name>Sonatype Releases Repository</name>
<url>https://mvnrepository.com/artifact/com.github.richardwilly98.elasticsearch/elasticsearch-river-mongodb</url>
</repository>
</repositories>
<issueManagement>
<system>GitHub</system>
<url>
https://github.com/richardwilly98/elasticsearch-river-mongodb/issues
</url>
</issueManagement>
</project>


3.日志log4j配置文件(官网)

And also provide a Log4j 2 configuration file in your classpath. For example, you can add in your src/main/resources project dir a log4j2.properties file like:

appender.console.type = Console
appender.console.name = console
appender.console.layout.type = PatternLayout

rootLogger.level = info
rootLogger.appenderRef.console.ref = console

4.简单测试程序

Settings settings = Settings.builder()
       .put("client.transport.sniff", true).build();
TransportClient client = new PreBuiltTransportClient(settings)
       .addTransportAddress(new TransportAddress(InetAddress.getByName("localhost"), 9300));
// 创建一条数据的索引
Map<String, Object> jsonmap = new  HashMap<String,Object>();
jsonmap.put("user", "zk");
jsonmap.put("postDate", new Date());
jsonmap.put("message", "frist Elasticsearch demo!");
IndexResponse response = client.prepareIndex("zkindex", "index1", "1").setSource(jsonmap).get();

// Index name
String _index = response.getIndex();
System.out.println("索引名="+_index);
// Type name
String _type = response.getType();
System.out.println("类型名称="+_type);
// Document ID (generated or not)
String _id = response.getId();
System.out.println("id="+_id);
// Version (if it's the first time you index this document, you will get: 1)
long _version = response.getVersion();
System.err.println(_version);
// status has stored current instance statement.
RestStatus status = response.status();
System.out.println(status);
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值