Class: Google::Cloud::Spanner::V1::SpannerClient
- Inherits:
-
Object
- Object
- Google::Cloud::Spanner::V1::SpannerClient
- Defined in:
- lib/google/cloud/spanner/v1/spanner_client.rb
Overview
Cloud Spanner API
The Cloud Spanner API can be used to manage sessions and execute transactions on data stored in Cloud Spanner databases.
Constant Summary collapse
- SERVICE_ADDRESS =
The default address of the service.
"spanner.googleapis.com".freeze
- DEFAULT_SERVICE_PORT =
The default port of the service.
443
- DEFAULT_TIMEOUT =
30
- ALL_SCOPES =
The scopes needed to make gRPC calls to all of the methods defined in this service.
[ "https://www.googleapis.com/auth/cloud-platform", "https://www.googleapis.com/auth/spanner.data" ].freeze
Instance Attribute Summary collapse
Class Method Summary collapse
-
.database_path(project, instance, database) ⇒ String
Returns a fully-qualified database resource name string.
-
.match_database_from_database_name(database_name) ⇒ String
Parses the database from a database resource.
-
.match_database_from_session_name(session_name) ⇒ String
Parses the database from a session resource.
-
.match_instance_from_database_name(database_name) ⇒ String
Parses the instance from a database resource.
-
.match_instance_from_session_name(session_name) ⇒ String
Parses the instance from a session resource.
-
.match_project_from_database_name(database_name) ⇒ String
Parses the project from a database resource.
-
.match_project_from_session_name(session_name) ⇒ String
Parses the project from a session resource.
-
.match_session_from_session_name(session_name) ⇒ String
Parses the session from a session resource.
-
.session_path(project, instance, database, session) ⇒ String
Returns a fully-qualified session resource name string.
Instance Method Summary collapse
-
#begin_transaction(session, options_, options: nil) ⇒ Google::Spanner::V1::Transaction
Begins a new transaction.
-
#commit(session, mutations, transaction_id: nil, single_use_transaction: nil, options: nil) ⇒ Google::Spanner::V1::CommitResponse
Commits a transaction.
-
#create_session(database, options: nil) ⇒ Google::Spanner::V1::Session
Creates a new session.
-
#delete_session(name, options: nil) ⇒ Object
Ends a session, releasing server resources associated with it.
-
#execute_sql(session, sql, transaction: nil, params: nil, param_types: nil, resume_token: nil, query_mode: nil, options: nil) ⇒ Google::Spanner::V1::ResultSet
Executes an SQL query, returning all rows in a single reply.
-
#execute_streaming_sql(session, sql, transaction: nil, params: nil, param_types: nil, resume_token: nil, query_mode: nil, options: nil) ⇒ Enumerable<Google::Spanner::V1::PartialResultSet>
Like ExecuteSql, except returns the result set as a stream.
-
#get_session(name, options: nil) ⇒ Google::Spanner::V1::Session
Gets a session.
-
#initialize(service_path: SERVICE_ADDRESS, port: DEFAULT_SERVICE_PORT, channel: nil, chan_creds: nil, scopes: ALL_SCOPES, client_config: {}, timeout: DEFAULT_TIMEOUT, app_name: nil, app_version: nil, lib_name: nil, lib_version: "") ⇒ SpannerClient
constructor
A new instance of SpannerClient.
-
#read(session, table, columns, key_set, transaction: nil, index: nil, limit: nil, resume_token: nil, options: nil) ⇒ Google::Spanner::V1::ResultSet
Reads rows from the database using key lookups and scans, as a simple key/value style alternative to ExecuteSql.
-
#rollback(session, transaction_id, options: nil) ⇒ Object
Rolls back a transaction, releasing any locks it holds.
-
#streaming_read(session, table, columns, key_set, transaction: nil, index: nil, limit: nil, resume_token: nil, options: nil) ⇒ Enumerable<Google::Spanner::V1::PartialResultSet>
Like Read, except returns the result set as a stream.
Constructor Details
#initialize(service_path: SERVICE_ADDRESS, port: DEFAULT_SERVICE_PORT, channel: nil, chan_creds: nil, scopes: ALL_SCOPES, client_config: {}, timeout: DEFAULT_TIMEOUT, app_name: nil, app_version: nil, lib_name: nil, lib_version: "") ⇒ SpannerClient
Returns a new instance of SpannerClient
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/google/cloud/spanner/v1/spanner_client.rb', line 165 def initialize \ service_path: SERVICE_ADDRESS, port: DEFAULT_SERVICE_PORT, channel: nil, chan_creds: nil, scopes: ALL_SCOPES, client_config: {}, timeout: DEFAULT_TIMEOUT, app_name: nil, app_version: nil, lib_name: nil, lib_version: "" # These require statements are intentionally placed here to initialize # the gRPC module only when it's required. # See https://github.com/googleapis/toolkit/issues/446 require "google/gax/grpc" require "google/spanner/v1/spanner_services_pb" if app_name || app_version warn "`app_name` and `app_version` are no longer being used in the request headers." end google_api_client = "gl-ruby/#{RUBY_VERSION}" google_api_client << " #{lib_name}/#{lib_version}" if lib_name google_api_client << " gapic/0.6.8 gax/#{Google::Gax::VERSION}" google_api_client << " grpc/#{GRPC::VERSION}" google_api_client.freeze headers = { :"x-goog-api-client" => google_api_client } client_config_file = Pathname.new(__dir__).join( "spanner_client_config.json" ) defaults = client_config_file.open do |f| Google::Gax.construct_settings( "google.spanner.v1.Spanner", JSON.parse(f.read), client_config, Google::Gax::Grpc::STATUS_CODE_NAMES, timeout, errors: Google::Gax::Grpc::API_ERRORS, kwargs: headers ) end @spanner_stub = Google::Gax::Grpc.create_stub( service_path, port, chan_creds: chan_creds, channel: channel, scopes: scopes, &Google::Spanner::V1::Spanner::Stub.method(:new) ) @create_session = Google::Gax.create_api_call( @spanner_stub.method(:create_session), defaults["create_session"] ) @get_session = Google::Gax.create_api_call( @spanner_stub.method(:get_session), defaults["get_session"] ) @delete_session = Google::Gax.create_api_call( @spanner_stub.method(:delete_session), defaults["delete_session"] ) @execute_sql = Google::Gax.create_api_call( @spanner_stub.method(:execute_sql), defaults["execute_sql"] ) @execute_streaming_sql = Google::Gax.create_api_call( @spanner_stub.method(:execute_streaming_sql), defaults["execute_streaming_sql"] ) @read = Google::Gax.create_api_call( @spanner_stub.method(:read), defaults["read"] ) @streaming_read = Google::Gax.create_api_call( @spanner_stub.method(:streaming_read), defaults["streaming_read"] ) @begin_transaction = Google::Gax.create_api_call( @spanner_stub.method(:begin_transaction), defaults["begin_transaction"] ) @commit = Google::Gax.create_api_call( @spanner_stub.method(:commit), defaults["commit"] ) @rollback = Google::Gax.create_api_call( @spanner_stub.method(:rollback), defaults["rollback"] ) end |
Instance Attribute Details
#spanner_stub ⇒ Google::Spanner::V1::Spanner::Stub (readonly)
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 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 117 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 152 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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 |
# File 'lib/google/cloud/spanner/v1/spanner_client.rb', line 43 class SpannerClient attr_reader :spanner_stub # The default address of the service. SERVICE_ADDRESS = "spanner.googleapis.com".freeze # The default port of the service. DEFAULT_SERVICE_PORT = 443 DEFAULT_TIMEOUT = 30 # The scopes needed to make gRPC calls to all of the methods defined in # this service. ALL_SCOPES = [ "https://www.googleapis.com/auth/cloud-platform", "https://www.googleapis.com/auth/spanner.data" ].freeze DATABASE_PATH_TEMPLATE = Google::Gax::PathTemplate.new( "projects/{project}/instances/{instance}/databases/{database}" ) private_constant :DATABASE_PATH_TEMPLATE SESSION_PATH_TEMPLATE = Google::Gax::PathTemplate.new( "projects/{project}/instances/{instance}/databases/{database}/sessions/{session}" ) private_constant :SESSION_PATH_TEMPLATE # Returns a fully-qualified database resource name string. # @param project [String] # @param instance [String] # @param database [String] # @return [String] def self.database_path project, instance, database DATABASE_PATH_TEMPLATE.render( :"project" => project, :"instance" => instance, :"database" => database ) end # Returns a fully-qualified session resource name string. # @param project [String] # @param instance [String] # @param database [String] # @param session [String] # @return [String] def self.session_path project, instance, database, session SESSION_PATH_TEMPLATE.render( :"project" => project, :"instance" => instance, :"database" => database, :"session" => session ) end # Parses the project from a database resource. # @param database_name [String] # @return [String] def self.match_project_from_database_name database_name DATABASE_PATH_TEMPLATE.match(database_name)["project"] end # Parses the instance from a database resource. # @param database_name [String] # @return [String] def self.match_instance_from_database_name database_name DATABASE_PATH_TEMPLATE.match(database_name)["instance"] end # Parses the database from a database resource. # @param database_name [String] # @return [String] def self.match_database_from_database_name database_name DATABASE_PATH_TEMPLATE.match(database_name)["database"] end # Parses the project from a session resource. # @param session_name [String] # @return [String] def self.match_project_from_session_name session_name SESSION_PATH_TEMPLATE.match(session_name)["project"] end # Parses the instance from a session resource. # @param session_name [String] # @return [String] def self.match_instance_from_session_name session_name SESSION_PATH_TEMPLATE.match(session_name)["instance"] end # Parses the database from a session resource. # @param session_name [String] # @return [String] def self.match_database_from_session_name session_name SESSION_PATH_TEMPLATE.match(session_name)["database"] end # Parses the session from a session resource. # @param session_name [String] # @return [String] def self.match_session_from_session_name session_name SESSION_PATH_TEMPLATE.match(session_name)["session"] end # @param service_path [String] # The domain name of the API remote host. # @param port [Integer] # The port on which to connect to the remote host. # @param channel [Channel] # A Channel object through which to make calls. # @param chan_creds [Grpc::ChannelCredentials] # A ChannelCredentials for the setting up the RPC client. # @param client_config[Hash] # A Hash for call options for each method. See # Google::Gax#construct_settings for the structure of # this data. Falls back to the default config if not specified # or the specified config is missing data points. # @param timeout [Numeric] # The default timeout, in seconds, for calls made through this client. def initialize \ service_path: SERVICE_ADDRESS, port: DEFAULT_SERVICE_PORT, channel: nil, chan_creds: nil, scopes: ALL_SCOPES, client_config: {}, timeout: DEFAULT_TIMEOUT, app_name: nil, app_version: nil, lib_name: nil, lib_version: "" # These require statements are intentionally placed here to initialize # the gRPC module only when it's required. # See https://github.com/googleapis/toolkit/issues/446 require "google/gax/grpc" require "google/spanner/v1/spanner_services_pb" if app_name || app_version warn "`app_name` and `app_version` are no longer being used in the request headers." end google_api_client = "gl-ruby/#{RUBY_VERSION}" google_api_client << " #{lib_name}/#{lib_version}" if lib_name google_api_client << " gapic/0.6.8 gax/#{Google::Gax::VERSION}" google_api_client << " grpc/#{GRPC::VERSION}" google_api_client.freeze headers = { :"x-goog-api-client" => google_api_client } client_config_file = Pathname.new(__dir__).join( "spanner_client_config.json" ) defaults = client_config_file.open do |f| Google::Gax.construct_settings( "google.spanner.v1.Spanner", JSON.parse(f.read), client_config, Google::Gax::Grpc::STATUS_CODE_NAMES, timeout, errors: Google::Gax::Grpc::API_ERRORS, kwargs: headers ) end @spanner_stub = Google::Gax::Grpc.create_stub( service_path, port, chan_creds: chan_creds, channel: channel, scopes: scopes, &Google::Spanner::V1::Spanner::Stub.method(:new) ) @create_session = Google::Gax.create_api_call( @spanner_stub.method(:create_session), defaults["create_session"] ) @get_session = Google::Gax.create_api_call( @spanner_stub.method(:get_session), defaults["get_session"] ) @delete_session = Google::Gax.create_api_call( @spanner_stub.method(:delete_session), defaults["delete_session"] ) @execute_sql = Google::Gax.create_api_call( @spanner_stub.method(:execute_sql), defaults["execute_sql"] ) @execute_streaming_sql = Google::Gax.create_api_call( @spanner_stub.method(:execute_streaming_sql), defaults["execute_streaming_sql"] ) @read = Google::Gax.create_api_call( @spanner_stub.method(:read), defaults["read"] ) @streaming_read = Google::Gax.create_api_call( @spanner_stub.method(:streaming_read), defaults["streaming_read"] ) @begin_transaction = Google::Gax.create_api_call( @spanner_stub.method(:begin_transaction), defaults["begin_transaction"] ) @commit = Google::Gax.create_api_call( @spanner_stub.method(:commit), defaults["commit"] ) @rollback = Google::Gax.create_api_call( @spanner_stub.method(:rollback), defaults["rollback"] ) end # Service calls # Creates a new session. A session can be used to perform # transactions that read and/or modify data in a Cloud Spanner database. # Sessions are meant to be reused for many consecutive # transactions. # # Sessions can only execute one transaction at a time. To execute # multiple concurrent read-write/write-only transactions, create # multiple sessions. Note that standalone reads and queries use a # transaction internally, and count toward the one transaction # limit. # # Cloud Spanner limits the number of sessions that can exist at any given # time; thus, it is a good idea to delete idle and/or unneeded sessions. # Aside from explicit deletes, Cloud Spanner can delete sessions for which no # operations are sent for more than an hour. If a session is deleted, # requests to it return +NOT_FOUND+. # # Idle sessions can be kept alive by sending a trivial SQL query # periodically, e.g., +"SELECT 1"+. # # @param database [String] # Required. The database in which the new session is created. # @param options [Google::Gax::CallOptions] # Overrides the default settings for this call, e.g, timeout, # retries, etc. # @return [Google::Spanner::V1::Session] # @raise [Google::Gax::GaxError] if the RPC is aborted. # @example # require "google/cloud/spanner/v1/spanner_client" # # SpannerClient = Google::Cloud::Spanner::V1::SpannerClient # # spanner_client = SpannerClient.new # formatted_database = SpannerClient.database_path("[PROJECT]", "[INSTANCE]", "[DATABASE]") # response = spanner_client.create_session(formatted_database) def create_session \ database, options: nil req = Google::Spanner::V1::CreateSessionRequest.new({ database: database }.delete_if { |_, v| v.nil? }) @create_session.call(req, ) end # Gets a session. Returns +NOT_FOUND+ if the session does not exist. # This is mainly useful for determining whether a session is still # alive. # # @param name [String] # Required. The name of the session to retrieve. # @param options [Google::Gax::CallOptions] # Overrides the default settings for this call, e.g, timeout, # retries, etc. # @return [Google::Spanner::V1::Session] # @raise [Google::Gax::GaxError] if the RPC is aborted. # @example # require "google/cloud/spanner/v1/spanner_client" # # SpannerClient = Google::Cloud::Spanner::V1::SpannerClient # # spanner_client = SpannerClient.new # formatted_name = SpannerClient.session_path("[PROJECT]", "[INSTANCE]", "[DATABASE]", "[SESSION]") # response = spanner_client.get_session(formatted_name) def get_session \ name, options: nil req = Google::Spanner::V1::GetSessionRequest.new({ name: name }.delete_if { |_, v| v.nil? }) @get_session.call(req, ) end # Ends a session, releasing server resources associated with it. # # @param name [String] # Required. The name of the session to delete. # @param options [Google::Gax::CallOptions] # Overrides the default settings for this call, e.g, timeout, # retries, etc. # @raise [Google::Gax::GaxError] if the RPC is aborted. # @example # require "google/cloud/spanner/v1/spanner_client" # # SpannerClient = Google::Cloud::Spanner::V1::SpannerClient # # spanner_client = SpannerClient.new # formatted_name = SpannerClient.session_path("[PROJECT]", "[INSTANCE]", "[DATABASE]", "[SESSION]") # spanner_client.delete_session(formatted_name) def delete_session \ name, options: nil req = Google::Spanner::V1::DeleteSessionRequest.new({ name: name }.delete_if { |_, v| v.nil? }) @delete_session.call(req, ) nil end # Executes an SQL query, returning all rows in a single reply. This # method cannot be used to return a result set larger than 10 MiB; # if the query yields more data than that, the query fails with # a +FAILED_PRECONDITION+ error. # # Queries inside read-write transactions might return +ABORTED+. If # this occurs, the application should restart the transaction from # the beginning. See Transaction for more details. # # Larger result sets can be fetched in streaming fashion by calling # ExecuteStreamingSql instead. # # @param session [String] # Required. The session in which the SQL query should be performed. # @param sql [String] # Required. The SQL query string. # @param transaction [Google::Spanner::V1::TransactionSelector] # The transaction to use. If none is provided, the default is a # temporary read-only transaction with strong concurrency. # @param params [Google::Protobuf::Struct] # The SQL query string can contain parameter placeholders. A parameter # placeholder consists of +'@'+ followed by the parameter # name. Parameter names consist of any combination of letters, # numbers, and underscores. # # Parameters can appear anywhere that a literal value is expected. The same # parameter name can be used more than once, for example: # +"WHERE id > @msg_id AND id < @msg_id + 100"+ # # It is an error to execute an SQL query with unbound parameters. # # Parameter values are specified using +params+, which is a JSON # object whose keys are parameter names, and whose values are the # corresponding parameter values. # @param param_types [Hash{String => Google::Spanner::V1::Type}] # It is not always possible for Cloud Spanner to infer the right SQL type # from a JSON value. For example, values of type +BYTES+ and values # of type +STRING+ both appear in Params as JSON strings. # # In these cases, +param_types+ can be used to specify the exact # SQL type for some or all of the SQL query parameters. See the # definition of Type for more information # about SQL types. # @param resume_token [String] # If this request is resuming a previously interrupted SQL query # execution, +resume_token+ should be copied from the last # PartialResultSet yielded before the interruption. Doing this # enables the new SQL query execution to resume where the last one left # off. The rest of the request parameters must exactly match the # request that yielded this token. # @param query_mode [Google::Spanner::V1::ExecuteSqlRequest::QueryMode] # Used to control the amount of debugging information returned in # ResultSetStats. # @param options [Google::Gax::CallOptions] # Overrides the default settings for this call, e.g, timeout, # retries, etc. # @return [Google::Spanner::V1::ResultSet] # @raise [Google::Gax::GaxError] if the RPC is aborted. # @example # require "google/cloud/spanner/v1/spanner_client" # # SpannerClient = Google::Cloud::Spanner::V1::SpannerClient # # spanner_client = SpannerClient.new # formatted_session = SpannerClient.session_path("[PROJECT]", "[INSTANCE]", "[DATABASE]", "[SESSION]") # sql = '' # response = spanner_client.execute_sql(formatted_session, sql) def execute_sql \ session, sql, transaction: nil, params: nil, param_types: nil, resume_token: nil, query_mode: nil, options: nil req = Google::Spanner::V1::ExecuteSqlRequest.new({ session: session, sql: sql, transaction: transaction, params: params, param_types: param_types, resume_token: resume_token, query_mode: query_mode }.delete_if { |_, v| v.nil? }) @execute_sql.call(req, ) end # Like ExecuteSql, except returns the result # set as a stream. Unlike ExecuteSql, there # is no limit on the size of the returned result set. However, no # individual row in the result set can exceed 100 MiB, and no # column value can exceed 10 MiB. # # @param session [String] # Required. The session in which the SQL query should be performed. # @param sql [String] # Required. The SQL query string. # @param transaction [Google::Spanner::V1::TransactionSelector] # The transaction to use. If none is provided, the default is a # temporary read-only transaction with strong concurrency. # @param params [Google::Protobuf::Struct] # The SQL query string can contain parameter placeholders. A parameter # placeholder consists of +'@'+ followed by the parameter # name. Parameter names consist of any combination of letters, # numbers, and underscores. # # Parameters can appear anywhere that a literal value is expected. The same # parameter name can be used more than once, for example: # +"WHERE id > @msg_id AND id < @msg_id + 100"+ # # It is an error to execute an SQL query with unbound parameters. # # Parameter values are specified using +params+, which is a JSON # object whose keys are parameter names, and whose values are the # corresponding parameter values. # @param param_types [Hash{String => Google::Spanner::V1::Type}] # It is not always possible for Cloud Spanner to infer the right SQL type # from a JSON value. For example, values of type +BYTES+ and values # of type +STRING+ both appear in Params as JSON strings. # # In these cases, +param_types+ can be used to specify the exact # SQL type for some or all of the SQL query parameters. See the # definition of Type for more information # about SQL types. # @param resume_token [String] # If this request is resuming a previously interrupted SQL query # execution, +resume_token+ should be copied from the last # PartialResultSet yielded before the interruption. Doing this # enables the new SQL query execution to resume where the last one left # off. The rest of the request parameters must exactly match the # request that yielded this token. # @param query_mode [Google::Spanner::V1::ExecuteSqlRequest::QueryMode] # Used to control the amount of debugging information returned in # ResultSetStats. # @param options [Google::Gax::CallOptions] # Overrides the default settings for this call, e.g, timeout, # retries, etc. # @return [Enumerable<Google::Spanner::V1::PartialResultSet>] # An enumerable of Google::Spanner::V1::PartialResultSet instances. # # @raise [Google::Gax::GaxError] if the RPC is aborted. # @example # require "google/cloud/spanner/v1/spanner_client" # # SpannerClient = Google::Cloud::Spanner::V1::SpannerClient # # spanner_client = SpannerClient.new # formatted_session = SpannerClient.session_path("[PROJECT]", "[INSTANCE]", "[DATABASE]", "[SESSION]") # sql = '' # spanner_client.execute_streaming_sql(formatted_session, sql).each do |element| # # Process element. # end def execute_streaming_sql \ session, sql, transaction: nil, params: nil, param_types: nil, resume_token: nil, query_mode: nil, options: nil req = Google::Spanner::V1::ExecuteSqlRequest.new({ session: session, sql: sql, transaction: transaction, params: params, param_types: param_types, resume_token: resume_token, query_mode: query_mode }.delete_if { |_, v| v.nil? }) @execute_streaming_sql.call(req, ) end # Reads rows from the database using key lookups and scans, as a # simple key/value style alternative to # ExecuteSql. This method cannot be used to # return a result set larger than 10 MiB; if the read matches more # data than that, the read fails with a +FAILED_PRECONDITION+ # error. # # Reads inside read-write transactions might return +ABORTED+. If # this occurs, the application should restart the transaction from # the beginning. See Transaction for more details. # # Larger result sets can be yielded in streaming fashion by calling # StreamingRead instead. # # @param session [String] # Required. The session in which the read should be performed. # @param table [String] # Required. The name of the table in the database to be read. # @param columns [Array<String>] # The columns of Table to be returned for each row matching # this request. # @param key_set [Google::Spanner::V1::KeySet] # Required. +key_set+ identifies the rows to be yielded. +key_set+ names the # primary keys of the rows in Table to be yielded, unless Index # is present. If Index is present, then Key_set instead names # index keys in Index. # # Rows are yielded in table primary key order (if Index is empty) # or index key order (if Index is non-empty). # # It is not an error for the +key_set+ to name rows that do not # exist in the database. Read yields nothing for nonexistent rows. # @param transaction [Google::Spanner::V1::TransactionSelector] # The transaction to use. If none is provided, the default is a # temporary read-only transaction with strong concurrency. # @param index [String] # If non-empty, the name of an index on Table. This index is # used instead of the table primary key when interpreting Key_set # and sorting result rows. See Key_set for further information. # @param limit [Integer] # If greater than zero, only the first +limit+ rows are yielded. If +limit+ # is zero, the default is no limit. # @param resume_token [String] # If this request is resuming a previously interrupted read, # +resume_token+ should be copied from the last # PartialResultSet yielded before the interruption. Doing this # enables the new read to resume where the last read left off. The # rest of the request parameters must exactly match the request # that yielded this token. # @param options [Google::Gax::CallOptions] # Overrides the default settings for this call, e.g, timeout, # retries, etc. # @return [Google::Spanner::V1::ResultSet] # @raise [Google::Gax::GaxError] if the RPC is aborted. # @example # require "google/cloud/spanner/v1/spanner_client" # # KeySet = Google::Spanner::V1::KeySet # SpannerClient = Google::Cloud::Spanner::V1::SpannerClient # # spanner_client = SpannerClient.new # formatted_session = SpannerClient.session_path("[PROJECT]", "[INSTANCE]", "[DATABASE]", "[SESSION]") # table = '' # columns = [] # key_set = KeySet.new # response = spanner_client.read(formatted_session, table, columns, key_set) def read \ session, table, columns, key_set, transaction: nil, index: nil, limit: nil, resume_token: nil, options: nil req = Google::Spanner::V1::ReadRequest.new({ session: session, table: table, columns: columns, key_set: key_set, transaction: transaction, index: index, limit: limit, resume_token: resume_token }.delete_if { |_, v| v.nil? }) @read.call(req, ) end # Like Read, except returns the result set as a # stream. Unlike Read, there is no limit on the # size of the returned result set. However, no individual row in # the result set can exceed 100 MiB, and no column value can exceed # 10 MiB. # # @param session [String] # Required. The session in which the read should be performed. # @param table [String] # Required. The name of the table in the database to be read. # @param columns [Array<String>] # The columns of Table to be returned for each row matching # this request. # @param key_set [Google::Spanner::V1::KeySet] # Required. +key_set+ identifies the rows to be yielded. +key_set+ names the # primary keys of the rows in Table to be yielded, unless Index # is present. If Index is present, then Key_set instead names # index keys in Index. # # Rows are yielded in table primary key order (if Index is empty) # or index key order (if Index is non-empty). # # It is not an error for the +key_set+ to name rows that do not # exist in the database. Read yields nothing for nonexistent rows. # @param transaction [Google::Spanner::V1::TransactionSelector] # The transaction to use. If none is provided, the default is a # temporary read-only transaction with strong concurrency. # @param index [String] # If non-empty, the name of an index on Table. This index is # used instead of the table primary key when interpreting Key_set # and sorting result rows. See Key_set for further information. # @param limit [Integer] # If greater than zero, only the first +limit+ rows are yielded. If +limit+ # is zero, the default is no limit. # @param resume_token [String] # If this request is resuming a previously interrupted read, # +resume_token+ should be copied from the last # PartialResultSet yielded before the interruption. Doing this # enables the new read to resume where the last read left off. The # rest of the request parameters must exactly match the request # that yielded this token. # @param options [Google::Gax::CallOptions] # Overrides the default settings for this call, e.g, timeout, # retries, etc. # @return [Enumerable<Google::Spanner::V1::PartialResultSet>] # An enumerable of Google::Spanner::V1::PartialResultSet instances. # # @raise [Google::Gax::GaxError] if the RPC is aborted. # @example # require "google/cloud/spanner/v1/spanner_client" # # KeySet = Google::Spanner::V1::KeySet # SpannerClient = Google::Cloud::Spanner::V1::SpannerClient # # spanner_client = SpannerClient.new # formatted_session = SpannerClient.session_path("[PROJECT]", "[INSTANCE]", "[DATABASE]", "[SESSION]") # table = '' # columns = [] # key_set = KeySet.new # spanner_client.streaming_read(formatted_session, table, columns, key_set).each do |element| # # Process element. # end def streaming_read \ session, table, columns, key_set, transaction: nil, index: nil, limit: nil, resume_token: nil, options: nil req = Google::Spanner::V1::ReadRequest.new({ session: session, table: table, columns: columns, key_set: key_set, transaction: transaction, index: index, limit: limit, resume_token: resume_token }.delete_if { |_, v| v.nil? }) @streaming_read.call(req, ) end # Begins a new transaction. This step can often be skipped: # Read, ExecuteSql and # Commit can begin a new transaction as a # side-effect. # # @param session [String] # Required. The session in which the transaction runs. # @param options_ [Google::Spanner::V1::TransactionOptions] # Required. Options for the new transaction. # @param options [Google::Gax::CallOptions] # Overrides the default settings for this call, e.g, timeout, # retries, etc. # @return [Google::Spanner::V1::Transaction] # @raise [Google::Gax::GaxError] if the RPC is aborted. # @example # require "google/cloud/spanner/v1/spanner_client" # # SpannerClient = Google::Cloud::Spanner::V1::SpannerClient # TransactionOptions = Google::Spanner::V1::TransactionOptions # # spanner_client = SpannerClient.new # formatted_session = SpannerClient.session_path("[PROJECT]", "[INSTANCE]", "[DATABASE]", "[SESSION]") # options_ = TransactionOptions.new # response = spanner_client.begin_transaction(formatted_session, options_) def begin_transaction \ session, , options: nil req = Google::Spanner::V1::BeginTransactionRequest.new({ session: session, options: }.delete_if { |_, v| v.nil? }) @begin_transaction.call(req, ) end # Commits a transaction. The request includes the mutations to be # applied to rows in the database. # # +Commit+ might return an +ABORTED+ error. This can occur at any time; # commonly, the cause is conflicts with concurrent # transactions. However, it can also happen for a variety of other # reasons. If +Commit+ returns +ABORTED+, the caller should re-attempt # the transaction from the beginning, re-using the same session. # # @param session [String] # Required. The session in which the transaction to be committed is running. # @param mutations [Array<Google::Spanner::V1::Mutation>] # The mutations to be executed when this transaction commits. All # mutations are applied atomically, in the order they appear in # this list. # @param transaction_id [String] # Commit a previously-started transaction. # @param single_use_transaction [Google::Spanner::V1::TransactionOptions] # Execute mutations in a temporary transaction. Note that unlike # commit of a previously-started transaction, commit with a # temporary transaction is non-idempotent. That is, if the # +CommitRequest+ is sent to Cloud Spanner more than once (for # instance, due to retries in the application, or in the # transport library), it is possible that the mutations are # executed more than once. If this is undesirable, use # BeginTransaction and # Commit instead. # @param options [Google::Gax::CallOptions] # Overrides the default settings for this call, e.g, timeout, # retries, etc. # @return [Google::Spanner::V1::CommitResponse] # @raise [Google::Gax::GaxError] if the RPC is aborted. # @example # require "google/cloud/spanner/v1/spanner_client" # # SpannerClient = Google::Cloud::Spanner::V1::SpannerClient # # spanner_client = SpannerClient.new # formatted_session = SpannerClient.session_path("[PROJECT]", "[INSTANCE]", "[DATABASE]", "[SESSION]") # mutations = [] # response = spanner_client.commit(formatted_session, mutations) def commit \ session, mutations, transaction_id: nil, single_use_transaction: nil, options: nil req = Google::Spanner::V1::CommitRequest.new({ session: session, mutations: mutations, transaction_id: transaction_id, single_use_transaction: single_use_transaction }.delete_if { |_, v| v.nil? }) @commit.call(req, ) end # Rolls back a transaction, releasing any locks it holds. It is a good # idea to call this for any transaction that includes one or more # Read or ExecuteSql requests and # ultimately decides not to commit. # # +Rollback+ returns +OK+ if it successfully aborts the transaction, the # transaction was already aborted, or the transaction is not # found. +Rollback+ never returns +ABORTED+. # # @param session [String] # Required. The session in which the transaction to roll back is running. # @param transaction_id [String] # Required. The transaction to roll back. # @param options [Google::Gax::CallOptions] # Overrides the default settings for this call, e.g, timeout, # retries, etc. # @raise [Google::Gax::GaxError] if the RPC is aborted. # @example # require "google/cloud/spanner/v1/spanner_client" # # SpannerClient = Google::Cloud::Spanner::V1::SpannerClient # # spanner_client = SpannerClient.new # formatted_session = SpannerClient.session_path("[PROJECT]", "[INSTANCE]", "[DATABASE]", "[SESSION]") # transaction_id = '' # spanner_client.rollback(formatted_session, transaction_id) def rollback \ session, transaction_id, options: nil req = Google::Spanner::V1::RollbackRequest.new({ session: session, transaction_id: transaction_id }.delete_if { |_, v| v.nil? }) @rollback.call(req, ) nil end end |
Class Method Details
.database_path(project, instance, database) ⇒ String
Returns a fully-qualified database resource name string.
78 79 80 81 82 83 84 |
# File 'lib/google/cloud/spanner/v1/spanner_client.rb', line 78 def self.database_path project, instance, database DATABASE_PATH_TEMPLATE.render( :"project" => project, :"instance" => instance, :"database" => database ) end |
.match_database_from_database_name(database_name) ⇒ String
Parses the database from a database resource.
118 119 120 |
# File 'lib/google/cloud/spanner/v1/spanner_client.rb', line 118 def self.match_database_from_database_name database_name DATABASE_PATH_TEMPLATE.match(database_name)["database"] end |
.match_database_from_session_name(session_name) ⇒ String
Parses the database from a session resource.
139 140 141 |
# File 'lib/google/cloud/spanner/v1/spanner_client.rb', line 139 def self.match_database_from_session_name session_name SESSION_PATH_TEMPLATE.match(session_name)["database"] end |
.match_instance_from_database_name(database_name) ⇒ String
Parses the instance from a database resource.
111 112 113 |
# File 'lib/google/cloud/spanner/v1/spanner_client.rb', line 111 def self.match_instance_from_database_name database_name DATABASE_PATH_TEMPLATE.match(database_name)["instance"] end |
.match_instance_from_session_name(session_name) ⇒ String
Parses the instance from a session resource.
132 133 134 |
# File 'lib/google/cloud/spanner/v1/spanner_client.rb', line 132 def self.match_instance_from_session_name session_name SESSION_PATH_TEMPLATE.match(session_name)["instance"] end |
.match_project_from_database_name(database_name) ⇒ String
Parses the project from a database resource.
104 105 106 |
# File 'lib/google/cloud/spanner/v1/spanner_client.rb', line 104 def self.match_project_from_database_name database_name DATABASE_PATH_TEMPLATE.match(database_name)["project"] end |
.match_project_from_session_name(session_name) ⇒ String
Parses the project from a session resource.
125 126 127 |
# File 'lib/google/cloud/spanner/v1/spanner_client.rb', line 125 def self.match_project_from_session_name session_name SESSION_PATH_TEMPLATE.match(session_name)["project"] end |
.match_session_from_session_name(session_name) ⇒ String
Parses the session from a session resource.
146 147 148 |
# File 'lib/google/cloud/spanner/v1/spanner_client.rb', line 146 def self.match_session_from_session_name session_name SESSION_PATH_TEMPLATE.match(session_name)["session"] end |
.session_path(project, instance, database, session) ⇒ String
Returns a fully-qualified session resource name string.
92 93 94 95 96 97 98 99 |
# File 'lib/google/cloud/spanner/v1/spanner_client.rb', line 92 def self.session_path project, instance, database, session SESSION_PATH_TEMPLATE.render( :"project" => project, :"instance" => instance, :"database" => database, :"session" => session ) end |
Instance Method Details
#begin_transaction(session, options_, options: nil) ⇒ Google::Spanner::V1::Transaction
Begins a new transaction. This step can often be skipped: Read, ExecuteSql and Commit can begin a new transaction as a side-effect.
740 741 742 743 744 745 746 747 748 749 |
# File 'lib/google/cloud/spanner/v1/spanner_client.rb', line 740 def begin_transaction \ session, , options: nil req = Google::Spanner::V1::BeginTransactionRequest.new({ session: session, options: }.delete_if { |_, v| v.nil? }) @begin_transaction.call(req, ) end |
#commit(session, mutations, transaction_id: nil, single_use_transaction: nil, options: nil) ⇒ Google::Spanner::V1::CommitResponse
Commits a transaction. The request includes the mutations to be applied to rows in the database.
+Commit+ might return an +ABORTED+ error. This can occur at any time; commonly, the cause is conflicts with concurrent transactions. However, it can also happen for a variety of other reasons. If +Commit+ returns +ABORTED+, the caller should re-attempt the transaction from the beginning, re-using the same session.
793 794 795 796 797 798 799 800 801 802 803 804 805 806 |
# File 'lib/google/cloud/spanner/v1/spanner_client.rb', line 793 def commit \ session, mutations, transaction_id: nil, single_use_transaction: nil, options: nil req = Google::Spanner::V1::CommitRequest.new({ session: session, mutations: mutations, transaction_id: transaction_id, single_use_transaction: single_use_transaction }.delete_if { |_, v| v.nil? }) @commit.call(req, ) end |
#create_session(database, options: nil) ⇒ Google::Spanner::V1::Session
Creates a new session. A session can be used to perform transactions that read and/or modify data in a Cloud Spanner database. Sessions are meant to be reused for many consecutive transactions.
Sessions can only execute one transaction at a time. To execute multiple concurrent read-write/write-only transactions, create multiple sessions. Note that standalone reads and queries use a transaction internally, and count toward the one transaction limit.
Cloud Spanner limits the number of sessions that can exist at any given time; thus, it is a good idea to delete idle and/or unneeded sessions. Aside from explicit deletes, Cloud Spanner can delete sessions for which no operations are sent for more than an hour. If a session is deleted, requests to it return +NOT_FOUND+.
Idle sessions can be kept alive by sending a trivial SQL query periodically, e.g., +"SELECT 1"+.
298 299 300 301 302 303 304 305 |
# File 'lib/google/cloud/spanner/v1/spanner_client.rb', line 298 def create_session \ database, options: nil req = Google::Spanner::V1::CreateSessionRequest.new({ database: database }.delete_if { |_, v| v.nil? }) @create_session.call(req, ) end |
#delete_session(name, options: nil) ⇒ Object
Ends a session, releasing server resources associated with it.
353 354 355 356 357 358 359 360 361 |
# File 'lib/google/cloud/spanner/v1/spanner_client.rb', line 353 def delete_session \ name, options: nil req = Google::Spanner::V1::DeleteSessionRequest.new({ name: name }.delete_if { |_, v| v.nil? }) @delete_session.call(req, ) nil end |
#execute_sql(session, sql, transaction: nil, params: nil, param_types: nil, resume_token: nil, query_mode: nil, options: nil) ⇒ Google::Spanner::V1::ResultSet
Executes an SQL query, returning all rows in a single reply. This method cannot be used to return a result set larger than 10 MiB; if the query yields more data than that, the query fails with a +FAILED_PRECONDITION+ error.
Queries inside read-write transactions might return +ABORTED+. If this occurs, the application should restart the transaction from the beginning. See Transaction for more details.
Larger result sets can be fetched in streaming fashion by calling ExecuteStreamingSql instead.
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 |
# File 'lib/google/cloud/spanner/v1/spanner_client.rb', line 431 def execute_sql \ session, sql, transaction: nil, params: nil, param_types: nil, resume_token: nil, query_mode: nil, options: nil req = Google::Spanner::V1::ExecuteSqlRequest.new({ session: session, sql: sql, transaction: transaction, params: params, param_types: param_types, resume_token: resume_token, query_mode: query_mode }.delete_if { |_, v| v.nil? }) @execute_sql.call(req, ) end |
#execute_streaming_sql(session, sql, transaction: nil, params: nil, param_types: nil, resume_token: nil, query_mode: nil, options: nil) ⇒ Enumerable<Google::Spanner::V1::PartialResultSet>
Like ExecuteSql, except returns the result set as a stream. Unlike ExecuteSql, there is no limit on the size of the returned result set. However, no individual row in the result set can exceed 100 MiB, and no column value can exceed 10 MiB.
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 |
# File 'lib/google/cloud/spanner/v1/spanner_client.rb', line 518 def execute_streaming_sql \ session, sql, transaction: nil, params: nil, param_types: nil, resume_token: nil, query_mode: nil, options: nil req = Google::Spanner::V1::ExecuteSqlRequest.new({ session: session, sql: sql, transaction: transaction, params: params, param_types: param_types, resume_token: resume_token, query_mode: query_mode }.delete_if { |_, v| v.nil? }) @execute_streaming_sql.call(req, ) end |
#get_session(name, options: nil) ⇒ Google::Spanner::V1::Session
Gets a session. Returns +NOT_FOUND+ if the session does not exist. This is mainly useful for determining whether a session is still alive.
327 328 329 330 331 332 333 334 |
# File 'lib/google/cloud/spanner/v1/spanner_client.rb', line 327 def get_session \ name, options: nil req = Google::Spanner::V1::GetSessionRequest.new({ name: name }.delete_if { |_, v| v.nil? }) @get_session.call(req, ) end |
#read(session, table, columns, key_set, transaction: nil, index: nil, limit: nil, resume_token: nil, options: nil) ⇒ Google::Spanner::V1::ResultSet
Reads rows from the database using key lookups and scans, as a simple key/value style alternative to ExecuteSql. This method cannot be used to return a result set larger than 10 MiB; if the read matches more data than that, the read fails with a +FAILED_PRECONDITION+ error.
Reads inside read-write transactions might return +ABORTED+. If this occurs, the application should restart the transaction from the beginning. See Transaction for more details.
Larger result sets can be yielded in streaming fashion by calling StreamingRead instead.
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 |
# File 'lib/google/cloud/spanner/v1/spanner_client.rb', line 606 def read \ session, table, columns, key_set, transaction: nil, index: nil, limit: nil, resume_token: nil, options: nil req = Google::Spanner::V1::ReadRequest.new({ session: session, table: table, columns: columns, key_set: key_set, transaction: transaction, index: index, limit: limit, resume_token: resume_token }.delete_if { |_, v| v.nil? }) @read.call(req, ) end |
#rollback(session, transaction_id, options: nil) ⇒ Object
Rolls back a transaction, releasing any locks it holds. It is a good idea to call this for any transaction that includes one or more Read or ExecuteSql requests and ultimately decides not to commit.
+Rollback+ returns +OK+ if it successfully aborts the transaction, the transaction was already aborted, or the transaction is not found. +Rollback+ never returns +ABORTED+.
835 836 837 838 839 840 841 842 843 844 845 |
# File 'lib/google/cloud/spanner/v1/spanner_client.rb', line 835 def rollback \ session, transaction_id, options: nil req = Google::Spanner::V1::RollbackRequest.new({ session: session, transaction_id: transaction_id }.delete_if { |_, v| v.nil? }) @rollback.call(req, ) nil end |
#streaming_read(session, table, columns, key_set, transaction: nil, index: nil, limit: nil, resume_token: nil, options: nil) ⇒ Enumerable<Google::Spanner::V1::PartialResultSet>
Like Read, except returns the result set as a stream. Unlike Read, there is no limit on the size of the returned result set. However, no individual row in the result set can exceed 100 MiB, and no column value can exceed 10 MiB.
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 |
# File 'lib/google/cloud/spanner/v1/spanner_client.rb', line 692 def streaming_read \ session, table, columns, key_set, transaction: nil, index: nil, limit: nil, resume_token: nil, options: nil req = Google::Spanner::V1::ReadRequest.new({ session: session, table: table, columns: columns, key_set: key_set, transaction: transaction, index: index, limit: limit, resume_token: resume_token }.delete_if { |_, v| v.nil? }) @streaming_read.call(req, ) end |