@Override public void clearStorage() throws BackendException { try { try { client.admin().indices() .delete(new DeleteIndexRequest(indexName)).actionGet(); // We wait for one second to let ES delete the river Thread.sleep(1000); } catch (IndexMissingException e) { // Index does not exist... Fine } } catch (Exception e) { throw new PermanentBackendException("Could not delete index " + indexName, e); } finally { close(); } }
private void updateLogIndexName() { this.logIndexName = this.logIndexPrefix + "_" + SIMPLE_DATE_FORMAT.format(new Date()); try { elasticSearchClient.admin() .indices() .prepareGetIndex() .addIndices(logIndexName) .execute() .actionGet(); } catch (IndexNotFoundException infe) { try { elasticSearchClient.admin() .indices() .prepareCreate(logIndexName) .execute() .actionGet(); } catch (ResourceAlreadyExistsException ilee) { // no-op } catch (Exception e) { logger.error("Failed to update log index name: {}", logIndexName, e); } } }
/** * If ES already contains this instance's target index, then do nothing. * Otherwise, create the index, then wait {@link #CREATE_SLEEP}. * <p> * The {@code client} field must point to a live, connected client. * The {@code indexName} field must be non-null and point to the name * of the index to check for existence or create. * * @param config the config for this ElasticSearchIndex * @throws java.lang.IllegalArgumentException if the index could not be created */ private void checkForOrCreateIndex(Configuration config) { Preconditions.checkState(null != client); //Create index if it does not already exist IndicesExistsResponse response = client.admin().indices().exists(new IndicesExistsRequest(indexName)).actionGet(); if (!response.isExists()) { ImmutableSettings.Builder settings = ImmutableSettings.settingsBuilder(); ElasticSearchSetup.applySettingsFromTitanConf(settings, config, ES_CREATE_EXTRAS_NS); CreateIndexResponse create = client.admin().indices().prepareCreate(indexName) .setSettings(settings.build()).execute().actionGet(); try { final long sleep = config.get(CREATE_SLEEP); log.debug("Sleeping {} ms after {} index creation returned from actionGet()", sleep, indexName); Thread.sleep(sleep); } catch (InterruptedException e) { throw new TitanException("Interrupted while waiting for index to settle in", e); } if (!create.isAcknowledged()) throw new IllegalArgumentException("Could not create index: " + indexName); } }
@Override public void recreateIndex(String indexName) throws IOException { DeleteIndexRequestBuilder dirBuilder = transportClient.admin().indices().prepareDelete(indexName); try { DeleteIndexResponse diResponse = dirBuilder.execute().actionGet(); } catch (IndexNotFoundException ie) { System.out.println("Index not found... that's ok"); } CreateIndexRequestBuilder cirBuilder = transportClient.admin().indices().prepareCreate(indexName); CreateIndexResponse ciResponse = cirBuilder.execute().actionGet(); Assert.assertTrue(ciResponse.isAcknowledged(), "Create index succeeeded"); }
private void addIndex(String indexName) { try { elasticSearchClient.admin() .indices() .prepareGetIndex() .addIndices(indexName) .execute() .actionGet(); } catch (IndexNotFoundException infe) { try { elasticSearchClient.admin() .indices() .prepareCreate(indexName) .execute() .actionGet(); } catch (ResourceAlreadyExistsException done) { // no-op } } }
client.admin().indices() .exists(Requests.indicesExistsRequest(indexKey)).actionGet() .isExists(); if (exists && newdb) { client.admin().indices().prepareDelete(indexKey).execute().actionGet(); client.admin().indices().create( new CreateIndexRequest(indexKey) .settings( .put("index.number_of_replicas", numberOfReplicas) .put("index.mapping._id.indexed", true) )).actionGet(); client.admin().cluster().health(new ClusterHealthRequest().waitForGreenStatus()).actionGet();
private ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetaData>> getMappings(TransportClient client, GetMappingsRequest request) { try { return retry() .maxAttempts(maxAttempts) .exponentialBackoff(maxRetryTime) .run("getMappings", () -> client.admin() .indices() .getMappings(request) .actionGet(requestTimeout.toMillis()) .getMappings()); } catch (Exception e) { throw new RuntimeException(e); } }
private String[] getIndexesFromEs(final String aliasName){ final AdminClient adminClient = this.provider.getClient().admin(); //remove write alias, can only have one ImmutableOpenMap<String, List<AliasMetaData>> aliasMap = adminClient.indices().getAliases( new GetAliasesRequest( aliasName ) ).actionGet().getAliases(); return aliasMap.keys().toArray( String.class ); }
/** * Initializes the index with required templates and mappings. */ private void initIndex() throws Exception { // 0. Add the tasklog template GetIndexTemplatesResponse result = elasticSearchClient.admin() .indices() .prepareGetTemplates("tasklog_template") .execute() .actionGet(); if (result.getIndexTemplates().isEmpty()) { logger.info("Creating the index template 'tasklog_template'"); InputStream stream = ElasticSearchDAOV5.class .getResourceAsStream("/template_tasklog.json"); byte[] templateSource = IOUtils.toByteArray(stream); try { elasticSearchClient.admin() .indices() .preparePutTemplate("tasklog_template") .setSource(templateSource, XContentType.JSON) .execute() .actionGet(); } catch (Exception e) { logger.error("Failed to init tasklog_template", e); } } }
@Override public void commit() { if (this.client != null) { this.flushIndex(true); this.client.admin().indices() .refresh(new RefreshRequest(this.indexName)).actionGet(); // enable index auto refresh ImmutableSettings.Builder indexSettings = ImmutableSettings .settingsBuilder(); indexSettings.put("refresh_interval", 1); this.client.admin().indices().prepareUpdateSettings(this.indexName) .setSettings(indexSettings).execute().actionGet(); this.client.admin().indices().prepareOptimize(this.indexName) .setMaxNumSegments(5).execute().actionGet(); } }
private String[] getIndices(TransportClient client, GetIndexRequest request) { try { return retry() .maxAttempts(maxAttempts) .exponentialBackoff(maxRetryTime) .run("getIndices", () -> client.admin() .indices() .getIndex(request) .actionGet(requestTimeout.toMillis()) .getIndices()); } catch (Exception e) { throw new RuntimeException(e); } }
final WhoAmIResponse whoAmIRes = tc.execute(WhoAmIAction.INSTANCE, new WhoAmIRequest()).actionGet(); sb.append(Strings.toString(whoAmIRes,true, true)); } catch (Exception e1) { LicenseInfoResponse res = tc.execute(LicenseInfoAction.INSTANCE, new LicenseInfoRequest()).actionGet(); sb.append(Strings.toString(res,true, true)); } catch (Exception e1) { ClusterHealthResponse nir = tc.admin().cluster().health(new ClusterHealthRequest()).actionGet(); sb.append(Strings.toString(nir,true, true)); } catch (Exception e1) { NodesInfoResponse nir = tc.admin().cluster().nodesInfo(new NodesInfoRequest()).actionGet(); sb.append(Strings.toString(nir,true, true)); } catch (Exception e1) { NodesStatsResponse nir = tc.admin().cluster().nodesStats(new NodesStatsRequest()).actionGet(); sb.append(Strings.toString(nir,true, true)); } catch (Exception e1) { IndicesStatsResponse nir = tc.admin().indices().stats(new IndicesStatsRequest()).actionGet(); sb.append(Strings.toString(nir, true, true)); } catch (Exception e1) {
public void setMapping(String indexName, File json) { try { this.elasticsearchClient.admin().indices().preparePutMapping(indexName) .setSource(new String(Files.readAllBytes(json.toPath()), StandardCharsets.UTF_8)) .setUpdateAllTypes(true) .setType("_default_") .execute() .actionGet(); } catch (Throwable e) { DAO.severe(e); }; }
IndicesExistsResponse response = client.admin().indices() .exists(new IndicesExistsRequest(indexName)).actionGet(); client.admin().indices().prepareDelete(this.indexName).execute() .actionGet(); forceCreate = true; CreateIndexResponse create = client.admin().indices() .prepareCreate(indexName).execute().actionGet(); try { Thread.sleep(200); .settingsBuilder(); indexSettings.put("refresh_interval", -1); this.client.admin().indices().prepareUpdateSettings(this.indexName) .setSettings(indexSettings).execute().actionGet();
client.admin().indices() .exists(Requests.indicesExistsRequest(indexKey)).actionGet() .isExists(); if (exists && newIndex) { client.admin().indices().prepareDelete(indexKey).get(); client.admin().indices().create( new CreateIndexRequest(indexKey) .settings( .put("index.number_of_shards", numberOfShards) .put("index.number_of_replicas", numberOfReplicas) )).actionGet(); client.admin().cluster().health(new ClusterHealthRequest().waitForGreenStatus()).actionGet();