class PatriotAWS::Command::RedshiftCommand

Public Instance Methods

configure() click to toggle source

@see Patriot::Command::Base#configure

# File lib/patriot_aws/command/redshift.rb, line 58
def configure
  @name_suffix ||= _date_
  self
end
execute() click to toggle source
# File lib/patriot_aws/command/redshift.rb, line 63
def execute
  @logger.info 'start redshift query...'
  @options ||= {}

  ini = IniFile.load(@inifile)
  raise Exception, 'inifile not found.' if ini.nil?
  raise Exception, 'query is not set.'  if @query.nil?

  _set_options

  begin
    # replace variables
    if ini['s3credentials']
      @query = @query % ini['s3credentials'].symbolize_keys
    end

    conn = PG::Connection.new(
      ini['connection'].symbolize_keys
    )
    res   = conn.exec(@query)

    output_arr = Array.new
    if res then
      res.each_with_index do |r, idx|
        # print header
        if @options[:with_header] && idx == 0
          output_arr.push r.keys.join(@options[:delimiter])
        end

        output_arr.push r.values.join(@options[:delimiter])
      end
    end

    puts output_arr.join("\n")
  # rescue PGError => ex
  #   # may cause PGError when connection setting is invalid
  #   # e.g.
  #   # PG::ConnectionBad -> could not translate host name "test" to address: nodename nor servname provided, or not known
  #   print(ex.class," -> ",ex.message)
  #
  #   raise PGError
  # rescue => ex
  #   # Other Error process
  #   print(ex.class," -> ",ex.message)
  ensure
    conn.close if conn
  end
end
job_id() click to toggle source
# File lib/patriot_aws/command/redshift.rb, line 52
def job_id
  job_id = "#{command_name}_#{@name}_#{@name_suffix}"
  job_id
end

Private Instance Methods

_set_options() click to toggle source

@private set default option parameters

# File lib/patriot_aws/command/redshift.rb, line 114
def _set_options
  @options[:with_header]  = false if @options[:with_header].nil?
  @options[:delimiter]    = "\t"  if @options[:delimiter].nil?
end