AWS Athena output result.json to s3 - CREATE TABLE AS / INSERT INTO SELECT? - json

Is it anyhow possible to write the results of an AWS Athena query to a results.json within an s3 bucket?
My first idea was to use INSERT INTO SELECT ID, COUNT(*) ... or INSERT OVERWRITE but this seems not be supported according Amazon Athena DDL Statements and tdhoppers Blogpost
Is it anyhow possible to CREATE TABLE with new data with AWS Athena?
Is there any work around with AWS Glue?
Anyhow possible to trigger an lambda function with the results of Athena?
(I'm aware of S3 Hooks)
It would not matter to me to overwrite the whole json file / table and always create a new json, since it is very limited statistics I aggregate.
I do know AWS Athena automatically writes the results to an S3 bucket as CSV. However I like to do simple aggregations and write the outputs directly to a public s3 so that an spa angular application in the browser is able to read it. Thus JSON Format and a specific path is important to me.

The work around for me with glue. Use Athena jdbc driver for running the query and load result in a dataframe. Then save the dataframe as the required format on specified S3 location.
df=spark.read.format('jdbc').options(url='jdbc:awsathena://AwsRegion=region;UID=your-access-key;PWD=your-secret-access-key;Schema=database name;S3OutputLocation=s3 location where jdbc drivers stores athena query results',
driver='com.simba.athena.jdbc42.Driver',
dbtable='(your athena query)').load()
df.repartition(1).write.format("json").save("s3 location")
Specify query in format dbtable='(select * from foo)'
Download jar from here and store it in S3.
While configuring etl job on glue specify s3 location for jar in Jar lib path.

you can get Athena to create data in s3 by using a "create table as select" (CTAS) query. In that query you can specify where and in what format you want the created table to store its data.
https://docs.aws.amazon.com/athena/latest/ug/ctas-examples.html
For json, the example you are looking for is:
CREATE TABLE ctas_json_unpartitioned
WITH (
format = 'JSON',
external_location = 's3://my_athena_results/ctas_json_unpartitioned/')
AS SELECT key1, name1, address1, comment1
FROM table1;
this would result in single lines json format

Related

How to index JSON files stored in S3 by keys?

Suppose I want to store hundreds of JSON files in S3. All these JSON files have the same schema. I would like to search these JSON files by keys and values: e.g. find all JSON files with a key a value = "abc*" and a key x value = "xyz" . I expect the search to return the file names and the keys that match the query.
What is the best way to index JSON files stored in S3 by keys ?
This is a follow-up of my previous question
You might want to consider using S3 Select.
With Amazon S3 Select, you can use simple structured query language
(SQL) statements to filter the contents of Amazon S3 objects and
retrieve just the subset of data that you need. By using Amazon S3
Select to filter this data, you can reduce the amount of data that
Amazon S3 transfers, which reduces the cost and latency to retrieve
this data.
Amazon S3 Select works on objects stored in CSV, JSON, or Apache
Parquet format.
Full docs on S3 Select.
Here's a nice blog post on how to use S3 Select.
https://aws.amazon.com/blogs/storage/querying-data-without-servers-or-databases-using-amazon-s3-select/
And a sample code would look like this:
import boto3
# S3 bucket to query (Change this to your bucket)
S3_BUCKET = 'greg-college-data'
s3 = boto3.client('s3')
r = s3.select_object_content(
Bucket=S3_BUCKET,
Key='COLLEGE_DATA_2015.csv',
ExpressionType='SQL',
Expression="select \"INSTNM\" from s3object s where s.\"STABBR\" in ['OR', 'IA']",
InputSerialization={'CSV': {"FileHeaderInfo": "Use"}},
OutputSerialization={'CSV': {}},
)
for event in r['Payload']:
if 'Records' in event:
records = event['Records']['Payload'].decode('utf-8')
print(records)
Code soruce.

Pass in list from S3 to Athena query

I've used AWS Glue to aggregate data from an S3 bucket, and am now using Athena to query that data. Can I now pass a separate file from S3 to query against my dataset?
For example, if I had the following query:
SELECT city,population,coordinates
FROM "Country"."Japan"
WHERE city IN ('Tokyo','Kyoto','Osaka');
I want to place a .txt file in S3 that lists ('Tokyo','Kyoto','Osaka'), so that my query would look something like:
SELECT city,population,coordinates
FROM "Country"."Japan"
WHERE city IN << some file path in s3 >>;
That is not part of the SQL standard.
However, you could create another table in Amazon Athena that contains a list of cities. The LOCATION parameter of that table (which points to the data) could point to the text file with the name of a city on each line. You could then use:
WHERE city IN (SELECT city FROM city_table)
This way, all the information is 'within' Athena, but Athena actually goes to S3 to read the contents of the table.

How to Export GA360 table from Big query to snowflake through GCS as json file without data loss?

I am exporting GA360 table from Big query to snowflake as json format using bq cli command. I am losing some fields when I load it as table in snowflake. I use the copy command to load my json data from GCS external stage in snowflake to snowflake tables. But, I am missing some fields that are part of nested array. I even tried compressing the file when I export to gcs but I still loose data. Can someone suggest me how I can do this. I don't want to flatten the table in bigquery and transfer that. My daily table size is minimum of 1.5GB to maximum of 4GB.
bq extract \
--project_id=myproject \
--destination_format=NEWLINE_DELIMITED_JSON \
--compression GZIP \
datasetid.ga_sessions_20191001 \
gs://test_bucket/ga_sessions_20191001-*.json
I have set up my integration, file format, and stage in snowflake. I copying data from this bucket to a table that has one variant field. The row count matches with Big query but the fields are missing.
I am guessing this is due to the limit snowflake has where each variant column should be of 16MB. Is there some way I can compress each variant field to be under 16MB?
I had no problem exporting GA360, and getting the full objects into Snowflake.
First I exported the demo table bigquery-public-data.google_analytics_sample.ga_sessions_20170801 into GCS, JSON formatted.
Then I loaded it into Snowflake:
create or replace table ga_demo2(src variant);
COPY INTO ga_demo2
FROM 'gcs://[...]/ga_sessions000000000000'
FILE_FORMAT=(TYPE='JSON');
And then to find the transactionIds:
SELECT src:visitId, hit.value:transaction.transactionId
FROM ga_demo1, lateral flatten(input => src:hits) hit
WHERE src:visitId='1501621191'
LIMIT 10
Cool things to notice:
I read the GCS files easily from Snowflake deployed in AWS.
JSON manipulation in Snowflake is really cool.
See https://hoffa.medium.com/funnel-analytics-with-sql-match-recognize-on-snowflake-8bd576d9b7b1 for more.

Reading JSON & CSV of same format through AWS Athena Stored in S3

I am having the different data formats in my S3 folder inside bucket -
Table created which has the same columns for all the CSV, JSON & gz format files.
Now i am querying the data from Athena through the query -
SELECT *
FROM test2
WHERE readtime >= '2016-10-12'
AND readtime < '2016-10-24'
ORDER BY readtime
I am able to get the data from CSV and GZ file formats but not from the JSON files.
Should I have to use the different format for querying the JSON files ?
and
Can i use the same table to Query the JSON files which i have used for CSV files ?
you will need to create a different table on top of JSON file which will use JsonSerDe.
Yes, per Tanveer's answer, you need to create a separate table for the JSON. But with a recently announced feature called Federated Queries (that's, at the time of writing this, in preview in us-east-1), you should be able to achieve querying both tables with the same query. See https://docs.aws.amazon.com/athena/latest/ug/writing-federated-queries.html

Convert file of JSON objects to Parquet file

Motivation: I want to load the data into Apache Drill. I understand that Drill can handle JSON input, but I want to see how it performs on Parquet data.
Is there any way to do this without first loading the data into Hive, etc and then using one of the Parquet connectors to generate an output file?
Kite has support for importing JSON to both Avro and Parquet formats via its command-line utility, kite-dataset.
First, you would infer the schema of your JSON:
kite-dataset json-schema sample-file.json -o schema.avsc
Then you can use that file to create a Parquet Hive table:
kite-dataset create mytable --schema schema.avsc --format parquet
And finally, you can load your JSON into the dataset.
kite-dataset json-import sample-file.json mytable
You can also import an entire directly stored in HDFS. In that case, Kite will use a MR job to do the import.
You can actually use Drill itself to create a parquet file from the output of any query.
create table student_parquet as select * from `student.json`;
The above line should be good enough. Drill interprets the types based on the data in the fields. You can substitute your own query and create a parquet file.
To complete the answer of #rahul, you can use drill to do this - but I needed to add more to the query to get it working out of the box with drill.
create table dfs.tmp.`filename.parquet` as select * from dfs.`/tmp/filename.json` t
I needed to give it the storage plugin (dfs) and the "root" config can read from the whole disk and is not writable. But the tmp config (dfs.tmp) is writable and writes to /tmp. So I wrote to there.
But the problem is that if the json is nested or perhaps contains unusual characters, I would get a cryptic
org.apache.drill.common.exceptions.UserRemoteException: SYSTEM ERROR: java.lang.IndexOutOfBoundsException:
If I have a structure that looks like members: {id:123, name:"joe"} I would have to change the select to
select members.id as members_id, members.name as members_name
or
select members.id as `members.id`, members.name as `members.name`
to get it to work.
I assume the reason is that parquet is a "column" store so you need columns. JSON isn't by default so you need to convert it.
The problem is I have to know my json schema and I have to build the select to include all the possibilities. I'd be happy if some knows a better way to do this.