Class: Daru::IO::Importers::Redis

Inherits:
Base
  • Object
show all
Defined in:
lib/daru/io/importers/redis.rb

Overview

Redis Importer Class, that extends from_redis method to Daru::DataFrame

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

guess_parse, read

Methods inherited from Base

#optional_gem

Constructor Details

#initializeRedis

Checks for required gem dependencies of Redis Importer



11
12
13
14
# File 'lib/daru/io/importers/redis.rb', line 11

def initialize
  require 'json'
  optional_gem 'redis'
end

Class Method Details

.from(connection) ⇒ Daru::IO::Importers::Redis

Loads data from a given connection

Examples:

Loading from a hash

instance = Daru::IO::Importers::Redis.from({url: "redis://:[password]@[hostname]:[port]/[db]"})

Loading from a Redis connection

instance = Daru::IO::Importers::Redis.from(Redis.new({url: "redis://:[password]@[hostname]:[port]/[db]"}))

Parameters:

  • connection (Hash or Redis Instance)

    Either a Hash of Redis configurations, or an existing Redis instance. For the hash configurations, have a look at Redis#initialize.

Returns:



32
33
34
35
# File 'lib/daru/io/importers/redis.rb', line 32

def from(connection={})
  @client = get_client(connection)
  self
end

Instance Method Details

#call(*keys, match: nil, count: nil) ⇒ Daru::DataFrame

Imports a Daru::DataFrame from a Redis Importer instance

Examples:

Importing with no options

# Say, the Redis connection has this setup
# Key "10001" => { "name" => "Tyrion", "age" => 32 }.to_json
# Key "10002" => { "name" => "Jamie", "age" => 37 }.to_json
# Key "10003" => { "name" => "Cersei", "age" => 37 }.to_json
# Key "10004" => { "name" => "Joffrey", "age" => 19 }.to_json

df = instance.call

#=> <Daru::DataFrame(4x2)>
#           name     age
# 10001  Tyrion      32
# 10002   Jamie      37
# 10003  Cersei      37
# 10004 Joffrey      19

Importing with keys

# Say, the Redis connection has this setup
# Key "10001" => { "name" => "Tyrion", "age" => 32 }.to_json
# Key "10002" => { "name" => "Jamie", "age" => 37 }.to_json
# Key "10003" => { "name" => "Cersei", "age" => 37 }.to_json
# Key "10004" => { "name" => "Joffrey", "age" => 19 }.to_json

df = instance.call("10001", "10002")

#=> <Daru::DataFrame(2x2)>
#           name     age
# 10001  Tyrion      32
# 10002   Jamie      37

Importing with query for matching keys and count

# Say, the Redis connection has this setup
# Key "key:1" => { "name" => "name1", "age" => "age1" }.to_json
# Key "key:2" => { "name" => "name2", "age" => "age2" }.to_json
# Key "key:3" => { "name" => "name3", "age" => "age3" }.to_json
# ...
# Key "key:2000" => { "name" => "name2000", "age" => "age2000" }.to_json

df = instance.call(match: "key:1*", count: 200)

#=> #<Daru::DataFrame(200x2)>
#              name      age
# key:1927 name1927  age1927
# key:1759 name1759  age1759
# key:1703 name1703  age1703
# key:1640 name1640  age1640
#   ...        ...      ...

Parameters:

  • keys (Array)

    Redis key(s) from whom, the Daru::DataFrame should be constructed. If no keys are given, all keys in the Redis connection will be used.

  • match (String) (defaults to: nil)

    A pattern to get matching keys.

  • count (Integer) (defaults to: nil)

    Number of matching keys to be obtained. Defaults to nil, to collect ALL matching keys.

Returns:



95
96
97
98
99
100
101
102
103
# File 'lib/daru/io/importers/redis.rb', line 95

def call(*keys, match: nil, count: nil)
  @match  = match
  @count  = count
  @keys   = keys
  @keys   = choose_keys(*@keys).map(&:to_sym)

  vals = @keys.map { |key| ::JSON.parse(@client.get(key), symbolize_names: true) }
  Base.guess_parse(@keys, vals)
end

#from(connection = {}) ⇒ Daru::IO::Importers::Redis

Loads data from a given connection

Examples:

Loading from a hash

instance = Daru::IO::Importers::Redis.from({url: "redis://:[password]@[hostname]:[port]/[db]"})

Loading from a Redis connection

instance = Daru::IO::Importers::Redis.from(Redis.new({url: "redis://:[password]@[hostname]:[port]/[db]"}))

Parameters:

  • connection (Hash or Redis Instance) (defaults to: {})

    Either a Hash of Redis configurations, or an existing Redis instance. For the hash configurations, have a look at Redis#initialize.

Returns:



32
33
34
35
# File 'lib/daru/io/importers/redis.rb', line 32

def from(connection={})
  @client = get_client(connection)
  self
end