You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by "jrmccluskey (via GitHub)" <gi...@apache.org> on 2023/08/01 19:36:52 UTC

[GitHub] [beam] jrmccluskey commented on a diff in pull request #27778: Implement go xlang wrapper for reading from Bigtable

jrmccluskey commented on code in PR #27778:
URL: https://github.com/apache/beam/pull/27778#discussion_r1281068872


##########
sdks/go/test/integration/io/xlang/bigtable/bigtable_test.go:
##########
@@ -0,0 +1,177 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License.  You may obtain a copy of the License at
+//
+//	http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package bigtable
+
+import (
+	"context"
+	"flag"
+	"fmt"
+	"log"
+	"regexp"
+	"strconv"
+	"testing"
+	"time"
+
+	bt "cloud.google.com/go/bigtable"
+	"github.com/apache/beam/sdks/v2/go/pkg/beam"
+	"github.com/apache/beam/sdks/v2/go/pkg/beam/io/bigtableio"
+	xlangbt "github.com/apache/beam/sdks/v2/go/pkg/beam/io/xlang/bigtableio"
+	"github.com/apache/beam/sdks/v2/go/pkg/beam/register"
+	_ "github.com/apache/beam/sdks/v2/go/pkg/beam/runners/dataflow"
+	"github.com/apache/beam/sdks/v2/go/pkg/beam/testing/passert"
+	"github.com/apache/beam/sdks/v2/go/pkg/beam/testing/ptest"
+	"github.com/apache/beam/sdks/v2/go/test/integration"
+)
+
+var expansionAddr string
+
+func init() {
+	beam.RegisterDoFn(&CreateTestRowsFn{})
+	beam.RegisterDoFn(&RowToMutationFn{})
+
+	register.DoFn2x0[[]byte, func(xlangbt.Row)](&CreateTestRowsFn{})
+	register.DoFn2x0[xlangbt.Row, func(mutation bigtableio.Mutation)](&RowToMutationFn{})
+	register.Emitter1[bigtableio.Mutation]()
+	register.Emitter1[xlangbt.Row]()
+}
+
+type CreateTestRowsFn struct {
+}
+
+type RowToMutationFn struct {
+}
+
+func (fn *RowToMutationFn) ProcessElement(r xlangbt.Row, emit func(mutation bigtableio.Mutation)) {
+	mut := bigtableio.NewMutation(string(r.Key))
+	for cf, cols := range r.Column_families {
+		for col, vals := range cols {
+			for _, val := range vals {
+				mut.Set(cf, col, bt.Timestamp(val.Timestamp_micros), val.Value)
+			}
+		}
+	}
+	emit(*mut)
+}
+
+func createTestRows() []xlangbt.Row {
+	rows := make([]xlangbt.Row, 100)
+
+	for i := 0; i < 100; i++ {
+		key := strconv.FormatInt(int64(i), 10)
+		row := xlangbt.Row{
+			Key: []byte(key),
+		}
+		row.AddCell("cf1", "q1", []byte(strconv.FormatInt(int64(i)*1000, 10)), int64(i)*1000)
+
+		rows[i] = row
+	}
+	return rows
+}
+
+func (fn *CreateTestRowsFn) ProcessElement(_ []byte, emit func(row xlangbt.Row)) {
+	for _, r := range createTestRows() {
+		emit(r)
+	}
+}
+
+func checkFlags(t *testing.T) {
+	if *integration.BigtableInstance == "" {
+		t.Skip("No Bigtable instance provided.")
+	}
+}
+
+func WritePipeline(project, instance, table string) *beam.Pipeline {
+	p := beam.NewPipeline()
+	s := p.Root()
+
+	rows := beam.ParDo(s, &CreateTestRowsFn{}, beam.Impulse(s))
+	muts := beam.ParDo(s, &RowToMutationFn{}, rows)
+	bigtableio.Write(s, project, instance, table, muts)
+	return p
+}
+
+func ReadPipeline(project, instance, table, expansionAddr string) *beam.Pipeline {
+	p := beam.NewPipeline()
+	s := p.Root()
+
+	rowsFromBt := xlangbt.Read(s, project, instance, table, xlangbt.ReadExpansionAddr(expansionAddr))
+
+	rows := beam.ParDo(s, &CreateTestRowsFn{}, beam.Impulse(s))
+
+	passert.Equals(s, rowsFromBt, rows)
+	return p
+}
+
+var instanceRegex = regexp.MustCompile("projects/(?P<project>[^/]+)/instances/(?P<instance>.+)")
+
+func createTempTable(admin *bt.AdminClient) string {
+	tableName := fmt.Sprintf("go_btio_it_temp_%v", time.Now().UnixNano())
+	err := admin.CreateTableFromConf(context.Background(), &bt.TableConf{
+		TableID: tableName,
+		Families: map[string]bt.GCPolicy{
+			"cf1": bt.NoGcPolicy(),
+		},
+	})
+	if err != nil {
+		panic(err)
+	}
+
+	return tableName
+}
+
+func deleteTempTable(admin *bt.AdminClient, table string) {
+	_ = admin.DeleteTable(context.Background(), table)
+}
+
+func TestBigtableIO_BasicWriteRead(t *testing.T) {
+	integration.CheckFilters(t)
+	checkFlags(t)
+
+	instancePath := *integration.BigtableInstance
+	matches := instanceRegex.FindStringSubmatch(instancePath)
+	project := matches[1]
+	instance := matches[2]
+
+	admin, err := bt.NewAdminClient(context.Background(), project, instance)
+	if err != nil {
+		panic(err)
+	}
+
+	table := createTempTable(admin)
+	defer deleteTempTable(admin, table)

Review Comment:
   Prefer t.Cleanup over defer in test cases 
   
   https://pkg.go.dev/testing#T.Cleanup



##########
sdks/go/pkg/beam/core/runtime/xlangx/payload.go:
##########
@@ -26,37 +26,49 @@ import (
 	"google.golang.org/protobuf/proto"
 )
 
-// EncodeStructPayload takes a native Go struct and returns a marshaled
-// ExternalConfigurationPayload proto, containing a Schema representation of
-// the original type and the original value encoded as a Row. This is intended
-// to be used as the expansion payload for an External transform.
-func EncodeStructPayload(pl any) ([]byte, error) {
+// CreateExternalConfigurationPayload takes a native Go struct and returns an
+// ExternalConfigurationPayload proto with the struct encoded as a Row and its
+// associated schema.
+func CreateExternalConfigurationPayload(pl any) (*pipepb.ExternalConfigurationPayload, error) {
 	rt := reflect.TypeOf(pl)
 
 	// Encode payload value as a Row.
 	enc, err := coder.RowEncoderForStruct(rt)
 	if err != nil {
 		err = errors.WithContext(err, "creating Row encoder for payload")
-		return []byte{}, errors.WithContextf(err, "encoding external payload %v", pl)
+		return nil, errors.WithContextf(err, "encoding external payload %v", pl)
 	}
 	var buf bytes.Buffer
 	if err := enc(pl, &buf); err != nil {
 		err = errors.WithContext(err, "encoding payload as Row")
-		return []byte{}, errors.WithContextf(err, "encoding external payload %v", pl)
+		return nil, errors.WithContextf(err, "encoding external payload %v", pl)
 	}
 
 	// Convert payload type into Schema representation.
 	scm, err := schema.FromType(rt)
 	if err != nil {
 		err = errors.WithContext(err, "creating schema for payload")
-		return []byte{}, errors.WithContextf(err, "encoding external payload %v", pl)
+		return nil, errors.WithContextf(err, "encoding external payload %v", pl)
 	}
 
 	// Put schema and row into payload proto, and marshal it.
 	ecp := &pipepb.ExternalConfigurationPayload{
 		Schema:  scm,
 		Payload: buf.Bytes(),
 	}
+	return ecp, nil
+}
+
+// EncodeStructPayload takes a native Go struct and returns a marshaled
+// ExternalConfigurationPayload proto, containing a Schema representation of
+// the original type and the original value encoded as a Row. This is intended
+// to be used as the expansion payload for an External transform.
+func EncodeStructPayload(pl any) ([]byte, error) {
+	ecp, err := CreateExternalConfigurationPayload(pl)
+	if err != nil {
+		return []byte{}, nil

Review Comment:
   ```suggestion
   		return []byte{}, err
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org