/
table_model.py
257 lines (217 loc) · 7.79 KB
/
table_model.py
1
2
3
4
5
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#!/usr/bin/env python
from tdclient.model import Model
class Table(Model):
"""Database table on Treasure Data Service
"""
def __init__(self, *args, **kwargs):
super(Table, self).__init__(args[0])
self.database = None
self._db_name = args[1]
self._table_name = args[2]
if 3 < len(args):
self._type = args[3]
self._schema = args[4]
self._count = args[5]
else:
self._type = kwargs.get("type")
self._schema = kwargs.get("schema")
self._count = kwargs.get("count")
self._created_at = kwargs.get("created_at")
self._updated_at = kwargs.get("updated_at")
self._estimated_storage_size = kwargs.get("estimated_storage_size")
self._last_import = kwargs.get("last_import")
self._last_log_timestamp = kwargs.get("last_log_timestamp")
self._expire_days = kwargs.get("expire_days")
self._primary_key = kwargs.get("primary_key")
self._primary_key_type = kwargs.get("primary_key_type")
@property
def type(self):
"""a string represents the type of the table
"""
return self._type
@property
def db_name(self):
"""a string represents the name of the database
"""
return self._db_name
@property
def table_name(self):
"""a string represents the name of the table
"""
return self._table_name
@property
def schema(self):
"""
[[column_name:str, column_type:str, alias:str]]: The :obj:`list` of a schema
"""
return self._schema
@property
def count(self):
"""int: total number of the table
"""
return self._count
@property
def estimated_storage_size(self):
"""estimated storage size
"""
return self._estimated_storage_size
@property
def primary_key(self):
"""
TODO: add docstring
"""
return self._primary_key
@property
def primary_key_type(self):
"""
TODO: add docstring
"""
return self._primary_key_type
@property
def database_name(self):
"""a string represents the name of the database
"""
return self._db_name
@property
def name(self):
"""a string represents the name of the table
"""
return self._table_name
@property
def created_at(self):
"""
:class:`datetime.datetime`: Created datetime
"""
return self._created_at
@property
def updated_at(self):
"""
:class:`datetime.datetime`: Updated datetime
"""
return self._updated_at
@property
def last_import(self):
""":class:`datetime.datetime`
"""
return self._last_import
@property
def last_log_timestamp(self):
""":class:`datetime.datetime`
"""
return self._last_log_timestamp
@property
def expire_days(self):
"""an int represents the days until expiration
"""
return self._expire_days
@property
def permission(self):
"""
str: permission for the database (e.g. "administrator", "full_access", etc.)
"""
if self.database is None:
self._update_database()
return self.database.permission
@property
def identifier(self):
"""a string identifier of the table
"""
return "%s.%s" % (self._db_name, self._table_name)
def delete(self):
"""a string represents the type of deleted table
"""
return self._client.delete_table(self._db_name, self._table_name)
def tail(self, count, to=None, _from=None):
"""
Args:
count (int): Number for record to show up from the end.
to: Deprecated parameter.
_from: Deprecated parameter.
Returns:
the contents of the table in reverse order based on the registered time
(last data first).
"""
return self._client.tail(self._db_name, self._table_name, count, to, _from)
def import_data(self, format, bytes_or_stream, size, unique_id=None):
"""Import data into Treasure Data Service
Args:
format (str): format of data type (e.g. "msgpack.gz")
bytes_or_stream (str or file-like): a byte string or a file-like object contains the data
size (int): the length of the data
unique_id (str): a unique identifier of the data
Returns:
second in float represents elapsed time to import data
"""
return self._client.import_data(
self._db_name,
self._table_name,
format,
bytes_or_stream,
size,
unique_id=unique_id,
)
def import_file(self, format, file, unique_id=None):
"""Import data into Treasure Data Service, from an existing file on filesystem.
This method will decompress/deserialize records from given file, and then
convert it into format acceptable from Treasure Data Service ("msgpack.gz").
Args:
file (str or file-like): a name of a file, or a file-like object contains the data
unique_id (str): a unique identifier of the data
Returns:
float represents the elapsed time to import data
"""
return self._client.import_file(
self._db_name, self._table_name, format, file, unique_id=unique_id
)
def export_data(self, storage_type, **kwargs):
"""Export data from Treasure Data Service
Args:
storage_type (str): type of the storage
**kwargs (dict): optional parameters. Assuming the following keys:
- access_key_id (str):
ID to access the information to be exported.
- secret_access_key (str):
Password for the `access_key_id`.
- file_prefix (str, optional):
Filename of exported file.
Default: "<database_name>/<table_name>"
- file_format (str, optional):
File format of the information to be
exported. {"jsonl.gz", "tsv.gz", "json.gz"}
- from (int, optional):
From Time of the data to be exported in Unix epoch format.
- to (int, optional):
End Time of the data to be exported in Unix epoch format.
- assume_role (str, optional):
Assume role.
- bucket (str):
Name of bucket to be used.
- domain_key (str, optional):
Job domain key.
- pool_name (str, optional):
For Presto only. Pool name to be used, if not
specified, default pool would be used.
Returns:
:class:`tdclient.models.Job`
"""
return self._client.export_data(
self._db_name, self._table_name, storage_type, kwargs
)
@property
def estimated_storage_size_string(self):
"""a string represents estimated size of the table in human-readable format
"""
if self._estimated_storage_size <= 1024 * 1024:
return "0.0 GB"
elif self._estimated_storage_size <= 60 * 1024 * 1024:
return "0.01 GB"
elif self._estimated_storage_size <= 60 * 1024 * 1024 * 1024:
return "%.1f GB" % (
float(self._estimated_storage_size) / (1024 * 1024 * 1024)
)
else:
return "%d GB" % int(
float(self._estimated_storage_size) / (1024 * 1024 * 1024)
)
def _update_database(self):
self.database = self._client.database(self._db_name)