Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add table_name setting #64

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions docs/index.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ This plugin supports the following configuration options plus the <<plugins-{typ
| <<plugins-{type}s-{plugin}-skip_invalid_rows>> |<<boolean,boolean>>|No
| <<plugins-{type}s-{plugin}-table_prefix>> |<<string,string>>|No
| <<plugins-{type}s-{plugin}-table_separator>> |<<string,string>>|No
| <<plugins-{type}s-{plugin}-table_name>> |<<string,string>>|No
| <<plugins-{type}s-{plugin}-temp_directory>> |<<string,string>>|__Deprecated__
| <<plugins-{type}s-{plugin}-temp_file_prefix>> |<<string,string>>|__Deprecated__
| <<plugins-{type}s-{plugin}-uploader_interval_secs>> |<<number,number>>|__Deprecated__
Expand Down Expand Up @@ -339,6 +340,15 @@ Table name will be `<table_prefix><table_separator><date>`
BigQuery table separator to be added between the table_prefix and the
date suffix.

[id="plugins-{type}s-{plugin}-table_name"]
===== `table_name`

* Value type is <<string,string>>
* Default value is `nil`

BigQuery table name to be used when inserting data into an existing table.
(Useful when using partitioned table)

[id="plugins-{type}s-{plugin}-temp_directory"]
===== `temp_directory`

Expand Down
9 changes: 7 additions & 2 deletions lib/logstash/outputs/google_bigquery.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ class LogStash::Outputs::GoogleBigQuery < LogStash::Outputs::Base
# date suffix.
config :table_separator, validate: :string, default: '_'

# BigQuery table name to be used when inserting data into an existing table
# (Useful when using partitioned table)
config :table_name, validate: :string, required: false, default: nil

# Schema for log data. It must follow the format `name1:type1(,name2:type2)*`.
# For example, `path:STRING,status:INTEGER,score:FLOAT`.
config :csv_schema, validate: :string, required: false, default: nil
Expand Down Expand Up @@ -179,7 +183,7 @@ class LogStash::Outputs::GoogleBigQuery < LogStash::Outputs::Base
def register
@logger.debug('Registering plugin')

@schema = LogStash::Outputs::BigQuery::Schema.parse_csv_or_json @csv_schema, @json_schema
@schema = LogStash::Outputs::BigQuery::Schema.parse_csv_or_json @csv_schema, @json_schema if @table_name.nil? || @table_name.empty?
@bq_client = LogStash::Outputs::BigQuery::StreamingClient.new @json_key_file, @project_id, @logger
@batcher = LogStash::Outputs::BigQuery::Batcher.new @batch_size, @batch_size_bytes
@stopping = Concurrent::AtomicBoolean.new(false)
Expand All @@ -202,6 +206,7 @@ def receive(event)
end

def get_table_name(time=nil)
return @table_name unless @table_name.nil? || @table_name.empty?
time ||= Time.now

str_time = time.strftime(@date_pattern)
Expand Down Expand Up @@ -236,7 +241,7 @@ def publish(messages)
table = get_table_name
@logger.info("Publishing #{messages.length} messages to #{table}")

create_table_if_not_exists table
create_table_if_not_exists table if @table_name.nil? || @table_name.empty?

failed_rows = @bq_client.append(@dataset, table, messages, @ignore_unknown_values, @skip_invalid_rows)
write_to_errors_file(failed_rows, table) unless failed_rows.empty?
Expand Down