Module: HipsterSqlToHbase

Defined in:
lib/hipster_sql_to_hbase.rb,
lib/executor.rb,
lib/result_tree_to_json_converter.rb,
lib/result_tree_to_hbase_converter.rb

Overview

This module provides the methods necessary to parse valid SQL sentences into:

  • Treetop Syntax Trees

  • Hash Trees

  • JSON Trees

  • HBase (Thrift)

It also allows to directly execute valid SQL sentences as Thrift methods.

Defined Under Namespace

Classes: Executor, ResultTree, ResultTreeToHbaseConverter, ResultTreeToJsonConverter, SyntaxParser, ThriftCallGroup

Class Method Summary collapse

Class Method Details

.execute(string, host = nil, port = nil) ⇒ Object

Generates and automatically executes a HipsterSqlToHbase::ThriftCallGroup from a valid, SQL string.

The returned value varies depending on the SQL query type (i.e. SELECT, INSERT, etc).



195
196
197
198
199
# File 'lib/hipster_sql_to_hbase.rb', line 195

def execute(string,host=nil,port=nil)
  parsed_tree = parse_tree(string)
  return nil if parsed_tree.nil?
  parsed_tree.execute(host,port)
end

.parse(string) ⇒ Object

Generate a HipsterSqlToHbase::ThriftCallGroup from a valid, SQL string.

example:

HipsterSqlToHbase.parse "INSERT INTO `users` (`user`,`pass`) VALUES ('andy','w00dy'),('zaphod','b33bl3br0x')"

outputs:

[
  {
    :method=>"mutateRow", 
    :arguments=>[
      "users", 
      "c6af1d5b-01d7-477c-9539-f35a1e0758e2", 
      [<Apache::Hadoop::Hbase::Thrift::Mutation>, <Apache::Hadoop::Hbase::Thrift::Mutation>], 
      {}
    ]
  }, 
  {
    :method=>"mutateRow", 
    :arguments=>[
      "users",
      "b630cf3c-c969-420e-afd9-b646466a6743", 
      [<Apache::Hadoop::Hbase::Thrift::Mutation>, <Apache::Hadoop::Hbase::Thrift::Mutation>], 
      {}
    ]
  }
]

note:

The resulting ThriftCallGroup can be executed simply by calling its .execute() method.



185
186
187
188
189
# File 'lib/hipster_sql_to_hbase.rb', line 185

def parse(string)
  result_tree = parse_tree(string)
  return nil if result_tree.nil?
  result_tree.to_hbase
end

.parse_hash(string) ⇒ Object

Generate a Hash from a valid, SQL string.

example:

HipsterSqlToHbase.parse_hash "SELECT user,password FROM users WHERE id=1"

outputs:

{
 :query_type=>:select,
 :query_hash=>{
   :select=>["user", "password"],
   :from=>"users",
   :where=>[{:column=>"id", :condition=>:"=", :value=>1}],
   :limit=>nil,
   :order_by=>nil
 }
}


113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/hipster_sql_to_hbase.rb', line 113

def parse_hash(string)
  syntax_tree = parse_syntax(string)
  
  return nil if syntax_tree.nil?
  
  result = { 
    :query_type => syntax_tree.query_type, 
    :query_hash => syntax_tree.tree
  }
  result[:query_hash][:create_sentence] = string if syntax_tree.query_type == :create_table
  
  result
end

.parse_syntax(string) ⇒ Object

Generate a Treetop syntax tree from a valid, SQL string.

example:

HipsterSqlToHbase.parse_syntax "INSERT INTO users (user,password) VALUES ('user1','pass123'),('user2','2girls1pass')"

outputs:

#<Class:#<Treetop::Runtime::SyntaxNode:0x3274c98>>


93
94
95
# File 'lib/hipster_sql_to_hbase.rb', line 93

def parse_syntax(string)
  HipsterSqlToHbase::SyntaxParser.new.parse(string.squish)
end

.parse_tree(string) ⇒ Object

Generate a HipsterSqlToHbase::ResultTree from a valid, SQL string.

example:

HipsterSqlToHbase.parse_tree "SELECT user,password FROM users WHERE id=1"

outputs:

{
 :query_type=>:select,
 :query_hash=>{
   :select=>["user", "password"],
   :from=>"users",
   :where=>[{:column=>"id", :condition=>:"=", :value=>1}],
   :limit=>nil,
   :order_by=>nil
 }
}

Note: the main difference between parse_tree and parse_hash is that a ResultTree can be further converted to Thrift calls while a Hash cannot.



145
146
147
148
149
150
151
152
153
# File 'lib/hipster_sql_to_hbase.rb', line 145

def parse_tree(string)
  parsed_hash = parse_hash(string)
  
  if parsed_hash.nil?
    nil
  else
    HipsterSqlToHbase::ResultTree.new(parsed_hash)
  end
end