Class: Pinot::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host:, port:, controller_port:, controller_host: nil, protocol: :http, socks5_uri: nil, bearer_token: nil) ⇒ Client

Returns a new instance of Client.



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

def initialize(host:, port:, controller_port:, controller_host: nil, protocol: :http, socks5_uri: nil, bearer_token: nil)
  @host = host
  @port = port
  @controller_port = controller_port
  @controller_host = controller_host || host
  @protocol = protocol
  @socks5_uri = socks5_uri
  @bearer_token = bearer_token
end

Instance Attribute Details

#bearer_tokenObject (readonly)

Returns the value of attribute bearer_token.



3
4
5
# File 'lib/pinot/client.rb', line 3

def bearer_token
  @bearer_token
end

#controller_hostObject (readonly)

Returns the value of attribute controller_host.



3
4
5
# File 'lib/pinot/client.rb', line 3

def controller_host
  @controller_host
end

#controller_portObject (readonly)

Returns the value of attribute controller_port.



3
4
5
# File 'lib/pinot/client.rb', line 3

def controller_port
  @controller_port
end

#hostObject (readonly)

Returns the value of attribute host.



3
4
5
# File 'lib/pinot/client.rb', line 3

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



3
4
5
# File 'lib/pinot/client.rb', line 3

def port
  @port
end

#protocolObject (readonly)

Returns the value of attribute protocol.



3
4
5
# File 'lib/pinot/client.rb', line 3

def protocol
  @protocol
end

#socks5_uriObject (readonly)

Returns the value of attribute socks5_uri.



3
4
5
# File 'lib/pinot/client.rb', line 3

def socks5_uri
  @socks5_uri
end

Instance Method Details

#controller_uriObject



90
91
92
# File 'lib/pinot/client.rb', line 90

def controller_uri
  "#{protocol}://#{controller_host}:#{controller_port}"
end

#create_schema(schema, override: true, force: false) ⇒ Object



63
64
65
66
67
# File 'lib/pinot/client.rb', line 63

def create_schema(schema, override: true, force: false)
  url = "#{controller_uri}/schemas?override=#{override}&force=#{force}"
  response = http.post(url, body: schema)
  JSON.parse(response)
end

#create_table(schema) ⇒ Object



34
35
36
37
38
# File 'lib/pinot/client.rb', line 34

def create_table(schema)
  url = "#{controller_uri}/tables"
  response = http.post(url, body: schema)
  JSON.parse(response)
end

#delete_segments(name, type: :offline) ⇒ Object



27
28
29
30
31
32
# File 'lib/pinot/client.rb', line 27

def delete_segments(name, type: :offline)
  type = type.to_s.upcase
  url = "#{controller_uri}/segments/#{name}?type=#{type}"
  response = http.delete(url)
  JSON.parse(response)
end

#delete_table(name, type: :offline) ⇒ Object



40
41
42
43
44
45
# File 'lib/pinot/client.rb', line 40

def delete_table(name, type: :offline)
  type = type.to_s.downcase
  url = "#{controller_uri}/tables/#{name}?type=#{type}"
  response = http.delete(url)
  JSON.parse(response)
end

#execute(sql) ⇒ Object



15
16
17
18
19
# File 'lib/pinot/client.rb', line 15

def execute(sql)
  response = http.post(query_sql_uri, json: {sql: sql})
  return response if response.is_a?(HTTPX::ErrorResponse)
  Response.new(JSON.parse(response))
end

#http(content_type: "application/json") ⇒ Object



75
76
77
78
79
80
81
82
83
84
# File 'lib/pinot/client.rb', line 75

def http(content_type: "application/json")
  return @http if !@http.nil?
  default_headers = {"Content-Type" => content_type}
  default_headers["Authorization"] = "Bearer #{bearer_token}" if bearer_token
  @http = HTTPX.with(headers: default_headers, timeout: {connect_timeout: 5})
  if socks5_uri
    @http = @http.plugin(:proxy).with_proxy(uri: socks5_uri) if socks5_uri
  end
  @http
end

#ingest_json(file, table:) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/pinot/client.rb', line 47

def ingest_json(file, table:)
  url = "#{controller_uri}/ingestFromFile?tableNameWithType=#{table}&batchConfigMapStr=%7B%22inputFormat%22%3A%22json%22%7D"
  content_type = "multipart/form-data"
  response = HTTPX.post(
    url,
    form: {
      file: {
        filename: File.basename(file.path),
        content_type: content_type,
        body: file.read
      }
    }
  )
  JSON.parse(response)
end

#query_sql_uriObject



94
95
96
# File 'lib/pinot/client.rb', line 94

def query_sql_uri
  "#{uri}/query/sql"
end

#schema(name) ⇒ Object



21
22
23
24
25
# File 'lib/pinot/client.rb', line 21

def schema(name)
  url = "#{controller_uri}/schemas/#{name}"
  response = http.get(url)
  JSON.parse(response)
end

#tablesObject



69
70
71
72
73
# File 'lib/pinot/client.rb', line 69

def tables
  url = "#{controller_uri}/tables"
  response = http.get(url)
  JSON.parse(response)
end

#uriObject



86
87
88
# File 'lib/pinot/client.rb', line 86

def uri
  "#{protocol}://#{host}:#{port}"
end