org.elasticsearch.client.AdminClient类的使用及代码示例

x33g5p2x  于2022-01-15 转载在 其他  
字(15.7k)|赞(0)|评价(0)|浏览(67)

本文整理了Java中org.elasticsearch.client.AdminClient类的一些代码示例,展示了AdminClient类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。AdminClient类的具体详情如下:
包路径:org.elasticsearch.client.AdminClient
类名称:AdminClient

AdminClient介绍

[英]Administrative actions/operations against the cluster or the indices.
[中]针对群集或索引的管理操作/操作。

代码示例

代码示例来源:origin: loklak/loklak_server

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

代码示例来源:origin: floragunncom/search-guard

private static void issueWarnings(Client tc) {
    NodesInfoResponse nir = tc.admin().cluster().nodesInfo(new NodesInfoRequest()).actionGet();
    Version maxVersion = nir.getNodes().stream().max((n1,n2) -> n1.getVersion().compareTo(n2.getVersion())).get().getVersion();
    Version minVersion = nir.getNodes().stream().min((n1,n2) -> n1.getVersion().compareTo(n2.getVersion())).get().getVersion();
    
    if(!maxVersion.equals(minVersion)) {
      System.out.println("WARNING: Your cluster consists of different node versions. It is not recommended to run sgadmin against a mixed cluster. This may fail.");
      System.out.println("         Minimum node version is "+minVersion.toString());
      System.out.println("         Maximum node version is "+maxVersion.toString());
    } else {
      System.out.println("Elasticsearch Version: "+minVersion.toString());
    }
    
    if(nir.getNodes().size() > 0) {
      List<PluginInfo> pluginInfos = nir.getNodes().get(0).getPlugins().getPluginInfos();
      String sgVersion = pluginInfos.stream().filter(p->p.getClassname().equals("com.floragunn.searchguard.SearchGuardPlugin")).map(p->p.getVersion()).findFirst().orElse("<unknown>");
      System.out.println("Search Guard Version: "+sgVersion);
    }
  }
}

代码示例来源:origin: floragunncom/search-guard

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) {
  PendingClusterTasksResponse nir = tc.admin().cluster().pendingClusterTasks(new PendingClusterTasksRequest()).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) {

代码示例来源:origin: brianfrankcooper/YCSB

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

代码示例来源:origin: Netflix/conductor

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

代码示例来源:origin: thinkaurelius/titan

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

代码示例来源:origin: SonarSource/sonarqube

CreateIndexResponse indexResponse = SHARED_NODE.client().admin().indices()
 .prepareCreate(index.getName())
 .setSettings(settings)
 .get();
 throw new IllegalStateException("Failed to create index " + index.getName());
SHARED_NODE.client().admin().cluster().prepareHealth(index.getName()).setWaitForStatus(ClusterHealthStatus.YELLOW).get();
 PutMappingResponse mappingResponse = SHARED_NODE.client().admin().indices().preparePutMapping(index.getName())
  .setType(entry.getKey())
  .setSource(entry.getValue().getAttributes())
SHARED_NODE.client().admin().cluster().prepareHealth(index.getName()).setWaitForStatus(ClusterHealthStatus.YELLOW).get();
result.add(index);

代码示例来源:origin: loklak/loklak_server

public boolean wait_ready(long maxtimemillis, ClusterHealthStatus status) {
  // wait for yellow status
  long start = System.currentTimeMillis();
  boolean is_ready;
  do {
    // wait for yellow status
    ClusterHealthResponse health = elasticsearchClient.admin().cluster().prepareHealth().setWaitForStatus(status).execute().actionGet();
    is_ready = !health.isTimedOut();
    if (!is_ready && System.currentTimeMillis() - start > maxtimemillis) return false;
  } while (!is_ready);
  return is_ready;
}

代码示例来源:origin: SonarSource/sonarqube

private void deleteIndex(String indexName) {
 client.nativeClient().admin().indices().prepareDelete(indexName).get();
}

代码示例来源:origin: SonarSource/sonarqube

@Override
public void deleteIndexes(String name, String... otherNames) {
 GetMappingsResponse mappings = client.nativeClient().admin().indices().prepareGetMappings("_all").get();
 Set<String> existingIndices = Sets.newHashSet(mappings.mappings().keysIt());
 Stream.concat(Stream.of(name), Arrays.stream(otherNames))
  .distinct()
  .filter(existingIndices::contains)
  .forEach(this::deleteIndex);
}

代码示例来源:origin: richardwilly98/elasticsearch-river-mongodb

public static long getIndexCount(Client client, MongoDBRiverDefinition definition) {
  if (client.admin().indices().prepareExists(definition.getIndexName()).get().isExists()) {
    if (definition.isImportAllCollections()) {
      return client.prepareCount(definition.getIndexName()).execute().actionGet().getCount();
    } else {
      if (client.admin().indices().prepareTypesExists(definition.getIndexName()).setTypes(definition.getTypeName()).get()
          .isExists()) {
        return client.prepareCount(definition.getIndexName()).setTypes(definition.getTypeName()).get().getCount();
      }
    }
  }
  return 0;
}

代码示例来源:origin: thinkaurelius/titan

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

代码示例来源:origin: Netflix/conductor

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

代码示例来源:origin: apache/usergrid

public Observable<IndexRefreshCommandInfo> refreshAsync() {
  refreshIndexMeter.mark();
  final long start = System.currentTimeMillis();
  String[] indexes = getIndexes();
  if (indexes.length == 0) {
    if (logger.isTraceEnabled()) {
      logger.trace("Not refreshing indexes. none found");
    }
  }
  //Added For Graphite Metrics
  RefreshResponse response =
    esProvider.getClient().admin().indices().prepareRefresh(indexes).execute().actionGet();
  int failedShards = response.getFailedShards();
  int successfulShards = response.getSuccessfulShards();
  ShardOperationFailedException[] sfes = response.getShardFailures();
  if (sfes != null) {
    for (ShardOperationFailedException sfe : sfes) {
      logger.error("Failed to refresh index:{} reason:{}", sfe.index(), sfe.reason());
    }
  }
  if (logger.isTraceEnabled()) {
    logger.trace("Refreshed indexes: {},success:{} failed:{} ", StringUtils.join(indexes, ", "),
      successfulShards, failedShards);
  }
  IndexRefreshCommandInfo refreshResults = new IndexRefreshCommandInfo(failedShards == 0,
    System.currentTimeMillis() - start);
  return ObservableTimer.time(Observable.just(refreshResults), refreshTimer);
}

代码示例来源:origin: Netflix/conductor

private void deleteAllIndices() {
  ImmutableOpenMap<String, IndexMetaData> indices = elasticSearchClient.admin().cluster()
      .prepareState().get().getState()
      .getMetaData().getIndices();
  indices.forEach(cursor -> {
    try {
      elasticSearchClient.admin()
          .indices()
          .delete(new DeleteIndexRequest(cursor.value.getIndex().getName()))
          .get();
    } catch (InterruptedException | ExecutionException e) {
      throw new RuntimeException(e);
    }
  });
}

代码示例来源:origin: floragunncom/search-guard

@Test
public void testAdvancedMapping() throws Exception {
  setup(Settings.EMPTY, new DynamicSgConfig(), Settings.EMPTY, true, ClusterConfiguration.DEFAULT);
  try (TransportClient tc = getInternalTransportClient(this.clusterInfo, Settings.EMPTY)) {
    tc.admin().indices().create(new CreateIndexRequest("myindex1")
    .mapping("mytype1", FileHelper.loadFile("mapping1.json"), XContentType.JSON)).actionGet();
    tc.admin().indices().create(new CreateIndexRequest("myindex2")
    .mapping("mytype2", FileHelper.loadFile("mapping2.json"), XContentType.JSON)).actionGet();
    tc.admin().indices().create(new CreateIndexRequest("myindex3")
    .mapping("mytype3", FileHelper.loadFile("mapping3.json"), XContentType.JSON)).actionGet();
    tc.admin().indices().create(new CreateIndexRequest("myindex4")
    .mapping("mytype4", FileHelper.loadFile("mapping4.json"), XContentType.JSON)).actionGet();
  }
  RestHelper rh = nonSslRestHelper();
  System.out.println("############ write into mapping 1");
  String data1 = FileHelper.loadFile("data1.json");
  System.out.println(rh.executePutRequest("myindex1/mytype1/1?refresh", data1, encodeBasicHeader("nagilum", "nagilum")));
  System.out.println(rh.executePutRequest("myindex1/mytype1/1?refresh", data1, encodeBasicHeader("nagilum", "nagilum")));
  System.out.println("############ write into mapping 2");
  System.out.println(rh.executePutRequest("myindex2/mytype2/2?refresh", data1, encodeBasicHeader("nagilum", "nagilum")));
  System.out.println(rh.executePutRequest("myindex2/mytype2/2?refresh", data1, encodeBasicHeader("nagilum", "nagilum")));
  System.out.println("############ write into mapping 3");
  String parent = FileHelper.loadFile("data2.json");
  String child = FileHelper.loadFile("data3.json");
  System.out.println(rh.executePutRequest("myindex3/mytype3/1?refresh", parent, encodeBasicHeader("nagilum", "nagilum")));
  System.out.println(rh.executePutRequest("myindex3/mytype3/2?routing=1&refresh", child, encodeBasicHeader("nagilum", "nagilum")));
  System.out.println("############ write into mapping 4");
  System.out.println(rh.executePutRequest("myindex4/mytype4/1?refresh", parent, encodeBasicHeader("nagilum", "nagilum")));
  System.out.println(rh.executePutRequest("myindex4/mytype4/2?routing=1&refresh", child, encodeBasicHeader("nagilum", "nagilum")));
}

代码示例来源:origin: apache/usergrid

@Override
  public void destroy() {
    try {
      logger.info( "dropping application keyspace" );
      dataStaxCluster.getClusterSession()
        .execute("DROP KEYSPACE "+ CQLUtils.quote(cassandraConfig.getApplicationKeyspace()));
      dataStaxCluster.waitForSchemaAgreement();

      logger.info( "dropping application local keyspace" );
      dataStaxCluster.getClusterSession()
        .execute("DROP KEYSPACE "+ CQLUtils.quote(cassandraConfig.getApplicationLocalKeyspace()));
      dataStaxCluster.waitForSchemaAgreement();

      dataStaxCluster.getClusterSession().close(); // close session so it's meta will get refreshed
    }
    catch ( Exception e ) {
      logger.error("Error dropping application keyspaces: {} error: {}", cassandraConfig.getApplicationKeyspace(), e);
    }
    logger.info( "keyspaces dropped" );
    logger.info( "dropping indices" );
    final EsProvider provider =
      SpringResource.getInstance().getBean( Injector.class ).getInstance( EsProvider.class );

    provider.getClient().admin().indices().prepareDelete( "_all" ).execute().actionGet();
  }
}

代码示例来源:origin: apache/usergrid

Boolean isAck;
final AdminClient adminClient = esProvider.getClient().admin();
IndicesAliasesRequestBuilder aliasesRequestBuilder = adminClient.indices().prepareAliases();
for (String currentIndex : indexNames) {
  aliasesRequestBuilder.removeAlias(currentIndex, alias.getWriteAlias());
  isAck = aliasesRequestBuilder.execute().actionGet().isAcknowledged();
  logger.info("Removed Index Name from Alias=[{}] ACK=[{}]", alias, isAck);
aliasesRequestBuilder = adminClient.indices().prepareAliases();
isAck = aliasesRequestBuilder.execute().actionGet().isAcknowledged();
logger.info("Created new read and write aliases ACK=[{}]", isAck);
aliasCache.invalidate(alias);

代码示例来源:origin: apache/usergrid

private long getIndexSize(){
  long indexSize = 0L;
  final String indexName = indexLocationStrategy.getIndexInitialName();
  try {
    final IndicesStatsResponse statsResponse = esProvider.getClient()
      .admin()
      .indices()
      .prepareStats(indexName)
      .all()
      .execute()
      .actionGet();
    final CommonStats indexStats = statsResponse.getIndex(indexName).getTotal();
    indexSize = indexStats.getStore().getSizeInBytes();
  } catch (IndexMissingException e) {
    // if for some reason the index size does not exist,
    // log an error and we can assume size is 0 as it doesn't exist
    logger.error("Unable to get size for index {} due to IndexMissingException for app {}",
      indexName, indexLocationStrategy.getApplicationScope().getApplication().getUuid());
  }
  return indexSize;
}

代码示例来源:origin: apache/usergrid

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

相关文章

微信公众号

最新文章

更多