private static void createMappingIfNeeded(JestClient jestClient, String indexName, String typeName) throws ElasticWriterException, IOException { synchronized (CREATE_MAPPING_LOCK) { IndicesExists indicesExists = new IndicesExists.Builder(indexName).build(); boolean indexExists = jestClient.execute(indicesExists).isSucceeded(); if (!indexExists) { CreateIndex createIndex = new CreateIndex.Builder(indexName).build(); JestResult result = jestClient.execute(createIndex); if (!result.isSucceeded()) { throw new ElasticWriterException(String.format("Failed to create index: %s", result.getErrorMessage())); } else { log.info("Created index {}", indexName); } URL url = ElasticWriter.class.getResource("/elastic-mapping.json"); String mapping = Resources.toString(url, Charsets.UTF_8); PutMapping putMapping = new PutMapping.Builder(indexName, typeName,mapping).build(); result = jestClient.execute(putMapping); if (!result.isSucceeded()) { throw new ElasticWriterException(String.format("Failed to create mapping: %s", result.getErrorMessage())); } else { log.info("Created mapping for index {}", indexName); } } } }
Action<JestResult> action = new IndicesExists.Builder(getIndexName()).build(); synchronized(EsStorage.class) { JestResult result = esClient.execute(action);
Action<JestResult> action = new IndicesExists.Builder(getIndexName()).build(); synchronized(EsStorage.class) { JestResult result = esClient.execute(action);
public boolean init(String elasticHost, String elasticPort, String elasticIndex, String logType, Path processResultMappingFile) { this.elasticHost = elasticHost; this.elasticPort = elasticPort; this.elasticIndex = elasticIndex; this.logType = logType; try { final JestClient client = getElasticSearchClient(); boolean indexExists = client.execute(new IndicesExists.Builder(elasticIndex).build()).isSucceeded(); if (!indexExists){ log.info("Creating elasticsearch index."); client.execute(new CreateIndex.Builder(elasticIndex).build()); } if (StringUtils.isNotBlank(logType) && (processResultMappingFile != null)) { log.info("Updating type mapping."); final String mappingJson = new String(ByteStreams.toByteArray(Files.newInputStream(processResultMappingFile))); client.execute(new PutMapping.Builder(elasticIndex, logType, mappingJson).build()); } log.info("Established elasticsearch connection to host '{}:{}', index '{}'.", elasticHost, elasticPort, elasticIndex); elasticClient = client; } catch (IOException e) { log.error("Error creating base index on ElasticSearch: {}", e.getMessage(), e); elasticClient = null; } return true; }
private static void createMappingIfNeeded(JestClient jestClient, String indexName, String typeName) throws ElasticWriterException, IOException { synchronized (CREATE_MAPPING_LOCK) { IndicesExists indicesExists = new IndicesExists.Builder(indexName).build(); boolean indexExists = jestClient.execute(indicesExists).isSucceeded(); if (!indexExists) { CreateIndex createIndex = new CreateIndex.Builder(indexName).build(); JestResult result = jestClient.execute(createIndex); if (!result.isSucceeded()) { throw new ElasticWriterException(String.format("Failed to create index: %s", result.getErrorMessage())); } else { log.info("Created index {}", indexName); } URL url = ElasticWriter.class.getResource("/elastic-mapping.json"); String mapping = Resources.toString(url, Charsets.UTF_8); PutMapping putMapping = new PutMapping.Builder(indexName, typeName,mapping).build(); result = jestClient.execute(putMapping); if (!result.isSucceeded()) { throw new ElasticWriterException(String.format("Failed to create mapping: %s", result.getErrorMessage())); } else { log.info("Created mapping for index {}", indexName); } } } }
/** * Called to initialize the storage. * @param client the jest client * @param indexName the name of the ES index to initialize * @param defaultIndexName the default ES index - used to determine which -settings.json file to use */ protected void initializeClient(JestClient client, String indexName, String defaultIndexName) { try { client.execute(new Health.Builder().build()); Action<JestResult> action = new IndicesExists.Builder(indexName).build(); // There was occasions where a race occurred here when multiple threads try to // create the index simultaneously. This caused a non-fatal, but annoying, exception. synchronized(AbstractClientFactory.class) { JestResult result = client.execute(action); if (!result.isSucceeded()) { createIndex(client, indexName, defaultIndexName + "-settings.json"); //$NON-NLS-1$ } } } catch (Exception e) { throw new RuntimeException(e); } }
public void validateIndex(String indexName) { try { IndicesExists indicesExistsRequest = new IndicesExists.Builder(indexName).build(); logger.debug("created indexExistsRequests: {}", indicesExistsRequest); JestResult existsResult = client.execute(indicesExistsRequest); logger.debug("indexExistsRequests result: {}", existsResult); if (!existsResult.isSucceeded()) { Settings settings = Settings.builder() // .put("index.analysis.analyzer.default.type", "keyword") .put("index.store.type", "mmapfs") .build(); CreateIndex createIndexRequest = new CreateIndex.Builder(indexName).settings(settings).build(); execute(createIndexRequest); //TODO: Make this work. Using the above "keyword" configuration in the meantime. PutMapping putMapping = new PutMapping.Builder(indexName, "_default_", STRING_NOT_ANALYZED).build(); execute(putMapping); logger.info("created index with settings: {}, indexName: {}, putMapping: {}", settings, indexName, putMapping); } } catch (IOException e) { logger.error("failed to connect to elastic cluster", e); } }
/** * Called to initialize the storage. * @param client the jest client * @param indexName the name of the ES index to initialize * @param defaultIndexName the default ES index - used to determine which -settings.json file to use */ protected void initializeClient(JestClient client, String indexName, String defaultIndexName) { try { client.execute(new Health.Builder().build()); Action<JestResult> action = new IndicesExists.Builder(indexName).build(); // There was occasions where a race occurred here when multiple threads try to // create the index simultaneously. This caused a non-fatal, but annoying, exception. synchronized(AbstractClientFactory.class) { JestResult result = client.execute(action); if (!result.isSucceeded()) { createIndex(client, indexName, defaultIndexName + "-settings.json"); //$NON-NLS-1$ } } } catch (Exception e) { throw new RuntimeException(e); } }
public boolean indicesExists(String indexName) throws Exception { boolean isIndicesExists = false; JestResult rst = jestClient.execute(new IndicesExists.Builder(indexName).build()); if (rst.isSucceeded()) { isIndicesExists = true; } else { switch (rst.getResponseCode()) { case 404: isIndicesExists = false; break; case 401: // 无权访问 default: log.warn(rst.getErrorMessage()); break; } } return isIndicesExists; }
private void initJestClient() throws IOException { JestClientFactory factory = new JestClientFactory(); // Custom feature converter is required Gson gson = new GsonBuilder() // .registerTypeAdapter(Feature.class, new FeatureConverter()) // .registerTypeAdapter(Property.class, new PropertyConverter()) // .create(); factory.setHttpClientConfig(new HttpClientConfig.Builder(mapUrl()) // .multiThreaded(true) // .gson(gson) // .build()); jestClient = factory.getObject(); boolean indexExists = jestClient.execute(new IndicesExists.Builder(indexName).build()).isSucceeded(); if (indexExists) { jestClient.execute(new DeleteIndex.Builder(indexName).build()); } jestClient.execute(new CreateIndex.Builder(indexName).build()); }
public Builder(Collection<? extends String> indices){ addIndex(indices); }
public Builder(String index){ addIndex(index); }
public Builder(Collection<? extends String> indices){ addIndices(indices); }
public Builder(String index){ addIndex(index); }
@Override public boolean indexExists(String indexName) { return executeWithAcknowledge(new IndicesExists.Builder(indexName).build()); }
@Override public final boolean isIndexExists(String indexName) { Action action = new IndicesExists.Builder(indexName).build(); JestResult result = jestClientHelper.execute(action); return result.isSucceeded(); }
private void deleteIndexIfExisting() { // Not actually needed, but do it to avoid cluttering the ES log if ( clientReference.get().executeRequest( new IndicesExists.Builder( actualIndexName ).build(), 404 ).getResponseCode() == 404 ) { return; } try { clientReference.get().executeRequest( new DeleteIndex.Builder( actualIndexName ).build() ); } catch (SearchException e) { // ignoring deletion of non-existing index if ( !e.getMessage().contains( "index_not_found_exception" ) ) { throw e; } } }
private void createIndexIfNotYetExisting() { if ( clientReference.get().executeRequest( new IndicesExists.Builder( actualIndexName ).build(), 404 ).getResponseCode() == 200 ) { return; } clientReference.get().executeRequest( new CreateIndex.Builder( actualIndexName ).build() ); }
@Override public final boolean isIndexExists(String indexName) { Action action = new IndicesExists.Builder(indexName).build(); JestResult result = jestClientHelper.execute(action); return result.isSucceeded(); }
public Builder(String index){ addIndex(index); }