// Add a row to the connected table. func (c *Client) InsertRow(rowData map[string]interface{}) error { service, _ := c.getService() if service == nil || c.datasetId == "" || c.tableId == "" { return fmt.Errorf("table not setup to add rows") } jsonRows := make(map[string]bigquery.JsonValue) for key, value := range rowData { jsonRows[key] = bigquery.JsonValue(value) } rows := []*bigquery.TableDataInsertAllRequestRows{ { Json: jsonRows, }, } // TODO(jnagal): Batch insert requests. insertRequest := &bigquery.TableDataInsertAllRequest{Rows: rows} result, err := service.Tabledata.InsertAll(*projectId, c.datasetId, c.tableId, insertRequest).Do() if err != nil { return fmt.Errorf("error inserting row: %v", err) } if len(result.InsertErrors) > 0 { errstr := fmt.Sprintf("Insertion for %d rows failed\n", len(result.InsertErrors)) for _, errors := range result.InsertErrors { for _, errorproto := range errors.Errors { errstr += fmt.Sprintf("Error inserting row %d: %+v\n", errors.Index, errorproto) } } return fmt.Errorf(errstr) } return nil }
func rowToBigQueryJSON(row map[string]interface{}) map[string]bigquery.JsonValue { // convert to the custom type bigquery lib wants jsonData := make(map[string]bigquery.JsonValue) for k, v := range row { jsonData[k] = bigquery.JsonValue(v) } return jsonData }
func (q *queue) add(insertId string, value map[string]interface{}) error { // convert value to the BigQuery type entity := make(map[string]bq.JsonValue) for k, v := range value { entity[k] = bq.JsonValue(v) } row := &bq.TableDataInsertAllRequestRows{InsertId: insertId, Json: entity} return q.addRow(row) }
func (s *bigqueryService) insertRows(ctx context.Context, projectID, datasetID, tableID string, rows []*insertionRow, conf *insertRowsConf) error { req := &bq.TableDataInsertAllRequest{ TemplateSuffix: conf.templateSuffix, IgnoreUnknownValues: conf.ignoreUnknownValues, SkipInvalidRows: conf.skipInvalidRows, } for _, row := range rows { m := make(map[string]bq.JsonValue) for k, v := range row.Row { m[k] = bq.JsonValue(v) } req.Rows = append(req.Rows, &bq.TableDataInsertAllRequestRows{ InsertId: row.InsertID, Json: m, }) } var res *bq.TableDataInsertAllResponse err := runWithRetry(ctx, func() error { var err error res, err = s.s.Tabledata.InsertAll(projectID, datasetID, tableID, req).Context(ctx).Do() return err }) if err != nil { return err } if len(res.InsertErrors) == 0 { return nil } var errs PutMultiError for _, e := range res.InsertErrors { if int(e.Index) > len(rows) { return fmt.Errorf("internal error: unexpected row index: %v", e.Index) } rie := RowInsertionError{ InsertID: rows[e.Index].InsertID, RowIndex: int(e.Index), } for _, errp := range e.Errors { rie.Errors = append(rie.Errors, errorFromErrorProto(errp)) } errs = append(errs, rie) } return errs }