es2bulk 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: cbf1ca459958c2be7fb2cc702e6fb8373f8382b0a885c4caa8a6ccf319beb280
4
+ data.tar.gz: 1cb6d8ec212ac8e692774620636faec422a03fe3c249d11d7201722c16554cea
5
+ SHA512:
6
+ metadata.gz: 7d7575c0759c7cfde77ae58e423c23b4021d3d1869be67921b1be9a591df28684b148b49fb2104feacdfc1f2e67bd955f14ac25df1225a554a71d371f2da82f7
7
+ data.tar.gz: 1c4ab221f9b17bd154456b523b5bbc1cf34bc99ee5fb134b6211cfa8481aec387ecb19ee19e60dab8ff6774322eb6918d9217022eb349a8da87e504aced0ab15
@@ -0,0 +1,64 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ # Used by dotenv library to load environment variables.
14
+ # .env
15
+
16
+ ## Specific to RubyMotion:
17
+ .dat*
18
+ .repl_history
19
+ build/
20
+ *.bridgesupport
21
+ build-iPhoneOS/
22
+ build-iPhoneSimulator/
23
+
24
+ ## Specific to RubyMotion (use of CocoaPods):
25
+ #
26
+ # We recommend against adding the Pods directory to your .gitignore. However
27
+ # you should judge for yourself, the pros and cons are mentioned at:
28
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
29
+ #
30
+ # vendor/Pods/
31
+
32
+ ## Documentation cache and generated files:
33
+ /.yardoc/
34
+ /_yardoc/
35
+ /doc/
36
+ /rdoc/
37
+
38
+ ## Environment normalization:
39
+ /.bundle/
40
+ /vendor/bundle
41
+ /lib/bundler/man/
42
+
43
+ # for a library or gem, you might want to ignore these files since the code is
44
+ # intended to run in multiple environments; otherwise, check them in:
45
+ # Gemfile.lock
46
+ # .ruby-version
47
+ # .ruby-gemset
48
+
49
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
50
+ .rvmrc
51
+ =======
52
+ /.bundle/
53
+ /.yardoc
54
+ /_yardoc/
55
+ /coverage/
56
+ /doc/
57
+ /pkg/
58
+ /spec/reports/
59
+ /tmp/
60
+ *.gem
61
+
62
+ # rspec failure tracking
63
+ .rspec_status
64
+ >>>>>>> 0f777b0... initial commit
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.3.7
7
+ before_install: gem install bundler -v 1.16.5
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at kzcat@users.noreply.github.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in es2bulk.gemspec
6
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018 kzcat
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 kzcat
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,47 @@
1
+ # Es2Bulk
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'es2bulk'
9
+ ```
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install es2bulk
18
+
19
+ ## Usage
20
+
21
+ ```consol
22
+ $ es2bulk --help
23
+ Usage: es2bulk [options]
24
+ -i, --index_pattern PATTERN
25
+ -h, --host ES_HOST
26
+ -p, --port ES_PORT
27
+ --without-id
28
+ $ es2bulk -h 192.168.0.3 -p 9200 -i test_index > test_index_bulk.json
29
+ ```
30
+
31
+ ## Development
32
+
33
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
34
+
35
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
36
+
37
+ ## Contributing
38
+
39
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/es2bulk. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
40
+
41
+ ## License
42
+
43
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
44
+
45
+ ## Code of Conduct
46
+
47
+ Everyone interacting in the Es2bulk project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/es2bulk/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,39 @@
1
+ #! /usr/bin/env ruby
2
+
3
+ require 'es2bulk'
4
+ require 'optparse'
5
+ require 'json'
6
+
7
+ class Es2BulkCommand
8
+ def initialize(argv)
9
+ index_pattern = nil
10
+ host = 'localhost'
11
+ port = 9200
12
+ without_id = false
13
+ OptionParser.new do |opt|
14
+ opt.on('-i', '--index_pattern PATTERN') { |v| index_pattern = v }
15
+ opt.on('-h', '--host ES_HOST') { |v| host = v }
16
+ opt.on('-p', '--port ES_PORT', Integer) { |v| port = v }
17
+ opt.on('--without-id') { without_id = true }
18
+ begin
19
+ opt.parse!(argv)
20
+ rescue OptionParser::ParseError => e
21
+ STDERR.puts e.message
22
+ abort opt.help
23
+ end
24
+ abort opt.help unless index_pattern
25
+ end
26
+ @es2bulk = Es2Bulk.new(index_pattern, host, port, without_id)
27
+ end
28
+
29
+ def run
30
+ @es2bulk.generate do |meta, body|
31
+ puts meta.to_json
32
+ puts body.to_json
33
+ end
34
+ rescue Es2Bulk::Error => e
35
+ abort e.message
36
+ end
37
+ end
38
+
39
+ Es2BulkCommand.new(ARGV).run
@@ -0,0 +1,26 @@
1
+ require_relative 'lib/es2bulk/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = 'es2bulk'
5
+ spec.version = Es2Bulk::VERSION
6
+ spec.authors = ['kzcat']
7
+ spec.email = ['kzcat@users.noreply.github.com']
8
+
9
+ spec.summary = 'Convert Elasticsearch documents to bulk format.'
10
+ spec.description = 'Elasticsearch helper command. Retrive all documents from index and convert documents to bulk format'
11
+ spec.homepage = 'https://github.com/kzcat/es2bulk'
12
+ spec.license = 'MIT'
13
+
14
+ # Specify which files should be added to the gem when it is released.
15
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
16
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
17
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ end
19
+ spec.bindir = 'bin'
20
+ spec.executables = ['es2bulk']
21
+ spec.require_paths = ['lib']
22
+
23
+ spec.add_development_dependency 'bundler', '~> 1.16'
24
+ spec.add_development_dependency 'rake', '~> 10.0'
25
+ spec.add_development_dependency 'rspec', '~> 3.0'
26
+ end
@@ -0,0 +1,2 @@
1
+ require 'es2bulk/es2bulk'
2
+ require 'es2bulk/version'
@@ -0,0 +1,70 @@
1
+ require 'optparse'
2
+ require 'net/http'
3
+ require 'uri'
4
+ require 'json'
5
+
6
+ class Es2Bulk
7
+ RETRIEVE_SIZE = 3000
8
+ class Error < StandardError; end
9
+ class EsConnectionError < Error; end
10
+ class EsResponseError < Error; end
11
+
12
+ def initialize(index_pattern, host = 'localhost', port = 9200, without_id)
13
+ @index_pattern = index_pattern
14
+ @host = host
15
+ @port = port
16
+ @without_id = without_id
17
+ end
18
+
19
+ def generate
20
+ path = "/#{@index_pattern}/_search"
21
+ search_after = nil
22
+ total = 0
23
+ connect
24
+ routing_field = get_version >= '6.0.0' ? :routing : :_routing
25
+ loop do
26
+ body = { query: { match_all: {} }, sort: [:_uid], size: RETRIEVE_SIZE }
27
+ body[:search_after] = search_after if search_after
28
+ req = Net::HTTP::Post.new(path, 'Content-Type' => 'application/json')
29
+ req.body = body.to_json
30
+ response = request(req)
31
+ result = JSON.parse(response.body, symbolize_names: true)
32
+ if result[:error]
33
+ raise EsResponseError, result[:error]
34
+ end
35
+ result[:hits][:hits].each do |hits|
36
+ meta = { index: { _index: hits[:_index], _type: hits[:_type] } }
37
+ meta[:index][routing_field] = hits[routing_field] if hits[routing_field]
38
+ meta[:index][:_id] = hits[:_id] unless @without_id
39
+ body = hits[:_source].sort.to_h
40
+ yield meta, body
41
+ end
42
+ total += result[:hits][:hits].size
43
+ break if result[:hits][:total] <= total
44
+
45
+ search_after = result[:hits][:hits][-1][:sort]
46
+ end
47
+ end
48
+
49
+ private
50
+
51
+ def connect
52
+ @http = Net::HTTP.start(@host, @port)
53
+ rescue StandardError => e
54
+ raise EsConnectionError, e.message
55
+ end
56
+
57
+ def request(req)
58
+ @http.request(req)
59
+ rescue StandardError => e
60
+ raise EsResponseError, e.message
61
+ end
62
+
63
+ def get_version
64
+ JSON.parse(@http.get('/').body, symbolize_names: true)[:version][:number]
65
+ rescue JSON::ParserError => e
66
+ raise EsResponseError, "JSON parse error: #{e.message}"
67
+ rescue Net::HTTPExceptions => e
68
+ raise EsResponseError, e.message
69
+ end
70
+ end
@@ -0,0 +1,3 @@
1
+ class Es2Bulk
2
+ VERSION = '0.1.0'.freeze
3
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: es2bulk
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - kzcat
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-12-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: Elasticsearch helper command. Retrive all documents from index and convert
56
+ documents to bulk format
57
+ email:
58
+ - kzcat@users.noreply.github.com
59
+ executables:
60
+ - es2bulk
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".gitignore"
65
+ - ".rspec"
66
+ - ".travis.yml"
67
+ - CODE_OF_CONDUCT.md
68
+ - Gemfile
69
+ - LICENSE
70
+ - LICENSE.txt
71
+ - README.md
72
+ - Rakefile
73
+ - bin/es2bulk
74
+ - es2bulk.gemspec
75
+ - lib/es2bulk.rb
76
+ - lib/es2bulk/es2bulk.rb
77
+ - lib/es2bulk/version.rb
78
+ homepage: https://github.com/kzcat/es2bulk
79
+ licenses:
80
+ - MIT
81
+ metadata: {}
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 2.7.6
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: Convert Elasticsearch documents to bulk format.
102
+ test_files: []