com.google.api.gax.rpc.AlreadyExistsException Java Examples

The following examples show how to use com.google.api.gax.rpc.AlreadyExistsException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PubSubChannelProvisioner.java    From spring-cloud-gcp with Apache License 2.0 6 votes vote down vote up
private Topic makeSureTopicExists(String topicName, boolean autoCreate) {
	Topic topic = this.pubSubAdmin.getTopic(topicName);
	if (topic == null) {
		if (autoCreate) {
			try {
				topic = this.pubSubAdmin.createTopic(topicName);
			}
			catch (AlreadyExistsException alreadyExistsException) {
				// Ignore concurrent topic creation - we're good as long as topic was created and exists
				LOGGER.info("Failed to auto-create topic '" + topicName + "' because it already exists.");
			}
		}
		else {
			throw new ProvisioningException("Non-existing '" + topicName + "' topic.");
		}
	}

	return topic;
}
 
Example #2
Source File: TableAdminExample.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
/** Demonstrates how to create a new instance of the DurationRule. */
public void addFamilyWithMaxAgeRule() {
  System.out.printf("%nCreating column family %s with max age GC rule%n", COLUMN_FAMILY_1);
  // [START bigtable_create_family_gc_max_age]
  // Creates a column family with GC policy : maximum age
  // where age = current time minus cell timestamp

  // Defines the GC rule to retain data with max age of 5 days.
  DurationRule maxAgeRule = GCRULES.maxAge(5, TimeUnit.DAYS);

  // Creates column family with given GC rule.
  try {
    // ModifyColumnFamiliesRequest can be used both for adding and modifying families, here it is
    // being used to add a family
    ModifyColumnFamiliesRequest columnFamiliesRequest =
        ModifyColumnFamiliesRequest.of(tableId).addFamily(COLUMN_FAMILY_1, maxAgeRule);
    adminClient.modifyFamilies(columnFamiliesRequest);
    System.out.println("Created column family: " + COLUMN_FAMILY_1);
  } catch (AlreadyExistsException e) {
    System.err.println(
        "Failed to create column family with rule, already exists: " + e.getMessage());
  }
  // [END bigtable_create_family_gc_max_age]
}
 
Example #3
Source File: TableAdminExample.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
/** Demonstrates how to create a new instance of the VersionRule. */
public void addFamilyWithMaxVersionsRule() {
  System.out.printf("%nCreating column family %s with max versions GC rule%n", COLUMN_FAMILY_2);
  // [START bigtable_create_family_gc_max_versions]
  // Creates a column family with GC policy : most recent N versions
  // where 1 = most recent version

  // Defines the GC policy to retain only the most recent 2 versions.
  VersionRule versionRule = GCRULES.maxVersions(2);

  // Creates column family with given GC rule.
  try {
    // ModifyColumnFamiliesRequest can be used both for adding and modifying families, here it is
    // being used to add a family
    ModifyColumnFamiliesRequest columnFamiliesRequest =
        ModifyColumnFamiliesRequest.of(tableId).addFamily(COLUMN_FAMILY_2, versionRule);
    adminClient.modifyFamilies(columnFamiliesRequest);
    System.out.println("Created column family: " + COLUMN_FAMILY_2);
  } catch (AlreadyExistsException e) {
    System.err.println(
        "Failed to create column family with rule, already exists: " + e.getMessage());
  }
  // [END bigtable_create_family_gc_max_versions]
}
 
Example #4
Source File: TableAdminExample.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
/** Demonstrates how to create a new instance of the UnionRule. */
public void addFamilyWithUnionRule() {
  System.out.printf("%nCreating column family %s with union GC rule%n", COLUMN_FAMILY_3);
  // [START bigtable_create_family_gc_union]
  // Creates a column family with GC policy to drop data that matches at least one condition.

  // Defines a list of GC rules to drop cells older than 5 days OR not the most recent
  // version.
  UnionRule unionRule =
      GCRULES.union().rule(GCRULES.maxAge(5, TimeUnit.DAYS)).rule(GCRULES.maxVersions(1));

  // Creates column family with given GC rule.
  try {
    // ModifyColumnFamiliesRequest can be used both for adding and modifying families, here it is
    // being used to add a family
    ModifyColumnFamiliesRequest columnFamiliesRequest =
        ModifyColumnFamiliesRequest.of(tableId).addFamily(COLUMN_FAMILY_3, unionRule);
    adminClient.modifyFamilies(columnFamiliesRequest);
    System.out.println("Created column family: " + COLUMN_FAMILY_3);
  } catch (AlreadyExistsException e) {
    System.err.println(
        "Failed to create column family with rule, already exists: " + e.getMessage());
  }
  // [END bigtable_create_family_gc_union]
}
 
Example #5
Source File: TableAdminExample.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
/** Demonstrates how to create a new instance of the IntersectionRule. */
public void addFamilyWithIntersectionRule() {
  System.out.printf("%nCreating column family %s with intersection GC rule%n", COLUMN_FAMILY_4);
  // [START bigtable_create_family_gc_intersection]
  // Creates a column family with GC policy to drop data that matches all conditions.

  // Defines a GC rule to drop cells older than 5 days AND older than the most recent 2 versions.
  DurationRule maxAgeRule = GCRULES.maxAge(5, TimeUnit.DAYS);
  VersionRule versionRule = GCRULES.maxVersions(2);
  IntersectionRule intersectionRule = GCRULES.intersection().rule(maxAgeRule).rule(versionRule);

  // Creates column family with given GC rule.
  try {
    // ModifyColumnFamiliesRequest can be used both for adding and modifying families, here it is
    // being used to add a family
    ModifyColumnFamiliesRequest columnFamiliesRequest =
        ModifyColumnFamiliesRequest.of(tableId).addFamily(COLUMN_FAMILY_4, intersectionRule);
    adminClient.modifyFamilies(columnFamiliesRequest);
    System.out.println("Created column family: " + COLUMN_FAMILY_4);
  } catch (AlreadyExistsException e) {
    System.err.println(
        "Failed to create column family with rule, already exists: " + e.getMessage());
  }
  // [END bigtable_create_family_gc_intersection]
}
 
Example #6
Source File: InstanceAdminExample.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
/** Demonstrates how to add a cluster to an instance. */
public void addCluster() {
  System.out.printf("%nAdding cluster: %s to instance: %s%n", CLUSTER, instanceId);
  // [START bigtable_create_cluster]
  try {
    adminClient.createCluster(
        CreateClusterRequest.of(instanceId, CLUSTER)
            .setZone("us-central1-c")
            .setServeNodes(3)
            .setStorageType(StorageType.SSD));
    System.out.printf("Cluster: %s created successfully%n", CLUSTER);
  } catch (AlreadyExistsException e) {
    System.err.println("Failed to add cluster, already exists: " + e.getMessage());
  }
  // [END bigtable_create_cluster]
}
 
Example #7
Source File: PubSubConnector.java    From smallrye-reactive-messaging with Apache License 2.0 5 votes vote down vote up
private void createTopic(final PubSubConfig config) {
    final TopicAdminClient topicAdminClient = pubSubManager.topicAdminClient(config);

    final ProjectTopicName topicName = ProjectTopicName.of(config.getProjectId(), config.getTopic());

    try {
        topicAdminClient.getTopic(topicName);
    } catch (final NotFoundException nf) {
        try {
            topicAdminClient.createTopic(topicName);
        } catch (final AlreadyExistsException ae) {
            log.topicExistAlready(topicName, ae);
        }
    }
}
 
Example #8
Source File: DataCatalogSchemaUtils.java    From DataflowTemplates with Apache License 2.0 5 votes vote down vote up
private void createEntryGroup() {
  if (this.entryGroupCreated) {
    return;
  }
  setupDataCatalogClient();
  EntryGroup entryGroup =
      EntryGroup.newBuilder()
          .setDisplayName(
              String.format("CDC_Debezium_on_Dataflow_%s", pubsubTopic))
          .setDescription("This EntryGroup represents a set of change streams from tables "
                              + "being replicated for CDC.")
          .build();

  // Construct the EntryGroup request to be sent by the client.
  CreateEntryGroupRequest entryGroupRequest =
      CreateEntryGroupRequest.newBuilder()
          .setParent(
              LocationName.of(getGcpProject(), location).toString())
          .setEntryGroupId(entryGroupNameForTopic(pubsubTopic))
          .setEntryGroup(entryGroup)
          .build();

  try {
    LOG.info("Creating EntryGroup {}", entryGroupRequest);
    EntryGroup createdEntryGroup = client.createEntryGroup(entryGroupRequest);
    LOG.info("Created EntryGroup: {}", createdEntryGroup.toString());

    this.entryGroupCreated = true;
  } catch (AlreadyExistsException e) {
    // EntryGroup already exists. There is no further action needed.
  }
}
 
Example #9
Source File: DataCatalogSchemaUtils.java    From DataflowTemplates with Apache License 2.0 5 votes vote down vote up
@Override
public Entry updateSchemaForTable(String tableName, Schema beamSchema) {
  LOG.debug("Converting Beam schema {} into a Data Catalog schema", beamSchema);
  com.google.cloud.datacatalog.v1beta1.Schema newEntrySchema =
      SchemaUtils.fromBeamSchema(beamSchema);
  LOG.info("Beam schema {} converted to Data Catalog schema {}", beamSchema, newEntrySchema);
  LOG.error("Entry group name: {}", EntryGroupName.of(
      getGcpProject(),
      location,
      entryGroupNameForTopic(pubsubTopic)).toString());

  CreateEntryRequest createEntryRequest =
      CreateEntryRequest.newBuilder()
          .setParent(
              EntryGroupName.of(
                  getGcpProject(),
                  location,
                  entryGroupNameForTopic(pubsubTopic)).toString())
          .setEntryId(sanitizeEntryName(tableName))
          .setEntry(
              Entry.newBuilder()
                  .setSchema(newEntrySchema)
                  .setDescription(tableName)
                  .setUserSpecifiedType("BEAM_ROW_DATA")
                  .setUserSpecifiedSystem("DATAFLOW_CDC_ON_DEBEZIUM_DATA")
                  .build())
          .build();

  LOG.info("CreateEntryRequest: {}", createEntryRequest.toString());

  try {
    return client.createEntry(createEntryRequest);
  } catch (AlreadyExistsException e) {
    // The Entry already exists. No further action is necessary.
    return createEntryRequest.getEntry();
  }
}
 
Example #10
Source File: PubSubChannelProvisionerTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Test
public void testProvisionConsumerDestination_concurrentTopicCreation() {
	when(this.pubSubAdminMock.createTopic(any())).thenThrow(AlreadyExistsException.class);
	when(this.pubSubAdminMock.getTopic(eq("already_existing_topic"))).thenReturn(null);

	// Ensure no exceptions occur if topic already exists on create call
	this.pubSubChannelProvisioner
			.provisionConsumerDestination("already_existing_topic", "group1", this.properties);
}
 
Example #11
Source File: Connection.java    From heroic with Apache License 2.0 5 votes vote down vote up
void createTopic() throws IOException {
    log.info("Creating topic {}", topicName);
    TopicAdminClient topicAdminClient = TopicAdminClient.create(
        TopicAdminSettings.newBuilder()
            .setTransportChannelProvider(channelProvider)
            .setCredentialsProvider(credentialsProvider)
            .build()
    );
    try {
        topicAdminClient.createTopic(topicName);
    } catch (AlreadyExistsException e) {
        log.info("Topic already exists");
    }
}
 
Example #12
Source File: Connection.java    From heroic with Apache License 2.0 5 votes vote down vote up
void createSubscription() throws IOException {
    log.info("Creating subscription {} for topic {}", subscriptionName, topicName);
    SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create(
        SubscriptionAdminSettings.newBuilder()
          .setTransportChannelProvider(channelProvider)
          .setCredentialsProvider(credentialsProvider)
          .build()
    );
    try {
        subscriptionAdminClient.createSubscription(
            subscriptionName, topicName, PushConfig.getDefaultInstance(), 0);
    } catch (AlreadyExistsException e) {
        log.info("Subscription already exists");
    }
}
 
Example #13
Source File: TableAdminExample.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
/** Demonstrates how to create a nested rule using the IntersectionRule and UnionRule. */
public void addFamilyWithNestedRule() {
  System.out.printf("%nCreating column family %s with a nested GC rule%n", COLUMN_FAMILY_5);
  // [START bigtable_create_family_gc_nested]
  // Creates a nested GC rule:
  // Drop cells that are either older than the 10 recent versions
  // OR
  // Drop cells that are older than a month AND older than the 2 recent versions
  VersionRule versionRule1 = GCRULES.maxVersions(10);
  VersionRule versionRule2 = GCRULES.maxVersions(2);
  DurationRule maxAgeRule = GCRULES.maxAge(30, TimeUnit.DAYS);
  IntersectionRule intersectionRule = GCRULES.intersection().rule(maxAgeRule).rule(versionRule2);
  UnionRule unionRule = GCRULES.union().rule(intersectionRule).rule(versionRule1);

  // Creates column family with given GC rule.
  try {
    // ModifyColumnFamiliesRequest can be used both for adding and modifying families, here it is
    // being used to add a family
    ModifyColumnFamiliesRequest columnFamiliesRequest =
        ModifyColumnFamiliesRequest.of(tableId).addFamily(COLUMN_FAMILY_5, unionRule);
    adminClient.modifyFamilies(columnFamiliesRequest);
    System.out.println("Created column family: " + COLUMN_FAMILY_5);
  } catch (AlreadyExistsException e) {
    System.err.println(
        "Failed to create column family with rule, already exists: " + e.getMessage());
  }
  // [END bigtable_create_family_gc_nested]
}
 
Example #14
Source File: CreateEntryGroup.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
public static void createEntryGroup(String projectId, String entryGroupId) {
  // Currently, Data Catalog stores metadata in the us-central1 region.
  String location = "us-central1";

  // 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 (DataCatalogClient dataCatalogClient = DataCatalogClient.create()) {
    // Construct the EntryGroup for the EntryGroup request.
    EntryGroup entryGroup =
        EntryGroup.newBuilder()
            .setDisplayName("My Fileset Entry Group")
            .setDescription("This Entry Group consists of ....")
            .build();

    // Construct the EntryGroup request to be sent by the client.
    CreateEntryGroupRequest entryGroupRequest =
        CreateEntryGroupRequest.newBuilder()
            .setParent(LocationName.of(projectId, location).toString())
            .setEntryGroupId(entryGroupId)
            .setEntryGroup(entryGroup)
            .build();

    // Use the client to send the API request.
    EntryGroup entryGroupResponse = dataCatalogClient.createEntryGroup(entryGroupRequest);
    System.out.printf("\nEntry Group created with name: %s\n", entryGroupResponse.getName());
  } catch (AlreadyExistsException | IOException e) {
    // AlreadyExistsException's are thrown if EntryGroup or Entry already exists.
    // IOException's are thrown when unable to create the DataCatalogClient,
    // for example an invalid Service Account path.
    System.out.println("Error in create entry process:\n" + e.toString());
  }
}
 
Example #15
Source File: CreateFilesetEntry.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
public static void createEntry(String projectId, String entryGroupId, String entryId) {
  // Currently, Data Catalog stores metadata in the us-central1 region.
  String location = "us-central1";

  // 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 (DataCatalogClient dataCatalogClient = DataCatalogClient.create()) {
    // Construct the Entry for the Entry request.
    Entry entry =
        Entry.newBuilder()
            .setDisplayName("My Fileset")
            .setDescription("This fileset consists of ....")
            .setGcsFilesetSpec(
                GcsFilesetSpec.newBuilder().addFilePatterns("gs://cloud-samples-data/*").build())
            .setSchema(
                Schema.newBuilder()
                    .addColumns(
                        ColumnSchema.newBuilder()
                            .setColumn("first_name")
                            .setDescription("First name")
                            .setMode("REQUIRED")
                            .setType("STRING")
                            .build())
                    .addColumns(
                        ColumnSchema.newBuilder()
                            .setColumn("last_name")
                            .setDescription("Last name")
                            .setMode("REQUIRED")
                            .setType("STRING")
                            .build())
                    .addColumns(
                        ColumnSchema.newBuilder()
                            .setColumn("addresses")
                            .setDescription("Addresses")
                            .setMode("REPEATED")
                            .setType("RECORD")
                            .addSubcolumns(
                                ColumnSchema.newBuilder()
                                    .setColumn("city")
                                    .setDescription("City")
                                    .setMode("NULLABLE")
                                    .setType("STRING")
                                    .build())
                            .addSubcolumns(
                                ColumnSchema.newBuilder()
                                    .setColumn("state")
                                    .setDescription("State")
                                    .setMode("NULLABLE")
                                    .setType("STRING")
                                    .build())
                            .build())
                    .build())
            .setType(EntryType.FILESET)
            .build();

    // Construct the Entry request to be sent by the client.
    CreateEntryRequest entryRequest =
        CreateEntryRequest.newBuilder()
            .setParent(EntryGroupName.of(projectId, location, entryGroupId).toString())
            .setEntryId(entryId)
            .setEntry(entry)
            .build();

    // Use the client to send the API request.
    Entry entryResponse = dataCatalogClient.createEntry(entryRequest);
    System.out.printf("\nEntry created with name: %s\n", entryResponse.getName());
  } catch (AlreadyExistsException | IOException e) {
    // AlreadyExistsException's are thrown if EntryGroup or Entry already exists.
    // IOException's are thrown when unable to create the DataCatalogClient,
    // for example an invalid Service Account path.
    System.out.println("Error in create entry process:\n" + e.toString());
  }
}