com.cognite.client.ThreeDModels Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cdf-sdk-java Show documentation
Show all versions of cdf-sdk-java Show documentation
Java SDK for reading and writing from/to CDF resources.
package com.cognite.client;
import com.cognite.client.config.ResourceType;
import com.cognite.client.config.UpsertMode;
import com.cognite.client.dto.*;
import com.cognite.client.servicesV1.ConnectorServiceV1;
import com.cognite.client.servicesV1.ItemReader;
import com.cognite.client.servicesV1.ResponseItems;
import com.cognite.client.servicesV1.parser.ThreeDModelParser;
import com.google.auto.value.AutoValue;
import org.apache.commons.lang3.RandomStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
/**
* This class represents the Cognite 3D models api endpoint.
*
* It provides methods for reading and writing {@link com.cognite.client.dto.ThreeDModel}.
*/
@AutoValue
public abstract class ThreeDModels extends ApiBase {
protected static final Logger LOG = LoggerFactory.getLogger(ThreeDModels.class);
private final static int MAX_UPSERT_BATCH_SIZE = 200;
/**
* Constructs a new {@link ThreeDModels} object using the provided client configuration.
*
* This method is intended for internal use--SDK clients should always use {@link CogniteClient}
* as the entry point to this class.
*
* @param client The {@link CogniteClient} to use for configuration settings.
* @return the 3D models api object.
*/
public static ThreeDModels of(CogniteClient client) {
return ThreeDModels.builder()
.setClient(client)
.build();
}
/**
* Returns {@link ThreeDModelsRevisions} representing 3D Models Revisions api endpoints.
*
* @return The ThreeDModelsRevisions api endpoints.
*/
public ThreeDModelsRevisions revisions() {
return ThreeDModelsRevisions.of(getClient());
}
private static ThreeDModels.Builder builder() {
return new AutoValue_ThreeDModels.Builder();
}
@AutoValue.Builder
abstract static class Builder extends ApiBase.Builder {
abstract ThreeDModels build();
}
/**
* Returns all {@link ThreeDModel} objects.
*
* @see #list(Request)
*/
public Iterator> list() throws Exception {
return this.list(Request.create());
}
/**
* Returns all {@link ThreeDModel} objects that matches the filters set in the {@link Request}.
*
* The results are paged through / iterated over via an {@link Iterator}--the entire results set is not buffered in
* memory, but streamed in "pages" from the Cognite api. If you need to buffer the entire results set, then you
* have to stream these results into your own data structure.
*
* The 3D models are retrieved using multiple, parallel request streams towards the Cognite api. The number of
* parallel streams are set in the {@link com.cognite.client.config.ClientConfig}.
*
* @param requestParameters the filters to use for retrieving the 3D models.
* @return an {@link Iterator} to page through the results set.
* @throws Exception
*/
public Iterator> list(Request requestParameters) throws Exception {
List partitions = buildPartitionsList(getClient().getClientConfig().getNoListPartitions());
return this.list(requestParameters, partitions.toArray(new String[0]));
}
/**
* Returns all {@link ThreeDModel} objects that matches the filters set in the {@link Request} for the
* specified partitions. This is method is intended for advanced use cases where you need direct control over
* the individual partitions. For example, when using the SDK in a distributed computing environment.
*
* The results are paged through / iterated over via an {@link Iterator}--the entire results set is not buffered in
* memory, but streamed in "pages" from the Cognite api. If you need to buffer the entire results set, then you
* have to stream these results into your own data structure.
*
* @param requestParameters the filters to use for retrieving the 3d models.
* @param partitions the partitions to include.
* @return an {@link Iterator} to page through the results set.
* @throws Exception
*/
public Iterator> list(Request requestParameters, String... partitions) throws Exception {
return AdapterIterator.of(listJson(ResourceType.THREED_MODEL, requestParameters, partitions), this::parseThreeDModel);
}
/**
* Retrieves 3D Models by id.
*
* @param items The item(s) {@code id} to retrieve.
* @return The retrieved 3D Models.
* @throws Exception
*/
public List retrieve(List- items) throws Exception {
String loggingPrefix = "retrieve() - " + RandomStringUtils.randomAlphanumeric(5) + " - ";
ConnectorServiceV1 connector = getClient().getConnectorService();
ItemReader
tdReader = connector.readThreeDModelsById();
List>> resultFutures = new ArrayList<>();
for (Item item : items) {
Request request = Request.create().withRootParameter("id", item.getId());
resultFutures.add(tdReader.getItemsAsync(addAuthInfo(request)));
}
// Sync all downloads to a single future. It will complete when all the upstream futures have completed.
CompletableFuture allFutures = CompletableFuture.allOf(resultFutures.toArray(
new CompletableFuture[resultFutures.size()]));
// Wait until the uber future completes.
allFutures.join();
// Collect the response items
List responseItems = new ArrayList<>();
for (CompletableFuture> responseItemsFuture : resultFutures) {
if (!responseItemsFuture.join().isSuccessful()) {
// something went wrong with the request
String message = loggingPrefix + "Retrieve 3d model failed: "
+ responseItemsFuture.join().getResponseBodyAsString();
LOG.error(message);
throw new Exception(message);
}
responseItems.add(responseItemsFuture.join().getResponseBodyAsString());
}
return responseItems.stream()
.map(this::parseThreeDModel)
.collect(Collectors.toList());
}
/**
* Creates or update a set of {@link ThreeDModel} objects.
*
* If it is a new {@link ThreeDModel} object (based on the {@code id}, then it will be created.
*
* If an {@link ThreeDModel} object already exists in Cognite Data Fusion, it will be updated. The update
* behaviour is specified via the update mode in the {@link com.cognite.client.config.ClientConfig} settings.
*
* @param threeDModels The 3D Models to upsert
* @return The upserted 3D Models
* @throws Exception
*/
public List upsert(List threeDModels) throws Exception {
String loggingPrefix = "upsert() - " + RandomStringUtils.randomAlphanumeric(5) + " - ";
ConnectorServiceV1 connector = getClient().getConnectorService();
ConnectorServiceV1.ItemWriter createItemWriter = connector.writeThreeDModels();
ConnectorServiceV1.ItemWriter updateItemWriter = connector.updateThreeDModels();
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy