We are using ruby and looking to switch from view-based to n1ql and have some question:
Is N1QL support in Ruby? Looking at http://www.rubydoc.info/github/couchbase/couchbase-ruby-client/Couchbase%2FBucket%3Aquery it says ‘Perform N1QL query to the cluster. This API is experimental and subject to change in future.’
A can query couchbase but how to do I set the consistency flag?
I tried
connection.query(query_limit, {:scan_consistency => 'request_plus'}}
but it seems it did not have affect.
Any update on how to set the consistency in the ruby gem?
avsej
August 22, 2018, 7:55pm
3
Hi @dipen_patel , with this patch, current master (and future 2.0 release) will support request_plus scan consistency as well as other query options.
committed 07:53PM - 22 Aug 18 UTC
Change-Id: Ib7e43a2cf70fdb0d1955cca461efe6e149a29bda
Reviewed-on: http://review.… couchbase.org/98657
Reviewed-by: Sergey Avseyev <sergey.avseyev@gmail.com>
Tested-by: Sergey Avseyev <sergey.avseyev@gmail.com>
Thanks for feedback
avsej:
Thanks for feedback
@avsej , This would be great. Do you know when it would be release?
I saw your updates to the SDK, and what to know if you can provide me more details?
When will it be released?
Minium ruby version?
It seems the libcouchbase requirement is gone is that correct?
Thanks.
avsej
May 28, 2020, 12:39pm
6
At this moment minimum version is MRI ruby 2.6. In couple of days there will be release with most features covered.
Yes, libcouchbase is not required, all dependencies included. I’m trying to prebuild binaries for MacOS X and some other platforms (Windows is not included yet). When installing from source, C, C++ compilers and OpenSSL headers are also required.
This is an example of Query API
require 'couchbase'
require 'byebug'
include Couchbase
options = Cluster::ClusterOptions.new
options.authenticate("Administrator", "password")
cluster = Cluster.connect("couchbase://localhost", options)
options = Cluster::QueryOptions.new
options.named_parameters({type: "hotel"})
options.metrics = true
res = cluster.query("SELECT * FROM `travel-sample` WHERE type = $type LIMIT 10", options)
res.rows.each do |row|
puts "#{row["travel-sample"]["country"]}. #{row["travel-sample"]["name"]}"
end
puts "Status: #{res.meta_data.status}. Execution time: #{res.meta_data.metrics.execution_time}"
Thanks one more question
Will it support N1QL Consistency flag?
avsej
May 28, 2020, 12:47pm
8
Yes, it will. I will add example showing this case.
avsej:
require 'couchbase'
require 'byebug'
include Couchbase
options = Cluster::ClusterOptions.new
options.authenticate("Administrator", "password")
cluster = Cluster.connect("couchbase://localhost", options)
options = Cluster::QueryOptions.new
options.named_parameters({type: "hotel"})
options.metrics = true
res = cluster.query("SELECT * FROM `travel-sample` WHERE type = $type LIMIT 10", options)
res.rows.each do |row|
puts "#{row["travel-sample"]["country"]}. #{row["travel-sample"]["name"]}"
end
puts "Status: #{res.meta_data.status}. Execution time: #{res.meta_data.metrics.execution_time}"
Thanks, that is the biggest factor we cannot move off view and looking at other solutions.
We will be to try it and provide feedback quickly
avsej
June 1, 2020, 1:03pm
10
So I’ve added example for query with consistency. Currently we have two ways to do it:
where the query engine will wait for all mutations that have to be indexed by the time it received new request
# METHOD 1
#
# The easiest way to enforce consistency is just set scan_consistency option to :request_plus
# Note: it waits until all documents queued to be indexed when the request has been receive,
# will be proceed and stored in the index. Often more granular approach is possible (skip to the next section)
puts "METHOD 1: using :request_plus"
random_number = rand(0..1_000_000_000)
collection.upsert("user:#{random_number}", {
"name" => ["Brass", "Doorknob"],
"email" => "brass.doorknob@example.com",
"data" => random_number,
})
options = Cluster::QueryOptions.new
options.timeout = 10_000
options.positional_parameters(["Brass"])
options.scan_consistency = :request_plus
res = cluster.query("SELECT name, email, data, META(`default`).id FROM `default` WHERE $1 IN name", options)
res.rows.each do |row|
This file has been truncated. show original
More granular approach, where application knows which mutation has to be applied before the request must be executed:
# METHOD 2
#
# More granular and light-weight approach is to use AT_PLUS consistency.
# The application need to to know exactly which mutation affect the result,
# and supply mutation tokens from those operations.
puts "METHOD 2: using MutationState"
random_number = rand(0..1_000_000_000)
res = collection.upsert("user:#{random_number}", {
"name" => ["Brass", "Doorknob"],
"email" => "brass.doorknob@example.com",
"data" => random_number,
})
state = MutationState.new(res.mutation_token)
# state.add(*tokens) could be used to add more tokens
options = Cluster::QueryOptions.new
options.timeout = 10_000
options.positional_parameters(["Brass"])
This file has been truncated. show original
This examples uses some features that are on master branch, but hadn’t been included into 3.0.0.alpha.1, so to try it, you have to install library from git.