import com.google.api.gax.longrunning.OperationFuture;
import com.google.cloud.automl.v1beta1.AutoMlClient;
import com.google.cloud.automl.v1beta1.ColumnSpec;
import com.google.cloud.automl.v1beta1.ColumnSpecName;
import com.google.cloud.automl.v1beta1.LocationName;
import com.google.cloud.automl.v1beta1.Model;
import com.google.cloud.automl.v1beta1.OperationMetadata;
import com.google.cloud.automl.v1beta1.TablesModelMetadata;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
class TablesCreateModel {
public static void main(String[] args)
throws IOException, ExecutionException, InterruptedException {
// TODO(developer): Replace these variables before running the sample.
String projectId = "YOUR_PROJECT_ID";
String datasetId = "YOUR_DATASET_ID";
String tableSpecId = "YOUR_TABLE_SPEC_ID";
String columnSpecId = "YOUR_COLUMN_SPEC_ID";
String displayName = "YOUR_DATASET_NAME";
createModel(projectId, datasetId, tableSpecId, columnSpecId, displayName);
}
// Create a model
static void createModel(
String projectId,
String datasetId,
String tableSpecId,
String columnSpecId,
String displayName)
throws IOException, ExecutionException, InterruptedException {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (AutoMlClient client = AutoMlClient.create()) {
// A resource that represents Google Cloud Platform location.
LocationName projectLocation = LocationName.of(projectId, "us-central1");
// Get the complete path of the column.
ColumnSpecName columnSpecName =
ColumnSpecName.of(projectId, "us-central1", datasetId, tableSpecId, columnSpecId);
// Build the get column spec.
ColumnSpec targetColumnSpec =
ColumnSpec.newBuilder().setName(columnSpecName.toString()).build();
// Set model metadata.
TablesModelMetadata metadata =
TablesModelMetadata.newBuilder()
.setTargetColumnSpec(targetColumnSpec)
.setTrainBudgetMilliNodeHours(24000)
.build();
Model model =
Model.newBuilder()
.setDisplayName(displayName)
.setDatasetId(datasetId)
.setTablesModelMetadata(metadata)
.build();
// Create a model with the model metadata in the region.
OperationFuture<Model, OperationMetadata> future =
client.createModelAsync(projectLocation, model);
// OperationFuture.get() will block until the model is created, which may take several hours.
// You can use OperationFuture.getInitialFuture to get a future representing the initial
// response to the request, which contains information while the operation is in progress.
System.out.format("Training operation name: %s%n", future.getInitialFuture().get().getName());
System.out.println("Training started...");
}
}
}
# TODO(developer): Uncomment and set the following variables
# project_id = 'PROJECT_ID_HERE'
# compute_region = 'COMPUTE_REGION_HERE'
# dataset_display_name = 'DATASET_DISPLAY_NAME_HERE'
# model_display_name = 'MODEL_DISPLAY_NAME_HERE'
# train_budget_milli_node_hours = 'TRAIN_BUDGET_MILLI_NODE_HOURS_HERE'
# include_column_spec_names = 'INCLUDE_COLUMN_SPEC_NAMES_HERE'
# or None if unspecified
# exclude_column_spec_names = 'EXCLUDE_COLUMN_SPEC_NAMES_HERE'
# or None if unspecified
from google.cloud import automl_v1beta1 as automl
client = automl.TablesClient(project=project_id, region=compute_region)
# Create a model with the model metadata in the region.
response = client.create_model(
model_display_name,
train_budget_milli_node_hours=train_budget_milli_node_hours,
dataset_display_name=dataset_display_name,
include_column_spec_names=include_column_spec_names,
exclude_column_spec_names=exclude_column_spec_names,
)
print("Training model...")
print("Training operation name: {}".format(response.operation.name))
print("Training completed: {}".format(response.result()))