fluent-plugin-bigquery 0.2.13 → 0.2.14

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8266112b60355067f61e514946b70ccea8b1eed4
4
- data.tar.gz: 8bb87ae21e1391c18f51ca4ae6806e5a7e8219ff
3
+ metadata.gz: 571266d1f383aaeeab090ba11195671a9fcd452f
4
+ data.tar.gz: 3caf0f7ce01b4396d198241b31cd1c4f65abfec8
5
5
  SHA512:
6
- metadata.gz: ea18b5361d0789b91bd41c7838a560d1c3207158b69b5f8abc269e44dd020a26b9db6b9e58c24b84114956384cd44b147a9e9f0e0eec4af920a4779e6493bffc
7
- data.tar.gz: 739c72ae73c4d28354c53e0bae9dc1b2f9126cd52cbe84784a36f2b69fcae42724a01c29688af15c5a3c8b891dcae909725929473c74e51ab1f1e02997ab8fd1
6
+ metadata.gz: 489ed6d5a4b10c2f52cf86a14193c4e53bc9cfeb641fb968552fec2e514b5bad708d845c449e8f8d036765f75de330cb01a6fe917a0b1bb79ace6d75361a73d3
7
+ data.tar.gz: 9148962886487649c2d83f1d211b9c6730f2fa0f66380539a3552eae716c8deaa1142347af50d13cfb7e06ca7bcc5f141b7ceb48ddb362d9eedcb75bb7508a1c
data/README.md CHANGED
@@ -233,6 +233,23 @@ The format can be suffixed with attribute name.
233
233
  If attribute name is given, the time to be used for formatting is value of each row.
234
234
  The value for the time should be a UNIX time.
235
235
 
236
+ Or, the options can use `%{time_slice}` placeholder.
237
+ `%{time_slice}` is replaced by formatted time slice key at runtime.
238
+
239
+ ```apache
240
+ <match dummy>
241
+ type bigquery
242
+
243
+ ...
244
+
245
+ project yourproject_id
246
+ dataset yourdataset_id
247
+ table accesslog%{time_slice}
248
+
249
+ ...
250
+ </match>
251
+ ```
252
+
236
253
  ### Dynamic table creating
237
254
 
238
255
  When `auto_create_table` is set to `true`, try to create the table using BigQuery API when insertion failed with code=404 "Not Found: Table ...".
@@ -1,6 +1,6 @@
1
1
  module Fluent
2
2
  module BigQueryPlugin
3
- VERSION = "0.2.13"
3
+ VERSION = "0.2.14"
4
4
  end
5
5
  end
6
6
 
@@ -13,7 +13,7 @@ module Fluent
13
13
  # class BigQueryAPIError < StandardError
14
14
  # end
15
15
 
16
- class BigQueryOutput < BufferedOutput
16
+ class BigQueryOutput < TimeSlicedOutput
17
17
  Fluent::Plugin.register_output('bigquery', self)
18
18
 
19
19
  # https://developers.google.com/bigquery/browser-tool-quickstart
@@ -278,7 +278,7 @@ module Fluent
278
278
  @cached_client = client
279
279
  end
280
280
 
281
- def generate_table_id(table_id_format, current_time, row)
281
+ def generate_table_id(table_id_format, current_time, row = nil, chunk = nil)
282
282
  format, col = table_id_format.split(/@/)
283
283
  time = if col && row
284
284
  keys = col.split('.')
@@ -287,7 +287,17 @@ module Fluent
287
287
  else
288
288
  current_time
289
289
  end
290
- time.strftime(format)
290
+ table_id = time.strftime(format)
291
+
292
+ if chunk
293
+ table_id.gsub(%r(%{time_slice})) { |expr|
294
+ chunk.key
295
+ }
296
+ else
297
+ table_id.gsub(%r(%{time_slice})) { |expr|
298
+ current_time.strftime(@time_slice_format)
299
+ }
300
+ end
291
301
  end
292
302
 
293
303
  def create_table(table_id)
@@ -385,24 +395,22 @@ module Fluent
385
395
  record
386
396
  end
387
397
 
388
- def format_stream(tag, es)
389
- super
398
+ def format(tag, time, record)
390
399
  buf = ''
391
- es.each do |time, record|
392
- if @replace_record_key
393
- record = replace_record_key(record)
394
- end
395
400
 
396
- if @convert_hash_to_json
397
- record = convert_hash_to_json(record)
398
- end
401
+ if @replace_record_key
402
+ record = replace_record_key(record)
403
+ end
399
404
 
400
- row = @fields.format(@add_time_field.call(record, time))
401
- unless row.empty?
402
- row = {"json" => row}
403
- row['insertId'] = @get_insert_id.call(record) if @get_insert_id
404
- buf << row.to_msgpack
405
- end
405
+ if @convert_hash_to_json
406
+ record = convert_hash_to_json(record)
407
+ end
408
+
409
+ row = @fields.format(@add_time_field.call(record, time))
410
+ unless row.empty?
411
+ row = {"json" => row}
412
+ row['insertId'] = @get_insert_id.call(record) if @get_insert_id
413
+ buf << row.to_msgpack
406
414
  end
407
415
  buf
408
416
  end
@@ -422,14 +430,14 @@ module Fluent
422
430
  t
423
431
  end
424
432
 
425
- rows.group_by {|row| generate_table_id(insert_table_format, Time.at(Fluent::Engine.now), row) }.each do |table_id, rows|
433
+ rows.group_by {|row| generate_table_id(insert_table_format, Time.at(Fluent::Engine.now), row, chunk) }.each do |table_id, rows|
426
434
  insert(table_id, rows)
427
435
  end
428
436
  end
429
437
 
430
438
  def fetch_schema
431
439
  table_id_format = @tablelist[0]
432
- table_id = generate_table_id(table_id_format, Time.at(Fluent::Engine.now), nil)
440
+ table_id = generate_table_id(table_id_format, Time.at(Fluent::Engine.now))
433
441
  res = client.execute(
434
442
  api_method: @bq.tables.get,
435
443
  parameters: {
@@ -27,7 +27,7 @@ class BigQueryOutputTest < Test::Unit::TestCase
27
27
  API_SCOPE = "https://www.googleapis.com/auth/bigquery"
28
28
 
29
29
  def create_driver(conf = CONFIG)
30
- Fluent::Test::OutputTestDriver.new(Fluent::BigQueryOutput).configure(conf)
30
+ Fluent::Test::TimeSlicedOutputTestDriver.new(Fluent::BigQueryOutput).configure(conf)
31
31
  end
32
32
 
33
33
  def stub_client(driver)
@@ -984,6 +984,18 @@ class BigQueryOutputTest < Test::Unit::TestCase
984
984
  assert_equal 'foo_2014_08_10', table_id
985
985
  end
986
986
 
987
+ def test_generate_table_id_with_time_sliced_format
988
+ driver = create_driver
989
+ table_id_format = 'foo_%{time_slice}'
990
+ current_time = Time.now
991
+ time = Time.local(2014, 8, 11, 21, 20, 56)
992
+ row = { "json" => { "foo" => "bar", "time" => time.to_i } }
993
+ chunk = Object.new
994
+ mock(chunk).key { time.strftime("%Y%m%d") }
995
+ table_id = driver.instance.generate_table_id(table_id_format, current_time, row, chunk)
996
+ assert_equal 'foo_20140811', table_id
997
+ end
998
+
987
999
  def test_auto_create_table_by_bigquery_api
988
1000
  now = Time.now
989
1001
  message = {
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fluent-plugin-bigquery
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.13
4
+ version: 0.2.14
5
5
  platform: ruby
6
6
  authors:
7
7
  - Naoya Ito
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-01-12 00:00:00.000000000 Z
11
+ date: 2016-01-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake