如同磁铁吸引四周的铁粉,热情也能吸引周围的人,改变周围的情况。

es 5.3.3 NoNodeAvailableException[None of the configured nodes are available:

Elasticsearch | 作者 wuyh | 发布于2018年05月26日 | 阅读数:4088

求指教!!为什么报这个错!防火墙关了的#------------------------------------------------------ 
import org.apache.log4j.Logger; 
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse; 
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse; 
import org.elasticsearch.client.IndicesAdminClient; 
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.xpack.client.PreBuiltXPackTransportClient; 
import org.elasticsearch.xpack.security.authc.support.SecuredString; 
import java.io.InputStreamReader; 
import java.net.InetAddress; 
import java.util.Collections; 
import java.util.Properties; 
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; 
import static org.elasticsearch.xpack.security.authc.support.UsernamePasswordToken.basicAuthHeaderValue; 
 
public class ElasticClientTest { 
private static final Logger logger = Logger.getLogger(ElasticClientTest.class); 
public static ElasticClientTest INSTANCE = new ElasticClientTest(); 
private static String clusterNodes; 
private static String clusterName; 
 
private static PreBuiltXPackTransportClient client; 
private static String userInfo; 
 
private ElasticClientTest(){ Properties prop = new Properties(); try { prop.load(new InputStreamReader(this.getClass().getClassLoader().getResourceAsStream("elasticsearch.properties"), "UTF-8")); } catch (Exception var5) { logger.error(var5); } clusterNodes=prop.getProperty("elasticsearch.clusterNodes"); clusterName=prop.getProperty("elasticsearch.cluster.name"); userInfo=prop.getProperty("elasticsearch.user"); } public static PreBuiltXPackTransportClient getClient(){ Settings settings = Settings.builder().put("cluster.name", clusterName) .put("xpack.security.user", userInfo) .put("client.transport.sniff", false) .build(); // 创建client if(null == client) { synchronized (PreBuiltXPackTransportClient.class) { try { client = new PreBuiltXPackTransportClient(settings); if (!"".equals(clusterNodes)) { String[] nodes = clusterNodes.split(","); for (String nodeInfo : nodes) { String InetSocket[] = nodeInfo.split(":"); String Address = InetSocket[0]; Integer port = Integer.valueOf(InetSocket[1]); client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(Address), port)); } } } catch (Exception e) { System.out.println("客户端获取异常"); e.printStackTrace(); } } } return client; } /** * 获取索引管理的IndicesAdminClient */ public static IndicesAdminClient getAdminClient() { return getClient().admin().indices(); } /** * 判定索引是否存在 * @param indexName * @return */ public static boolean isExists(String indexName){ IndicesExistsResponse response=getAdminClient().prepareExists(indexName).get(); return response.isExists()?true:false; } /** * 创建索引 * @param indexName 索引名 * @param shards 分片数 * @param replicas 副本数 * @return */ public static boolean createIndex(String indexName, int shards, int replicas,String typeName, XContentBuilder mapping) { Settings settings = Settings.builder() .put("index.number_of_shards", shards) .put("index.number_of_replicas", replicas) .build(); CreateIndexResponse createIndexResponse = getAdminClient() .prepareCreate(indexName.toLowerCase()) .addMapping(typeName,mapping) .setSettings(settings) .execute().actionGet(); return createIndexResponse.isAcknowledged()?true:false; } public static void main(String[] args){ //1.判定索引是否存在 try { TransportClient client=ElasticClientTest.INSTANCE.getClient(); Long start=System.currentTimeMillis(); XContentBuilder builder = jsonBuilder() .startObject() .startObject("properties") .startObject("id") .field("type", "long") .endObject() .startObject("title") .field("type", "text") .field("analyzer", "ik_max_word") .field("search_analyzer", "ik_max_word") .field("boost", 2) .endObject() .startObject("content") .field("type", "text") .field("analyzer", "ik_smart") .field("search_analyzer", "ik_smart") .endObject() .startObject("pubName") .field("type", "text") .field("analyzer", "ik_max_word") .field("search_analyzer", "ik_max_word") .endObject() .startObject("number") .field("type", "text") .field("analyzer", "ik_max_word") .field("search_analyzer", "ik_max_word") .endObject() .startObject("postdate") .field("type", "date") .field("format", "yyyy-MM-dd") .endObject() .startObject("createDate") .field("type", "date") .field("format", "epoch_millis") .endObject() .startObject("type") .field("type", "keyword") .endObject() .startObject("indexnumber") .field("type", "keyword") .endObject() .endObject() .endObject(); System.out.println(ElasticClientTest.isExists("navgationinfos")); // ElasticClientTest.createIndex("navgationinfos",2,2,"fulltext",builder); System.out.println(System.currentTimeMillis()-start +" ms"); }catch (Exception e){ e.printStackTrace(); }finally { if(client!=null){ client.close(); client=null; } } } }
#-----------------------------------------------

elasticsearch.clusterNodes=127.0.0.1:9300 
elasticsearch.cluster.name=node-101 
elasticsearch.user=elastic:changeme
 
#---------------------------------------------

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance&quot; xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/ma ... gt%3B <modelVersion>4.0.0</modelVersion> <groupId>com.wuyh</groupId> <artifactId>elasticsearch</artifactId> <version>5.3.3</version> <repositories> <!-- add the elasticsearch repo --> <repository> <id>elasticsearch-releases</id> <url>https://artifacts.elastic.co/maven&lt;/url&gt; <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <dependencies> <!-- https://mvnrepository.com/artifact/log4j/log4j --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.1.41</version> </dependency> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>x-pack-transport</artifactId> <version>5.3.3</version> </dependency> <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>5.3.3</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.7</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.7</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.4</version> <scope>test</scope> </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>18.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>6</source> <target>6</target> </configuration> </plugin> </plugins> </build> </project>
 

 
已邀请:

要回复问题请先登录注册