Class: Aliyun::Odps::Tables

Inherits:
ServiceObject show all
Defined in:
lib/aliyun/odps/model/tables.rb

Overview

Methods for Tables

Instance Attribute Summary

Attributes inherited from ServiceObject

#master

Instance Method Summary collapse

Methods inherited from ServiceObject

build, #client, #initialize, #project, service_pool

Constructor Details

This class inherits a constructor from Aliyun::Odps::ServiceObject

Instance Method Details

#create(name, schema, options = {}) ⇒ Object

Create Table

Parameters:

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

    a customizable set of options

Options Hash (options):

  • :comment (String)

    specify table comment

Returns:

  • Table

Raises:

  • InstanceTaskNotSuccessError if instance task failed



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/aliyun/odps/model/tables.rb', line 56

def create(name, schema, options = {})
  Utils.stringify_keys!(options)
  table = Table.new(name: name, schema: schema, project: project)
  table.comment = options['comment'] if options.key?('comment')

  task = InstanceTask.new(name: 'SQLCreateTableTask', type: 'SQL', query: generate_create_sql(table))

  instance = project.instances.create([task])
  instance.wait_for_success
  table
end

#delete(name) ⇒ Object

Delete Table

Returns:

  • true

Raises:

  • InstanceTaskNotSuccessError if instance task failed



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/aliyun/odps/model/tables.rb', line 75

def delete(name)
  table = Table.new(name: name, project: project)

  task = InstanceTask.new(
    name: 'SQLDropTableTask',
    type: 'SQL',
    query: generate_drop_sql(table)
  )

  instance = project.instances.create([task])
  instance.wait_for_success
  true
end

#get(name) ⇒ Object Also known as: table

Get Table

See Also:



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/aliyun/odps/model/tables.rb', line 31

def get(name)
  path = "/projects/#{project.name}/tables/#{name}"
  resp = client.get(path)

  build_table(
    Utils.dig_value(resp.parsed_response, 'Table')
    .merge(
      'creation_time' => resp.headers['x-odps-creation-time'],
      'last_modified' => resp.headers['Last-Modified'],
      'owner' => resp.headers['x-odps-owner']
    )
  )
end

#list(options = {}) ⇒ Object

List tables in this project

Parameters:

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

    a customizable set of options

Options Hash (options):

  • :name (String)

    specify the table name

  • :owner (String)

    specify the table owner

  • :marker (String)
  • :maxitems (String) — default: 1000

See Also:



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/aliyun/odps/model/tables.rb', line 14

def list(options = {})
  Utils.stringify_keys!(options)
  path = "/projects/#{project.name}/tables"
  query = Utils.hash_slice(options, 'name', 'owner', 'marker', 'maxitems')
  query.merge!(tables: true, expectmarker: true)
  result = client.get(path, query: query).parsed_response

  Aliyun::Odps::List.build(result, %w(Tables Table)) do |hash|
    build_table(hash)
  end
end