Module: TreasureData::API::Table

Included in:
TreasureData::API
Defined in:
lib/td/client/api/table.rb

Instance Method Summary collapse

Instance Method Details

#create_item_table(db, table, primary_key, primary_key_type) ⇒ true

Parameters:

  • db (String)
  • table (String)
  • primary_key (String)
  • primary_key_type (String)

Returns:

  • (true)


60
61
62
63
# File 'lib/td/client/api/table.rb', line 60

def create_item_table(db, table, primary_key, primary_key_type)
  params = {'primary_key' => primary_key, 'primary_key_type' => primary_key_type}
  create_table(db, table, :item, params)
end

#create_log_table(db, table) ⇒ true

Parameters:

  • db (String)
  • table (String)

Returns:

  • (true)


51
52
53
# File 'lib/td/client/api/table.rb', line 51

def create_log_table(db, table)
  create_table(db, table, :log)
end

#delete_table(db, table) ⇒ Symbol

Parameters:

  • db (String)
  • table (String)

Returns:

  • (Symbol)


119
120
121
122
123
124
125
126
127
# File 'lib/td/client/api/table.rb', line 119

def delete_table(db, table)
  code, body, res = post("/v3/table/delete/#{e db}/#{e table}")
  if code != "200"
    raise_error("Delete table failed", res)
  end
  js = checked_json(body, %w[])
  type = (js['type'] || '?').to_sym
  return type
end

#list_tables(db) ⇒ Array

Parameters:

  • db (String)

Returns:

  • (Array)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/td/client/api/table.rb', line 10

def list_tables(db)
  code, body, res = get("/v3/table/list/#{e db}")
  if code != "200"
    raise_error("List tables failed", res)
  end
  js = checked_json(body, %w[tables])
  result = {}
  js["tables"].map {|m|
    name = m['name']
    type = (m['type'] || '?').to_sym
    count = (m['count'] || 0).to_i  # TODO?
    created_at = m['created_at']
    updated_at = m['updated_at']
    last_import = m['counter_updated_at']
    last_log_timestamp = m['last_log_timestamp']
    estimated_storage_size = m['estimated_storage_size'].to_i
    schema = JSON.parse(m['schema'] || '[]')
    expire_days = m['expire_days']
    primary_key = m['primary_key']
    primary_key_type = m['primary_key_type']
    result[name] = [type, schema, count, created_at, updated_at, estimated_storage_size, last_import, last_log_timestamp, expire_days, primary_key, primary_key_type]
  }
  return result
end

#swap_table(db, table1, table2) ⇒ true

Parameters:

  • db (String)
  • table1 (String)
  • table2 (String)

Returns:

  • (true)


84
85
86
87
88
89
90
# File 'lib/td/client/api/table.rb', line 84

def swap_table(db, table1, table2)
  code, body, res = post("/v3/table/swap/#{e db}/#{e table1}/#{e table2}")
  if code != "200"
    raise_error("Swap tables failed", res)
  end
  return true
end

#tail(db, table, count, to, from, &block) ⇒ Array?

Parameters:

  • db (String)
  • table (String)
  • count (Fixnum)
  • to (Fixnum)
  • from (Fixnum)
  • block (Proc)

Returns:

  • (Array, nil)


136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/td/client/api/table.rb', line 136

def tail(db, table, count, to, from, &block)
  params = {'format' => 'msgpack'}
  params['count'] = count.to_s if count
  params['to'] = to.to_s if to
  params['from'] = from.to_s if from
  code, body, res = get("/v3/table/tail/#{e db}/#{e table}", params)
  if code != "200"
    raise_error("Tail table failed", res)
  end
  if block
    MessagePack::Unpacker.new.feed_each(body, &block)
    nil
  else
    result = []
    MessagePack::Unpacker.new.feed_each(body) {|row|
      result << row
    }
    return result
  end
end

#update_expire(db, table, expire_days) ⇒ true

Parameters:

  • db (String)
  • table (String)
  • expire_days (Fixnum)

Returns:

  • (true)


108
109
110
111
112
113
114
# File 'lib/td/client/api/table.rb', line 108

def update_expire(db, table, expire_days)
  code, body, res = post("/v3/table/update/#{e db}/#{e table}", {'expire_days'=>expire_days})
  if code != "200"
    raise_error("Update table expiration failed", res)
  end
  return true
end

#update_schema(db, table, schema_json) ⇒ true

Parameters:

  • db (String)
  • table (String)
  • schema_json (String)

Returns:

  • (true)


96
97
98
99
100
101
102
# File 'lib/td/client/api/table.rb', line 96

def update_schema(db, table, schema_json)
  code, body, res = post("/v3/table/update-schema/#{e db}/#{e table}", {'schema'=>schema_json})
  if code != "200"
    raise_error("Create schema table failed", res)
  end
  return true
end