Class: Aq::QueryBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/aq/query_builder.rb

Class Method Summary collapse

Class Method Details

.head(table, num) ⇒ Object



13
14
15
16
# 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) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 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(':', ' ')})"

  "CREATE EXTERNAL TABLE IF NOT EXISTS \#{table} (\n\#{schema_state}\n)\n\#{patition_state}\nROW FORMAT SERDE \"\#{serde}\"\nLOCATION \"\#{source}\"\n"
end

.ls(database) ⇒ Object



5
6
7
8
9
10
11
# 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) ⇒ Object



18
19
20
21
22
23
24
# 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) ⇒ Object



45
46
47
48
49
50
51
# 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