-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Jai2305 <[email protected]>
- Loading branch information
Showing
10 changed files
with
234 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
java-client/src/main/java/org/opensearch/client/json/PlainDeserializable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.client.json; | ||
|
||
import jakarta.json.stream.JsonParser; | ||
import java.io.InputStream; | ||
import java.io.Reader; | ||
|
||
/** Base interface to set JSON properties **/ | ||
|
||
public interface PlainDeserializable<B> { | ||
|
||
B self(); | ||
|
||
/** Updates object with newly provided JSON properties | ||
@param parser the JsonParser parser | ||
@param mapper the JsonpMapper mapper used to deserialize values | ||
@return this object | ||
**/ | ||
|
||
default B withJson(JsonParser parser, JsonpMapper mapper) { | ||
JsonpDeserializer<?> deserializer = JsonpMapperBase.findDeserializer(this.getClass().getEnclosingClass()); | ||
@SuppressWarnings("unchecked") | ||
ObjectDeserializer<B> objectDeserializer = (ObjectDeserializer<B>) DelegatingDeserializer.unwrap(deserializer); | ||
assert objectDeserializer != null; | ||
return objectDeserializer.deserialize(self(), parser, mapper, parser.next()); | ||
} | ||
|
||
/** Updates object with newly provided JSON properties | ||
@param inputStream the stream to read from | ||
@return this object | ||
* **/ | ||
default B withJson(InputStream inputStream) { | ||
JsonpMapper defaultMapper = JsonpUtils.DEFAULT_JSONP_MAPPER; | ||
try (JsonParser parser = defaultMapper.jsonProvider().createParser(inputStream)) { | ||
return withJson(parser, defaultMapper); | ||
} | ||
} | ||
|
||
/** Updates object with newly provided JSON properties | ||
@param reader the stream to read from | ||
@return this object | ||
* **/ | ||
default B withJson(Reader reader) { | ||
JsonpMapper defaultMapper = JsonpUtils.DEFAULT_JSONP_MAPPER; | ||
try (JsonParser parser = defaultMapper.jsonProvider().createParser(reader)) { | ||
return withJson(parser, defaultMapper); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
java-client/src/test/java/org/opensearch/client/opensearch/json/PlainDeserializableTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.client.opensearch.json; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.InputStream; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import org.junit.Test; | ||
import org.opensearch.client.opensearch.indices.PutIndexTemplateRequest; | ||
import org.opensearch.client.opensearch.indices.put_index_template.IndexTemplateMapping; | ||
|
||
public class PlainDeserializableTest { | ||
@Test | ||
public void testWithJsonPutIndexTemplateRequest() { | ||
|
||
String stringTemplate = | ||
"{\"mappings\":{\"properties\":{\"age\":{\"type\":\"integer\"}}},\"settings\":{\"number_of_shards\":\"2\",\"number_of_replicas\":\"1\"}}"; | ||
InputStream inputStream = new ByteArrayInputStream(stringTemplate.getBytes(StandardCharsets.UTF_8)); | ||
|
||
PutIndexTemplateRequest indexTemplateRequest = new PutIndexTemplateRequest.Builder().name("My index") | ||
.indexPatterns("index_pattern1") | ||
.template(new IndexTemplateMapping.Builder().withJson(inputStream).build()) | ||
.build(); | ||
|
||
String expectedName = "My index"; | ||
List<String> expectedIndexPatterns = Arrays.asList("index_pattern1"); | ||
String expectedNumberOfShards = "2"; | ||
|
||
assertEquals(indexTemplateRequest.name(), expectedName); | ||
assertEquals(expectedIndexPatterns, indexTemplateRequest.indexPatterns()); | ||
assertEquals(expectedNumberOfShards, indexTemplateRequest.template().settings().numberOfShards()); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
samples/src/main/java/org/opensearch/client/samples/json/DeserializationBasics.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.client.samples.json; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.InputStream; | ||
import java.nio.charset.StandardCharsets; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.opensearch.client.opensearch.OpenSearchClient; | ||
import org.opensearch.client.opensearch.indices.PutIndexTemplateRequest; | ||
import org.opensearch.client.opensearch.indices.PutIndexTemplateResponse; | ||
import org.opensearch.client.opensearch.indices.put_index_template.IndexTemplateMapping; | ||
import org.opensearch.client.samples.SampleClient; | ||
import org.opensearch.client.samples.Search; | ||
|
||
public class DeserializationBasics { | ||
private static final Logger LOGGER = LogManager.getLogger(Search.class); | ||
|
||
private static OpenSearchClient client; | ||
|
||
public static void main(String[] args) { | ||
try { | ||
client = SampleClient.create(); | ||
|
||
var version = client.info().version(); | ||
LOGGER.info("Server: {}@{}.", version.distribution(), version.number()); | ||
|
||
final var indexTemplateName = "my-index"; | ||
|
||
// Use Json String/File for storing template. | ||
String stringTemplate = | ||
"{\"mappings\":{\"properties\":{\"age\":{\"type\":\"integer\"}}},\"settings\":{\"number_of_shards\":\"2\",\"number_of_replicas\":\"1\"}}"; | ||
// Create Input Stream for the above json template | ||
InputStream inputStream = new ByteArrayInputStream(stringTemplate.getBytes(StandardCharsets.UTF_8)); | ||
|
||
// Create Index Template Request for index 'my-index'. | ||
PutIndexTemplateRequest putIndexTemplateRequest = new PutIndexTemplateRequest.Builder().name(indexTemplateName) | ||
.template(new IndexTemplateMapping.Builder().withJson(inputStream).build()) // Use the Builder.withJson method to | ||
// deserialize the inputStream into an instance | ||
// of the IndexTemplateMapping class. | ||
.build(); | ||
|
||
LOGGER.info("Creating index template {}.", indexTemplateName); | ||
|
||
// Use toJsonString method to log Request and Response string. | ||
LOGGER.debug("Index Template Request: {}.", putIndexTemplateRequest.toJsonString()); | ||
PutIndexTemplateResponse response = client.indices().putIndexTemplate(putIndexTemplateRequest); | ||
LOGGER.info("Index Template Response: {}.", response.toJsonString()); | ||
|
||
} catch (Exception e) { | ||
LOGGER.error("Exception occurred.", e); | ||
} | ||
} | ||
} |