Class: Pinot::Client

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

Defined Under Namespace

Classes: QueryOptions

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, query_options: {}) ⇒ Client

Returns a new instance of Client.



7
8
9
10
11
12
13
14
15
16
# File 'lib/pinot/client.rb', line 7

def initialize(host:, port:, controller_port:, controller_host: nil, protocol: :http, socks5_uri: nil, bearer_token: nil, query_options: {})
  @host = host
  @port = port
  @controller_port = controller_port
  @controller_host = controller_host || host
  @protocol = protocol
  @socks5_uri = socks5_uri
  @bearer_token = bearer_token
  @query_options = QueryOptions.new(query_options)
end

Instance Attribute Details

#bearer_tokenObject (readonly)

Returns the value of attribute bearer_token.



5
6
7
# File 'lib/pinot/client.rb', line 5

def bearer_token
  @bearer_token
end

#controller_hostObject (readonly)

Returns the value of attribute controller_host.



5
6
7
# File 'lib/pinot/client.rb', line 5

def controller_host
  @controller_host
end

#controller_portObject (readonly)

Returns the value of attribute controller_port.



5
6
7
# File 'lib/pinot/client.rb', line 5

def controller_port
  @controller_port
end

#hostObject (readonly)

Returns the value of attribute host.



5
6
7
# File 'lib/pinot/client.rb', line 5

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



5
6
7
# File 'lib/pinot/client.rb', line 5

def port
  @port
end

#protocolObject (readonly)

Returns the value of attribute protocol.



5
6
7
# File 'lib/pinot/client.rb', line 5

def protocol
  @protocol
end

#query_optionsObject (readonly)

Returns the value of attribute query_options.



5
6
7
# File 'lib/pinot/client.rb', line 5

def query_options
  @query_options
end

#socks5_uriObject (readonly)

Returns the value of attribute socks5_uri.



5
6
7
# File 'lib/pinot/client.rb', line 5

def socks5_uri
  @socks5_uri
end

Instance Method Details

#controller_uriObject



98
99
100
# File 'lib/pinot/client.rb', line 98

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

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



71
72
73
74
75
# File 'lib/pinot/client.rb', line 71

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



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

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

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



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

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



48
49
50
51
52
53
# File 'lib/pinot/client.rb', line 48

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



18
19
20
21
22
23
24
25
26
27
# File 'lib/pinot/client.rb', line 18

def execute(sql)
  query_params = {sql: sql}
  if (value = query_options.to_query_options)
    query_params["queryOptions"] = value
  end

  response = http.post(query_sql_uri, json: query_params)
  return response if response.is_a?(HTTPX::ErrorResponse)
  Response.new(JSON.parse(response))
end

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



83
84
85
86
87
88
89
90
91
92
# File 'lib/pinot/client.rb', line 83

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



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/pinot/client.rb', line 55

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



102
103
104
# File 'lib/pinot/client.rb', line 102

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

#schema(name) ⇒ Object



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

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

#tablesObject



77
78
79
80
81
# File 'lib/pinot/client.rb', line 77

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

#uriObject



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

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