forked from feast-dev/feast
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
359 lines (293 loc) · 8.79 KB
/
cli.py
File metadata and controls
359 lines (293 loc) · 8.79 KB
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
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
347
348
349
350
351
352
353
354
355
356
357
358
359
# Copyright 2019 The Feast Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import json
import logging
import sys
import click
import pkg_resources
import yaml
from feast.client import Client
from feast.config import Config
from feast.core.IngestionJob_pb2 import IngestionJobStatus
from feast.feature_set import FeatureSet, FeatureSetRef
from feast.loaders.yaml import yaml_loader
_logger = logging.getLogger(__name__)
_common_options = [
click.option("--core-url", help="Set Feast core URL to connect to"),
click.option("--serving-url", help="Set Feast serving URL to connect to"),
]
def common_options(func):
"""
Options that are available for most CLI commands
"""
for option in reversed(_common_options):
func = option(func)
return func
@click.group()
def cli():
pass
@cli.command()
@click.option(
"--client-only", "-c", is_flag=True, help="Print only the version of the CLI"
)
@common_options
def version(client_only: bool, **kwargs):
"""
Displays version and connectivity information
"""
try:
feast_versions_dict = {
"sdk": {"version": str(pkg_resources.get_distribution("feast"))}
}
if not client_only:
feast_client = Client(**kwargs)
feast_versions_dict.update(feast_client.version())
print(json.dumps(feast_versions_dict))
except Exception as e:
_logger.error("Error initializing backend store")
_logger.exception(e)
sys.exit(1)
@cli.group()
def config():
"""
View and edit Feast properties
"""
pass
@config.command(name="list")
def config_list():
"""
List Feast properties for the currently active configuration
"""
try:
print(Config())
except Exception as e:
_logger.error("Error occurred when reading Feast configuration file")
_logger.exception(e)
sys.exit(1)
@config.command(name="set")
@click.argument("prop")
@click.argument("value")
def config_set(prop, value):
"""
Set a Feast properties for the currently active configuration
"""
try:
conf = Config()
conf.set(option=prop.strip(), value=value.strip())
conf.save()
except Exception as e:
_logger.error("Error in reading config file")
_logger.exception(e)
sys.exit(1)
@cli.group(name="feature-sets")
def feature_set():
"""
Create and manage feature sets
"""
pass
@feature_set.command(name="list")
def feature_set_list():
"""
List all feature sets
"""
feast_client = Client() # type: Client
table = []
for fs in feast_client.list_feature_sets():
table.append([fs.name, fs.version, repr(fs)])
from tabulate import tabulate
print(tabulate(table, headers=["NAME", "VERSION", "REFERENCE"], tablefmt="plain"))
@feature_set.command("apply")
@click.option(
"--filename",
"-f",
help="Path to a feature set configuration file that will be applied",
type=click.Path(exists=True),
)
def feature_set_create(filename):
"""
Create or update a feature set
"""
feature_sets = [FeatureSet.from_dict(fs_dict) for fs_dict in yaml_loader(filename)]
feast_client = Client() # type: Client
feast_client.apply(feature_sets)
@feature_set.command("describe")
@click.argument("name", type=click.STRING)
@click.argument("version", type=click.INT)
def feature_set_describe(name: str, version: int):
"""
Describe a feature set
"""
feast_client = Client() # type: Client
fs = feast_client.get_feature_set(name=name, version=version)
if not fs:
print(
f'Feature set with name "{name}" and version "{version}" could not be found'
)
return
print(yaml.dump(yaml.safe_load(str(fs)), default_flow_style=False, sort_keys=False))
@cli.group(name="projects")
def project():
"""
Create and manage projects
"""
pass
@project.command(name="create")
@click.argument("name", type=click.STRING)
def project_create(name: str):
"""
Create a project
"""
feast_client = Client() # type: Client
feast_client.create_project(name)
@project.command(name="archive")
@click.argument("name", type=click.STRING)
def project_archive(name: str):
"""
Archive a project
"""
feast_client = Client() # type: Client
feast_client.archive_project(name)
@project.command(name="list")
def project_list():
"""
List all projects
"""
feast_client = Client() # type: Client
table = []
for project in feast_client.list_projects():
table.append([project])
from tabulate import tabulate
print(tabulate(table, headers=["NAME"], tablefmt="plain"))
@cli.group(name="ingest-jobs")
def ingest_job():
"""
Manage ingestion jobs
"""
pass
@ingest_job.command("list")
@click.option("--job-id", "-i", help="Show only ingestion jobs with the given job id")
@click.option(
"--feature-set-ref",
"-f",
help="Show only ingestion job targeting the feature set with the given reference",
)
@click.option(
"--store-name",
"-s",
help="List only ingestion job that ingest into feast store with given name",
)
# TODO: types
def ingest_job_list(job_id, feature_set_ref, store_name):
"""
List ingestion jobs
"""
# parse feature set reference
if feature_set_ref is not None:
feature_set_ref = FeatureSetRef.from_str(feature_set_ref)
# pull & render ingestion jobs as a table
feast_client = Client()
table = []
for ingest_job in feast_client.list_ingest_jobs(
job_id=job_id, feature_set_ref=feature_set_ref, store_name=store_name
):
table.append([ingest_job.id, IngestionJobStatus.Name(ingest_job.status)])
from tabulate import tabulate
print(tabulate(table, headers=["ID", "STATUS"], tablefmt="plain"))
@ingest_job.command("describe")
@click.argument("job_id")
def ingest_job_describe(job_id: str):
"""
Describe the ingestion job with the given id.
"""
# find ingestion job for id
feast_client = Client()
jobs = feast_client.list_ingest_jobs(job_id=job_id)
if len(jobs) < 1:
print(f"Ingestion Job with id {job_id} could not be found")
sys.exit(1)
job = jobs[0]
# pretty render ingestion job as yaml
print(
yaml.dump(yaml.safe_load(str(job)), default_flow_style=False, sort_keys=False)
)
@ingest_job.command("stop")
@click.option(
"--wait", "-w", is_flag=True, help="Wait for the ingestion job to fully stop."
)
@click.option(
"--timeout",
"-t",
default=600,
help="Timeout in seconds to wait for the job to stop.",
)
@click.argument("job_id")
def ingest_job_stop(wait: bool, timeout: int, job_id: str):
"""
Stop ingestion job for id.
"""
# find ingestion job for id
feast_client = Client()
jobs = feast_client.list_ingest_jobs(job_id=job_id)
if len(jobs) < 1:
print(f"Ingestion Job with id {job_id} could not be found")
sys.exit(1)
job = jobs[0]
feast_client.stop_ingest_job(job)
# wait for ingestion job to stop
if wait:
job.wait(IngestionJobStatus.ABORTED, timeout=timeout)
@ingest_job.command("restart")
@click.argument("job_id")
def ingest_job_restart(job_id: str):
"""
Restart job for id.
Waits for the job to fully restart.
"""
# find ingestion job for id
feast_client = Client()
jobs = feast_client.list_ingest_jobs(job_id=job_id)
if len(jobs) < 1:
print(f"Ingestion Job with id {job_id} could not be found")
sys.exit(1)
job = jobs[0]
feast_client.restart_ingest_job(job)
@cli.command()
@click.option(
"--name", "-n", help="Feature set name to ingest data into", required=True
)
@click.option(
"--version", "-v", help="Feature set version to ingest data into", type=int
)
@click.option(
"--filename",
"-f",
help="Path to file to be ingested",
type=click.Path(exists=True),
required=True,
)
@click.option(
"--file-type",
"-t",
type=click.Choice(["CSV"], case_sensitive=False),
help="Type of file to ingest. Defaults to CSV.",
)
def ingest(name, version, filename, file_type):
"""
Ingest feature data into a feature set
"""
feast_client = Client() # type: Client
feature_set = feast_client.get_feature_set(name=name, version=version)
feature_set.ingest_file(file_path=filename)
if __name__ == "__main__":
cli()