output consolidation; added executable via poetry

This commit is contained in:
Ari Brown 2023-09-19 16:12:58 -04:00
parent ab344374d9
commit ffe0eddd9b
4 changed files with 46 additions and 34 deletions

View file

@ -1,32 +0,0 @@
#!/usr/bin/env python3
import minerva
import pprint
import readline
import argparse
pp = pprint.PrettyPrinter(indent=4)
parser = argparse.ArgumentParser(description="""
REPL for the Athena SQL engine
""")
parser.add_argument("-p", "--profile", default="hay", help="The AWS profile to use")
parser.add_argument("-o", "--output", default="s3://haystac-pmo-athena/output")
args = parser.parse_args()
m = minerva.Minerva(args.profile)
athena = m.athena(args.output)
text = input("> ")
while text != "\\q":
query = athena.query(text)
try:
data = query.results()
pp.pprint(data.head(10))
print()
print(f"\t({'$%.2f' % query.cost}, {query.runtime})")
except Exception as e:
print(e)
text = input("> ")

View file

@ -50,7 +50,11 @@ class Execute:
# Send the SQL to Athena for running # Send the SQL to Athena for running
def run(self): def run(self):
config = {"OutputLocation": self.athena.output} if self.__class__ == Query:
config = {"OutputLocation": os.path.join(self.athena.output, "results")}
else:
config = {"OutputLocation": self.athena.output}
if self.params: if self.params:
resp = self.client.start_query_execution(QueryString=self.query(), resp = self.client.start_query_execution(QueryString=self.query(),
ResultConfiguration=config, ResultConfiguration=config,
@ -94,6 +98,7 @@ class Query(Execute):
# Automatically includes unloading the results to Parquet format # Automatically includes unloading the results to Parquet format
def query(self): def query(self):
out = os.path.join(self.athena.output, out = os.path.join(self.athena.output,
"results",
str(random.random())) str(random.random()))
query = f"unload ({self.sql}) to {repr(out)} " + \ query = f"unload ({self.sql}) to {repr(out)} " + \
f"with (format = '{self.DATA_STYLE}')" f"with (format = '{self.DATA_STYLE}')"

37
minerva/console.py Executable file
View file

@ -0,0 +1,37 @@
import minerva
import pprint
import readline
import argparse
def main():
pp = pprint.PrettyPrinter(indent=4)
parser = argparse.ArgumentParser(description="""
REPL for the Athena SQL engine
""")
parser.add_argument("-p", "--profile", default="hay", help="The AWS profile to use")
parser.add_argument("-o", "--output", default="s3://haystac-pmo-athena/")
args = parser.parse_args()
m = minerva.Minerva(args.profile)
athena = m.athena(args.output)
text = input("> ")
while text != "\\q":
try:
first_word = text.split()[0]
if first_word.lower() in ["drop", "alter", "create", "update"]:
query = athena.execute(text)
query.finish()
else:
query = athena.query(text)
data = query.results()
pp.pprint(data.head(10))
print()
print(f"\t({'$%.2f' % query.cost}, {query.runtime})")
except Exception as e:
print(e)
text = input("> ")

View file

@ -9,10 +9,12 @@ authors = [
] ]
packages = [ packages = [
{ include = "minerva/**/*.py" }, { include = "minerva/**/*.py" },
{ include = "bin/*" }
] ]
readme = "README.md" readme = "README.md"
[tool.poetry.scripts]
minerva-console = "minerva.console:main"
[tool.poetry.dependencies] [tool.poetry.dependencies]
python = ">3.9, <3.11" python = ">3.9, <3.11"
boto3 = "^1.26.161" boto3 = "^1.26.161"