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

[GitHub] [beam] johannaojeling commented on issue #28446: Postgres not connecting in apache beam with go sdk[Bug]:

johannaojeling commented on issue #28446:
URL: https://github.com/apache/beam/issues/28446#issuecomment-1722209708

   @sroutweave the native `databaseio` connector uses the `database/sql` package, which requires a [supported driver](https://github.com/golang/go/wiki/SQLDrivers) to be registered. For Postgres, you could use for instance [pq](https://github.com/lib/pq) or [pgx](https://github.com/jackc/pgx).
   
   In your Go program, you will need to add a blank import to the selected driver package in order for its driver to get registered. The pq driver will be registered under the name `postgres` and pgx under `pgx`. You will need to pass the same name as the driver argument to the `databaseio.Read` function, instead of the JDBC name `org.postgresql.Driver` for the correct driver to be picked up.
   
   Example program:
   
   ```go
   package main
   
   import (
   	"context"
   	"flag"
   	"log"
   	"reflect"
   
   	"github.com/apache/beam/sdks/v2/go/pkg/beam"
   	"github.com/apache/beam/sdks/v2/go/pkg/beam/io/databaseio"
   	"github.com/apache/beam/sdks/v2/go/pkg/beam/x/beamx"
   	"github.com/apache/beam/sdks/v2/go/pkg/beam/x/debug"
   	_ "github.com/jackc/pgx/v5/stdlib" // driver: "pgx"
   	//_ "github.com/lib/pq" // driver: "postgres"
   )
   
   var (
   	driver = flag.String("driver", "pgx", "Database driver")
   	dsn    = flag.String("dsn", "host=localhost port=5432 user=postgres password=pwd dbname=postgres sslmode=disable", "Database DSN")
   	table  = flag.String("table", "users", "Database table")
   )
   
   type User struct {
   	ID    string
   	Name  string
   	Email string
   }
   
   func main() {
   	flag.Parse()
   
   	beam.Init()
   	p, s := beam.NewPipelineWithRoot()
   
   	rows := databaseio.Read(s, *driver, *dsn, *table, reflect.TypeOf(User{}))
   	debug.Print(s, rows)
   
   	ctx := context.Background()
   	if err := beamx.Run(ctx, p); err != nil {
   		log.Fatalf("Failed to execute job: %v", 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