|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.client.samples.json; |
| 10 | + |
| 11 | +import java.io.ByteArrayInputStream; |
| 12 | +import java.io.InputStream; |
| 13 | +import java.nio.charset.StandardCharsets; |
| 14 | +import org.apache.logging.log4j.LogManager; |
| 15 | +import org.apache.logging.log4j.Logger; |
| 16 | +import org.opensearch.client.opensearch.OpenSearchClient; |
| 17 | +import org.opensearch.client.opensearch.indices.PutIndexTemplateRequest; |
| 18 | +import org.opensearch.client.opensearch.indices.PutIndexTemplateResponse; |
| 19 | +import org.opensearch.client.opensearch.indices.put_index_template.IndexTemplateMapping; |
| 20 | +import org.opensearch.client.samples.SampleClient; |
| 21 | +import org.opensearch.client.samples.Search; |
| 22 | + |
| 23 | +public class DeserializationBasics { |
| 24 | + private static final Logger LOGGER = LogManager.getLogger(Search.class); |
| 25 | + |
| 26 | + private static OpenSearchClient client; |
| 27 | + |
| 28 | + public static void main(String[] args) { |
| 29 | + try { |
| 30 | + client = SampleClient.create(); |
| 31 | + |
| 32 | + var version = client.info().version(); |
| 33 | + LOGGER.info("Server: {}@{}.", version.distribution(), version.number()); |
| 34 | + |
| 35 | + final var indexTemplateName = "my-index"; |
| 36 | + |
| 37 | + // Use Json String/File for storing template. |
| 38 | + String stringTemplate = |
| 39 | + "{\"mappings\":{\"properties\":{\"age\":{\"type\":\"integer\"}}},\"settings\":{\"number_of_shards\":\"2\",\"number_of_replicas\":\"1\"}}"; |
| 40 | + // Create Input Stream for the above json template |
| 41 | + InputStream inputStream = new ByteArrayInputStream(stringTemplate.getBytes(StandardCharsets.UTF_8)); |
| 42 | + |
| 43 | + // Create Index Template Request for index 'my-index'. |
| 44 | + PutIndexTemplateRequest putIndexTemplateRequest = new PutIndexTemplateRequest.Builder().name(indexTemplateName) |
| 45 | + .template(new IndexTemplateMapping.Builder().withJson(inputStream).build()) // Use the Builder.withJson method to |
| 46 | + // deserialize the inputStream into an instance |
| 47 | + // of the IndexTemplateMapping class. |
| 48 | + .build(); |
| 49 | + |
| 50 | + LOGGER.info("Creating index template {}.", indexTemplateName); |
| 51 | + |
| 52 | + // Use toJsonString method to log Request and Response string. |
| 53 | + LOGGER.debug("Index Template Request: {}.", putIndexTemplateRequest.toJsonString()); |
| 54 | + PutIndexTemplateResponse response = client.indices().putIndexTemplate(putIndexTemplateRequest); |
| 55 | + LOGGER.info("Index Template Response: {}.", response.toJsonString()); |
| 56 | + |
| 57 | + } catch (Exception e) { |
| 58 | + LOGGER.error("Exception occurred.", e); |
| 59 | + } |
| 60 | + } |
| 61 | +} |
0 commit comments