Documentation ¶
Overview ¶
Package repository provides the Repository struct that is used to read data coming from Big Table when a mapping is defined.
Index ¶
- type Adapter
- type DebugAdapter
- func (a DebugAdapter) ApplyBulk(ctx context.Context, rowKeys []string, muts []*bigtable.Mutation, ...) (errs []error, err error)
- func (a DebugAdapter) ReadRow(ctx context.Context, row string, opts ...bigtable.ReadOption) (bigtable.Row, error)
- func (a DebugAdapter) ReadRows(ctx context.Context, arg bigtable.RowSet, f func(bigtable.Row) bool, ...) (err error)
- type DebugAdapterOption
- type MaxRowsOption
- type Option
- type Repository
- func (r *Repository) Read(ctx context.Context, key string) (*data.Set, error)
- func (r *Repository) ReadFamily(ctx context.Context, key string, family string) (*data.Set, error)
- func (r *Repository) ReadLast(ctx context.Context, key string) (*data.Set, error)
- func (r *Repository) Search(ctx context.Context, rowSet bigtable.RowSet, filter bigtable.Filter) (*data.Set, error)
- func (r *Repository) Write(ctx context.Context, eventSet *data.Set) ([]error, error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Adapter ¶
type Adapter interface { ReadRow(ctx context.Context, row string, opts ...bigtable.ReadOption) (bigtable.Row, error) ReadRows(ctx context.Context, arg bigtable.RowSet, f func(bigtable.Row) bool, opts ...bigtable.ReadOption) (err error) ApplyBulk(ctx context.Context, rowKeys []string, muts []*bigtable.Mutation, opts ...bigtable.ApplyOption) (errs []error, err error) }
Adapter acts as a proxy between the repository and the actual data source. It allows to easily mock the data source in tests.
type DebugAdapter ¶
type DebugAdapter struct {
// contains filtered or unexported fields
}
func (DebugAdapter) ApplyBulk ¶
func (a DebugAdapter) ApplyBulk(ctx context.Context, rowKeys []string, muts []*bigtable.Mutation, opts ...bigtable.ApplyOption) (errs []error, err error)
type DebugAdapterOption ¶
type DebugAdapterOption struct {
// contains filtered or unexported fields
}
func NewDebugAdapterOption ¶
func NewDebugAdapterOption(w io.Writer) *DebugAdapterOption
type MaxRowsOption ¶
type MaxRowsOption struct {
// contains filtered or unexported fields
}
type Repository ¶
type Repository struct {
// contains filtered or unexported fields
}
func NewRepository ¶
NewRepository creates a new Repository for the given table.
func (*Repository) Read ¶
Read a row from the repository and map it to a data.Set
This method takes a row key as an argument, uses its internal adapter to read the row from Big Table, parses all cells contained in the row to turn it into a map of data.Event and finally returns the data.Set that contains all the events.
Example ¶
ctx := context.Background() client := getBigTableClient(ctx) c, err := fs.ReadFile("testdata/mapping.json") if err != nil { log.Fatalln(err) } jsonMapping, err := mapping.LoadMapping(c) if err != nil { log.Fatalln(err) } mapper := mapping.NewMapper(jsonMapping) tbl := client.Open(table) repo := NewRepository(tbl, mapper) eventSet, err := repo.Read(ctx, "contact-3") if err != nil { log.Fatalln(err) } fmt.Println(eventSet.Events["front"][0].Cells["event_type"])
Output: page_view
func (*Repository) ReadFamily ¶ added in v1.1.0
ReadFamily reads a row from the repository keeping only the desired column family and map it to a data.Set
This method takes a row key and the column family as an argument, uses its internal adapter to read the row from Big Table, parses all cells contained in the row to turn it into a map of data.Event and finally returns the data.Set that contains all the events.
Be careful, this method will perform an exact match on the column family name.
Example ¶
ctx := context.Background() client := getBigTableClient(ctx) c, err := fs.ReadFile("testdata/mapping.json") if err != nil { log.Fatalln(err) } jsonMapping, err := mapping.LoadMapping(c) if err != nil { log.Fatalln(err) } mapper := mapping.NewMapper(jsonMapping) tbl := client.Open(table) repo := NewRepository(tbl, mapper) eventSet := &data.Set{Events: map[string][]*data.Event{ "front": { { RowKey: "contactz-102", Date: time.Date(2018, time.January, 1, 0, 2, 0, 0, time.UTC), Cells: map[string]string{ "event_type": "add_to_cart", "device_type": "Computer", "url": "https://example.org/some/product", }, }, }, "blog": { { RowKey: "contactz-102", Date: time.Date(2018, time.January, 1, 0, 2, 0, 0, time.UTC), Cells: map[string]string{ "event_type": "page_view", "device_type": "Computer", "url": "https://example.org/blog/article/1", }, }, }, }} // insert errs, err := repo.Write(ctx, eventSet) if err != nil { log.Fatalln(err) } if len(errs) > 0 { log.Fatalln(errs) } readSet, err := repo.ReadFamily(ctx, "contactz-102", "blog") if err != nil { log.Fatalln(err) } for _, event := range readSet.Events["blog"] { fmt.Println(event.Cells["event_type"]) fmt.Println(event.Cells["device_type"]) }
Output: page_view Computer
func (*Repository) ReadLast ¶ added in v1.1.0
ReadLast reads a row from the repository while returning only the latest cell values after mapping it to a data.Set. This method takes a row key as an argument, uses its internal adapter to read the row from Big Table, parses only the latest cells contained in the row to turn it into a map of data.Event and finally returns the data.Set that contains all the events.
Example ¶
ctx := context.Background() client := getBigTableClient(ctx) c, err := fs.ReadFile("testdata/mapping.json") if err != nil { log.Fatalln(err) } jsonMapping, err := mapping.LoadMapping(c) if err != nil { log.Fatalln(err) } mapper := mapping.NewMapper(jsonMapping) tbl := client.Open(table) repo := NewRepository(tbl, mapper) eventSet := &data.Set{Events: map[string][]*data.Event{ "front": { { RowKey: "contactz-102", Date: time.Date(2018, time.January, 1, 0, 2, 0, 0, time.UTC), Cells: map[string]string{ "event_type": "add_to_cart", "device_type": "Computer", "url": "https://example.org/some/product", }, }, }, }} // insert errs, err := repo.Write(ctx, eventSet) if err != nil { log.Fatalln(err) } if len(errs) > 0 { log.Fatalln(errs) } // update eventSet = &data.Set{Events: map[string][]*data.Event{ "front": { { RowKey: "contactz-102", Date: time.Now(), Cells: map[string]string{ "event_type": "purchase", "device_type": "Smartphone", "url": "https://example.org/some/cart", }, }, }, }} errs, err = repo.Write(ctx, eventSet) if err != nil { log.Fatalln(err) } if len(errs) > 0 { log.Fatalln(errs) } readSet, err := repo.ReadLast(ctx, "contactz-102") if err != nil { log.Fatalln(err) } for _, event := range readSet.Events["front"] { fmt.Println(event.Cells["event_type"]) fmt.Println(event.Cells["device_type"]) }
Output: purchase Smartphone
func (*Repository) Search ¶
func (r *Repository) Search(ctx context.Context, rowSet bigtable.RowSet, filter bigtable.Filter) (*data.Set, error)
Search for rows in the repository that match the given filter and return the according data.Set
Example ¶
ctx := context.Background() client := getBigTableClient(ctx) c, err := fs.ReadFile("testdata/mapping.json") if err != nil { log.Fatalln(err) } jsonMapping, err := mapping.LoadMapping(c) if err != nil { log.Fatalln(err) } mapper := mapping.NewMapper(jsonMapping) tbl := client.Open(table) repo := NewRepository(tbl, mapper) eventSet := &data.Set{Events: map[string][]*data.Event{ "front": { { RowKey: "contactx-101", Date: time.Date(2018, time.January, 1, 0, 0, 0, 0, time.UTC), Cells: map[string]string{ "event_type": "page_view", "device_type": "Smartphone", "url": "https://example.org/some/product", }, }, { RowKey: "contactx-101", Date: time.Date(2018, time.January, 1, 0, 1, 0, 0, time.UTC), Cells: map[string]string{ "event_type": "add_to_cart", "device_type": "Smartphone", "url": "https://example.org/some/product", }, }, { RowKey: "contactx-101", Date: time.Date(2018, time.January, 1, 0, 2, 0, 0, time.UTC), Cells: map[string]string{ "event_type": "purchase", "device_type": "Smartphone", "url": "https://example.org/some/product", }, }, { RowKey: "contactx-102", Date: time.Date(2018, time.January, 1, 0, 2, 0, 0, time.UTC), Cells: map[string]string{ "event_type": "add_to_cart", "device_type": "Computer", "url": "https://example.org/some/product", }, }, { RowKey: "contacty-102", Date: time.Date(2018, time.January, 1, 0, 2, 0, 0, time.UTC), Cells: map[string]string{ "event_type": "add_to_cart", "device_type": "Computer", "url": "https://example.org/some/product", }, }, }, }} errs, err := repo.Write(ctx, eventSet) if err != nil { log.Fatalln(err) } if len(errs) > 0 { log.Fatalln(errs) } readSet, err := repo.Search(ctx, bigtable.PrefixRange("contactx"), bigtable.CellsPerRowLimitFilter(1)) if err != nil { log.Fatalln(err) } events := sortByContactID(readSet.Events["front"]) for _, event := range events { fmt.Println(event.Date.UTC()) fmt.Println(event.RowKey) fmt.Println(event.Cells["event_type"]) fmt.Println(event.Cells["device_type"]) }
Output: 2018-01-01 00:02:00 +0000 UTC contactx-101 purchase Smartphone 2018-01-01 00:02:00 +0000 UTC contactx-102 add_to_cart Computer
func (*Repository) Write ¶
Example ¶
ctx := context.Background() client := getBigTableClient(ctx) c, err := fs.ReadFile("testdata/mapping.json") if err != nil { log.Fatalln(err) } jsonMapping, err := mapping.LoadMapping(c) if err != nil { log.Fatalln(err) } mapper := mapping.NewMapper(jsonMapping) tbl := client.Open(table) repo := NewRepository(tbl, mapper) eventSet := &data.Set{Events: map[string][]*data.Event{ "front": { { RowKey: "contact-101", Date: time.Date(2018, time.January, 1, 0, 0, 0, 0, time.UTC), Cells: map[string]string{ "event_type": "page_view", "device_type": "Smartphone", "url": "https://example.org/some/product", }, }, { RowKey: "contact-101", Date: time.Date(2018, time.January, 1, 0, 1, 0, 0, time.UTC), Cells: map[string]string{ "event_type": "add_to_cart", "device_type": "Smartphone", "url": "https://example.org/some/product", }, }, { RowKey: "contact-101", Date: time.Date(2018, time.January, 1, 0, 2, 0, 0, time.UTC), Cells: map[string]string{ "event_type": "purchase", "device_type": "Smartphone", "url": "https://example.org/some/product", }, }, { RowKey: "contact-102", Date: time.Date(2018, time.January, 1, 0, 2, 0, 0, time.UTC), Cells: map[string]string{ "event_type": "add_to_cart", "device_type": "Computer", "url": "https://example.org/some/product", }, }, }, }} errs, err := repo.Write(ctx, eventSet) if err != nil { log.Fatalln(err) } if len(errs) > 0 { log.Fatalln(errs) } row, err := tbl.ReadRow(ctx, "contact-101") if err != nil { log.Fatalln(err) } fmt.Println("cells-for_contact-101") for _, family := range row { for _, cell := range family { fmt.Println(cell.Column, string(cell.Value)) } } fmt.Println("cells-for_contact-102") row, err = tbl.ReadRow(ctx, "contact-102") if err != nil { log.Fatalln(err) } for _, family := range row { for _, cell := range family { fmt.Println(cell.Column, string(cell.Value)) } } readSet, err := repo.Read(ctx, "contact-102") if err != nil { log.Fatalln(err) } fmt.Println("mapped-event-for_contact-102") for _, event := range readSet.Events["front"] { fmt.Println(event.Date.UTC()) fmt.Println(event.RowKey) fmt.Println(event.Cells["event_type"]) fmt.Println(event.Cells["device_type"]) }
Output: cells-for_contact-101 front:d 1 front:d 1 front:d 1 front:e 13 front:e 12 front:e 11 front:u https://example.org/some/product front:u https://example.org/some/product front:u https://example.org/some/product cells-for_contact-102 front:d 2 front:e 12 front:u https://example.org/some/product mapped-event-for_contact-102 2018-01-01 00:02:00 +0000 UTC contact-102 add_to_cart Computer