job.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright 2015 Google Inc. All Rights Reserved.
  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
  15. import (
  16. "errors"
  17. "golang.org/x/net/context"
  18. bq "google.golang.org/api/bigquery/v2"
  19. )
  20. // A Job represents an operation which has been submitted to BigQuery for processing.
  21. type Job struct {
  22. service service
  23. projectID string
  24. jobID string
  25. isQuery bool
  26. }
  27. // JobFromID creates a Job which refers to an existing BigQuery job. The job
  28. // need not have been created by this package. For example, the job may have
  29. // been created in the BigQuery console.
  30. func (c *Client) JobFromID(ctx context.Context, id string) (*Job, error) {
  31. jobType, err := c.service.getJobType(ctx, c.projectID, id)
  32. if err != nil {
  33. return nil, err
  34. }
  35. return &Job{
  36. service: c.service,
  37. projectID: c.projectID,
  38. jobID: id,
  39. isQuery: jobType == queryJobType,
  40. }, nil
  41. }
  42. func (j *Job) ID() string {
  43. return j.jobID
  44. }
  45. // State is one of a sequence of states that a Job progresses through as it is processed.
  46. type State int
  47. const (
  48. Pending State = iota
  49. Running
  50. Done
  51. )
  52. // JobStatus contains the current State of a job, and errors encountered while processing that job.
  53. type JobStatus struct {
  54. State State
  55. err error
  56. // All errors encountered during the running of the job.
  57. // Not all Errors are fatal, so errors here do not necessarily mean that the job has completed or was unsuccessful.
  58. Errors []*Error
  59. }
  60. // jobOption is an Option which modifies a bq.Job proto.
  61. // This is used for configuring values that apply to all operations, such as setting a jobReference.
  62. type jobOption interface {
  63. customizeJob(job *bq.Job, projectID string)
  64. }
  65. type jobID string
  66. // JobID returns an Option that sets the job ID of a BigQuery job.
  67. // If this Option is not used, a job ID is generated automatically.
  68. func JobID(ID string) Option {
  69. return jobID(ID)
  70. }
  71. func (opt jobID) implementsOption() {}
  72. func (opt jobID) customizeJob(job *bq.Job, projectID string) {
  73. job.JobReference = &bq.JobReference{
  74. JobId: string(opt),
  75. ProjectId: projectID,
  76. }
  77. }
  78. // Done reports whether the job has completed.
  79. // After Done returns true, the Err method will return an error if the job completed unsuccesfully.
  80. func (s *JobStatus) Done() bool {
  81. return s.State == Done
  82. }
  83. // Err returns the error that caused the job to complete unsuccesfully (if any).
  84. func (s *JobStatus) Err() error {
  85. return s.err
  86. }
  87. // Status returns the current status of the job. It fails if the Status could not be determined.
  88. func (j *Job) Status(ctx context.Context) (*JobStatus, error) {
  89. return j.service.jobStatus(ctx, j.projectID, j.jobID)
  90. }
  91. func (j *Job) implementsReadSource() {}
  92. func (j *Job) customizeReadQuery(cursor *readQueryConf) error {
  93. // There are mulitple kinds of jobs, but only a query job is suitable for reading.
  94. if !j.isQuery {
  95. return errors.New("Cannot read from a non-query job")
  96. }
  97. cursor.projectID = j.projectID
  98. cursor.jobID = j.jobID
  99. return nil
  100. }