class Aq::QueryBuilder

Public Class Methods

head(table, num) click to toggle source
# File lib/aq/query_builder.rb, line 13
def self.head(table, num)
  raise InvalidParameterError.new 'The table name must be specified in the format `DATABSE.TABLE`' unless table.include? '.'
  "SELECT * FROM #{table} LIMIT #{num}"
end
load(table, source, schema, source_format, patition) click to toggle source
# File lib/aq/query_builder.rb, line 26
    def self.load(table, source, schema, source_format, patition)
      raise InvalidParameterError.new '`SOURCE` must start with "s3://"' unless source.start_with? 's3://'
      schema_state = schema.get_all.map do |s|
        "`#{s[:name]}` #{s[:type]}"
      end.join(',')
      raise InvalidParameterError.new 'Now aq support only NEWLINE_DELIMITED_JSON.' unless %w(NEWLINE_DELIMITED_JSON).include? source_format
      serde = 'org.apache.hive.hcatalog.data.JsonSerDe'
      patition_state = patition.nil? ? '' : "PARTITIONED BY (#{patition.gsub(':', ' ')})"

      <<"SQL"
CREATE EXTERNAL TABLE IF NOT EXISTS #{table} (
#{schema_state}
)
#{patition_state}
ROW FORMAT SERDE "#{serde}"
LOCATION "#{source}"
SQL
    end
ls(database) click to toggle source
# File lib/aq/query_builder.rb, line 5
def self.ls(database)
  if database.nil?
    'SHOW DATABASES'
  else
    "SHOW TABLES IN #{database}"
  end
end
mk(name) click to toggle source
# File lib/aq/query_builder.rb, line 18
def self.mk(name)
  if !name.include? '.'
    "CREATE DATABASE IF NOT EXISTS #{name}"
  else
    raise InvalidParameterError.new 'Use `load` command if you create new table.'
  end
end
rm(name) click to toggle source
# File lib/aq/query_builder.rb, line 45
def self.rm(name)
  if !name.include? '.'
    "DROP DATABASE IF EXISTS #{name}"
  else
    "DROP TABLE IF EXISTS #{name}"
  end
end