Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get results from Athena for the past week?

I am using Amazon Athena to get all console login that happened in past week, right now i am able to just get all the console logins regardless of the data. I need to modify the below query so that this query picks up all the aws console logins that happened in last week always.

WITH events AS (
  SELECT
    event.eventVersion,
    event.eventID,
    event.eventTime,
    event.eventName,
    event.eventType,
    event.eventSource,
    event.awsRegion,
    event.sourceIPAddress,
    event.userAgent,  
    event.userIdentity.type AS userType,
    event.userIdentity.arn AS userArn,
    event.userIdentity.principalId as userPrincipalId,
    event.userIdentity.accountId as userAccountId,
    event.userIdentity.userName as userName
  FROM cloudtrail.events
  CROSS JOIN UNNEST (Records) AS r (event)
)
SELECT userName,sourceIPAddress,eventName,eventTime FROM events WHERE eventName='ConsoleLogin';

The eventTime looks like": enter image description here

T

like image 397
Kittystone Avatar asked Apr 21 '17 07:04

Kittystone


People also ask

How do I get Athena query history?

Viewing recent queries Athena keeps a query history for 45 days. Open the Athena console at https://console.aws.amazon.com/athena/ . Choose Recent queries. The Recent queries tab shows information about each query that ran.

How do you get Athena yesterday's date?

SELECT current_date - interval '1' day AS yesterday_in_iso; I love the interval datatype, it reminds me of PostgreSQL.

How do you get the last day of the month in Athena?

LAST_DAY returns the date of the last day of the month that contains date . The return type is always DATE , regardless of the datatype of date .

How do I download Athena query results?

You cannot save results from the AWS CLI, but you can Specify a Query Result Location and Amazon Athena will automatically save a copy of the query results in an Amazon S3 location that you specify. You could then use the AWS CLI to download that results file.


2 Answers

If that column is coming through as text, you could convert it into a timestamp. I found that Amazon Athena can convert '2016-05-03 05:46:00' into a timestamp, so use a replace() function to get it into the right format:

select cast(replace(replace('2016-05-03T05:46:00Z', 'Z'), 'T', ' ') as timestamp)

Therefore, in your WITH section, replace event.eventType with:

cast(replace(replace(event.eventType, 'Z'), 'T', ' ') as timestamp) AS eventType,

You can then use standard WHERE statements against the date, such as:

WHERE eventType > '2017-04-01'

Or for the past week (based on the Presto documentation):

WHERE eventType > current_date - interval '7' day
like image 96
John Rotenstein Avatar answered Nov 13 '22 00:11

John Rotenstein


You can use from_iso8601_timestamp for the conversion like for example

SELECT *
FROM my_table
WHERE from_iso8601_timestamp(my_iso_field) > current_timestamp - interval '7' day
like image 24
xorho Avatar answered Nov 12 '22 22:11

xorho