Class: Druid::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/druid/client.rb

Constant Summary collapse

TIMEOUT =
2 * 60 * 1000

Instance Method Summary collapse

Constructor Details

#initialize(zookeeper_uri, opts = nil) ⇒ Client



5
6
7
8
9
10
11
12
13
14
# File 'lib/druid/client.rb', line 5

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
end

Instance Method Details

#data_source(source) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/druid/client.rb', line 71

def data_source(source)
  uri = data_source_uri(source)
  raise "data source #{source} (currently) not available" unless uri

  meta_path = "#{uri.path}datasources/#{source.split('/').last}"

  req = Net::HTTP::Get.new(meta_path)

  response = Net::HTTP.new(uri.host, uri.port).start do |http|
    http.read_timeout = TIMEOUT
    http.request(req)
  end

  if response.code == "200"
    meta = JSON.parse(response.body)
    meta.define_singleton_method(:dimensions) { self['dimensions'] }
    meta.define_singleton_method(:metrics) { self['metrics'] }
    meta
  else
    raise "Request failed: #{response.code}: #{response.body}"
  end
end

#data_source_uri(source) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/druid/client.rb', line 62

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_sourcesObject



58
59
60
# File 'lib/druid/client.rb', line 58

def data_sources
  (ds.nil? ? @static : ds).keys
end

#dsObject



54
55
56
# File 'lib/druid/client.rb', line 54

def ds
  @cached_data_sources || (@zk.data_sources unless @zk.nil?)
end

#query(id, &block) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/druid/client.rb', line 35

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



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/druid/client.rb', line 16

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 = 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



44
45
46
47
48
49
50
51
52
# File 'lib/druid/client.rb', line 44

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