redis-load 0.1 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES CHANGED
@@ -1,3 +1,6 @@
1
+ == Release 0.2
2
+ Added support for dumping out JSON to file and via API
3
+
1
4
  == Release 0.1
2
5
  First release to github
3
6
  Added initial support for loading JSON into Redis. Path support not yet fully tested
@@ -0,0 +1,73 @@
1
+ redis-load
2
+ ==========
3
+
4
+ Utility for managing data structures in Redis, allowing lists, sets, etc to be populated by loading data from flat files and JSON. Data structures can also be saved out to local files.
5
+
6
+ The rationale here was to provide an easy way to persist the results of data manipulations from Redis in common formats. Similarly, I also wanted to load data structures from files for use in Redis, but where the original data has been created separately, e.g. by a map-reduce job.
7
+
8
+ Author
9
+ ______
10
+
11
+ Leigh Dodds (leigh.dodds@talis.com)
12
+
13
+ Download
14
+ --------
15
+
16
+ [http://github.com/ldodds/redis-load][0]
17
+
18
+ Installation
19
+ ------------
20
+
21
+ redis-load is available as a gem:
22
+
23
+ sudo gem install redis-load
24
+
25
+ It has dependencies on the ruby redis library, json/pure and siren
26
+
27
+ If you grab a copy from github then there's an install target in the Rakefile that will build and install the gem
28
+
29
+ Usage
30
+ -----
31
+
32
+ The command-line app is called redis-load. You need to provide it with a filename and generally a key to be populated.
33
+
34
+ Load the contents of a json file as entries into a server:
35
+
36
+ redis-load load-json --file myfile.json
37
+
38
+ Load the contents of a file as key-value pairs. (Must be key,value):
39
+
40
+ redis-load load-keys --file mykeys.txt
41
+
42
+ Load the contents of a file into a list. Each line is a list entry:
43
+
44
+ redis-load load-list --key mylist --file mylist.txt
45
+
46
+ Load the contents of a file into a set. Each line is a set entry:
47
+
48
+ redis-load load-set --key myset --file myset.txt
49
+
50
+ Load the contents of a file into a sort set. Each line is a key-value pair of entry,score:
51
+
52
+ redis-load load-zset --key myzset --file myzset.txt
53
+
54
+ Save a list into a file:
55
+
56
+ redis-load save-list --key mylist --file mylist.txt
57
+
58
+ Save a set into a file:
59
+
60
+ redis-load save-set --key myset --file myset.txt
61
+
62
+ Save a sorted set into a file:
63
+
64
+ redis-load save-zset --key myzset --file myzset.txt
65
+
66
+ Note that when saving a file any existing file will automatically be over-written.
67
+
68
+ License
69
+ -------
70
+
71
+ redis-load is free and unencumbered public domain software. For more information, see http://unlicense.org/ or the accompanying UNLICENSE file.
72
+
73
+ [0]: http://github.com/ldodds/redis-load
data/Rakefile CHANGED
@@ -5,11 +5,11 @@ require 'rake/testtask'
5
5
  require 'rake/clean'
6
6
 
7
7
  NAME = "redis-load"
8
- VER = "0.1"
8
+ VER = "0.2"
9
9
 
10
- RDOC_OPTS = ['--quiet', '--title', 'redis-load Reference', '--main', 'README']
10
+ RDOC_OPTS = ['--quiet', '--title', 'redis-load Reference', '--main', 'README.md']
11
11
 
12
- PKG_FILES = %w( README Rakefile CHANGES ) +
12
+ PKG_FILES = %w( README.md Rakefile CHANGES ) +
13
13
  Dir.glob("{bin,doc,tests,examples,lib}/**/*")
14
14
 
15
15
  CLEAN.include ['*.gem', 'pkg']
@@ -20,7 +20,7 @@ SPEC =
20
20
  s.platform = Gem::Platform::RUBY
21
21
  s.required_ruby_version = ">= 1.8.5"
22
22
  s.has_rdoc = true
23
- s.extra_rdoc_files = ["README", "CHANGES"]
23
+ s.extra_rdoc_files = ["README.md", "CHANGES"]
24
24
  s.rdoc_options = RDOC_OPTS
25
25
  s.summary = "Utility of loading/saving data structures from Redis"
26
26
  s.description = s.summary
@@ -45,8 +45,8 @@ end
45
45
  Rake::RDocTask.new do |rdoc|
46
46
  rdoc.rdoc_dir = 'doc/rdoc'
47
47
  rdoc.options += RDOC_OPTS
48
- rdoc.rdoc_files.include("README", "CHANGES", "lib/**/*.rb")
49
- rdoc.main = "README"
48
+ rdoc.rdoc_files.include("README.md", "CHANGES", "lib/**/*.rb")
49
+ rdoc.main = "README.md"
50
50
 
51
51
  end
52
52
 
@@ -32,6 +32,9 @@ MODE
32
32
  load-zset
33
33
  Load the contents of a file into a sort set. Each line is a key-value pair of entry,score
34
34
 
35
+ save-json
36
+ Save all or some keys as JSON
37
+
35
38
  save-list
36
39
  Save a list into a file
37
40
 
@@ -71,7 +74,8 @@ opts = GetoptLong::new(
71
74
  [ "--database" , "-d" , GetoptLong::REQUIRED_ARGUMENT ],
72
75
  [ "--key" , "-k" , GetoptLong::REQUIRED_ARGUMENT ],
73
76
  [ "--file" , "-f" , GetoptLong::REQUIRED_ARGUMENT ],
74
- [ "--jsonpath" , "-j" , GetoptLong::REQUIRED_ARGUMENT ]
77
+ [ "--jsonpath" , "-j" , GetoptLong::REQUIRED_ARGUMENT ],
78
+ [ "--keys" , "-e" , GetoptLong::REQUIRED_ARGUMENT ]
75
79
  ).enum_for.inject({}) { |h, (k, v)| h.update k.delete('-') => v }
76
80
 
77
81
  mode = "help" if opts["--help"]
@@ -84,7 +88,7 @@ else
84
88
  abort USAGE
85
89
  end
86
90
 
87
- if (mode != "load-json" && mode != "load-keys") && opts["key"] == nil
91
+ if (mode != "load-json" && mode != "load-keys" && mode != "save-json") && opts["key"] == nil
88
92
  abort USAGE
89
93
  end
90
94
 
@@ -101,6 +105,8 @@ else
101
105
  cmdline.load_set()
102
106
  when "load-zset"
103
107
  cmdline.load_zset()
108
+ when "save-json"
109
+ cmdline.save_json()
104
110
  when "save-list"
105
111
  cmdline.save_list()
106
112
  when "save-set"
@@ -49,6 +49,13 @@ module RedisLoad
49
49
  puts "#{counter} lines loaded."
50
50
  end
51
51
 
52
+ def save_json()
53
+ puts "Saving json into #{@opts["file"]}"
54
+ keys = @opts["keys"] || "*"
55
+ counter = @loader.save_json( @opts["file"], keys )
56
+ puts "#{counter} keys saved."
57
+ end
58
+
52
59
  def save_set()
53
60
  puts "Saving set #{@opts["key"]} into #{@opts["file"]}"
54
61
  size = @loader.save_set( @opts["key"], @opts["file"] )
@@ -10,6 +10,28 @@ module RedisLoad
10
10
  @redis = redis
11
11
  end
12
12
 
13
+ def to_json( pattern="*", limit=-1, opts=nil )
14
+ keys = @redis.keys(pattern)
15
+ h = Hash.new
16
+ counter = 0
17
+ keys.each do |key|
18
+ counter = counter + 1
19
+ type = @redis.type(key)
20
+ if type == "hash"
21
+ h[key] = @redis.hgetall(key)
22
+ elsif type == "list"
23
+ h[key] = @redis.lrange(key, 0, limit)
24
+ elsif type == "set"
25
+ h[key] = @redis.smembers(key)
26
+ elsif type == "zset"
27
+ h[key] = @redis.zrange(key, 0, limit)
28
+ else
29
+ h[key] = @redis.get(key)
30
+ end
31
+ end
32
+ return JSON.fast_generate(h, opts), counter
33
+ end
34
+
13
35
  def load_keys(file)
14
36
  f = File.new( file )
15
37
  counter = process_lines(f) do |redis,line|
@@ -36,7 +58,7 @@ module RedisLoad
36
58
  json = Siren.query(query, json)
37
59
  end
38
60
  counter = 0
39
- #TODO could support proper results, not just path?
61
+ #TODO could support proper results, not just hash?
40
62
  if json != nil
41
63
  json.each_pair do |key,value|
42
64
  if value.class() == Array
@@ -88,6 +110,16 @@ module RedisLoad
88
110
  return counter
89
111
  end
90
112
 
113
+ def save_json(file, pattern="*", limit=-1, opts=nil)
114
+ counter = 0
115
+ File.open( file, "w" ) do |file|
116
+ json, count = to_json(pattern, limit, opts)
117
+ file.write( json )
118
+ counter = count
119
+ end
120
+ return counter
121
+ end
122
+
91
123
  def save_list(key, file)
92
124
  size = @redis.llen( key )
93
125
  count = 0
metadata CHANGED
@@ -1,12 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redis-load
3
3
  version: !ruby/object:Gem::Version
4
- hash: 9
4
+ hash: 15
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 1
9
- version: "0.1"
8
+ - 2
9
+ version: "0.2"
10
10
  platform: ruby
11
11
  authors:
12
12
  - Leigh Dodds
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-07-22 00:00:00 +01:00
17
+ date: 2010-07-24 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -66,10 +66,10 @@ executables:
66
66
  extensions: []
67
67
 
68
68
  extra_rdoc_files:
69
- - README
69
+ - README.md
70
70
  - CHANGES
71
71
  files:
72
- - README
72
+ - README.md
73
73
  - Rakefile
74
74
  - CHANGES
75
75
  - bin/redis-load
@@ -87,7 +87,7 @@ rdoc_options:
87
87
  - --title
88
88
  - redis-load Reference
89
89
  - --main
90
- - README
90
+ - README.md
91
91
  require_paths:
92
92
  - lib
93
93
  required_ruby_version: !ruby/object:Gem::Requirement
data/README DELETED
@@ -1,64 +0,0 @@
1
- Utility for managing data structures in Redis, allowing lists, sets, etc to be populated by loading
2
- data from flat files and JSON. Data structures can also be saved out to local files.
3
-
4
- The rationale here was to provide some easy ways to persistent out the results of data manipulations from
5
- Redis in easy to process formats. Similarly I also wanted to load data structures from files for use in Redis,
6
- but where the original data has been created separately, e.g. by a map-reduce job.
7
-
8
- == Author
9
-
10
- Leigh Dodds (leigh.dodds@talis.com)
11
-
12
- == Download
13
-
14
- http://github.com/ldodds/redis-load
15
-
16
- == Installation
17
-
18
- redis-load is available as a gem:
19
-
20
- sudo gem install redis-load
21
-
22
- It has dependencies on the ruby redis library, json/pure and siren
23
-
24
- If you grab a copy from github then there's an install target in the Rakefile that will build and install the gem
25
-
26
- == Usage
27
-
28
- The command-line app is called redis-load. You need to provide it with a file name and generally
29
- a key to be populated:
30
-
31
- Load the contents of a json file as entries into a server
32
-
33
- redis-load load-json --file myfile.json
34
-
35
- Load the contents of a file as key-value pairs. Must be: key,value
36
-
37
- redis-load load-keys --file mykeys.txt
38
-
39
- Load the contents of a file into a list. Each line is a list entry
40
-
41
- redis-load load-list --key mylist --file mylist.txt
42
-
43
- Load the contents of a file into a set. Each line is a set entry
44
-
45
- redis-load load-set --key myset --file myset.txt
46
-
47
- Load the contents of a file into a sort set. Each line is a key-value pair of entry,score
48
-
49
- redis-load load-zset --key myzset --file myzset.txt
50
-
51
- Save a list into a file
52
-
53
- redis-load save-list --key mylist --file mylist.txt
54
-
55
- Save a set into a file
56
-
57
- redis-load save-set --key myset --file myset.txt
58
-
59
- Save a sorted set into a file
60
-
61
- redis-load save-zset --key myzset --file myzset.txt
62
-
63
- Note that when saving a file any existing file will automatically be over-written.
64
-