...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package bigquery
16
17 import (
18 "io"
19
20 bq "google.golang.org/api/bigquery/v2"
21 )
22
23
24
25
26
27
28
29 type ReaderSource struct {
30 r io.Reader
31 FileConfig
32 }
33
34
35
36
37 func NewReaderSource(r io.Reader) *ReaderSource {
38 return &ReaderSource{r: r}
39 }
40
41 func (r *ReaderSource) populateLoadConfig(lc *bq.JobConfigurationLoad) io.Reader {
42 r.FileConfig.populateLoadConfig(lc)
43 return r.r
44 }
45
46
47
48
49
50 type FileConfig struct {
51
52
53 SourceFormat DataFormat
54
55
56
57 AutoDetect bool
58
59
60
61 MaxBadRecords int64
62
63
64
65
66
67
68
69 IgnoreUnknownValues bool
70
71
72
73 Schema Schema
74
75
76 CSVOptions
77
78
79 ParquetOptions *ParquetOptions
80
81
82 AvroOptions *AvroOptions
83 }
84
85 func (fc *FileConfig) populateLoadConfig(conf *bq.JobConfigurationLoad) {
86 conf.SkipLeadingRows = fc.SkipLeadingRows
87 conf.SourceFormat = string(fc.SourceFormat)
88 conf.Autodetect = fc.AutoDetect
89 conf.AllowJaggedRows = fc.AllowJaggedRows
90 conf.AllowQuotedNewlines = fc.AllowQuotedNewlines
91 conf.Encoding = string(fc.Encoding)
92 conf.FieldDelimiter = fc.FieldDelimiter
93 conf.IgnoreUnknownValues = fc.IgnoreUnknownValues
94 conf.MaxBadRecords = fc.MaxBadRecords
95 conf.NullMarker = fc.NullMarker
96 conf.PreserveAsciiControlCharacters = fc.PreserveASCIIControlCharacters
97 if fc.Schema != nil {
98 conf.Schema = fc.Schema.toBQ()
99 }
100 if fc.ParquetOptions != nil {
101 conf.ParquetOptions = &bq.ParquetOptions{
102 EnumAsString: fc.ParquetOptions.EnumAsString,
103 EnableListInference: fc.ParquetOptions.EnableListInference,
104 }
105 }
106 if fc.AvroOptions != nil {
107 conf.UseAvroLogicalTypes = fc.AvroOptions.UseAvroLogicalTypes
108 }
109 conf.Quote = fc.quote()
110 }
111
112 func bqPopulateFileConfig(conf *bq.JobConfigurationLoad, fc *FileConfig) {
113 fc.SourceFormat = DataFormat(conf.SourceFormat)
114 fc.AutoDetect = conf.Autodetect
115 fc.MaxBadRecords = conf.MaxBadRecords
116 fc.IgnoreUnknownValues = conf.IgnoreUnknownValues
117 fc.Schema = bqToSchema(conf.Schema)
118 fc.SkipLeadingRows = conf.SkipLeadingRows
119 fc.AllowJaggedRows = conf.AllowJaggedRows
120 fc.AllowQuotedNewlines = conf.AllowQuotedNewlines
121 fc.Encoding = Encoding(conf.Encoding)
122 fc.FieldDelimiter = conf.FieldDelimiter
123 fc.CSVOptions.NullMarker = conf.NullMarker
124 fc.CSVOptions.PreserveASCIIControlCharacters = conf.PreserveAsciiControlCharacters
125 fc.CSVOptions.setQuote(conf.Quote)
126 }
127
128 func (fc *FileConfig) populateExternalDataConfig(conf *bq.ExternalDataConfiguration) {
129 format := fc.SourceFormat
130 if format == "" {
131
132 format = CSV
133 }
134 conf.Autodetect = fc.AutoDetect
135 conf.IgnoreUnknownValues = fc.IgnoreUnknownValues
136 conf.MaxBadRecords = fc.MaxBadRecords
137 conf.SourceFormat = string(format)
138 if fc.Schema != nil {
139 conf.Schema = fc.Schema.toBQ()
140 }
141 if format == CSV {
142 fc.CSVOptions.populateExternalDataConfig(conf)
143 }
144 if fc.AvroOptions != nil {
145 conf.AvroOptions = &bq.AvroOptions{
146 UseAvroLogicalTypes: fc.AvroOptions.UseAvroLogicalTypes,
147 }
148 }
149 if fc.ParquetOptions != nil {
150 conf.ParquetOptions = &bq.ParquetOptions{
151 EnumAsString: fc.ParquetOptions.EnumAsString,
152 EnableListInference: fc.ParquetOptions.EnableListInference,
153 }
154 }
155 }
156
157
158
159
160 type Encoding string
161
162 const (
163
164 UTF_8 Encoding = "UTF-8"
165
166 ISO_8859_1 Encoding = "ISO-8859-1"
167 )
168
View as plain text