You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

830 lines
20 KiB

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