org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest Java Examples

The following examples show how to use org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ElasticsearchIndexInitializer.java    From staccato with Apache License 2.0 6 votes vote down vote up
private boolean createTemplate(CollectionMetadata collection) throws Exception {
    String searchAlias = collection.getId() + "-search";
    String indexTemplateName = collection.getId() + "-template";
    String indexTemplatePattern = collection.getId() + "-*";

    log.debug("Attempting to initialize template for collection '" + collection.getId() + "'");

    PutIndexTemplateRequest request = new PutIndexTemplateRequest(indexTemplateName)
            .patterns(Arrays.asList(indexTemplatePattern))
            .alias(new Alias(searchAlias))
            .mapping("_doc", buildMappings(collection));

    client.indices().putTemplate(request, RequestOptions.DEFAULT);

    log.info("Template '" + indexTemplateName + "' for collection '" + collection.getId() + "' initialized");
    return true;
}
 
Example #2
Source File: BulkProcessorObjectFactory.java    From log4j2-elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void execute(IndexTemplate indexTemplate) {
    try {
        createClient().admin().indices().putTemplate(
                new PutIndexTemplateRequest()
                        .name(indexTemplate.getName())
                        .source(indexTemplate.getSource(), XContentType.JSON)
        );
    } catch (Exception e) {
        throw new ConfigurationException(e.getMessage(), e);
    }
}
 
Example #3
Source File: TableCreator.java    From crate with Apache License 2.0 5 votes vote down vote up
public CompletableFuture<Long> create(BoundCreateTable createTable) {
    var templateName = createTable.templateName();
    var relationName = createTable.tableIdent();
    var createTableRequest = templateName == null
        ? new CreateTableRequest(
            new CreateIndexRequest(
                relationName.indexNameOrAlias(),
                createTable.tableParameter().settings()
            ).mapping(Constants.DEFAULT_MAPPING_TYPE, createTable.mapping())
        )
        : new CreateTableRequest(
            new PutIndexTemplateRequest(templateName)
                .mapping(Constants.DEFAULT_MAPPING_TYPE, createTable.mapping())
                .create(true)
                .settings(createTable.tableParameter().settings())
                .patterns(Collections.singletonList(createTable.templatePrefix()))
                .order(100)
                .alias(new Alias(relationName.indexNameOrAlias()))
    );
    return Transports.execute(transportCreateTableAction, createTableRequest, resp -> {
        if (!resp.isAllShardsAcked() && LOGGER.isWarnEnabled()) {
            LOGGER.warn("CREATE TABLE `{}` was not acknowledged. This could lead to inconsistent state.", relationName.fqn());
        }
        return 1L;
    }).exceptionally(error -> {
        Throwable t = SQLExceptions.unwrap(error);
        String message = t.getMessage();
        Throwable cause = t.getCause();
        if ("mapping [default]".equals(message) && cause != null) {
            // this is a generic mapping parse exception,
            // the cause has usually a better more detailed error message
            return Exceptions.rethrowRuntimeException(cause);
        } else if (createTable.ifNotExists() && isTableExistsError(t, templateName)) {
            return 0L;
        } else {
            return Exceptions.rethrowRuntimeException(t);
        }
    });
}
 
Example #4
Source File: TransportCreateTableAction.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
protected ClusterBlockException checkBlock(CreateTableRequest request, ClusterState state) {
    if (request.getCreateIndexRequest() != null) {
        CreateIndexRequest createIndexRequest = request.getCreateIndexRequest();
        return transportCreateIndexAction.checkBlock(createIndexRequest, state);
    } else if (request.getPutIndexTemplateRequest() != null) {
        PutIndexTemplateRequest putIndexTemplateRequest = request.getPutIndexTemplateRequest();
        return transportPutIndexTemplateAction.checkBlock(putIndexTemplateRequest, state);
    } else {
        throw new IllegalStateException("Unknown table request");
    }
}
 
Example #5
Source File: PutIndexTemplateRequestBuilder.java    From elasticshell with Apache License 2.0 5 votes vote down vote up
@Override
protected XContentBuilder toXContent(PutIndexTemplateRequest request, PutIndexTemplateResponse response, XContentBuilder builder) throws IOException {
    builder.startObject()
            .field(Fields.OK, true)
            .field(Fields.ACKNOWLEDGED, response.isAcknowledged())
            .endObject();
    return builder;
}
 
Example #6
Source File: InitializerCommand.java    From foxtrot with Apache License 2.0 5 votes vote down vote up
@Override
protected void run(Bootstrap<FoxtrotServerConfiguration> bootstrap, Namespace namespace, FoxtrotServerConfiguration configuration)
        throws Exception {
    ElasticsearchConfig esConfig = configuration.getElasticsearch();
    ElasticsearchConnection connection = new ElasticsearchConnection(esConfig);
    connection.start();

    ClusterHealthResponse clusterHealth = connection.getClient()
            .admin()
            .cluster()
            .health(new ClusterHealthRequest())
            .actionGet();
    int numDataNodes = clusterHealth.getNumberOfDataNodes();
    int numReplicas = (numDataNodes < 2) ? 0 : 1;

    logger.info("# data nodes: {}, Setting replica count to: {}", numDataNodes, numReplicas);

    createMetaIndex(connection, ElasticsearchConsolePersistence.INDEX, numReplicas);
    createMetaIndex(connection, ElasticsearchConsolePersistence.INDEX_V2, numReplicas);
    createMetaIndex(connection, TableMapStore.TABLE_META_INDEX, numReplicas);
    createMetaIndex(connection, ElasticsearchConsolePersistence.INDEX_HISTORY, numReplicas);
    createMetaIndex(connection, FqlStoreServiceImpl.FQL_STORE_INDEX, numReplicas);

    logger.info("Creating mapping");
    PutIndexTemplateRequest putIndexTemplateRequest = ElasticsearchUtils.getClusterTemplateMapping();
    PutIndexTemplateResponse response = connection.getClient()
            .admin()
            .indices()
            .putTemplate(putIndexTemplateRequest)
            .actionGet();
    logger.info("Created mapping: {}", response.isAcknowledged());

    logger.info("Creating hbase table");
    HBaseUtil.createTable(configuration.getHbase(), configuration.getHbase()
            .getTableName());
}
 
Example #7
Source File: ElasticsearchUtils.java    From foxtrot with Apache License 2.0 5 votes vote down vote up
public static void initializeMappings(Client client) {
    PutIndexTemplateRequest templateRequest = getClusterTemplateMapping();
    client.admin()
            .indices()
            .putTemplate(templateRequest)
            .actionGet();
}
 
Example #8
Source File: ElasticsearchUtils.java    From foxtrot with Apache License 2.0 5 votes vote down vote up
public static PutIndexTemplateRequest getClusterTemplateMapping() {
    try {
        return new PutIndexTemplateRequest().name("template_foxtrot_mappings")
                .patterns(Lists.newArrayList(String.format("%s-*", getTableNamePrefix())))
                .mapping(DOCUMENT_TYPE_NAME, getDocumentMapping());
    }
    catch (IOException ex) {
        logger.error("TEMPLATE_CREATION_FAILED", ex);
        return null;
    }
}
 
Example #9
Source File: RepositoryIntegrationTest.java    From elastic-crud with Apache License 2.0 5 votes vote down vote up
@Before
public void before() throws IOException {
  repository = factory.create(Person.class);
  repository.refreshPolicy(IMMEDIATE);
  final IndicesAdminClient indices = client.admin().indices();

  final PutIndexTemplateRequest datas = indices.preparePutTemplate("datas")
    .setSource(toByteArray(getClass().getResourceAsStream("/datas.json")), JSON)
    .request();
  checkState(indices.putTemplate(datas).actionGet().isAcknowledged());
}
 
Example #10
Source File: BulkProcessorObjectFactory.java    From log4j2-elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void execute(IndexTemplate indexTemplate) {
    try {
        createClient().admin().indices().putTemplate(
                new PutIndexTemplateRequest()
                        .name(indexTemplate.getName())
                        .source(indexTemplate.getSource())
        );
    } catch (Exception e) {
        throw new ConfigurationException(e);
    }
}
 
Example #11
Source File: BulkProcessorObjectFactory.java    From log4j2-elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void execute(IndexTemplate indexTemplate) {
    try {
        createClient().admin().indices().putTemplate(
                new PutIndexTemplateRequest()
                        .name(indexTemplate.getName())
                        .source(indexTemplate.getSource(), XContentType.JSON)
        );
    } catch (Exception e) {
        throw new ConfigurationException(e.getMessage(), e);
    }
}
 
Example #12
Source File: TableCreator.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private PutIndexTemplateRequest createTemplateRequest(CreateTableAnalyzedStatement statement) {
    PutIndexTemplateRequest putIndexTemplateRequest = new PutIndexTemplateRequest(statement.templateName())
            .mapping(Constants.DEFAULT_MAPPING_TYPE, statement.mapping())
            .create(true)
            .settings(settings(statement))
            .template(statement.templatePrefix())
            .order(100)
            .alias(new Alias(statement.tableIdent().indexName()));
    putIndexTemplateRequest.putHeader(LoginUserContext.USER_INFO_KEY, statement.getParameterContext().getLoginUserContext());
    return putIndexTemplateRequest;
}
 
Example #13
Source File: ElasticSearchReader.java    From garmadon with Apache License 2.0 5 votes vote down vote up
private static void putGarmadonTemplate(RestHighLevelClient esClient, ElasticsearchConfiguration elasticsearch)
    throws IOException, GarmadonEsException {
    PutIndexTemplateRequest indexRequest = new PutIndexTemplateRequest("garmadon");
    indexRequest.patterns(Collections.singletonList(elasticsearch.getIndexPrefix() + "*"));

    // Create template settings with mandatory one
    Settings.Builder templateSettings = Settings.builder()
        .put("sort.field", "timestamp")
        .put("sort.order", "desc")
        .put("analysis.analyzer.path_analyzer.tokenizer", "path_tokenizer")
        .put("analysis.tokenizer.path_tokenizer.type", "path_hierarchy")
        .put("analysis.tokenizer.path_tokenizer.delimiter", "/")
        .put("index.lifecycle.name", "garmadon")
        .put("index.lifecycle.rollover_alias", "garmadon");

    // Add settings from config
    elasticsearch.getSettings().forEach(templateSettings::put);

    indexRequest.settings(templateSettings);

    // Add garmadon alias
    Alias alias = new Alias("garmadon");
    indexRequest.alias(alias);

    String template = IOUtils.toString(Objects.requireNonNull(ElasticSearchReader.class.getClassLoader()
        .getResourceAsStream("template.json")), "UTF-8");

    indexRequest.mapping(ES_TYPE, template, XContentType.JSON);

    if (!esClient.indices().putTemplate(indexRequest, RequestOptions.DEFAULT).isAcknowledged()) {
        throw new GarmadonEsException("Failed to insert garmadon template");
    }
}
 
Example #14
Source File: DocIndexMetaData.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private void updateTemplate(DocIndexMetaData md,
                            TransportPutIndexTemplateAction transportPutIndexTemplateAction,
                            Settings updateSettings) {
    String templateName = PartitionName.templateName(ident.schema(), ident.name());
    PutIndexTemplateRequest request = new PutIndexTemplateRequest(templateName)
            .mapping(Constants.DEFAULT_MAPPING_TYPE, md.defaultMappingMap)
            .create(false)
            .settings(updateSettings)
            .template(templateName + "*");
    for (String alias : md.aliases()) {
        request = request.alias(new Alias(alias));
    }
    transportPutIndexTemplateAction.execute(request);
}
 
Example #15
Source File: AlterTableOperation.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private ListenableFuture<Long> updateTemplate(Map<String, Object> newMappings,
                                              Settings newSettings,
                                              TableIdent tableIdent,
                                              AbstractDDLAnalyzedStatement statement) {
    String templateName = PartitionName.templateName(tableIdent.schema(), tableIdent.name());
    IndexTemplateMetaData indexTemplateMetaData =
            clusterService.state().metaData().templates().get(templateName);
    if (indexTemplateMetaData == null) {
        return Futures.immediateFailedFuture(new RuntimeException("Template for partitioned table is missing"));
    }

    // merge mappings
    Map<String, Object> mapping = mergeTemplateMapping(indexTemplateMetaData, newMappings);

    // merge settings
    Settings.Builder settingsBuilder = Settings.builder();
    settingsBuilder.put(indexTemplateMetaData.settings());
    settingsBuilder.put(newSettings);

    PutIndexTemplateRequest request = new PutIndexTemplateRequest(templateName)
            .create(false)
            .mapping(Constants.DEFAULT_MAPPING_TYPE, mapping)
            .order(indexTemplateMetaData.order())
            .settings(settingsBuilder.build())
            .template(indexTemplateMetaData.template());

    request.putHeader(LoginUserContext.USER_INFO_KEY, statement.getParameterContext().getLoginUserContext());
    for (ObjectObjectCursor<String, AliasMetaData> container : indexTemplateMetaData.aliases()) {
        Alias alias = new Alias(container.key);
        request.alias(alias);
    }

    SettableFuture<Long> result = SettableFuture.create();
    transportActionProvider.transportPutIndexTemplateAction().execute(request,
            new SettableFutureToNullActionListener<PutIndexTemplateResponse>(result));

    return result;
}
 
Example #16
Source File: RestPutIndexTemplateAction.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({"unchecked"})
@Override
public void handleRequest(final RestRequest request, final RestChannel channel, final Client client) {
    PutIndexTemplateRequest putRequest = new PutIndexTemplateRequest(request.param("name"));
    putRequest.template(request.param("template", putRequest.template()));
    putRequest.order(request.paramAsInt("order", putRequest.order()));
    putRequest.masterNodeTimeout(request.paramAsTime("master_timeout", putRequest.masterNodeTimeout()));
    putRequest.create(request.paramAsBoolean("create", false));
    putRequest.cause(request.param("cause", ""));
    putRequest.source(request.content());
    client.admin().indices().putTemplate(putRequest, new AcknowledgedRestListener<PutIndexTemplateResponse>(channel));
}
 
Example #17
Source File: ESCreateTemplateTask.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private PutIndexTemplateRequest buildRequest(ESCreateTemplateNode node) {
    PutIndexTemplateRequest templateRequest = new PutIndexTemplateRequest(node.templateName())
            .mapping(Constants.DEFAULT_MAPPING_TYPE, node.mapping())
            .create(true)
            .settings(node.indexSettings())
            .template(node.indexMatch());
    if (node.alias() != null) {
        templateRequest.alias(new Alias(node.alias()));
    }

    templateRequest.putHeader(LoginUserContext.USER_INFO_KEY, getParamContext().getLoginUserContext());
    return templateRequest;
}
 
Example #18
Source File: AdminOperationsTest.java    From log4j2-elasticsearch with Apache License 2.0 4 votes vote down vote up
private String extractPayload(PutIndexTemplateRequest putIndexTemplateRequest) throws IOException {
    BytesStreamOutput out = new BytesStreamOutput();
    putIndexTemplateRequest.writeTo(out);
    return new String(out.bytes().toBytesRef().bytes);
}
 
Example #19
Source File: AbstractClient.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public void putTemplate(final PutIndexTemplateRequest request, final ActionListener<PutIndexTemplateResponse> listener) {
    execute(PutIndexTemplateAction.INSTANCE, request, listener);
}
 
Example #20
Source File: AbstractClient.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public ActionFuture<PutIndexTemplateResponse> putTemplate(final PutIndexTemplateRequest request) {
    return execute(PutIndexTemplateAction.INSTANCE, request);
}
 
Example #21
Source File: AdminOperationsTest.java    From log4j2-elasticsearch with Apache License 2.0 4 votes vote down vote up
private String extractPayload(PutIndexTemplateRequest putIndexTemplateRequest) throws IOException {
    BytesStreamOutput out = new BytesStreamOutput();
    putIndexTemplateRequest.writeTo(out);
    return new String(out.bytes().toBytesRef().bytes);
}
 
Example #22
Source File: AdminOperationsTest.java    From log4j2-elasticsearch with Apache License 2.0 4 votes vote down vote up
private String extractPayload(PutIndexTemplateRequest putIndexTemplateRequest) throws IOException {
    BytesStreamOutput out = new BytesStreamOutput();
    putIndexTemplateRequest.writeTo(out);
    return new String(out.bytes().toBytesRef().bytes);
}
 
Example #23
Source File: PutIndexTemplateRequestBuilder.java    From elasticshell with Apache License 2.0 4 votes vote down vote up
public PutIndexTemplateRequestBuilder(Client client, JsonToString<JsonInput> jsonToString, StringToJson<JsonOutput> stringToJson) {
    super(client, new PutIndexTemplateRequest(null), jsonToString, stringToJson);
}
 
Example #24
Source File: PutIndexTemplateRequestBuilder.java    From elasticshell with Apache License 2.0 4 votes vote down vote up
@Override
protected ActionFuture<PutIndexTemplateResponse> doExecute(PutIndexTemplateRequest request) {
    return client.admin().indices().putTemplate(request);
}
 
Example #25
Source File: CreateTableRequest.java    From crate with Apache License 2.0 4 votes vote down vote up
@Nullable
public PutIndexTemplateRequest getPutIndexTemplateRequest() {
    return putIndexTemplateRequest;
}
 
Example #26
Source File: AbstractClient.java    From crate with Apache License 2.0 4 votes vote down vote up
@Override
public ActionFuture<AcknowledgedResponse> putTemplate(final PutIndexTemplateRequest request) {
    return execute(PutIndexTemplateAction.INSTANCE, request);
}
 
Example #27
Source File: AbstractClient.java    From crate with Apache License 2.0 4 votes vote down vote up
@Override
public void putTemplate(final PutIndexTemplateRequest request, final ActionListener<AcknowledgedResponse> listener) {
    execute(PutIndexTemplateAction.INSTANCE, request, listener);
}
 
Example #28
Source File: CreateTableRequest.java    From crate with Apache License 2.0 4 votes vote down vote up
public CreateTableRequest(PutIndexTemplateRequest putIndexTemplateRequest) {
    this.createIndexRequest = null;
    this.putIndexTemplateRequest = putIndexTemplateRequest;
}
 
Example #29
Source File: AdminOperationsTest.java    From log4j2-elasticsearch with Apache License 2.0 3 votes vote down vote up
@Test
public void passesIndexTemplateToClient() throws IOException {

    //given
    BulkProcessorObjectFactory factory = spy(createTestObjectFactoryBuilder().build());

    IndicesAdminClient indicesAdminClient = mockedIndicesAdminClient(factory);

    IndexTemplate indexTemplate = spy(IndexTemplate.newBuilder()
            .withPath("classpath:indexTemplate.json")
            .withName("testName")
            .build());

    String expectedPayload = indexTemplate.getSource().replaceAll("\\s+","");

    // when
    factory.execute(indexTemplate);

    // then
    ArgumentCaptor<PutIndexTemplateRequest> requestArgumentCaptor = ArgumentCaptor.forClass(PutIndexTemplateRequest.class);
    verify(indicesAdminClient).putTemplate(requestArgumentCaptor.capture());

    String actualPayload = extractPayload(requestArgumentCaptor.getValue());

    Assert.assertTrue(actualPayload.contains(new ObjectMapper().readTree(expectedPayload).get("mappings").toString()));

}
 
Example #30
Source File: AdminOperationsTest.java    From log4j2-elasticsearch with Apache License 2.0 3 votes vote down vote up
@Test
public void passesIndexTemplateToClient() throws IOException {

    //given
    BulkProcessorObjectFactory factory = spy(createTestObjectFactoryBuilder().build());

    IndicesAdminClient indicesAdminClient = mockedIndicesAdminClient(factory);

    IndexTemplate indexTemplate = spy(IndexTemplate.newBuilder()
            .withPath("classpath:indexTemplate.json")
            .withName("testName")
            .build());

    String expectedPayload = indexTemplate.getSource().replaceAll("\\s+","");

    // when
    factory.execute(indexTemplate);

    // then
    ArgumentCaptor<PutIndexTemplateRequest> requestArgumentCaptor = ArgumentCaptor.forClass(PutIndexTemplateRequest.class);
    verify(indicesAdminClient).putTemplate(requestArgumentCaptor.capture());

    String actualPayload = extractPayload(requestArgumentCaptor.getValue());

    Assert.assertTrue(actualPayload.contains(new ObjectMapper().readTree(expectedPayload).get("mappings").toString()));

}