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);
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章