Skip to content
This repository has been archived by the owner on Feb 19, 2018. It is now read-only.

Commit

Permalink
Add documentation for dripl and some minor clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
Hagen Rother committed Apr 4, 2013
1 parent 3fff15d commit f597ae2
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 45 deletions.
51 changes: 26 additions & 25 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
ruby-druid (0.0.3)
ruby-druid (0.0.4)
rest-client
zk

Expand All @@ -11,34 +11,35 @@ GEM
activesupport (3.2.13)
i18n (= 0.6.1)
multi_json (~> 1.0)
addressable (2.3.2)
addressable (2.3.3)
awesome_print (1.1.0)
bond (0.4.3)
coderay (1.0.8)
coderay (1.0.9)
crack (0.3.2)
diff-lcs (1.1.3)
guard (1.6.2)
diff-lcs (1.2.2)
formatador (0.2.4)
guard (1.7.0)
formatador (>= 0.2.4)
listen (>= 0.6.0)
lumberjack (>= 1.0.2)
pry (>= 0.9.10)
terminal-table (>= 1.4.3)
thor (>= 0.14.6)
guard-bundler (1.0.0)
bundler (~> 1.0)
guard (~> 1.1)
guard-rspec (2.4.0)
guard-rspec (2.5.2)
guard (>= 1.1)
rspec (~> 2.11)
i18n (0.6.1)
listen (0.7.2)
listen (0.7.3)
little-plugger (1.1.3)
logging (1.7.2)
little-plugger (>= 1.1.3)
lumberjack (1.0.2)
lumberjack (1.0.3)
method_source (0.8.1)
mime-types (1.20.1)
mime-types (1.22)
multi_json (1.7.2)
pry (0.9.11.4)
pry (0.9.12)
coderay (~> 1.0.5)
method_source (~> 0.8)
slop (~> 3.4)
Expand All @@ -47,25 +48,25 @@ GEM
mime-types (>= 1.16)
ripl (0.7.0)
bond (~> 0.4.2)
rspec (2.12.0)
rspec-core (~> 2.12.0)
rspec-expectations (~> 2.12.0)
rspec-mocks (~> 2.12.0)
rspec-core (2.12.2)
rspec-expectations (2.12.1)
diff-lcs (~> 1.1.3)
rspec-mocks (2.12.2)
rspec (2.13.0)
rspec-core (~> 2.13.0)
rspec-expectations (~> 2.13.0)
rspec-mocks (~> 2.13.0)
rspec-core (2.13.1)
rspec-expectations (2.13.0)
diff-lcs (>= 1.1.3, < 2.0)
rspec-mocks (2.13.0)
ruby_gntp (0.3.4)
slop (3.4.3)
slop (3.4.4)
terminal-table (1.4.5)
thor (0.17.0)
webmock (1.9.0)
thor (0.18.1)
webmock (1.11.0)
addressable (>= 2.2.7)
crack (>= 0.1.7)
zk (1.7.5)
crack (>= 0.3.2)
zk (1.8.0)
logging (~> 1.7.2)
zookeeper (~> 1.4.0)
zookeeper (1.4.2)
zookeeper (1.4.3)

PLATFORMS
ruby
Expand Down
61 changes: 60 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,69 @@
# Ruby-druid
# ruby-druid

A ruby client for [druid](https://github.com/madvertise/druid).

ruby-druid generates complete JSON queries by chaining methods.
The resulting JSON can be send directly to a druid server or handled seperatly.

## bin/dripl

ruby-druid now includes a repl:

```ruby
$ bin/dripl
>> metrics
[
[0] "actions"
]

>> dimensions
[
[0] "actions"
]

>> long_sum(:actions)
+---------+
| actions |
+---------+
| 98575 |
+---------+

>> long_sum(:actions)[-7.days].granularity(:day)
+-------------------------------+----------+
| timestamp | actions |
+-------------------------------+----------+
| 2013-03-28T00:00:00.000+01:00 | 93371 |
| 2013-03-29T00:00:00.000+01:00 | 448200 |
| 2013-03-30T00:00:00.000+01:00 | 117167 |
| 2013-03-31T00:00:00.000+01:00 | 828321 |
| 2013-04-01T00:00:00.000+02:00 | 261578 |
| 2013-04-02T00:00:00.000+02:00 | 05149 |
| 2013-04-03T00:00:00.000+02:00 | 27512 |
| 2013-04-04T00:00:00.000+02:00 | 18897 |
+-------------------------------+----------+

>> long_sum(:actions)[-7.days].granularity(:day).properties
{
:dataSource => "events",
:granularity => {
:type => "period",
:period => "P1D",
:timeZone => "Europe/Berlin"
},
:intervals => [
[0] "2013-03-28T11:57:20+01:00/2013-04-04T11:57:20+02:00"
],
:queryType => :groupBy,
:aggregations => [
[0] {
:type => "longSum",
:name => :actions,
:fieldName => :actions
}
]
}
```

## Getting started

In your Gemfile:
Expand Down
28 changes: 24 additions & 4 deletions bin/dripl
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,36 @@ $:.unshift(File.join(File.expand_path("../..", __FILE__), 'lib'))

$0 = "dripl"

def zookeeper(value)
@zk_uri = value
end

def uri(value)
@uri = value
puts "using 'uri' in the config is deprecated, use 'zookeeper' instead"
zookeeper value
end

def source(value)
@source = value
end

driplrc = File.join(File.expand_path("../..", __FILE__), '.driplrc')
instance_eval(File.read(driplrc))
def options(value)
@options = value
end

begin
driplrc = File.read(File.join(File.expand_path("../..", __FILE__), '.driplrc'))
rescue
puts "You need to create a .driplrc, take a look at dot_driplrc_example"
exit 1
end

instance_eval(driplrc)

unless @zk_uri || (@options && @options[:static_setup])
puts "Your .driplrc is incomplete, please fix"
exit 1
end

require 'druid/console'
Druid::Console.new(@uri, @source)
Druid::Console.new(@zk_uri, @source, @options)
12 changes: 12 additions & 0 deletions dot_driplrc_example
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
## your zookeeper config. For static scenarios (i.e. ssh tunnels) see options
##
# zookeeper "localhost:2181/druid"

## using options, you can disable zookeeper lookup
## options[:static_setup], the key is the source name, the value is the brokers post uri
##
# options :static_setup => { 'example/events' => 'http://localhost:8080/druid/v2/' }

## dripl will default to use the first available data source. use this to override
##
# source "example/events"
3 changes: 2 additions & 1 deletion lib/druid/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ module Druid
class Client
TIMEOUT = 2 * 60 * 1000

def initialize(zookeeper_uri, opts = {})
def initialize(zookeeper_uri, opts = nil)
opts ||= {}
if opts[:static_setup]
@static = opts[:static_setup]
else
Expand Down
19 changes: 12 additions & 7 deletions lib/druid/console.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@
require 'druid'

Ripl::Shell.class_eval do
def format_query_result(result)
def format_query_result(result, query)

include_timestamp = query.properties[:granularity] != 'all'

Terminal::Table.new({
headings: result.last.keys,
rows: result.map(&:values),
headings: (include_timestamp ? ["timestamp"] : []) + result.last.keys,
rows: result.map { |row| (include_timestamp ? [row.timestamp] : []) + row.values }
})
end

def format_result(result)
if result.is_a?(Druid::Query)
puts format_query_result(result.send)
puts format_query_result(result.send, result)
else
ap(result)
end
Expand All @@ -29,13 +32,15 @@ class Console

extend Forwardable

def initialize(uri, source)
@uri, @source = uri, source
def initialize(uri, source, options)
@uri, @source, @options = uri, source, options
Ripl.start(binding: binding)
end

def client
@client ||= Druid::Client.new(@uri)
@client ||= Druid::Client.new(@uri, @options)
@source ||= @client.data_sources[0]
@client
end

def source
Expand Down
4 changes: 3 additions & 1 deletion lib/druid/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ def initialize(source, client = nil)
# set some defaults
data_source(source)
granularity(:all)
interval(Time.now - 86400, Time.now)

today = Time.at ((Time.now.to_i / 86400.0).floor * 86400)
interval(today, today + 86400)
end

def send
Expand Down
4 changes: 2 additions & 2 deletions ruby-druid.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ require 'rake'

Gem::Specification.new do |gem|
gem.name = 'ruby-druid'
gem.version = '0.0.4'
gem.date = '2013-04-03'
gem.version = '0.0.5'
gem.date = '2013-04-04'
gem.summary = 'Ruby client for druid'
gem.description = 'Ruby client for metamx druid'
gem.authors = ['Hagen Rother', 'Holger Pillmann']
Expand Down
8 changes: 4 additions & 4 deletions spec/lib/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,23 @@

it 'parses response on 200' do
stub_request(:post, "http://www.example.com/druid/v2").
with(:body => "{\"dataSource\":\"test\"}",
with(:body => "{\"dataSource\":\"test\",\"granularity\":\"all\",\"intervals\":[\"2013-04-04T00:00:00+00:00/2013-04-04T00:00:00+00:00\"]}",
:headers => {'Accept'=>'*/*', 'Content-Type'=>'application/json', 'User-Agent'=>'Ruby'}).
to_return(:status => 200, :body => "[]", :headers => {})
Druid::ZooHandler.stub!(:new).and_return(mock(Druid::ZooHandler, :data_sources => {'test/test' => 'http://www.example.com/druid/v2'}))
client = Druid::Client.new('test_uri')
JSON.should_receive(:parse).and_return([])
client.send(client.query('test/test'))
client.send(client.query('test/test').interval("2013-04-04", "2013-04-04"))
end

it 'raises on request failure' do
stub_request(:post, "http://www.example.com/druid/v2").
with(:body => "{\"dataSource\":\"test\"}",
with(:body => "{\"dataSource\":\"test\",\"granularity\":\"all\",\"intervals\":[\"2013-04-04T00:00:00+00:00/2013-04-04T00:00:00+00:00\"]}",
:headers => {'Accept'=>'*/*', 'Content-Type'=>'application/json', 'User-Agent'=>'Ruby'}).
to_return(:status => 666, :body => "Strange server error", :headers => {})
Druid::ZooHandler.stub!(:new).and_return(mock(Druid::ZooHandler, :data_sources => {'test/test' => 'http://www.example.com/druid/v2'}))
client = Druid::Client.new('test_uri')
expect { client.send(client.query('test/test')) }.to raise_error(RuntimeError, /Request failed: 666: Strange server error/)
expect { client.send(client.query('test/test').interval("2013-04-04", "2013-04-04")) }.to raise_error(RuntimeError, /Request failed: 666: Strange server error/)
end

it 'should have a static setup' do
Expand Down

0 comments on commit f597ae2

Please sign in to comment.