Class: KijiRest::Client
- Inherits:
-
Object
- Object
- KijiRest::Client
- Defined in:
- lib/kijirest/client.rb
Overview
Provides a simple wrapper interface around the KijiREST server. The return of most of the methods are parsed JSON.
Defined Under Namespace
Classes: KijiRestClientError
Constant Summary collapse
- ENTITY_ID =
Some global helpful constants for clients.
"entityId"
Instance Method Summary collapse
-
#initialize(base_uri = "http://localhost:8080") ⇒ Client
constructor
Constructor for the KijiRest::Client.
-
#instance(instance_name) ⇒ Object
Returns the instance description param: instance_name is the name of the instance to describe.
-
#instances ⇒ Object
Returns a list of visible instances.
-
#row(instance_name, table_name, entity_id, filters = {}) ⇒ Object
Returns a single row (identified by the specified entity_id) from the given table.
-
#rows(instance_name, table_name, filters = {}, &block) ⇒ Object
Performs a scan (via HTTP streaming) yielding each row to the given block.
-
#table(instance_name, table_name) ⇒ Object
Returns the table layout for a given table.
-
#tables(instance_name) ⇒ Object
Returns a list of the tables in the given instance.
-
#write_row(instance_name, table_name, row_hash, strip_timestamp = false, timeout_seconds = nil) ⇒ Object
Creates or updates a new row by POSTing the row_hash to the server.
Constructor Details
#initialize(base_uri = "http://localhost:8080") ⇒ Client
Constructor for the KijiRest::Client. The only optional argument is the base_uri defining the location of the server.
48 49 50 51 |
# File 'lib/kijirest/client.rb', line 48 def initialize(base_uri= "http://localhost:8080") @base_uri = base_uri @version = "v1" end |
Instance Method Details
#instance(instance_name) ⇒ Object
Returns the instance description param: instance_name is the name of the instance to describe.
62 63 64 |
# File 'lib/kijirest/client.rb', line 62 def instance(instance_name) get_json(instance_endpoint(instance_name)) end |
#instances ⇒ Object
Returns a list of visible instances
55 56 57 |
# File 'lib/kijirest/client.rb', line 55 def instances get_json(instances_endpoint) end |
#row(instance_name, table_name, entity_id, filters = {}) ⇒ Object
Returns a single row (identified by the specified entity_id) from the given table. param: instance_name is the name of the instance param: table_name is the name of the table param: entity_id is an array of components comprising the entity_id to fetch. param: filters is a hash containing any filters to apply which will get translated as
query parameters in the REST call.
72 73 74 75 76 77 78 79 |
# File 'lib/kijirest/client.rb', line 72 def row(instance_name, table_name, entity_id, filters= {}) eid_filter = if(entity_id.is_a? Array) entity_id.to_json else entity_id end get_json(rows_endpoint(instance_name, table_name), filters.merge({:eid => eid_filter})) end |
#rows(instance_name, table_name, filters = {}, &block) ⇒ Object
Performs a scan (via HTTP streaming) yielding each row to the given block. param: instance_name is the name of the instance param: table_name is the name of the table param: filters is a hash containing any filters to apply which will get translated as
query parameters in the REST call.
param: block is an anonymous block of code that will be given a hash representing the
current row.
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/kijirest/client.rb', line 101 def rows(instance_name, table_name, filters= {}, &block) if !block raise "No block given!" end url_query_params = filters.map {|k, v| "#{k}=#{CGI.escape(v.to_s)}"}.join("&") uri = URI(@base_uri) http = Net::HTTP.new(uri.host, uri.port) http.request_get("#{rows_endpoint(instance_name, table_name)}?#{url_query_params}") \ do |response| case response when Net::HTTPSuccess then remainder_json_line = "" response.read_body { |chunk| if chunk.size > 0 # Few possible situations: Not sure what can actually happen let's prepare for all # 1) chunk is a line that ends with \r\n # 2) chunk could be multiple lines and also ends with \r\n # 3) chunk has multiples lines but an incomplete last line. remainder_json_line = remainder_json_line + chunk if remainder_json_line[-2..-1] == "\r\n" json_lines = remainder_json_line.split("\r\n") json_lines.each {|line| yield JSON.parse(line) } remainder_json_line = "" else json_lines = remainder_json_line.split("\r\n") json_lines.slice(0..-2).each {|line| yield JSON.parse(line) } remainder_json_line = json_lines.last end end } else raise_exception(response.body) end end end |
#table(instance_name, table_name) ⇒ Object
Returns the table layout for a given table. param: instance_name is the name of the instance param: table_name is the name of the table
90 91 92 |
# File 'lib/kijirest/client.rb', line 90 def table(instance_name, table_name) get_json(table_endpoint(instance_name, table_name)) end |
#tables(instance_name) ⇒ Object
Returns a list of the tables in the given instance. param: instance_name is the name of the instance
83 84 85 |
# File 'lib/kijirest/client.rb', line 83 def tables(instance_name) get_json(tables_endpoint(instance_name)) end |
#write_row(instance_name, table_name, row_hash, strip_timestamp = false, timeout_seconds = nil) ⇒ Object
Creates or updates a new row by POSTing the row_hash to the server. Will create/update the row specified by the entityId key in the row_hash. param: instance_name is the instance param: table_name is the table param: row_hash is a hash containing the data to POST. Must be of the format:
{
"entityId" : [//JSON component Array],
"cells" : {
"family" : {
"qualifier" : [
{
"timestamp" : <timestamp>,
"value": <JSON representation of object to post>,
"writer_schema": <JSON representation of the writer
schema (Avro) OR the Kiji ID of the
schema>
},
...
]
}
}
}
param: strip_timestamp will remove any timestamp fields from the cells allowing the server
to set the timestamp automatically (default false).
param: timeout_seconds specifies the number of seconds before a Timeout::Error is thrown
(default nil, which falls back to Net:HTTP default of 60 seconds)
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/kijirest/client.rb', line 168 def write_row(instance_name, table_name, row_hash, = false, timeout_seconds = nil) if local_row_hash = row_hash.clone if local_row_hash.include?("cells") local_row_hash["cells"].each { |family,qualifierMap| if qualifierMap qualifierMap.each_value { |cells| cells.each {|cell| # TODO: Remove condition when there's a better way to represent cell level error # conditions on GETs. cell.delete("timestamp") unless cell["timestamp"] == -1 } } end } end row_json = local_row_hash.to_json else row_json = row_hash.to_json end uri= URI(@base_uri) http = Net::HTTP.new(uri.host, uri.port) if timeout_seconds http.read_timeout = timeout_seconds end response = http.post(rows_endpoint(instance_name, table_name), row_json, "Content-Type" => "application/json") case response when Net::HTTPSuccess then JSON.parse(response.body) else raise_exception(response.body) end end |