use of com.ibm.watson.developer_cloud.discovery.v1.model.Configuration in project java-sdk by watson-developer-cloud.
the class DiscoveryServiceIT method issueNumber517.
// Tests for reported issues
@Test
public void issueNumber517() {
String uniqueConfigName = uniqueName + "-config";
CreateConfigurationOptions.Builder createBuilder = new CreateConfigurationOptions.Builder(environmentId);
Configuration configuration = getTestConfiguration(DISCOVERY1_TEST_CONFIG_FILE);
configuration.setName(uniqueConfigName);
createBuilder.configuration(configuration);
Configuration createResponse = createConfiguration(createBuilder.build());
GetConfigurationOptions getOptions = new GetConfigurationOptions.Builder(environmentId, createResponse.getConfigurationId()).build();
Configuration getResponse = discovery.getConfiguration(getOptions).execute();
// returned config should have some json data
assertEquals(1, getResponse.getConversions().getJsonNormalizations().size());
}
use of com.ibm.watson.developer_cloud.discovery.v1.model.Configuration in project java-sdk by watson-developer-cloud.
the class DiscoveryServiceIT method exampleIsSuccessful.
@Test
public void exampleIsSuccessful() {
// Discovery discovery = new Discovery("2016-12-15");
// discovery.setEndPoint("https://gateway.watsonplatform.net/discovery/api");
// discovery.setUsernameAndPassword("<username>", "<password");
String environmentId = null;
String configurationId = null;
String collectionId = null;
String documentId = null;
// See if an environment already exists
System.out.println("Check if environment exists");
ListEnvironmentsOptions listOptions = new ListEnvironmentsOptions.Builder().build();
ListEnvironmentsResponse listResponse = discovery.listEnvironments(listOptions).execute();
for (Environment environment : listResponse.getEnvironments()) {
// look for an existing environment that isn't read only
if (!environment.isReadOnly()) {
environmentId = environment.getEnvironmentId();
System.out.println("Found existing environment ID: " + environmentId);
break;
}
}
if (environmentId == null) {
System.out.println("No environment found, creating new one...");
// no environment found, create a new one (assuming we are a FREE plan)
String environmentName = "watson_developer_cloud_test_environment";
CreateEnvironmentOptions createOptions = new CreateEnvironmentOptions.Builder().name(environmentName).size(0L).build();
Environment createResponse = discovery.createEnvironment(createOptions).execute();
environmentId = createResponse.getEnvironmentId();
System.out.println("Created new environment ID: " + environmentId);
// wait for environment to be ready
System.out.println("Waiting for environment to be ready...");
boolean environmentReady = false;
while (!environmentReady) {
GetEnvironmentOptions getEnvironmentOptions = new GetEnvironmentOptions.Builder(environmentId).build();
Environment getEnvironmentResponse = discovery.getEnvironment(getEnvironmentOptions).execute();
environmentReady = getEnvironmentResponse.getStatus().equals(Environment.Status.ACTIVE);
try {
if (!environmentReady) {
Thread.sleep(500);
}
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted", e);
}
}
System.out.println("Environment Ready!");
}
// find the default configuration
System.out.println("Finding the default configuration");
ListConfigurationsOptions listConfigsOptions = new ListConfigurationsOptions.Builder(environmentId).build();
ListConfigurationsResponse listConfigsResponse = discovery.listConfigurations(listConfigsOptions).execute();
for (Configuration configuration : listConfigsResponse.getConfigurations()) {
if (configuration.getName().equals(DEFAULT_CONFIG_NAME)) {
configurationId = configuration.getConfigurationId();
System.out.println("Found default configuration ID: " + configurationId);
break;
}
}
// create a new collection
System.out.println("Creating a new collection...");
String collectionName = "my_watson_developer_cloud_collection" + UUID.randomUUID();
CreateCollectionOptions createCollectionOptions = new CreateCollectionOptions.Builder(environmentId, collectionName).configurationId(configurationId).build();
Collection collection = discovery.createCollection(createCollectionOptions).execute();
collectionId = collection.getCollectionId();
System.out.println("Created a collection ID: " + collectionId);
// wait for the collection to be "available"
System.out.println("Waiting for collection to be ready...");
boolean collectionReady = false;
while (!collectionReady) {
GetCollectionOptions getCollectionOptions = new GetCollectionOptions.Builder(environmentId, collectionId).build();
Collection getCollectionResponse = discovery.getCollection(getCollectionOptions).execute();
collectionReady = getCollectionResponse.getStatus().equals(Collection.Status.ACTIVE);
try {
if (!collectionReady) {
Thread.sleep(500);
}
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted", e);
}
}
System.out.println("Collection Ready!");
// add a document
System.out.println("Creating a new document...");
String documentJson = "{\"field\":\"value\"}";
InputStream documentStream = new ByteArrayInputStream(documentJson.getBytes());
AddDocumentOptions.Builder createDocumentBuilder = new AddDocumentOptions.Builder(environmentId, collectionId);
createDocumentBuilder.file(documentStream).fileContentType(HttpMediaType.APPLICATION_JSON);
createDocumentBuilder.filename("test_file");
DocumentAccepted createDocumentResponse = discovery.addDocument(createDocumentBuilder.build()).execute();
documentId = createDocumentResponse.getDocumentId();
System.out.println("Created a document ID: " + documentId);
// wait for document to be ready
System.out.println("Waiting for document to be ready...");
boolean documentReady = false;
while (!documentReady) {
GetDocumentStatusOptions getDocumentStatusOptions = new GetDocumentStatusOptions.Builder(environmentId, collectionId, documentId).build();
DocumentStatus getDocumentResponse = discovery.getDocumentStatus(getDocumentStatusOptions).execute();
documentReady = !getDocumentResponse.getStatus().equals(DocumentStatus.Status.PROCESSING);
try {
if (!documentReady) {
Thread.sleep(500);
}
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted");
}
}
System.out.println("Document Ready!");
// query document
System.out.println("Querying the collection...");
QueryOptions.Builder queryBuilder = new QueryOptions.Builder(environmentId, collectionId);
queryBuilder.query("field:value");
QueryResponse queryResponse = discovery.query(queryBuilder.build()).execute();
// print out the results
System.out.println("Query Results:");
System.out.println(queryResponse);
// cleanup the collection created
System.out.println("Deleting the collection...");
DeleteCollectionOptions deleteOptions = new DeleteCollectionOptions.Builder(environmentId, collectionId).build();
discovery.deleteCollection(deleteOptions).execute();
System.out.println("Collection deleted!");
System.out.println("Discovery example finished");
}
use of com.ibm.watson.developer_cloud.discovery.v1.model.Configuration in project java-sdk by watson-developer-cloud.
the class Discovery method createConfiguration.
/**
* Add configuration.
*
* Creates a new configuration. If the input configuration contains the `configuration_id`, `created`, or `updated`
* properties, then they are ignored and overridden by the system, and an error is not returned so that the overridden
* fields do not need to be removed when copying a configuration. The configuration can contain unrecognized JSON
* fields. Any such fields are ignored and do not generate an error. This makes it easier to use newer configuration
* files with older versions of the API and the service. It also makes it possible for the tooling to add additional
* metadata and information to the configuration.
*
* @param createConfigurationOptions the {@link CreateConfigurationOptions} containing the options for the call
* @return a {@link ServiceCall} with a response type of {@link Configuration}
*/
public ServiceCall<Configuration> createConfiguration(CreateConfigurationOptions createConfigurationOptions) {
Validator.notNull(createConfigurationOptions, "createConfigurationOptions cannot be null");
String[] pathSegments = { "v1/environments", "configurations" };
String[] pathParameters = { createConfigurationOptions.environmentId() };
RequestBuilder builder = RequestBuilder.post(RequestBuilder.constructHttpUrl(getEndPoint(), pathSegments, pathParameters));
builder.query(VERSION, versionDate);
final JsonObject contentJson = new JsonObject();
if (createConfigurationOptions.name() != null) {
contentJson.addProperty("name", createConfigurationOptions.name());
}
if (createConfigurationOptions.description() != null) {
contentJson.addProperty("description", createConfigurationOptions.description());
}
if (createConfigurationOptions.conversions() != null) {
contentJson.add("conversions", GsonSingleton.getGson().toJsonTree(createConfigurationOptions.conversions()));
}
if (createConfigurationOptions.enrichments() != null) {
contentJson.add("enrichments", GsonSingleton.getGson().toJsonTree(createConfigurationOptions.enrichments()));
}
if (createConfigurationOptions.normalizations() != null) {
contentJson.add("normalizations", GsonSingleton.getGson().toJsonTree(createConfigurationOptions.normalizations()));
}
builder.bodyJson(contentJson);
return createServiceCall(builder.build(), ResponseConverterUtils.getObject(Configuration.class));
}
use of com.ibm.watson.developer_cloud.discovery.v1.model.Configuration in project java-sdk by watson-developer-cloud.
the class Discovery method updateConfiguration.
/**
* Update a configuration.
*
* Replaces an existing configuration. * Completely replaces the original configuration. * The `configuration_id`,
* `updated`, and `created` fields are accepted in the request, but they are ignored, and an error is not generated.
* It is also acceptable for users to submit an updated configuration with none of the three properties. * Documents
* are processed with a snapshot of the configuration as it was at the time the document was submitted to be ingested.
* This means that already submitted documents will not see any updates made to the configuration.
*
* @param updateConfigurationOptions the {@link UpdateConfigurationOptions} containing the options for the call
* @return a {@link ServiceCall} with a response type of {@link Configuration}
*/
public ServiceCall<Configuration> updateConfiguration(UpdateConfigurationOptions updateConfigurationOptions) {
Validator.notNull(updateConfigurationOptions, "updateConfigurationOptions cannot be null");
String[] pathSegments = { "v1/environments", "configurations" };
String[] pathParameters = { updateConfigurationOptions.environmentId(), updateConfigurationOptions.configurationId() };
RequestBuilder builder = RequestBuilder.put(RequestBuilder.constructHttpUrl(getEndPoint(), pathSegments, pathParameters));
builder.query(VERSION, versionDate);
final JsonObject contentJson = new JsonObject();
if (updateConfigurationOptions.name() != null) {
contentJson.addProperty("name", updateConfigurationOptions.name());
}
if (updateConfigurationOptions.description() != null) {
contentJson.addProperty("description", updateConfigurationOptions.description());
}
if (updateConfigurationOptions.conversions() != null) {
contentJson.add("conversions", GsonSingleton.getGson().toJsonTree(updateConfigurationOptions.conversions()));
}
if (updateConfigurationOptions.enrichments() != null) {
contentJson.add("enrichments", GsonSingleton.getGson().toJsonTree(updateConfigurationOptions.enrichments()));
}
if (updateConfigurationOptions.normalizations() != null) {
contentJson.add("normalizations", GsonSingleton.getGson().toJsonTree(updateConfigurationOptions.normalizations()));
}
builder.bodyJson(contentJson);
return createServiceCall(builder.build(), ResponseConverterUtils.getObject(Configuration.class));
}
Aggregations