1// Copyright 2016 Google LLC 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15package bigquery_test 16 17import ( 18 "context" 19 "fmt" 20 "os" 21 "time" 22 23 "cloud.google.com/go/bigquery" 24 "google.golang.org/api/iterator" 25) 26 27func ExampleNewClient() { 28 ctx := context.Background() 29 client, err := bigquery.NewClient(ctx, "project-id") 30 if err != nil { 31 // TODO: Handle error. 32 } 33 _ = client // TODO: Use client. 34} 35 36func ExampleClient_Dataset() { 37 ctx := context.Background() 38 client, err := bigquery.NewClient(ctx, "project-id") 39 if err != nil { 40 // TODO: Handle error. 41 } 42 ds := client.Dataset("my_dataset") 43 fmt.Println(ds) 44} 45 46func ExampleClient_DatasetInProject() { 47 ctx := context.Background() 48 client, err := bigquery.NewClient(ctx, "project-id") 49 if err != nil { 50 // TODO: Handle error. 51 } 52 ds := client.DatasetInProject("their-project-id", "their-dataset") 53 fmt.Println(ds) 54} 55 56func ExampleClient_Datasets() { 57 ctx := context.Background() 58 client, err := bigquery.NewClient(ctx, "project-id") 59 if err != nil { 60 // TODO: Handle error. 61 } 62 it := client.Datasets(ctx) 63 _ = it // TODO: iterate using Next or iterator.Pager. 64} 65 66func ExampleClient_DatasetsInProject() { 67 ctx := context.Background() 68 client, err := bigquery.NewClient(ctx, "project-id") 69 if err != nil { 70 // TODO: Handle error. 71 } 72 it := client.DatasetsInProject(ctx, "their-project-id") 73 _ = it // TODO: iterate using Next or iterator.Pager. 74} 75 76func getJobID() string { return "" } 77 78func ExampleClient_JobFromID() { 79 ctx := context.Background() 80 client, err := bigquery.NewClient(ctx, "project-id") 81 if err != nil { 82 // TODO: Handle error. 83 } 84 jobID := getJobID() // Get a job ID using Job.ID, the console or elsewhere. 85 job, err := client.JobFromID(ctx, jobID) 86 if err != nil { 87 // TODO: Handle error. 88 } 89 fmt.Println(job.LastStatus()) // Display the job's status. 90} 91 92func ExampleClient_Jobs() { 93 ctx := context.Background() 94 client, err := bigquery.NewClient(ctx, "project-id") 95 if err != nil { 96 // TODO: Handle error. 97 } 98 it := client.Jobs(ctx) 99 it.State = bigquery.Running // list only running jobs. 100 _ = it // TODO: iterate using Next or iterator.Pager. 101} 102 103func ExampleNewGCSReference() { 104 gcsRef := bigquery.NewGCSReference("gs://my-bucket/my-object") 105 fmt.Println(gcsRef) 106} 107 108func ExampleClient_Query() { 109 ctx := context.Background() 110 client, err := bigquery.NewClient(ctx, "project-id") 111 if err != nil { 112 // TODO: Handle error. 113 } 114 q := client.Query("select name, num from t1") 115 q.DefaultProjectID = "project-id" 116 // TODO: set other options on the Query. 117 // TODO: Call Query.Run or Query.Read. 118} 119 120func ExampleClient_Query_parameters() { 121 ctx := context.Background() 122 client, err := bigquery.NewClient(ctx, "project-id") 123 if err != nil { 124 // TODO: Handle error. 125 } 126 q := client.Query("select num from t1 where name = @user") 127 q.Parameters = []bigquery.QueryParameter{ 128 {Name: "user", Value: "Elizabeth"}, 129 } 130 // TODO: set other options on the Query. 131 // TODO: Call Query.Run or Query.Read. 132} 133 134// This example demonstrates how to run a query job on a table 135// with a customer-managed encryption key. The same 136// applies to load and copy jobs as well. 137func ExampleClient_Query_encryptionKey() { 138 ctx := context.Background() 139 client, err := bigquery.NewClient(ctx, "project-id") 140 if err != nil { 141 // TODO: Handle error. 142 } 143 q := client.Query("select name, num from t1") 144 // TODO: Replace this key with a key you have created in Cloud KMS. 145 keyName := "projects/P/locations/L/keyRings/R/cryptoKeys/K" 146 q.DestinationEncryptionConfig = &bigquery.EncryptionConfig{KMSKeyName: keyName} 147 // TODO: set other options on the Query. 148 // TODO: Call Query.Run or Query.Read. 149} 150 151func ExampleQuery_Read() { 152 ctx := context.Background() 153 client, err := bigquery.NewClient(ctx, "project-id") 154 if err != nil { 155 // TODO: Handle error. 156 } 157 q := client.Query("select name, num from t1") 158 it, err := q.Read(ctx) 159 if err != nil { 160 // TODO: Handle error. 161 } 162 _ = it // TODO: iterate using Next or iterator.Pager. 163} 164 165func ExampleRowIterator_Next() { 166 ctx := context.Background() 167 client, err := bigquery.NewClient(ctx, "project-id") 168 if err != nil { 169 // TODO: Handle error. 170 } 171 q := client.Query("select name, num from t1") 172 it, err := q.Read(ctx) 173 if err != nil { 174 // TODO: Handle error. 175 } 176 for { 177 var row []bigquery.Value 178 err := it.Next(&row) 179 if err == iterator.Done { 180 break 181 } 182 if err != nil { 183 // TODO: Handle error. 184 } 185 fmt.Println(row) 186 } 187} 188 189func ExampleRowIterator_Next_struct() { 190 ctx := context.Background() 191 client, err := bigquery.NewClient(ctx, "project-id") 192 if err != nil { 193 // TODO: Handle error. 194 } 195 196 type score struct { 197 Name string 198 Num int 199 } 200 201 q := client.Query("select name, num from t1") 202 it, err := q.Read(ctx) 203 if err != nil { 204 // TODO: Handle error. 205 } 206 for { 207 var s score 208 err := it.Next(&s) 209 if err == iterator.Done { 210 break 211 } 212 if err != nil { 213 // TODO: Handle error. 214 } 215 fmt.Println(s) 216 } 217} 218 219func ExampleJob_Read() { 220 ctx := context.Background() 221 client, err := bigquery.NewClient(ctx, "project-id") 222 if err != nil { 223 // TODO: Handle error. 224 } 225 q := client.Query("select name, num from t1") 226 // Call Query.Run to get a Job, then call Read on the job. 227 // Note: Query.Read is a shorthand for this. 228 job, err := q.Run(ctx) 229 if err != nil { 230 // TODO: Handle error. 231 } 232 it, err := job.Read(ctx) 233 if err != nil { 234 // TODO: Handle error. 235 } 236 _ = it // TODO: iterate using Next or iterator.Pager. 237} 238 239func ExampleJob_Wait() { 240 ctx := context.Background() 241 client, err := bigquery.NewClient(ctx, "project-id") 242 if err != nil { 243 // TODO: Handle error. 244 } 245 ds := client.Dataset("my_dataset") 246 job, err := ds.Table("t1").CopierFrom(ds.Table("t2")).Run(ctx) 247 if err != nil { 248 // TODO: Handle error. 249 } 250 status, err := job.Wait(ctx) 251 if err != nil { 252 // TODO: Handle error. 253 } 254 if status.Err() != nil { 255 // TODO: Handle error. 256 } 257} 258 259func ExampleJob_Config() { 260 ctx := context.Background() 261 client, err := bigquery.NewClient(ctx, "project-id") 262 if err != nil { 263 // TODO: Handle error. 264 } 265 ds := client.Dataset("my_dataset") 266 job, err := ds.Table("t1").CopierFrom(ds.Table("t2")).Run(ctx) 267 if err != nil { 268 // TODO: Handle error. 269 } 270 jc, err := job.Config() 271 if err != nil { 272 // TODO: Handle error. 273 } 274 copyConfig := jc.(*bigquery.CopyConfig) 275 fmt.Println(copyConfig.Dst, copyConfig.CreateDisposition) 276} 277 278func ExampleDataset_Create() { 279 ctx := context.Background() 280 client, err := bigquery.NewClient(ctx, "project-id") 281 if err != nil { 282 // TODO: Handle error. 283 } 284 ds := client.Dataset("my_dataset") 285 if err := ds.Create(ctx, &bigquery.DatasetMetadata{Location: "EU"}); err != nil { 286 // TODO: Handle error. 287 } 288} 289 290func ExampleDataset_Delete() { 291 ctx := context.Background() 292 client, err := bigquery.NewClient(ctx, "project-id") 293 if err != nil { 294 // TODO: Handle error. 295 } 296 if err := client.Dataset("my_dataset").Delete(ctx); err != nil { 297 // TODO: Handle error. 298 } 299} 300 301func ExampleDataset_Metadata() { 302 ctx := context.Background() 303 client, err := bigquery.NewClient(ctx, "project-id") 304 if err != nil { 305 // TODO: Handle error. 306 } 307 md, err := client.Dataset("my_dataset").Metadata(ctx) 308 if err != nil { 309 // TODO: Handle error. 310 } 311 fmt.Println(md) 312} 313 314// This example illustrates how to perform a read-modify-write sequence on dataset 315// metadata. Passing the metadata's ETag to the Update call ensures that the call 316// will fail if the metadata was changed since the read. 317func ExampleDataset_Update_readModifyWrite() { 318 ctx := context.Background() 319 client, err := bigquery.NewClient(ctx, "project-id") 320 if err != nil { 321 // TODO: Handle error. 322 } 323 ds := client.Dataset("my_dataset") 324 md, err := ds.Metadata(ctx) 325 if err != nil { 326 // TODO: Handle error. 327 } 328 md2, err := ds.Update(ctx, 329 bigquery.DatasetMetadataToUpdate{Name: "new " + md.Name}, 330 md.ETag) 331 if err != nil { 332 // TODO: Handle error. 333 } 334 fmt.Println(md2) 335} 336 337// To perform a blind write, ignoring the existing state (and possibly overwriting 338// other updates), pass the empty string as the etag. 339func ExampleDataset_Update_blindWrite() { 340 ctx := context.Background() 341 client, err := bigquery.NewClient(ctx, "project-id") 342 if err != nil { 343 // TODO: Handle error. 344 } 345 md, err := client.Dataset("my_dataset").Update(ctx, bigquery.DatasetMetadataToUpdate{Name: "blind"}, "") 346 if err != nil { 347 // TODO: Handle error. 348 } 349 fmt.Println(md) 350} 351 352func ExampleDataset_Table() { 353 ctx := context.Background() 354 client, err := bigquery.NewClient(ctx, "project-id") 355 if err != nil { 356 // TODO: Handle error. 357 } 358 // Table creates a reference to the table. It does not create the actual 359 // table in BigQuery; to do so, use Table.Create. 360 t := client.Dataset("my_dataset").Table("my_table") 361 fmt.Println(t) 362} 363 364func ExampleDataset_Tables() { 365 ctx := context.Background() 366 client, err := bigquery.NewClient(ctx, "project-id") 367 if err != nil { 368 // TODO: Handle error. 369 } 370 it := client.Dataset("my_dataset").Tables(ctx) 371 _ = it // TODO: iterate using Next or iterator.Pager. 372} 373 374func ExampleDatasetIterator_Next() { 375 ctx := context.Background() 376 client, err := bigquery.NewClient(ctx, "project-id") 377 if err != nil { 378 // TODO: Handle error. 379 } 380 it := client.Datasets(ctx) 381 for { 382 ds, err := it.Next() 383 if err == iterator.Done { 384 break 385 } 386 if err != nil { 387 // TODO: Handle error. 388 } 389 fmt.Println(ds) 390 } 391} 392 393func ExampleInferSchema() { 394 type Item struct { 395 Name string 396 Size float64 397 Count int 398 } 399 schema, err := bigquery.InferSchema(Item{}) 400 if err != nil { 401 fmt.Println(err) 402 // TODO: Handle error. 403 } 404 for _, fs := range schema { 405 fmt.Println(fs.Name, fs.Type) 406 } 407 // Output: 408 // Name STRING 409 // Size FLOAT 410 // Count INTEGER 411} 412 413func ExampleInferSchema_tags() { 414 type Item struct { 415 Name string 416 Size float64 417 Count int `bigquery:"number"` 418 Secret []byte `bigquery:"-"` 419 Optional bigquery.NullBool 420 OptBytes []byte `bigquery:",nullable"` 421 } 422 schema, err := bigquery.InferSchema(Item{}) 423 if err != nil { 424 fmt.Println(err) 425 // TODO: Handle error. 426 } 427 for _, fs := range schema { 428 fmt.Println(fs.Name, fs.Type, fs.Required) 429 } 430 // Output: 431 // Name STRING true 432 // Size FLOAT true 433 // number INTEGER true 434 // Optional BOOLEAN false 435 // OptBytes BYTES false 436} 437 438func ExampleTable_Create() { 439 ctx := context.Background() 440 client, err := bigquery.NewClient(ctx, "project-id") 441 if err != nil { 442 // TODO: Handle error. 443 } 444 t := client.Dataset("my_dataset").Table("new-table") 445 if err := t.Create(ctx, nil); err != nil { 446 // TODO: Handle error. 447 } 448} 449 450// Initialize a new table by passing TableMetadata to Table.Create. 451func ExampleTable_Create_initialize() { 452 ctx := context.Background() 453 // Infer table schema from a Go type. 454 schema, err := bigquery.InferSchema(Item{}) 455 if err != nil { 456 // TODO: Handle error. 457 } 458 client, err := bigquery.NewClient(ctx, "project-id") 459 if err != nil { 460 // TODO: Handle error. 461 } 462 t := client.Dataset("my_dataset").Table("new-table") 463 if err := t.Create(ctx, 464 &bigquery.TableMetadata{ 465 Name: "My New Table", 466 Schema: schema, 467 ExpirationTime: time.Now().Add(24 * time.Hour), 468 }); err != nil { 469 // TODO: Handle error. 470 } 471} 472 473// This example demonstrates how to create a table with 474// a customer-managed encryption key. 475func ExampleTable_Create_encryptionKey() { 476 ctx := context.Background() 477 // Infer table schema from a Go type. 478 schema, err := bigquery.InferSchema(Item{}) 479 if err != nil { 480 // TODO: Handle error. 481 } 482 client, err := bigquery.NewClient(ctx, "project-id") 483 if err != nil { 484 // TODO: Handle error. 485 } 486 t := client.Dataset("my_dataset").Table("new-table") 487 488 // TODO: Replace this key with a key you have created in Cloud KMS. 489 keyName := "projects/P/locations/L/keyRings/R/cryptoKeys/K" 490 if err := t.Create(ctx, 491 &bigquery.TableMetadata{ 492 Name: "My New Table", 493 Schema: schema, 494 EncryptionConfig: &bigquery.EncryptionConfig{KMSKeyName: keyName}, 495 }); err != nil { 496 // TODO: Handle error. 497 } 498} 499 500func ExampleTable_Delete() { 501 ctx := context.Background() 502 client, err := bigquery.NewClient(ctx, "project-id") 503 if err != nil { 504 // TODO: Handle error. 505 } 506 if err := client.Dataset("my_dataset").Table("my_table").Delete(ctx); err != nil { 507 // TODO: Handle error. 508 } 509} 510 511func ExampleTable_Metadata() { 512 ctx := context.Background() 513 client, err := bigquery.NewClient(ctx, "project-id") 514 if err != nil { 515 // TODO: Handle error. 516 } 517 md, err := client.Dataset("my_dataset").Table("my_table").Metadata(ctx) 518 if err != nil { 519 // TODO: Handle error. 520 } 521 fmt.Println(md) 522} 523 524func ExampleTable_Inserter() { 525 ctx := context.Background() 526 client, err := bigquery.NewClient(ctx, "project-id") 527 if err != nil { 528 // TODO: Handle error. 529 } 530 ins := client.Dataset("my_dataset").Table("my_table").Inserter() 531 _ = ins // TODO: Use ins. 532} 533 534func ExampleTable_Inserter_options() { 535 ctx := context.Background() 536 client, err := bigquery.NewClient(ctx, "project-id") 537 if err != nil { 538 // TODO: Handle error. 539 } 540 ins := client.Dataset("my_dataset").Table("my_table").Inserter() 541 ins.SkipInvalidRows = true 542 ins.IgnoreUnknownValues = true 543 _ = ins // TODO: Use ins. 544} 545 546func ExampleTable_CopierFrom() { 547 ctx := context.Background() 548 client, err := bigquery.NewClient(ctx, "project-id") 549 if err != nil { 550 // TODO: Handle error. 551 } 552 ds := client.Dataset("my_dataset") 553 c := ds.Table("combined").CopierFrom(ds.Table("t1"), ds.Table("t2")) 554 c.WriteDisposition = bigquery.WriteTruncate 555 // TODO: set other options on the Copier. 556 job, err := c.Run(ctx) 557 if err != nil { 558 // TODO: Handle error. 559 } 560 status, err := job.Wait(ctx) 561 if err != nil { 562 // TODO: Handle error. 563 } 564 if status.Err() != nil { 565 // TODO: Handle error. 566 } 567} 568 569func ExampleTable_ExtractorTo() { 570 ctx := context.Background() 571 client, err := bigquery.NewClient(ctx, "project-id") 572 if err != nil { 573 // TODO: Handle error. 574 } 575 gcsRef := bigquery.NewGCSReference("gs://my-bucket/my-object") 576 gcsRef.FieldDelimiter = ":" 577 // TODO: set other options on the GCSReference. 578 ds := client.Dataset("my_dataset") 579 extractor := ds.Table("my_table").ExtractorTo(gcsRef) 580 extractor.DisableHeader = true 581 // TODO: set other options on the Extractor. 582 job, err := extractor.Run(ctx) 583 if err != nil { 584 // TODO: Handle error. 585 } 586 status, err := job.Wait(ctx) 587 if err != nil { 588 // TODO: Handle error. 589 } 590 if status.Err() != nil { 591 // TODO: Handle error. 592 } 593} 594 595func ExampleTable_LoaderFrom() { 596 ctx := context.Background() 597 client, err := bigquery.NewClient(ctx, "project-id") 598 if err != nil { 599 // TODO: Handle error. 600 } 601 gcsRef := bigquery.NewGCSReference("gs://my-bucket/my-object") 602 gcsRef.AllowJaggedRows = true 603 gcsRef.MaxBadRecords = 5 604 gcsRef.Schema = schema 605 // TODO: set other options on the GCSReference. 606 ds := client.Dataset("my_dataset") 607 loader := ds.Table("my_table").LoaderFrom(gcsRef) 608 loader.CreateDisposition = bigquery.CreateNever 609 // TODO: set other options on the Loader. 610 job, err := loader.Run(ctx) 611 if err != nil { 612 // TODO: Handle error. 613 } 614 status, err := job.Wait(ctx) 615 if err != nil { 616 // TODO: Handle error. 617 } 618 if status.Err() != nil { 619 // TODO: Handle error. 620 } 621} 622 623func ExampleTable_LoaderFrom_reader() { 624 ctx := context.Background() 625 client, err := bigquery.NewClient(ctx, "project-id") 626 if err != nil { 627 // TODO: Handle error. 628 } 629 f, err := os.Open("data.csv") 630 if err != nil { 631 // TODO: Handle error. 632 } 633 rs := bigquery.NewReaderSource(f) 634 rs.AllowJaggedRows = true 635 rs.MaxBadRecords = 5 636 rs.Schema = schema 637 // TODO: set other options on the GCSReference. 638 ds := client.Dataset("my_dataset") 639 loader := ds.Table("my_table").LoaderFrom(rs) 640 loader.CreateDisposition = bigquery.CreateNever 641 // TODO: set other options on the Loader. 642 job, err := loader.Run(ctx) 643 if err != nil { 644 // TODO: Handle error. 645 } 646 status, err := job.Wait(ctx) 647 if err != nil { 648 // TODO: Handle error. 649 } 650 if status.Err() != nil { 651 // TODO: Handle error. 652 } 653} 654 655func ExampleTable_Read() { 656 ctx := context.Background() 657 client, err := bigquery.NewClient(ctx, "project-id") 658 if err != nil { 659 // TODO: Handle error. 660 } 661 it := client.Dataset("my_dataset").Table("my_table").Read(ctx) 662 _ = it // TODO: iterate using Next or iterator.Pager. 663} 664 665// This example illustrates how to perform a read-modify-write sequence on table 666// metadata. Passing the metadata's ETag to the Update call ensures that the call 667// will fail if the metadata was changed since the read. 668func ExampleTable_Update_readModifyWrite() { 669 ctx := context.Background() 670 client, err := bigquery.NewClient(ctx, "project-id") 671 if err != nil { 672 // TODO: Handle error. 673 } 674 t := client.Dataset("my_dataset").Table("my_table") 675 md, err := t.Metadata(ctx) 676 if err != nil { 677 // TODO: Handle error. 678 } 679 md2, err := t.Update(ctx, 680 bigquery.TableMetadataToUpdate{Name: "new " + md.Name}, 681 md.ETag) 682 if err != nil { 683 // TODO: Handle error. 684 } 685 fmt.Println(md2) 686} 687 688// To perform a blind write, ignoring the existing state (and possibly overwriting 689// other updates), pass the empty string as the etag. 690func ExampleTable_Update_blindWrite() { 691 ctx := context.Background() 692 client, err := bigquery.NewClient(ctx, "project-id") 693 if err != nil { 694 // TODO: Handle error. 695 } 696 t := client.Dataset("my_dataset").Table("my_table") 697 tm, err := t.Update(ctx, bigquery.TableMetadataToUpdate{ 698 Description: "my favorite table", 699 }, "") 700 if err != nil { 701 // TODO: Handle error. 702 } 703 fmt.Println(tm) 704} 705 706func ExampleTableIterator_Next() { 707 ctx := context.Background() 708 client, err := bigquery.NewClient(ctx, "project-id") 709 if err != nil { 710 // TODO: Handle error. 711 } 712 it := client.Dataset("my_dataset").Tables(ctx) 713 for { 714 t, err := it.Next() 715 if err == iterator.Done { 716 break 717 } 718 if err != nil { 719 // TODO: Handle error. 720 } 721 fmt.Println(t) 722 } 723} 724 725type Item struct { 726 Name string 727 Size float64 728 Count int 729} 730 731// Save implements the ValueSaver interface. 732func (i *Item) Save() (map[string]bigquery.Value, string, error) { 733 return map[string]bigquery.Value{ 734 "Name": i.Name, 735 "Size": i.Size, 736 "Count": i.Count, 737 }, "", nil 738} 739 740func ExampleInserter_Put() { 741 ctx := context.Background() 742 client, err := bigquery.NewClient(ctx, "project-id") 743 if err != nil { 744 // TODO: Handle error. 745 } 746 ins := client.Dataset("my_dataset").Table("my_table").Inserter() 747 // Item implements the ValueSaver interface. 748 items := []*Item{ 749 {Name: "n1", Size: 32.6, Count: 7}, 750 {Name: "n2", Size: 4, Count: 2}, 751 {Name: "n3", Size: 101.5, Count: 1}, 752 } 753 if err := ins.Put(ctx, items); err != nil { 754 // TODO: Handle error. 755 } 756} 757 758var schema bigquery.Schema 759 760func ExampleInserter_Put_structSaver() { 761 ctx := context.Background() 762 client, err := bigquery.NewClient(ctx, "project-id") 763 if err != nil { 764 // TODO: Handle error. 765 } 766 ins := client.Dataset("my_dataset").Table("my_table").Inserter() 767 768 type score struct { 769 Name string 770 Num int 771 } 772 773 // Assume schema holds the table's schema. 774 savers := []*bigquery.StructSaver{ 775 {Struct: score{Name: "n1", Num: 12}, Schema: schema, InsertID: "id1"}, 776 {Struct: score{Name: "n2", Num: 31}, Schema: schema, InsertID: "id2"}, 777 {Struct: score{Name: "n3", Num: 7}, Schema: schema, InsertID: "id3"}, 778 } 779 if err := ins.Put(ctx, savers); err != nil { 780 // TODO: Handle error. 781 } 782} 783 784func ExampleInserter_Put_struct() { 785 ctx := context.Background() 786 client, err := bigquery.NewClient(ctx, "project-id") 787 if err != nil { 788 // TODO: Handle error. 789 } 790 ins := client.Dataset("my_dataset").Table("my_table").Inserter() 791 792 type score struct { 793 Name string 794 Num int 795 } 796 scores := []score{ 797 {Name: "n1", Num: 12}, 798 {Name: "n2", Num: 31}, 799 {Name: "n3", Num: 7}, 800 } 801 // Schema is inferred from the score type. 802 if err := ins.Put(ctx, scores); err != nil { 803 // TODO: Handle error. 804 } 805} 806 807func ExampleInserter_Put_valuesSaver() { 808 ctx := context.Background() 809 client, err := bigquery.NewClient(ctx, "project-id") 810 if err != nil { 811 // TODO: Handle error. 812 } 813 814 ins := client.Dataset("my_dataset").Table("my_table").Inserter() 815 816 var vss []*bigquery.ValuesSaver 817 for i, name := range []string{"n1", "n2", "n3"} { 818 // Assume schema holds the table's schema. 819 vss = append(vss, &bigquery.ValuesSaver{ 820 Schema: schema, 821 InsertID: name, 822 Row: []bigquery.Value{name, int64(i)}, 823 }) 824 } 825 826 if err := ins.Put(ctx, vss); err != nil { 827 // TODO: Handle error. 828 } 829} 830