presto-client 0.4.12 → 0.4.13

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cc62d8a09800529582760c70ab8b9edac4c96da1
4
- data.tar.gz: cc1c5b78f899093813f80d3176595cba68f41b3a
3
+ metadata.gz: 0bee1b8fb22804f12e8a2fe9ee8f9fca6262cf4f
4
+ data.tar.gz: 2bd49e06c846141c5d909c507716970454565270
5
5
  SHA512:
6
- metadata.gz: 075a2ef5b04abd2677b18ac84d13df013eeaaaa60faaa6a0e686a5c2ecc35bffe8c0273db0dcce997d59dfad93e010b50a45f6e3e1cf4bc2e376eb96c8daa310
7
- data.tar.gz: cd1751cc00c4f8ddc93737b48778d3878ba9a7cb50049d3244a32936db11103289b94b32198462ff7b94fcac067983766171f75dcf719ffd264f037b13b0c28d
6
+ metadata.gz: 0128a6bdf303a34f156916fb460c4e133277d41de5ff9b839fb19c969e569e71bdef2174a40be8e5bb5b43868ab41d192afb7a9320dbb750ca83990929fd734d
7
+ data.tar.gz: 30486a370eb854050e71dd0cdec598e17fe0aa3bec43feb6f1b20f7292c8b8d8362e29ce35d06a5757726d547c3f507af2751726092fbbf8580325d17cc66823
data/ChangeLog CHANGED
@@ -1,3 +1,8 @@
1
+ 2016-08-02 version 0.4.13:
2
+
3
+ * Added support for :http_proxy option to use a HTTP proxy server
4
+ * Added support for hashed Client response using `run_with_names` (thanks to MoovWeb for allowing me to contribute)
5
+ * Upgraded Presto model version to 0.134
1
6
 
2
7
  2015-04-01 version 0.4.5:
3
8
 
@@ -62,4 +67,3 @@ since Presto 0.78
62
67
  2014-01-07 version 0.1.0:
63
68
 
64
69
  * First release
65
-
data/README.md CHANGED
@@ -21,6 +21,7 @@ client = Presto::Client.new(
21
21
  time_zone: "US/Pacific", # optional
22
22
  language: "English", # optional
23
23
  properties: {"hello" => "world", "mycatalog.hello" => "world"}, # optional
24
+ http_proxy: "proxy.example.com:8080", # optional
24
25
  http_debug: true,
25
26
  )
26
27
 
@@ -30,6 +31,15 @@ rows.each {|row|
30
31
  p row
31
32
  }
32
33
 
34
+ # run a query and get results as a hash:
35
+ results = client.run_with_names("select alpha, 1 AS beta from tablename")
36
+ results.each {|row|
37
+ p row['alpha'] # access by name
38
+ p row['beta']
39
+ p row.values[0] # access by index
40
+ p row.values[1]
41
+ }
42
+
33
43
  # another way to run a query and fetch results streamingly:
34
44
  # start running a query on presto
35
45
  client.query("select * from sys.node") do |q|
@@ -54,6 +64,7 @@ end
54
64
  * **user** sets user name to connect to a Presto.
55
65
  * **time_zone** sets time zone of the query. Time zone affects some functions such as `format_datetime`.
56
66
  * **language** sets language of the query. Language affects some functions such as `format_datetime`.
67
+ * **http_proxy** sets host:port of a HTTP proxy server.
57
68
  * **http_debug** enables debug message to STDOUT for each HTTP requests
58
69
  * **http_open_timeout** sets timeout in seconds to open new HTTP connection
59
70
  * **http_timeout** sets timeout in seconds to read data from a server
@@ -48,10 +48,23 @@ module Presto::Client
48
48
  q.close
49
49
  end
50
50
  end
51
+
52
+ # Accepts the raw response from the Presto Client and returns an
53
+ # array of hashes where you can access the data in each row using the
54
+ # output name specified in the query with AS:
55
+ # SELECT expression AS output_name
56
+ def run_with_names(query)
57
+ columns, rows = run(query)
58
+
59
+ column_names = columns.map(&:name)
60
+
61
+ rows.map do |row|
62
+ Hash[column_names.zip(row)]
63
+ end
64
+ end
51
65
  end
52
66
 
53
67
  def self.new(*args)
54
68
  Client.new(*args)
55
69
  end
56
-
57
70
  end
@@ -27,7 +27,8 @@ module Presto::Client
27
27
  raise ArgumentError, ":server option is required"
28
28
  end
29
29
 
30
- faraday = Faraday.new(url: "http://#{server}") do |faraday|
30
+ proxy = options[:http_proxy] || options[:proxy] # :proxy is obsoleted
31
+ faraday = Faraday.new(url: "http://#{server}", proxy: "#{proxy}") do |faraday|
31
32
  #faraday.request :url_encoded
32
33
  faraday.response :logger if options[:http_debug]
33
34
  faraday.adapter Faraday.default_adapter
@@ -47,9 +47,39 @@ module Presto::Client
47
47
  @query = query
48
48
  @closed = false
49
49
  @exception = nil
50
+
51
+ @faraday.headers.merge!(optional_headers)
50
52
  post_query_request!
51
53
  end
52
54
 
55
+ def optional_headers
56
+ headers = {}
57
+ if v = @options[:user]
58
+ headers[PrestoHeaders::PRESTO_USER] = v
59
+ end
60
+ if v = @options[:source]
61
+ headers[PrestoHeaders::PRESTO_SOURCE] = v
62
+ end
63
+ if v = @options[:catalog]
64
+ headers[PrestoHeaders::PRESTO_CATALOG] = v
65
+ end
66
+ if v = @options[:schema]
67
+ headers[PrestoHeaders::PRESTO_SCHEMA] = v
68
+ end
69
+ if v = @options[:time_zone]
70
+ headers[PrestoHeaders::PRESTO_TIME_ZONE] = v
71
+ end
72
+ if v = @options[:language]
73
+ headers[PrestoHeaders::PRESTO_LANGUAGE] = v
74
+ end
75
+ if v = @options[:properties]
76
+ headers[PrestoHeaders::PRESTO_SESSION] = encode_properties(v)
77
+ end
78
+ headers
79
+ end
80
+
81
+ private :optional_headers
82
+
53
83
  def init_request(req)
54
84
  req.options.timeout = @options[:http_timeout] || 300
55
85
  req.options.open_timeout = @options[:http_open_timeout] || 60
@@ -61,28 +91,6 @@ module Presto::Client
61
91
  response = @faraday.post do |req|
62
92
  req.url "/v1/statement"
63
93
 
64
- if v = @options[:user]
65
- req.headers[PrestoHeaders::PRESTO_USER] = v
66
- end
67
- if v = @options[:source]
68
- req.headers[PrestoHeaders::PRESTO_SOURCE] = v
69
- end
70
- if v = @options[:catalog]
71
- req.headers[PrestoHeaders::PRESTO_CATALOG] = v
72
- end
73
- if v = @options[:schema]
74
- req.headers[PrestoHeaders::PRESTO_SCHEMA] = v
75
- end
76
- if v = @options[:time_zone]
77
- req.headers[PrestoHeaders::PRESTO_TIME_ZONE] = v
78
- end
79
- if v = @options[:language]
80
- req.headers[PrestoHeaders::PRESTO_LANGUAGE] = v
81
- end
82
- if v = @options[:properties]
83
- req.headers[PrestoHeaders::PRESTO_SESSION] = encode_properties(v)
84
- end
85
-
86
94
  req.body = @query
87
95
  init_request(req)
88
96
  end
@@ -15,6 +15,6 @@
15
15
  #
16
16
  module Presto
17
17
  module Client
18
- VERSION = "0.4.12"
18
+ VERSION = "0.4.13"
19
19
  end
20
20
  end
@@ -0,0 +1,75 @@
1
+ require 'spec_helper'
2
+
3
+ describe Presto::Client::Client do
4
+ let(:client) { Presto::Client.new({}) }
5
+
6
+ describe 'rehashes' do
7
+ let(:columns) do
8
+ [
9
+ Models::Column.new(name: 'animal', type: 'string'),
10
+ Models::Column.new(name: 'score', type: 'integer'),
11
+ Models::Column.new(name: 'name', type: 'string')
12
+ ]
13
+ end
14
+
15
+ it 'multiple rows' do
16
+ rows = [
17
+ ['dog', 1, 'Lassie'],
18
+ ['horse', 5, 'Mr. Ed'],
19
+ ['t-rex', 37, 'Doug']
20
+ ]
21
+ client.stub(:run).and_return([columns, rows])
22
+
23
+ rehashed = client.run_with_names('fake query')
24
+
25
+ rehashed.length.should == 3
26
+
27
+ rehashed[0]['animal'].should == 'dog'
28
+ rehashed[0]['score'].should == 1
29
+ rehashed[0]['name'].should == 'Lassie'
30
+
31
+ rehashed[0].values[0].should == 'dog'
32
+ rehashed[0].values[1].should == 1
33
+ rehashed[0].values[2].should == 'Lassie'
34
+
35
+ rehashed[1]['animal'].should == 'horse'
36
+ rehashed[1]['score'].should == 5
37
+ rehashed[1]['name'].should == 'Mr. Ed'
38
+
39
+ rehashed[1].values[0].should == 'horse'
40
+ rehashed[1].values[1].should == 5
41
+ rehashed[1].values[2].should == 'Mr. Ed'
42
+ end
43
+
44
+ it 'empty results' do
45
+ rows = []
46
+ client.stub(:run).and_return([columns, rows])
47
+
48
+ rehashed = client.run_with_names('fake query')
49
+
50
+ rehashed.length.should == 0
51
+ end
52
+
53
+ it 'handles too few result columns' do
54
+ rows = [['wrong', 'count']]
55
+ client.stub(:run).and_return([columns, rows])
56
+
57
+ client.run_with_names('fake query').should == [{
58
+ "animal" => "wrong",
59
+ "score" => "count",
60
+ "name" => nil,
61
+ }]
62
+ end
63
+
64
+ it 'handles too many result columns' do
65
+ rows = [['wrong', 'count', 'too', 'much', 'columns']]
66
+ client.stub(:run).and_return([columns, rows])
67
+
68
+ client.run_with_names('fake query').should == [{
69
+ "animal" => "wrong",
70
+ "score" => "count",
71
+ "name" => 'too',
72
+ }]
73
+ end
74
+ end
75
+ end
@@ -67,6 +67,12 @@ describe Presto::Client::StatementClient do
67
67
  stub_request(:get, "localhost/v1/next_uri").
68
68
  with(headers: {
69
69
  "User-Agent" => "presto-ruby/#{VERSION}",
70
+ "X-Presto-Catalog" => options[:catalog],
71
+ "X-Presto-Schema" => options[:schema],
72
+ "X-Presto-User" => options[:user],
73
+ "X-Presto-Language" => options[:language],
74
+ "X-Presto-Time-Zone" => options[:time_zone],
75
+ "X-Presto-Session" => options[:properties].map {|k,v| "#{k}=#{v}"}.join("\r\nX-Presto-Session: ")
70
76
  }).to_return(body: lambda{|req|if retry_p; response_json.to_json; else; retry_p=true; raise Timeout::Error.new("execution expired"); end })
71
77
 
72
78
  faraday = Faraday.new(url: "http://localhost")
@@ -75,5 +81,6 @@ describe Presto::Client::StatementClient do
75
81
  sc.advance.should be_true
76
82
  retry_p.should be_true
77
83
  end
84
+
78
85
  end
79
86
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: presto-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.12
4
+ version: 0.4.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sadayuki Furuhashi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-01-28 00:00:00.000000000 Z
11
+ date: 2016-08-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -125,6 +125,7 @@ files:
125
125
  - modelgen/models.rb
126
126
  - modelgen/presto_models.rb
127
127
  - presto-client.gemspec
128
+ - spec/client_spec.rb
128
129
  - spec/spec_helper.rb
129
130
  - spec/statement_client_spec.rb
130
131
  homepage: https://github.com/treasure-data/presto-client-ruby
@@ -147,10 +148,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
147
148
  version: '0'
148
149
  requirements: []
149
150
  rubyforge_project:
150
- rubygems_version: 2.4.8
151
+ rubygems_version: 2.5.1
151
152
  signing_key:
152
153
  specification_version: 4
153
154
  summary: Presto client library
154
155
  test_files:
156
+ - spec/client_spec.rb
155
157
  - spec/spec_helper.rb
156
158
  - spec/statement_client_spec.rb