func TestCreateTable(t *testing.T) { testTableName := "test1_" + getTimestampString() t.Log("testTableName=" + testTableName) ac := gohbase.NewAdminClient(*host) crt := hrpc.NewCreateTable(context.Background(), []byte(testTableName), cFamilies) if err := ac.CreateTable(crt); err != nil { t.Errorf("CreateTable returned an error: %v", err) } // check in hbase:meta if there's a region for the table c := gohbase.NewClient(*host) metaKey := testTableName + ",," keyFilter := filter.NewPrefixFilter([]byte(metaKey)) scan, err := hrpc.NewScanStr(context.Background(), metaTableName, hrpc.Filters(keyFilter)) if err != nil { t.Fatalf("Failed to create Scan request: %s", err) } rsp, err := c.Scan(scan) if err != nil { t.Errorf("Scan returned an error: %v", err) } if len(rsp) != 1 { t.Errorf("Meta returned %s rows for prefix '%s' , want 1", len(rsp), metaKey) } }
func TestNewScan(t *testing.T) { ctx := context.Background() table := "test" tableb := []byte(table) fam := make(map[string][]string) fam["info"] = []string{"c1"} filter1 := filter.NewFirstKeyOnlyFilter() start := "0" stop := "100" startb := []byte("0") stopb := []byte("100") scan, err := hrpc.NewScan(ctx, tableb) if err != nil || !confirmScanAttributes(scan, ctx, tableb, nil, nil, nil, nil) { t.Errorf("Scan1 didn't set attributes correctly.") } scan, err = hrpc.NewScanRange(ctx, tableb, startb, stopb) if err != nil || !confirmScanAttributes(scan, ctx, tableb, startb, stopb, nil, nil) { t.Errorf("Scan2 didn't set attributes correctly.") } scan, err = hrpc.NewScanStr(ctx, table) if err != nil || !confirmScanAttributes(scan, ctx, tableb, nil, nil, nil, nil) { t.Errorf("Scan3 didn't set attributes correctly.") } scan, err = hrpc.NewScanRangeStr(ctx, table, start, stop) if err != nil || !confirmScanAttributes(scan, ctx, tableb, startb, stopb, nil, nil) { t.Errorf("Scan4 didn't set attributes correctly.") } scan, err = hrpc.NewScanRange(ctx, tableb, startb, stopb, hrpc.Families(fam), hrpc.Filters(filter1)) if err != nil || !confirmScanAttributes(scan, ctx, tableb, startb, stopb, fam, filter1) { t.Errorf("Scan5 didn't set attributes correctly.") } scan, err = hrpc.NewScan(ctx, tableb, hrpc.Filters(filter1), hrpc.Families(fam)) if err != nil || !confirmScanAttributes(scan, ctx, tableb, nil, nil, fam, filter1) { t.Errorf("Scan6 didn't set attributes correctly.") } }
func TestNewGet(t *testing.T) { ctx := context.Background() table := "test" tableb := []byte(table) key := "45" keyb := []byte(key) fam := make(map[string][]string) fam["info"] = []string{"c1"} filter1 := filter.NewFirstKeyOnlyFilter() get, err := hrpc.NewGet(ctx, tableb, keyb) if err != nil || !confirmGetAttributes(get, ctx, tableb, keyb, nil, nil) { t.Errorf("Get1 didn't set attributes correctly.") } get, err = hrpc.NewGetStr(ctx, table, key) if err != nil || !confirmGetAttributes(get, ctx, tableb, keyb, nil, nil) { t.Errorf("Get2 didn't set attributes correctly.") } get, err = hrpc.NewGet(ctx, tableb, keyb, hrpc.Families(fam)) if err != nil || !confirmGetAttributes(get, ctx, tableb, keyb, fam, nil) { t.Errorf("Get3 didn't set attributes correctly.") } get, err = hrpc.NewGet(ctx, tableb, keyb, hrpc.Filters(filter1)) if err != nil || !confirmGetAttributes(get, ctx, tableb, keyb, nil, filter1) { t.Errorf("Get4 didn't set attributes correctly.") } get, err = hrpc.NewGet(ctx, tableb, keyb, hrpc.Filters(filter1), hrpc.Families(fam)) if err != nil || !confirmGetAttributes(get, ctx, tableb, keyb, fam, filter1) { t.Errorf("Get5 didn't set attributes correctly.") } get, err = hrpc.NewGet(ctx, tableb, keyb, hrpc.Filters(filter1)) err = hrpc.Families(fam)(get) if err != nil || !confirmGetAttributes(get, ctx, tableb, keyb, fam, filter1) { t.Errorf("Get6 didn't set attributes correctly.") } }
// Scan retrieves the values specified in families from the given range. func (c *Client) Scan(s *hrpc.Scan) ([]*hrpc.Result, error) { var results []*pb.Result var scanres *pb.ScanResponse var rpc *hrpc.Scan ctx := s.GetContext() table := s.Table() families := s.GetFamilies() filters := s.GetFilter() startRow := s.GetStartRow() stopRow := s.GetStopRow() for { // Make a new Scan RPC for this region if rpc != nil { // If it's not the first region, we want to start at whatever the // last region's StopKey was startRow = rpc.GetRegionStop() } rpc, err := hrpc.NewScanRange(ctx, table, startRow, stopRow, hrpc.Families(families), hrpc.Filters(filters)) if err != nil { return nil, err } res, err := c.sendRPC(rpc) if err != nil { return nil, err } scanres = res.(*pb.ScanResponse) results = append(results, scanres.Results...) // TODO: The more_results field of the ScanResponse object was always // true, so we should figure out if there's a better way to know when // to move on to the next region than making an extra request and // seeing if there were no results for len(scanres.Results) != 0 { rpc = hrpc.NewScanFromID(ctx, table, *scanres.ScannerId, rpc.Key()) res, err = c.sendRPC(rpc) if err != nil { return nil, err } scanres = res.(*pb.ScanResponse) results = append(results, scanres.Results...) } rpc = hrpc.NewCloseFromID(ctx, table, *scanres.ScannerId, rpc.Key()) if err != nil { return nil, err } res, err = c.sendRPC(rpc) // Check to see if this region is the last we should scan (either // because (1) it's the last region or (3) because its stop_key is // greater than or equal to the stop_key of this scanner provided // that (2) we're not trying to scan until the end of the table). // (1) if len(rpc.GetRegionStop()) == 0 || // (2) (3) len(stopRow) != 0 && bytes.Compare(stopRow, rpc.GetRegionStop()) <= 0 { // Do we want to be returning a slice of Result objects or should we just // put all the Cells into the same Result object? localResults := make([]*hrpc.Result, len(results)) for idx, result := range results { localResults[idx] = hrpc.ToLocalResult(result) } return localResults, nil } } }