embulk-output-elasticsearch_using_url 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/Gemfile +2 -0
- data/LICENSE.txt +21 -0
- data/README.md +28 -0
- data/Rakefile +3 -0
- data/embulk-output-elasticsearch_using_url.gemspec +21 -0
- data/lib/embulk/output/elasticsearch_using_url.rb +148 -0
- metadata +120 -0
    
        checksums.yaml
    ADDED
    
    | @@ -0,0 +1,7 @@ | |
| 1 | 
            +
            ---
         | 
| 2 | 
            +
            SHA1:
         | 
| 3 | 
            +
              metadata.gz: 685d2edcad17b21ddc3ed1513b21d0eb60a65fbd
         | 
| 4 | 
            +
              data.tar.gz: b2f159f1f335d2b702505a5ebec892fc3f3462fc
         | 
| 5 | 
            +
            SHA512:
         | 
| 6 | 
            +
              metadata.gz: f951209582d56ebbc82cf13e92e9bd52b49abf17cafd7bdea36d64fdb7f58254e103d69665f40a2f6a8282faadc7084cf1a56cc81024578fda37d49f19c47237
         | 
| 7 | 
            +
              data.tar.gz: 0461770d6eedf3ab7f00204f7e31d06288768eb47a2453c20fb539695d2ff1c82fd387c476e27460ae56c5280bcabffa10453b7b43050cc47023f796de4c91aa
         | 
    
        data/Gemfile
    ADDED
    
    
    
        data/LICENSE.txt
    ADDED
    
    | @@ -0,0 +1,21 @@ | |
| 1 | 
            +
             | 
| 2 | 
            +
            MIT License
         | 
| 3 | 
            +
             | 
| 4 | 
            +
            Permission is hereby granted, free of charge, to any person obtaining
         | 
| 5 | 
            +
            a copy of this software and associated documentation files (the
         | 
| 6 | 
            +
            "Software"), to deal in the Software without restriction, including
         | 
| 7 | 
            +
            without limitation the rights to use, copy, modify, merge, publish,
         | 
| 8 | 
            +
            distribute, sublicense, and/or sell copies of the Software, and to
         | 
| 9 | 
            +
            permit persons to whom the Software is furnished to do so, subject to
         | 
| 10 | 
            +
            the following conditions:
         | 
| 11 | 
            +
             | 
| 12 | 
            +
            The above copyright notice and this permission notice shall be
         | 
| 13 | 
            +
            included in all copies or substantial portions of the Software.
         | 
| 14 | 
            +
             | 
| 15 | 
            +
            THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
         | 
| 16 | 
            +
            EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
         | 
| 17 | 
            +
            MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
         | 
| 18 | 
            +
            NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
         | 
| 19 | 
            +
            LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
         | 
| 20 | 
            +
            OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
         | 
| 21 | 
            +
            WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
         | 
    
        data/README.md
    ADDED
    
    | @@ -0,0 +1,28 @@ | |
| 1 | 
            +
            # Elaasticsearch Using Url output plugin for Embulk
         | 
| 2 | 
            +
            This plugin is forked from https://github.com/ipros-team/embulk-output-elasticsearch_ruby
         | 
| 3 | 
            +
             | 
| 4 | 
            +
            ## Overview
         | 
| 5 | 
            +
             | 
| 6 | 
            +
            * **Plugin type**: output
         | 
| 7 | 
            +
            * **Load all or nothing**: no
         | 
| 8 | 
            +
            * **Resume supported**: no
         | 
| 9 | 
            +
            * **Cleanup supported**: no
         | 
| 10 | 
            +
             | 
| 11 | 
            +
            ## Configuration
         | 
| 12 | 
            +
             | 
| 13 | 
            +
            ## Example
         | 
| 14 | 
            +
             | 
| 15 | 
            +
            ```yaml
         | 
| 16 | 
            +
            out:
         | 
| 17 | 
            +
              type: elaasticsearch_using_url
         | 
| 18 | 
            +
              mode: normal
         | 
| 19 | 
            +
              nodes:
         | 
| 20 | 
            +
                - url: "http://localhost:9200"
         | 
| 21 | 
            +
            ```
         | 
| 22 | 
            +
             | 
| 23 | 
            +
             | 
| 24 | 
            +
            ## Build
         | 
| 25 | 
            +
             | 
| 26 | 
            +
            ```
         | 
| 27 | 
            +
            $ rake
         | 
| 28 | 
            +
            ```
         | 
    
        data/Rakefile
    ADDED
    
    
| @@ -0,0 +1,21 @@ | |
| 1 | 
            +
             | 
| 2 | 
            +
            Gem::Specification.new do |spec|
         | 
| 3 | 
            +
              spec.name          = "embulk-output-elasticsearch_using_url"
         | 
| 4 | 
            +
              spec.version       = "0.1.0"
         | 
| 5 | 
            +
              spec.authors       = ["Yuma Murata"]
         | 
| 6 | 
            +
              spec.summary       = "Elasticsearch Using Url output plugin for Embulk"
         | 
| 7 | 
            +
              spec.description   = "Dumps records to Elasticsearch Using Url."
         | 
| 8 | 
            +
              spec.email         = ["murata@ebisol.co.jp"]
         | 
| 9 | 
            +
              spec.licenses      = ["MIT"]
         | 
| 10 | 
            +
              spec.homepage      = "https://github.com/ymurata/embulk-output-elasticsearch_using_url"
         | 
| 11 | 
            +
             | 
| 12 | 
            +
              spec.files         = `git ls-files`.split("\n") + Dir["classpath/*.jar"]
         | 
| 13 | 
            +
              spec.test_files    = spec.files.grep(%r{^(test|spec)/})
         | 
| 14 | 
            +
              spec.require_paths = ["lib"]
         | 
| 15 | 
            +
             | 
| 16 | 
            +
              spec.add_dependency 'elasticsearch'
         | 
| 17 | 
            +
              spec.add_dependency 'excon'
         | 
| 18 | 
            +
              spec.add_development_dependency 'embulk', ['>= 0.8.16']
         | 
| 19 | 
            +
              spec.add_development_dependency 'bundler', ['>= 1.10.6']
         | 
| 20 | 
            +
              spec.add_development_dependency 'rake', ['>= 10.0']
         | 
| 21 | 
            +
            end
         | 
| @@ -0,0 +1,148 @@ | |
| 1 | 
            +
            module Embulk
         | 
| 2 | 
            +
              module Output
         | 
| 3 | 
            +
             | 
| 4 | 
            +
                class ElasticsearchUsingUrl < OutputPlugin
         | 
| 5 | 
            +
                  Plugin.register_output("elasticsearch_using_url", self)
         | 
| 6 | 
            +
                  ENABLE_MODE = %w[normal update]
         | 
| 7 | 
            +
             | 
| 8 | 
            +
                  def self.transaction(config, schema, count, &control)
         | 
| 9 | 
            +
                    task = {
         | 
| 10 | 
            +
                      "nodes" => config.param("nodes", :array, default: [{ 'url' => 'http://localhost:9200' }]),
         | 
| 11 | 
            +
                      "request_timeout" => config.param("request_timeout", :integer, default: 60),
         | 
| 12 | 
            +
                      "index" => config.param("index", :string, default: 'logstash-%Y.%m.%d'),
         | 
| 13 | 
            +
                      "mode" => config.param("mode", :string, default: 'normal'),
         | 
| 14 | 
            +
                      "reload_connections" => config.param("reload_connections", :bool, default: true),
         | 
| 15 | 
            +
                      "reload_on_failure" => config.param("reload_on_failure", :bool, default: false),
         | 
| 16 | 
            +
                      "delete_old_index" => config.param("delete_old_index", :bool, default: false),
         | 
| 17 | 
            +
                      "index_type" => config.param("index_type", :string),
         | 
| 18 | 
            +
                      "id_keys" => config.param("id_keys", :array, default: nil),
         | 
| 19 | 
            +
                      "id_format" => config.param("id_format", :string, default: nil),
         | 
| 20 | 
            +
                      "array_columns" => config.param("array_columns", :array, default: nil),
         | 
| 21 | 
            +
                      "bulk_actions" => config.param("bulk_actions", :integer, default: 1000),
         | 
| 22 | 
            +
                      "retry_on_failure" => config.param("retry_on_failure", :integer, default: 5),
         | 
| 23 | 
            +
                      "before_template_name" => config.param("before_template_name", :string, default: nil),
         | 
| 24 | 
            +
                      "before_template" => config.param("before_template", :hash, default: nil),
         | 
| 25 | 
            +
                    }
         | 
| 26 | 
            +
                    task['time_value'] = Time.now.strftime('%Y.%m.%d.%H.%M.%S')
         | 
| 27 | 
            +
                    task['index'] = Time.now.strftime(task['index'])
         | 
| 28 | 
            +
             | 
| 29 | 
            +
                    unless ENABLE_MODE.include?(task['mode'])
         | 
| 30 | 
            +
                      raise ConfigError.new "`mode` must be one of #{ENABLE_MODE.join(', ')}"
         | 
| 31 | 
            +
                    end
         | 
| 32 | 
            +
                    Embulk.logger.info("mode => #{task['mode']}")
         | 
| 33 | 
            +
             | 
| 34 | 
            +
                    if task['before_template_name'] && task['before_template']
         | 
| 35 | 
            +
                      client = create_client(task)
         | 
| 36 | 
            +
                      Embulk.logger.info("put template => #{task['before_template_name']}")
         | 
| 37 | 
            +
                      client.indices.put_template name: task['before_template_name'], body: task['before_template']
         | 
| 38 | 
            +
                    end
         | 
| 39 | 
            +
             | 
| 40 | 
            +
                    return self.resume(task, schema, count, &control)
         | 
| 41 | 
            +
                  end
         | 
| 42 | 
            +
             | 
| 43 | 
            +
                  def self.resume(task, schema, count, &control)
         | 
| 44 | 
            +
                    task_reports = yield(task)
         | 
| 45 | 
            +
                    next_config_diff = {}
         | 
| 46 | 
            +
                    return next_config_diff
         | 
| 47 | 
            +
                  end
         | 
| 48 | 
            +
             | 
| 49 | 
            +
                  def init
         | 
| 50 | 
            +
                    @nodes = task["nodes"]
         | 
| 51 | 
            +
                    @index_type = task["index_type"]
         | 
| 52 | 
            +
                    @id_keys = task["id_keys"]
         | 
| 53 | 
            +
                    @id_format = task["id_format"]
         | 
| 54 | 
            +
                    @bulk_actions = task["bulk_actions"]
         | 
| 55 | 
            +
                    @array_columns = task["array_columns"]
         | 
| 56 | 
            +
                    @retry_on_failure = task["retry_on_failure"]
         | 
| 57 | 
            +
                    @mode = task["mode"]
         | 
| 58 | 
            +
                    @index = task['index']
         | 
| 59 | 
            +
             | 
| 60 | 
            +
                    @client = create_client(task)
         | 
| 61 | 
            +
                    @bulk_message = []
         | 
| 62 | 
            +
                  end
         | 
| 63 | 
            +
             | 
| 64 | 
            +
                  def close
         | 
| 65 | 
            +
                  end
         | 
| 66 | 
            +
             | 
| 67 | 
            +
                  def add(page)
         | 
| 68 | 
            +
                    page.each do |record|
         | 
| 69 | 
            +
                      hash = Hash[schema.names.zip(record)]
         | 
| 70 | 
            +
                      action = (@mode == 'update') ? :update : :index
         | 
| 71 | 
            +
                      meta = {}
         | 
| 72 | 
            +
                      meta[action] = { _index: @index, _type: @index_type }
         | 
| 73 | 
            +
                      meta[action][:_id] = generate_id(@id_format, hash, @id_keys) unless @id_keys.nil?
         | 
| 74 | 
            +
                      source = generate_array(hash)
         | 
| 75 | 
            +
                      @bulk_message << meta
         | 
| 76 | 
            +
                      @bulk_message << source
         | 
| 77 | 
            +
                      if @bulk_actions * 2 <= @bulk_message.size
         | 
| 78 | 
            +
                        send
         | 
| 79 | 
            +
                      end
         | 
| 80 | 
            +
                    end
         | 
| 81 | 
            +
                  end
         | 
| 82 | 
            +
             | 
| 83 | 
            +
                  def finish
         | 
| 84 | 
            +
                    if @bulk_message.size > 0
         | 
| 85 | 
            +
                      send
         | 
| 86 | 
            +
                    end
         | 
| 87 | 
            +
                  end
         | 
| 88 | 
            +
             | 
| 89 | 
            +
                  def abort
         | 
| 90 | 
            +
                  end
         | 
| 91 | 
            +
             | 
| 92 | 
            +
                  def commit
         | 
| 93 | 
            +
                    task_report = {}
         | 
| 94 | 
            +
                    return task_report
         | 
| 95 | 
            +
                  end
         | 
| 96 | 
            +
             | 
| 97 | 
            +
                  private
         | 
| 98 | 
            +
             | 
| 99 | 
            +
                  def create_client(task)
         | 
| 100 | 
            +
                    return ::Elasticsearch::Client.new urls: task['nodes'].map{|v| v['url']}.join(','),
         | 
| 101 | 
            +
                      reload_connections: task['reload_connections'],
         | 
| 102 | 
            +
                      reload_on_failure: task['reload_on_failure'],
         | 
| 103 | 
            +
                      retry_on_failure: task['retry_on_failure'],
         | 
| 104 | 
            +
                      transport_options: {
         | 
| 105 | 
            +
                        request: { timeout: task['request_timeout'] }
         | 
| 106 | 
            +
                      }
         | 
| 107 | 
            +
                  end
         | 
| 108 | 
            +
             | 
| 109 | 
            +
                  def generate_array(record)
         | 
| 110 | 
            +
                    result = {}
         | 
| 111 | 
            +
             | 
| 112 | 
            +
                    record.each { |key, value|
         | 
| 113 | 
            +
                      result[key] = value
         | 
| 114 | 
            +
                      next if (value.nil? || !@array_columns)
         | 
| 115 | 
            +
                      @array_columns.each do |array_column|
         | 
| 116 | 
            +
                        if array_column['name'] == key
         | 
| 117 | 
            +
                          array_value = value.split(array_column['delimiter']).reject(&:empty?)
         | 
| 118 | 
            +
                          array_value = array_value.map(&:to_i) if array_column['is_integer']
         | 
| 119 | 
            +
                          result[key] = array_value
         | 
| 120 | 
            +
                        end
         | 
| 121 | 
            +
                      end
         | 
| 122 | 
            +
                    }
         | 
| 123 | 
            +
                    (@mode == 'update') ? {doc: result} : result
         | 
| 124 | 
            +
                  end
         | 
| 125 | 
            +
             | 
| 126 | 
            +
                  def generate_id(template, record, id_keys)
         | 
| 127 | 
            +
                    template % id_keys.map { |key| record[key] }
         | 
| 128 | 
            +
                  end
         | 
| 129 | 
            +
             | 
| 130 | 
            +
                  def send
         | 
| 131 | 
            +
                    retries = 0
         | 
| 132 | 
            +
                    begin
         | 
| 133 | 
            +
                      @client.bulk body: @bulk_message
         | 
| 134 | 
            +
                      Embulk.logger.info "bulk: #{@bulk_message.size/2} success."
         | 
| 135 | 
            +
                    rescue => e
         | 
| 136 | 
            +
                      if retries < @retry_on_failure
         | 
| 137 | 
            +
                        retries += 1
         | 
| 138 | 
            +
                        Embulk.logger.warn "Could not push logs to Elasticsearch, resetting connection and trying again. #{e.message}"
         | 
| 139 | 
            +
                        sleep 2**retries
         | 
| 140 | 
            +
                        retry
         | 
| 141 | 
            +
                      end
         | 
| 142 | 
            +
                      raise "Could not push logs to Elasticsearch after #{retries} retries. #{e.message}"
         | 
| 143 | 
            +
                    end
         | 
| 144 | 
            +
                    @bulk_message.clear
         | 
| 145 | 
            +
                  end
         | 
| 146 | 
            +
                end
         | 
| 147 | 
            +
              end
         | 
| 148 | 
            +
            end
         | 
    
        metadata
    ADDED
    
    | @@ -0,0 +1,120 @@ | |
| 1 | 
            +
            --- !ruby/object:Gem::Specification
         | 
| 2 | 
            +
            name: embulk-output-elasticsearch_using_url
         | 
| 3 | 
            +
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            +
              version: 0.1.0
         | 
| 5 | 
            +
            platform: ruby
         | 
| 6 | 
            +
            authors:
         | 
| 7 | 
            +
            - Yuma Murata
         | 
| 8 | 
            +
            autorequire: 
         | 
| 9 | 
            +
            bindir: bin
         | 
| 10 | 
            +
            cert_chain: []
         | 
| 11 | 
            +
            date: 2017-02-20 00:00:00.000000000 Z
         | 
| 12 | 
            +
            dependencies:
         | 
| 13 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 14 | 
            +
              name: elasticsearch
         | 
| 15 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 16 | 
            +
                requirements:
         | 
| 17 | 
            +
                - - ">="
         | 
| 18 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 19 | 
            +
                    version: '0'
         | 
| 20 | 
            +
              type: :runtime
         | 
| 21 | 
            +
              prerelease: false
         | 
| 22 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 23 | 
            +
                requirements:
         | 
| 24 | 
            +
                - - ">="
         | 
| 25 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 26 | 
            +
                    version: '0'
         | 
| 27 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 28 | 
            +
              name: excon
         | 
| 29 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 30 | 
            +
                requirements:
         | 
| 31 | 
            +
                - - ">="
         | 
| 32 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 33 | 
            +
                    version: '0'
         | 
| 34 | 
            +
              type: :runtime
         | 
| 35 | 
            +
              prerelease: false
         | 
| 36 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 37 | 
            +
                requirements:
         | 
| 38 | 
            +
                - - ">="
         | 
| 39 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 40 | 
            +
                    version: '0'
         | 
| 41 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 42 | 
            +
              name: embulk
         | 
| 43 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 44 | 
            +
                requirements:
         | 
| 45 | 
            +
                - - ">="
         | 
| 46 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 47 | 
            +
                    version: 0.8.16
         | 
| 48 | 
            +
              type: :development
         | 
| 49 | 
            +
              prerelease: false
         | 
| 50 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 51 | 
            +
                requirements:
         | 
| 52 | 
            +
                - - ">="
         | 
| 53 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 54 | 
            +
                    version: 0.8.16
         | 
| 55 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 56 | 
            +
              name: bundler
         | 
| 57 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 58 | 
            +
                requirements:
         | 
| 59 | 
            +
                - - ">="
         | 
| 60 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 61 | 
            +
                    version: 1.10.6
         | 
| 62 | 
            +
              type: :development
         | 
| 63 | 
            +
              prerelease: false
         | 
| 64 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 65 | 
            +
                requirements:
         | 
| 66 | 
            +
                - - ">="
         | 
| 67 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 68 | 
            +
                    version: 1.10.6
         | 
| 69 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 70 | 
            +
              name: rake
         | 
| 71 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 72 | 
            +
                requirements:
         | 
| 73 | 
            +
                - - ">="
         | 
| 74 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 75 | 
            +
                    version: '10.0'
         | 
| 76 | 
            +
              type: :development
         | 
| 77 | 
            +
              prerelease: false
         | 
| 78 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 79 | 
            +
                requirements:
         | 
| 80 | 
            +
                - - ">="
         | 
| 81 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 82 | 
            +
                    version: '10.0'
         | 
| 83 | 
            +
            description: Dumps records to Elasticsearch Using Url.
         | 
| 84 | 
            +
            email:
         | 
| 85 | 
            +
            - murata@ebisol.co.jp
         | 
| 86 | 
            +
            executables: []
         | 
| 87 | 
            +
            extensions: []
         | 
| 88 | 
            +
            extra_rdoc_files: []
         | 
| 89 | 
            +
            files:
         | 
| 90 | 
            +
            - Gemfile
         | 
| 91 | 
            +
            - LICENSE.txt
         | 
| 92 | 
            +
            - README.md
         | 
| 93 | 
            +
            - Rakefile
         | 
| 94 | 
            +
            - embulk-output-elasticsearch_using_url.gemspec
         | 
| 95 | 
            +
            - lib/embulk/output/elasticsearch_using_url.rb
         | 
| 96 | 
            +
            homepage: https://github.com/ymurata/embulk-output-elasticsearch_using_url
         | 
| 97 | 
            +
            licenses:
         | 
| 98 | 
            +
            - MIT
         | 
| 99 | 
            +
            metadata: {}
         | 
| 100 | 
            +
            post_install_message: 
         | 
| 101 | 
            +
            rdoc_options: []
         | 
| 102 | 
            +
            require_paths:
         | 
| 103 | 
            +
            - lib
         | 
| 104 | 
            +
            required_ruby_version: !ruby/object:Gem::Requirement
         | 
| 105 | 
            +
              requirements:
         | 
| 106 | 
            +
              - - ">="
         | 
| 107 | 
            +
                - !ruby/object:Gem::Version
         | 
| 108 | 
            +
                  version: '0'
         | 
| 109 | 
            +
            required_rubygems_version: !ruby/object:Gem::Requirement
         | 
| 110 | 
            +
              requirements:
         | 
| 111 | 
            +
              - - ">="
         | 
| 112 | 
            +
                - !ruby/object:Gem::Version
         | 
| 113 | 
            +
                  version: '0'
         | 
| 114 | 
            +
            requirements: []
         | 
| 115 | 
            +
            rubyforge_project: 
         | 
| 116 | 
            +
            rubygems_version: 2.5.1
         | 
| 117 | 
            +
            signing_key: 
         | 
| 118 | 
            +
            specification_version: 4
         | 
| 119 | 
            +
            summary: Elasticsearch Using Url output plugin for Embulk
         | 
| 120 | 
            +
            test_files: []
         |