Class: Druid::Client
- Inherits:
-
Object
- Object
- Druid::Client
- Defined in:
- lib/druid/client.rb
Instance Method Summary collapse
- #data_source(source) ⇒ Object
- #data_source_uri(source) ⇒ Object
- #data_sources ⇒ Object
- #ds ⇒ Object
-
#initialize(zookeeper_uri, opts = nil) ⇒ Client
constructor
A new instance of Client.
- #query(id, &block) ⇒ Object
- #send(query) ⇒ Object
- #zookeeper_caching_management!(zookeeper_uri, opts) ⇒ Object
Constructor Details
#initialize(zookeeper_uri, opts = nil) ⇒ Client
Returns a new instance of Client.
4 5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/druid/client.rb', line 4 def initialize(zookeeper_uri, opts = nil) opts ||= {} if opts[:static_setup] && !opts[:fallback] @static = opts[:static_setup] else @backup = opts[:static_setup] if opts[:fallback] zookeeper_caching_management!(zookeeper_uri, opts) end @http_timeout = opts[:http_timeout] || 2 * 60 end |
Instance Method Details
#data_source(source) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/druid/client.rb', line 72 def data_source(source) uri = data_source_uri(source) raise "data source #{source} (currently) not available" unless uri = "#{uri.path}datasources/#{source.split('/').last}" req = Net::HTTP::Get.new() response = Net::HTTP.new(uri.host, uri.port).start do |http| http.read_timeout = @http_timeout http.request(req) end if response.code == "200" = JSON.parse(response.body) .define_singleton_method(:dimensions) { self['dimensions'] } .define_singleton_method(:metrics) { self['metrics'] } else raise "Request failed: #{response.code}: #{response.body}" end end |
#data_source_uri(source) ⇒ Object
63 64 65 66 67 68 69 70 |
# File 'lib/druid/client.rb', line 63 def data_source_uri(source) uri = (ds.nil? ? @static : ds)[source] begin return URI(uri) if uri rescue return URI(@backup) if @backup end end |
#data_sources ⇒ Object
59 60 61 |
# File 'lib/druid/client.rb', line 59 def data_sources (ds.nil? ? @static : ds).keys end |
#ds ⇒ Object
55 56 57 |
# File 'lib/druid/client.rb', line 55 def ds @cached_data_sources || (@zk.data_sources unless @zk.nil?) end |
#query(id, &block) ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/druid/client.rb', line 36 def query(id, &block) uri = data_source_uri(id) raise "data source #{id} (currently) not available" unless uri query = Query.new(id, self) return query unless block send query end |
#send(query) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/druid/client.rb', line 17 def send(query) uri = data_source_uri(query.source) raise "data source #{query.source} (currently) not available" unless uri req = Net::HTTP::Post.new(uri.path, {'Content-Type' =>'application/json'}) req.body = query.to_json response = Net::HTTP.new(uri.host, uri.port).start do |http| http.read_timeout = @http_timeout http.request(req) end if response.code == "200" JSON.parse(response.body).map{ |row| ResponseRow.new(row) } else raise "Request failed: #{response.code}: #{response.body}" end end |
#zookeeper_caching_management!(zookeeper_uri, opts) ⇒ Object
45 46 47 48 49 50 51 52 53 |
# File 'lib/druid/client.rb', line 45 def zookeeper_caching_management!(zookeeper_uri, opts) @zk = ZooHandler.new(zookeeper_uri, opts) unless opts[:zk_keepalive] @cached_data_sources = @zk.data_sources unless @zk.nil? @zk.close! end end |