Class: RubySnowflake::Client
- Inherits:
-
Object
- Object
- RubySnowflake::Client
- Defined in:
- lib/ruby_snowflake/client.rb,
lib/ruby_snowflake/client/http_connection_wrapper.rb,
lib/ruby_snowflake/client/key_pair_jwt_auth_manager.rb,
lib/ruby_snowflake/client/streaming_result_strategy.rb,
lib/ruby_snowflake/client/threaded_in_memory_strategy.rb,
lib/ruby_snowflake/client/single_thread_in_memory_strategy.rb
Defined Under Namespace
Classes: HttpConnectionWrapper, KeyPairJwtAuthManager, SingleThreadInMemoryStrategy, StreamingResultStrategy, ThreadedInMemoryStrategy
Constant Summary collapse
- DEFAULT_LOGGER =
Logger.new(STDOUT)
- DEFAULT_LOG_LEVEL =
Logger::INFO
- DEFAULT_JWT_TOKEN_TTL =
seconds (59 min), this is the max supported by snowflake - 1 minute
3540- DEFAULT_CONNECTION_TIMEOUT =
seconds, how long for a thread to wait for a connection before erroring
60- DEFAULT_MAX_CONNECTIONS =
default maximum size of the http connection pool
16- DEFAULT_MAX_THREADS_PER_QUERY =
default maximum size of the thread pool on a single query
8- DEFAULT_THREAD_SCALE_FACTOR =
partition count factor for number of threads (i.e. 2 == once we have 4 partitions, spin up a second thread)
4- DEFAULT_HTTP_RETRIES =
how many times to retry common retryable HTTP responses (i.e. 429, 504)
2- DEFAULT_QUERY_TIMEOUT =
how long to wait to allow a query to complete, in seconds
600- DEFAULT_ROLE =
default role to use
nil- JSON_PARSE_OPTIONS =
{ decimal_class: BigDecimal }.freeze
- VALID_RESPONSE_CODES =
%w(200 202).freeze
- POLLING_RESPONSE_CODE =
"202"- POLLING_INTERVAL =
seconds
2
Instance Attribute Summary collapse
-
#connection_timeout ⇒ Object
readonly
can’t be set after initialization.
-
#default_role ⇒ Object
readonly
can’t be set after initialization.
-
#http_retries ⇒ Object
readonly
can’t be set after initialization.
-
#logger ⇒ Object
readonly
can’t be set after initialization.
-
#max_connections ⇒ Object
readonly
can’t be set after initialization.
-
#max_threads_per_query ⇒ Object
readonly
can’t be set after initialization.
-
#query_timeout ⇒ Object
readonly
can’t be set after initialization.
-
#thread_scale_factor ⇒ Object
readonly
can’t be set after initialization.
Class Method Summary collapse
Instance Method Summary collapse
-
#create_jwt_token ⇒ Object
This method can be used to populate the JWT token used for authentication in tests that require time travel.
-
#initialize(uri, private_key, organization, account, user, default_warehouse, default_database, default_role: nil, logger: DEFAULT_LOGGER, log_level: DEFAULT_LOG_LEVEL, jwt_token_ttl: DEFAULT_JWT_TOKEN_TTL, connection_timeout: DEFAULT_CONNECTION_TIMEOUT, max_connections: DEFAULT_MAX_CONNECTIONS, max_threads_per_query: DEFAULT_MAX_THREADS_PER_QUERY, thread_scale_factor: DEFAULT_THREAD_SCALE_FACTOR, http_retries: DEFAULT_HTTP_RETRIES, query_timeout: DEFAULT_QUERY_TIMEOUT) ⇒ Client
constructor
A new instance of Client.
- #query(query, warehouse: nil, streaming: false, database: nil, schema: nil, bindings: nil, role: nil, query_name: nil, query_timeout: nil) ⇒ Object (also: #fetch)
Constructor Details
#initialize(uri, private_key, organization, account, user, default_warehouse, default_database, default_role: nil, logger: DEFAULT_LOGGER, log_level: DEFAULT_LOG_LEVEL, jwt_token_ttl: DEFAULT_JWT_TOKEN_TTL, connection_timeout: DEFAULT_CONNECTION_TIMEOUT, max_connections: DEFAULT_MAX_CONNECTIONS, max_threads_per_query: DEFAULT_MAX_THREADS_PER_QUERY, thread_scale_factor: DEFAULT_THREAD_SCALE_FACTOR, http_retries: DEFAULT_HTTP_RETRIES, query_timeout: DEFAULT_QUERY_TIMEOUT) ⇒ Client
Returns a new instance of Client.
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/ruby_snowflake/client.rb', line 118 def initialize( uri, private_key, organization, account, user, default_warehouse, default_database, default_role: nil, logger: DEFAULT_LOGGER, log_level: DEFAULT_LOG_LEVEL, jwt_token_ttl: DEFAULT_JWT_TOKEN_TTL, connection_timeout: DEFAULT_CONNECTION_TIMEOUT, max_connections: DEFAULT_MAX_CONNECTIONS, max_threads_per_query: DEFAULT_MAX_THREADS_PER_QUERY, thread_scale_factor: DEFAULT_THREAD_SCALE_FACTOR, http_retries: DEFAULT_HTTP_RETRIES, query_timeout: DEFAULT_QUERY_TIMEOUT ) @base_uri = uri @key_pair_jwt_auth_manager = KeyPairJwtAuthManager.new(organization, account, user, private_key, jwt_token_ttl) @default_warehouse = default_warehouse @default_database = default_database @default_role = default_role # set defaults for config settings @logger = logger @logger.level = log_level @connection_timeout = connection_timeout @max_connections = max_connections @max_threads_per_query = max_threads_per_query @thread_scale_factor = thread_scale_factor @http_retries = http_retries @query_timeout = query_timeout # Do NOT use normally, this exists for tests so we can reliably trigger the polling # response workflow from snowflake in tests @_enable_polling_queries = false end |
Instance Attribute Details
#connection_timeout ⇒ Object (readonly)
can’t be set after initialization
76 77 78 |
# File 'lib/ruby_snowflake/client.rb', line 76 def connection_timeout @connection_timeout end |
#default_role ⇒ Object (readonly)
can’t be set after initialization
76 77 78 |
# File 'lib/ruby_snowflake/client.rb', line 76 def default_role @default_role end |
#http_retries ⇒ Object (readonly)
can’t be set after initialization
76 77 78 |
# File 'lib/ruby_snowflake/client.rb', line 76 def http_retries @http_retries end |
#logger ⇒ Object (readonly)
can’t be set after initialization
76 77 78 |
# File 'lib/ruby_snowflake/client.rb', line 76 def logger @logger end |
#max_connections ⇒ Object (readonly)
can’t be set after initialization
76 77 78 |
# File 'lib/ruby_snowflake/client.rb', line 76 def max_connections @max_connections end |
#max_threads_per_query ⇒ Object (readonly)
can’t be set after initialization
76 77 78 |
# File 'lib/ruby_snowflake/client.rb', line 76 def max_threads_per_query @max_threads_per_query end |
#query_timeout ⇒ Object (readonly)
can’t be set after initialization
76 77 78 |
# File 'lib/ruby_snowflake/client.rb', line 76 def query_timeout @query_timeout end |
#thread_scale_factor ⇒ Object (readonly)
can’t be set after initialization
76 77 78 |
# File 'lib/ruby_snowflake/client.rb', line 76 def thread_scale_factor @thread_scale_factor end |
Class Method Details
.from_env(logger: DEFAULT_LOGGER, log_level: DEFAULT_LOG_LEVEL, jwt_token_ttl: env_option("SNOWFLAKE_JWT_TOKEN_TTL", DEFAULT_JWT_TOKEN_TTL), connection_timeout: env_option("SNOWFLAKE_CONNECTION_TIMEOUT", DEFAULT_CONNECTION_TIMEOUT ), max_connections: env_option("SNOWFLAKE_MAX_CONNECTIONS", DEFAULT_MAX_CONNECTIONS ), max_threads_per_query: env_option("SNOWFLAKE_MAX_THREADS_PER_QUERY", DEFAULT_MAX_THREADS_PER_QUERY), thread_scale_factor: env_option("SNOWFLAKE_THREAD_SCALE_FACTOR", DEFAULT_THREAD_SCALE_FACTOR), http_retries: env_option("SNOWFLAKE_HTTP_RETRIES", DEFAULT_HTTP_RETRIES), query_timeout: env_option("SNOWFLAKE_QUERY_TIMEOUT", DEFAULT_QUERY_TIMEOUT), default_role: env_option("SNOWFLAKE_DEFAULT_ROLE", DEFAULT_ROLE)) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/ruby_snowflake/client.rb', line 78 def self.from_env(logger: DEFAULT_LOGGER, log_level: DEFAULT_LOG_LEVEL, jwt_token_ttl: env_option("SNOWFLAKE_JWT_TOKEN_TTL", DEFAULT_JWT_TOKEN_TTL), connection_timeout: env_option("SNOWFLAKE_CONNECTION_TIMEOUT", DEFAULT_CONNECTION_TIMEOUT ), max_connections: env_option("SNOWFLAKE_MAX_CONNECTIONS", DEFAULT_MAX_CONNECTIONS ), max_threads_per_query: env_option("SNOWFLAKE_MAX_THREADS_PER_QUERY", DEFAULT_MAX_THREADS_PER_QUERY), thread_scale_factor: env_option("SNOWFLAKE_THREAD_SCALE_FACTOR", DEFAULT_THREAD_SCALE_FACTOR), http_retries: env_option("SNOWFLAKE_HTTP_RETRIES", DEFAULT_HTTP_RETRIES), query_timeout: env_option("SNOWFLAKE_QUERY_TIMEOUT", DEFAULT_QUERY_TIMEOUT), default_role: env_option("SNOWFLAKE_DEFAULT_ROLE", DEFAULT_ROLE)) private_key = if key = ENV["SNOWFLAKE_PRIVATE_KEY"] key elsif path = ENV["SNOWFLAKE_PRIVATE_KEY_PATH"] File.read(path) else raise MissingConfig, "Either ENV['SNOWFLAKE_PRIVATE_KEY'] or ENV['SNOWFLAKE_PRIVATE_KEY_PATH'] must be set" end new( ENV.fetch("SNOWFLAKE_URI"), private_key, ENV.fetch("SNOWFLAKE_ORGANIZATION"), ENV.fetch("SNOWFLAKE_ACCOUNT"), ENV.fetch("SNOWFLAKE_USER"), ENV["SNOWFLAKE_DEFAULT_WAREHOUSE"], ENV["SNOWFLAKE_DEFAULT_DATABASE"], default_role: ENV.fetch("SNOWFLAKE_DEFAULT_ROLE", nil), logger: logger, log_level: log_level, jwt_token_ttl: jwt_token_ttl, connection_timeout: connection_timeout, max_connections: max_connections, max_threads_per_query: max_threads_per_query, thread_scale_factor: thread_scale_factor, http_retries: http_retries, query_timeout: query_timeout, ) end |
Instance Method Details
#create_jwt_token ⇒ Object
This method can be used to populate the JWT token used for authentication in tests that require time travel.
193 194 195 |
# File 'lib/ruby_snowflake/client.rb', line 193 def create_jwt_token @key_pair_jwt_auth_manager.jwt_token end |
#query(query, warehouse: nil, streaming: false, database: nil, schema: nil, bindings: nil, role: nil, query_name: nil, query_timeout: nil) ⇒ Object Also known as: fetch
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/ruby_snowflake/client.rb', line 153 def query(query, warehouse: nil, streaming: false, database: nil, schema: nil, bindings: nil, role: nil, query_name: nil, query_timeout: nil) warehouse ||= @default_warehouse database ||= @default_database role ||= @default_role query_timeout ||= @query_timeout with_instrumentation({ database:, schema:, warehouse:, query_name: }) do query_start_time = Time.now.to_i response = nil connection_pool.with do |connection| request_body = { "warehouse" => warehouse&.upcase, "schema" => schema&.upcase, "database" => database&.upcase, "statement" => query, "bindings" => bindings, "role" => role, "timeout" => query_timeout } response = request_with_auth_and_headers( connection, Net::HTTP::Post, "/api/v2/statements?requestId=#{SecureRandom.uuid}&async=#{@_enable_polling_queries}", request_body.to_json ) end retrieve_result_set(query_start_time, query, response, streaming, query_timeout) end end |