Entity and methods
Dataitem
Bases: Entity
A class representing a dataitem.
Source code in digitalhub_data/entities/dataitems/entity/_base.py
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 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 |
|
__init__(project, name, uuid, kind, metadata, spec, status, user=None)
Constructor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
project |
str
|
Project name. |
required |
name |
str
|
Name of the object. |
required |
uuid |
str
|
Version of the object. |
required |
kind |
str
|
Kind the object. |
required |
metadata |
Metadata
|
Metadata of the object. |
required |
spec |
DataitemSpec
|
Specification of the object. |
required |
status |
DataitemStatus
|
Status of the object. |
required |
user |
str
|
Owner of the object. |
None
|
Source code in digitalhub_data/entities/dataitems/entity/_base.py
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 |
|
as_file()
Get dataitem as file. In the case of a local store, the store returns the current path of the dataitem. In the case of a remote store, the dataitem is downloaded in a temporary directory.
Returns:
Type | Description |
---|---|
str
|
Path of the dataitem. |
Source code in digitalhub_data/entities/dataitems/entity/_base.py
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
|
download(destination=None, overwrite=False)
Download dataitem from remote storage.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
destination |
str
|
Destination path as filename. |
None
|
overwrite |
bool
|
Specify if overwrite an existing file. Default value is False. |
False
|
Returns:
Type | Description |
---|---|
str
|
Path of the downloaded dataitem. |
Source code in digitalhub_data/entities/dataitems/entity/_base.py
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 |
|
export(filename=None)
Export object as a YAML file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filename |
str
|
Name of the export YAML file. If not specified, the default value is used. |
None
|
Returns:
Type | Description |
---|---|
None
|
|
Source code in digitalhub_data/entities/dataitems/entity/_base.py
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
|
refresh()
Refresh object from backend.
Returns:
Type | Description |
---|---|
Dataitem
|
Entity refreshed. |
Source code in digitalhub_data/entities/dataitems/entity/_base.py
113 114 115 116 117 118 119 120 121 122 123 124 |
|
save(update=False)
Save entity into backend.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
update |
bool
|
Flag to indicate update. |
False
|
Returns:
Type | Description |
---|---|
Dataitem
|
Entity saved. |
Source code in digitalhub_data/entities/dataitems/entity/_base.py
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 |
|
write_file(src)
Write file into dataitem path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
src |
str
|
Local path of the file to write. |
required |
Returns:
Type | Description |
---|---|
str
|
Path to the written file. |
Source code in digitalhub_data/entities/dataitems/entity/_base.py
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 |
|
DataitemDataitem
Bases: Dataitem
Dataitem dataitem.
Source code in digitalhub_data/entities/dataitems/entity/dataitem.py
6 7 8 9 |
|
DataitemIceberg
Bases: Dataitem
Iceberg dataitem.
Source code in digitalhub_data/entities/dataitems/entity/iceberg.py
4 5 6 7 |
|
DataitemTable
Bases: Dataitem
Table dataitem.
Source code in digitalhub_data/entities/dataitems/entity/table.py
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 |
|
as_df(file_format=None, **kwargs)
Read dataitem as a DataFrame. If the dataitem is not local, it will be downloaded
to a temporary folder and deleted after the method is executed. If no file_format is passed,
the function will try to infer it from the dataitem.spec.path attribute.
The path of the dataitem is specified in the spec attribute, and must be a store aware path.
If the dataitem is stored on s3 bucket, the path must be s3:// Parameters: Format of the file. (Supported csv and parquet). Keyword arguments passed to the read_df function. Returns: DataFrame. Write DataFrame as parquet/csv/table into dataitem path. Parameters: DataFrame to write. Extension of the file. Keyword arguments passed to the write_df function. Returns: Path to the written dataframe. Create dataitem from dictionary. Parameters: Dictionary to create object from. Returns: Dataitem object. Create a new object instance. Parameters: Project name. Object name. Kind the object. ID of the object (UUID4). Description of the object (human readable). Remote git source for object. List of labels. Flag to determine if object must be embedded in project. Object path on local file system or remote storage.
If not provided, it's generated. Spec keyword arguments. Returns: Object instance..
Name
Type
Description
Default
file_format
str
None
**kwargs
dict
{}
Type
Description
Any
Source code in
digitalhub_data/entities/dataitems/entity/table.py
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
def as_df(self, file_format: str | None = None, **kwargs) -> Any:
"""
Read dataitem as a DataFrame. If the dataitem is not local, it will be downloaded
to a temporary folder and deleted after the method is executed. If no file_format is passed,
the function will try to infer it from the dataitem.spec.path attribute.
The path of the dataitem is specified in the spec attribute, and must be a store aware path.
If the dataitem is stored on s3 bucket, the path must be s3://<bucket>/<path_to_dataitem>.
If the dataitem is stored on database (Postgres is the only one supported), the path must
be sql://postgres/<database>/<schema>/<table/view>.
Parameters
----------
file_format : str
Format of the file. (Supported csv and parquet).
**kwargs : dict
Keyword arguments passed to the read_df function.
Returns
-------
Any
DataFrame.
"""
datastore = get_datastore(self.spec.path)
tmp_path = False
# Download dataitem if not local
if not self._check_local(self.spec.path):
path = datastore.download(self.spec.path)
tmp_path = True
else:
path = self.spec.path
# Get file info
store = get_store(path)
file_info = store.get_file_info(self.spec.path, path)
if file_info is not None:
self.refresh()
self.status.add_file(file_info)
self.save(update=True)
# Check file format and get dataitem as DataFrame
extension = self._get_extension(self.spec.path, file_format)
df = datastore.read_df(path, extension, **kwargs)
# Delete tmp folder
if tmp_path:
pth = Path(path)
if pth.is_file():
pth = pth.parent
shutil.rmtree(pth)
return df
write_df(df, extension=None, **kwargs)
Name
Type
Description
Default
df
Any
required
extension
str
None
**kwargs
dict
{}
Type
Description
str
Source code in
digitalhub_data/entities/dataitems/entity/table.py
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
def write_df(
self,
df: Any,
extension: str | None = None,
**kwargs,
) -> str:
"""
Write DataFrame as parquet/csv/table into dataitem path.
Parameters
----------
df : Any
DataFrame to write.
extension : str
Extension of the file.
**kwargs : dict
Keyword arguments passed to the write_df function.
Returns
-------
str
Path to the written dataframe.
"""
datastore = get_datastore(self.spec.path)
target = datastore.write_df(df, self.spec.path, extension=extension, **kwargs)
# Get file info
store = get_store(target)
file_info = store.get_file_info(target, self.spec.path)
if file_info is not None:
self.status.add_file(file_info)
return target
dataitem_from_dict(obj)
Name
Type
Description
Default
obj
dict
required
Type
Description
Dataitem
Source code in
digitalhub_data/entities/dataitems/builder.py
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
def dataitem_from_dict(obj: dict) -> Dataitem:
"""
Create dataitem from dictionary.
Parameters
----------
obj : dict
Dictionary to create object from.
Returns
-------
Dataitem
Dataitem object.
"""
kind = obj.get("kind")
cls = _choose_dataitem_type(kind)
return cls.from_dict(obj)
dataitem_from_parameters(project, name, kind, uuid=None, description=None, git_source=None, labels=None, embedded=True, path=None, **kwargs)
Name
Type
Description
Default
project
str
required
name
str
required
kind
str
required
uuid
str
None
description
str
None
git_source
str
None
labels
list[str]
None
embedded
bool
True
path
str
None
**kwargs
dict
{}
Type
Description
Dataitem
Source code in
digitalhub_data/entities/dataitems/builder.py
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
def dataitem_from_parameters(
project: str,
name: str,
kind: str,
uuid: str | None = None,
description: str | None = None,
git_source: str | None = None,
labels: list[str] | None = None,
embedded: bool = True,
path: str | None = None,
**kwargs,
) -> Dataitem:
"""
Create a new object instance.
Parameters
----------
project : str
Project name.
name : str
Object name.
kind : str
Kind the object.
uuid : str
ID of the object (UUID4).
description : str
Description of the object (human readable).
git_source : str
Remote git source for object.
labels : list[str]
List of labels.
embedded : bool
Flag to determine if object must be embedded in project.
path : str
Object path on local file system or remote storage.
If not provided, it's generated.
**kwargs : dict
Spec keyword arguments.
Returns
-------
Dataitem
Object instance.
"""
name = build_name(name)
uuid = build_uuid(uuid)
if path is None:
bucket = get_s3_bucket()
entity_type = EntityTypes.DATAITEMS.value
path = f"s3://{bucket}/{project}/{entity_type}/{name}/{uuid}/data.parquet"
metadata = build_metadata(
kind,
project=project,
name=name,
version=uuid,
description=description,
source=git_source,
labels=labels,
embedded=embedded,
)
spec = build_spec(
kind,
path=path,
**kwargs,
)
status = build_status(kind)
cls = _choose_dataitem_type(kind)
return cls(
project=project,
name=name,
uuid=uuid,
kind=kind,
metadata=metadata,
spec=spec,
status=status,
)