import (
"context"
"fmt"
"io"
"time"
admin "cloud.google.com/go/bigtable/admin/apiv2"
"cloud.google.com/go/bigtable/admin/apiv2/adminpb"
"google.golang.org/protobuf/types/known/durationpb"
)
func createFamilyGCNested(w io.Writer, projectID, instanceID string, tableName string) error {
// projectID := "my-project-id"
// instanceID := "my-instance-id"
// tableName := "my-table-name"
ctx := context.Background()
adminClient, err := admin.NewBigtableTableAdminClient(ctx)
if err != nil {
return fmt.Errorf("admin.NewBigtableTableAdminClient: %w", err)
}
defer adminClient.Close()
// Create 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
maxAge := time.Hour * 24 * 5
policy := &adminpb.GcRule{
Rule: &adminpb.GcRule_Union_{
Union: &adminpb.GcRule_Union{
Rules: []*adminpb.GcRule{
{
Rule: &adminpb.GcRule_MaxNumVersions{
MaxNumVersions: 10,
},
},
{
Rule: &adminpb.GcRule_Intersection_{
Intersection: &adminpb.GcRule_Intersection{
Rules: []*adminpb.GcRule{
{
Rule: &adminpb.GcRule_MaxNumVersions{
MaxNumVersions: 2,
},
},
{
Rule: &adminpb.GcRule_MaxAge{
MaxAge: durationpb.New(maxAge),
},
},
},
},
},
},
},
},
},
}
columnFamilyName := "cf5"
req := &adminpb.ModifyColumnFamiliesRequest{
Name: fmt.Sprintf("projects/%s/instances/%s/tables/%s", projectID, instanceID, tableName),
Modifications: []*adminpb.ModifyColumnFamiliesRequest_Modification{
{
Id: columnFamilyName,
Mod: &adminpb.ModifyColumnFamiliesRequest_Modification_Create{
Create: &adminpb.ColumnFamily{
GcRule: policy,
},
},
},
},
}
if _, err := adminClient.ModifyColumnFamilies(ctx, req); err != nil {
return fmt.Errorf("ModifyColumnFamilies(%s): %w", columnFamilyName, err)
}
fmt.Fprintf(w, "created column family %s with policy: %v\n", columnFamilyName, policy)
return nil
}