forked from feast-dev/feast
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcli.py
More file actions
292 lines (237 loc) · 7.2 KB
/
cli.py
File metadata and controls
292 lines (237 loc) · 7.2 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
# 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 sys
import logging
import click
from feast import config as feast_config
from feast.client import Client
from feast.resource import ResourceFactory
from feast.feature_set import FeatureSet
import toml
import pkg_resources
from feast.loaders.yaml import yaml_loader
import yaml
import json
_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(
core_url=feast_config.get_config_property_or_fail(
"core_url", force_config=kwargs
),
serving_url=feast_config.get_config_property_or_fail(
"serving_url", force_config=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:
feast_config_string = toml.dumps(feast_config._get_or_create_config())
if not feast_config_string.strip():
print("Configuration has not been set")
else:
print(feast_config_string.replace('""', "").strip())
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:
feast_config.set_property(prop.strip(), value.strip())
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(
core_url=feast_config.get_config_property_or_fail("core_url")
) # type: Client
table = []
for fs in feast_client.list_feature_sets():
table.append([fs.name, fs.version])
from tabulate import tabulate
print(tabulate(table, headers=["NAME", "VERSION"], tablefmt="plain"))
@feature_set.command("create")
@click.argument("name")
def feature_set_create(name):
"""
Create a feature set
"""
feast_client = Client(
core_url=feast_config.get_config_property_or_fail("core_url")
) # type: Client
feast_client.apply(FeatureSet(name=name))
@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(
core_url=feast_config.get_config_property_or_fail("core_url")
) # 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(
core_url=feast_config.get_config_property_or_fail("core_url")
) # 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(
core_url=feast_config.get_config_property_or_fail("core_url")
) # type: Client
feast_client.archive_project(name)
@project.command(name="list")
def feature_set_list():
"""
List all projects
"""
feast_client = Client(
core_url=feast_config.get_config_property_or_fail("core_url")
) # 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.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(
core_url=feast_config.get_config_property_or_fail("core_url")
) # type: Client
feature_set = feast_client.get_feature_set(name=name, version=version)
feature_set.ingest_file(file_path=filename)
@cli.command()
@click.option(
"--filename",
"-f",
help="Path to the configuration file that will be applied",
type=click.Path(exists=True),
)
def apply(filename):
"""
Apply a configuration to a resource by filename or stdin
"""
resources = [
ResourceFactory.get_resource(res_dict["kind"]).from_dict(res_dict)
for res_dict in yaml_loader(filename)
]
feast_client = Client(
core_url=feast_config.get_config_property_or_fail("core_url")
) # type: Client
feast_client.apply(resources)
if __name__ == "__main__":
cli()