Class: HBase

Inherits:
Object
  • Object
show all
Includes:
Admin
Defined in:
lib/hbase-jruby/hbase.rb,
lib/hbase-jruby/cell.rb,
lib/hbase-jruby/util.rb,
lib/hbase-jruby/admin.rb,
lib/hbase-jruby/table.rb,
lib/hbase-jruby/result.rb,
lib/hbase-jruby/scoped.rb,
lib/hbase-jruby/version.rb,
lib/hbase-jruby/byte_array.rb,
lib/hbase-jruby/column_key.rb,
lib/hbase-jruby/dependency.rb,
lib/hbase-jruby/scoped/aggregation.rb

Overview

HBase connection

Defined Under Namespace

Modules: Admin, JRuby, Util Classes: ByteArray, Cell, ColumnKey, Result, Scoped, Table

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ HBase

Connects to HBase

Parameters:

  • config (Hash) (defaults to: {})

    A key-value pairs to build HBaseConfiguration from



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/hbase-jruby/hbase.rb', line 13

def initialize config = {}
  Util.import_java_classes!

  @config =
    case config
    when org.apache.hadoop.conf.Configuration
      config
    else
      HBaseConfiguration.create.tap do |hbcfg|
        config.each do |k, v|
          hbcfg.set k.to_s, v.to_s
        end
      end
    end
  @htable_pool = HTablePool.new @config, java.lang.Integer::MAX_VALUE
end

Instance Attribute Details

#configObject (readonly)



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/hbase-jruby/hbase.rb', line 6

class HBase
  attr_reader :config

  include Admin

  # Connects to HBase
  # @param [Hash] config A key-value pairs to build HBaseConfiguration from
  def initialize config = {}
    Util.import_java_classes!

    @config =
      case config
      when org.apache.hadoop.conf.Configuration
        config
      else
        HBaseConfiguration.create.tap do |hbcfg|
          config.each do |k, v|
            hbcfg.set k.to_s, v.to_s
          end
        end
      end
    @htable_pool = HTablePool.new @config, java.lang.Integer::MAX_VALUE
  end

  # Returns an HBaseAdmin object for administration
  # @yield [org.apache.hadoop.hbase.client.HBaseAdmin]
  # @return [org.apache.hadoop.hbase.client.HBaseAdmin]
  def admin
    if block_given?
      with_admin { |admin| yield admin }
    else
      HBaseAdmin.new @config
    end
  end

  # Closes HTablePool and connection
  # @return [nil]
  def close
    @htable_pool.close
    HConnectionManager.deleteConnection(@config, true)
  end

  # Returns the list of HBase::Table instances
  # @return [Array<HBase::Table>]
  def tables
    table_names.map { |tn| table(tn) }
  end

  # Returns the list of table names
  # @return [Array<String>]
  def table_names
    with_admin { |admin| admin.list_tables.map(&:name_as_string) }
  end

  # Creates HBase::Table instance for the specified name
  # @param [#to_s] table_name The name of the table
  # @return [HBase::Table]
  def table table_name
    ht = HBase::Table.send :new, @config, @htable_pool, table_name

    if block_given?
      begin
        yield ht
      ensure
        ht.close
      end
    else
      ht
    end
  end
end

#javaObject (readonly)



# File 'lib/hbase-jruby/cell.rb', line 3

#nameObject (readonly)



# File 'lib/hbase-jruby/table.rb', line 5

#tableObject (readonly)



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/hbase-jruby/hbase.rb', line 63

def table table_name
  ht = HBase::Table.send :new, @config, @htable_pool, table_name

  if block_given?
    begin
      yield ht
    ensure
      ht.close
    end
  else
    ht
  end
end

Class Method Details

.ColumnKey(cf, cq) ⇒ Object

Shortcut method to HBase::ColumnKey.new

Parameters:

  • cf (Object)

    Column family

  • cq (Object)

    Column qualifier



6
7
8
# File 'lib/hbase-jruby/column_key.rb', line 6

def ColumnKey cf, cq
  ColumnKey.new cf, cq
end

.resolve_dependency!(dist, verbose = false) ⇒ Array<String>

Resolve Hadoop and HBase dependency with Maven or hbase command (Experimental)

Parameters:

  • dist (String)

    Distribution version or path to pom.xml file

  • verbose (true, false) (defaults to: false)

    Verbose output

Returns:

  • (Array<String>)

    Loaded JAR files



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/hbase-jruby/dependency.rb', line 12

def resolve_dependency! dist, verbose = false
  silencer = verbose ? '' : '> /dev/null'
  tempfiles = []
  jars =
    if dist == :hbase
      # Check for hbase executable
      hbase = `which hbase`
      raise RuntimeError, "Cannot find executable `hbase`" if hbase.empty?
      `hbase classpath`.split(':')
    else
      # Check for Maven executable
      mvn = `which mvn`
      raise RuntimeError, "Cannot find executable `mvn`" if mvn.empty?

      distname = dist.downcase.sub(/\.xml$/, '')
      path = [
        File.expand_path("../pom/#{distname}.xml", __FILE__),
        dist.to_s,
      ].select { |f| File.exists? f }.first

      # Try github head
      unless path
        begin
          xml = open("https://raw.github.com/junegunn/hbase-jruby/master/lib/hbase-jruby/pom/#{distname}.xml").read
          tempfiles << tf = Tempfile.new("#{distname}.xml")
          tf.close(false)
          path = tf.path
          File.open(path, 'w') do |f|
            f << xml
          end
        rescue OpenURI::HTTPError => e
          # No such distribution anywhere
        end
      end

      raise ArgumentError, "Invalid distribution: #{dist}" unless path

      # Download dependent JAR files and build classpath string
      tempfiles << tf = Tempfile.new('hbase-jruby-classpath')
      tf.close(false)
      system "mvn org.apache.maven.plugins:maven-dependency-plugin:2.5.1:resolve org.apache.maven.plugins:maven-dependency-plugin:2.5.1:build-classpath -Dsilent=true -Dmdep.outputFile=#{tf.path} -f #{path} #{silencer}"

      raise RuntimeError.new("Error occurred. Set verbose parameter to see the log.") unless $?.exitstatus == 0

      File.read(tf.path).split(':')
    end

  # Load jars
  jars.select { |jar| File.exists?(jar) && File.extname(jar) == '.jar' }.select do |jar|
    require jar
  end

  Util.import_java_classes!
ensure
  tempfiles.each { |tempfile| tempfile.unlink rescue nil }
end

Instance Method Details

#admin {|org.apache.hadoop.hbase.client.HBaseAdmin| ... } ⇒ org.apache.hadoop.hbase.client.HBaseAdmin

Returns an HBaseAdmin object for administration

Yields:

  • (org.apache.hadoop.hbase.client.HBaseAdmin)

Returns:

  • (org.apache.hadoop.hbase.client.HBaseAdmin)


33
34
35
36
37
38
39
# File 'lib/hbase-jruby/hbase.rb', line 33

def admin
  if block_given?
    with_admin { |admin| yield admin }
  else
    HBaseAdmin.new @config
  end
end

#closenil

Closes HTablePool and connection

Returns:

  • (nil)


43
44
45
46
# File 'lib/hbase-jruby/hbase.rb', line 43

def close
  @htable_pool.close
  HConnectionManager.deleteConnection(@config, true)
end

#table_namesArray<String>

Returns the list of table names

Returns:

  • (Array<String>)


56
57
58
# File 'lib/hbase-jruby/hbase.rb', line 56

def table_names
  with_admin { |admin| admin.list_tables.map(&:name_as_string) }
end

#tablesArray<HBase::Table>

Returns the list of HBase::Table instances

Returns:



50
51
52
# File 'lib/hbase-jruby/hbase.rb', line 50

def tables
  table_names.map { |tn| table(tn) }
end