AWS Athena select query to fetch error code from status column - amazon-web-services

AWS Athena trying to run a select query as below to fetch error code from the status column, but getting the below error
The query which I am trying:
select * from s3_accesslog where status = '404'
Error: SYNTAX_ERROR: line 1:78: '=' cannot be applied to integer, varchar(3)
select * from s3_accesslog where status like '%404%'
Error: SYNTAX_ERROR: line 1:71: Left side of LIKE expression must evaluate to a varchar (actual: integer)

Looks like your status codes are stored in the table as integers, if you remove the quotes the query should work.
So try:
select * from s3_accesslog where status = 404

Related

AWS Athena, Erro when Len of String

I tried to show the len of one string..
SELECT length(fieldA) FROM "data_prod"."myscore" limit 10;
but I receive that error
Your query has the following error(s):
SYNTAX_ERROR: line 1:8: Unexpected parameters (bigint) for function length. Expected: length(varchar(x)) , length(char(x)) , length(varbinary)
This query ran against the "raw_public_data_prod" database, unless qualified by the query. Please post the error message on our forum or contact customer support with Query Id: 92f6401b-108a-4671-951e-3a8e882f3b20.
enter image description here
This is what you are looking for.
SELECT length(cast(fieldA as varchar)) FROM "data_prod"."myscore" limit 10;
length function doesn't work for bigint data type. So you have to cast it as varchar to do so.

AWS Hive query for INPUT__FILE__NAME

AWS Hive with GLUE metastore
i'm trying to get this query
select partition_name from mytable where
substr(reverse(split(reverse(INPUT__FILE__NAME), '/')[0]),5,8) = '20200705';
file names in S3 looks like abc_20200705
and get an issue from Glue
2020-08-11T23:20:55,496 FAILED: SemanticException InvalidObjectException(message:null (Service: AWSGlue; Status Code: 400; Error Code: InvalidInputException; Request ID: 7f35e813-b495-4137-8eb7-c43cd09d))
is that possible to do "where" expression from INPUT__FILE__NAME virtual column?
Looks like Glue doesn't support filtering INPUT__FILE__NAME on a partitioned table. You can achieve this by using a subquery as shown below:
select
partition_name
from
(
select
substr(reverse(split(reverse(INPUT__FILE__NAME), '/')[0]), 5, 8) as t,* from mytable
)
tmp
where
t = '20200705';

How to print special characters in Athena/Presto

I'm looking for a way to print tab separated values in aws Athena/presto. The following query doesn't do it:
select 'fielf1\tfield2'
which gives (unsurprisingyl)
field1\tfield2
while I would like
field1 field2
where the two fields are separated by a tab character.
EDIT: The "standard" syntax proposed by Piotr Findeisen:
SELECT U&'field1\0009field2'
Returns:
Your query has the following error(s):
Queries of this type are not supported (Service: AmazonAthena; Status Code: 400; Error Code: InvalidRequestException; Request ID: [...])
For the record, a possible solution is:
select 'field1'||chr(9)||'field2'
SELECT U&'field1\0009field2'
See more examples at https://trino.io/docs/current/language/types.html#varchar

Trying to Fetch only multiple column from select query where status column with error code

I'm trying to fetch only specific columns(uri,hostheader) from an Athena query where the status column is like 404.
When I execute the query I get the output for uri and hostheader unable to fetch the results for status 404 with the below query.
select
uri,
hostheader
from
accesslogs
where
CAST(status AS VARCHAR) like '%404%'
The solution was to not cast as varchar and instead use the native int type
select
uri,
hostheader
from
accesslogs
where
status = 404

Is it possible to create download link for Left Joined classic report?

I have classic report in the page, which has the following SQL query:
SELECT CELLS.ID, CELLS.NAME, CELLS.NUM, CELLS.AC_ID, AC.SERIAL,AC.FILE_NAME, AC.FILE_DATA
FROM CELLS
LEFT JOIN AC
ON CELLS.AC_ID = AC.ID
WHERE CELLS.AC_ID IS NOT NULL
ORDER BY CELLS.NUM
I would like to have download link for AC.FILE_DATA, which is BLOB. So in Attributes FILE_DATA column I set the following:
Type: Download BLOB
Table Name: AC
BLOB Column: FILE_DATA
Primary Key Column 1: ID
The page then is generating error in the place of classic report region:
Error: ORA-06502: PL/SQL: numeric or value error: character to number
conversion error
Looking in Debug log shows some more:
Exception in "AC Region": Sqlerrm: ORA-06502: PL/SQL: numeric or value
error: character to number conversion error Backtrace: ORA-06512: at
"APEX_050100.WWV_RENDER_REPORT3", line 7965
Without AC.FILE_DATA in left join there is no exception. So can I actually have blob download column when using joins in report query?
As far as I can tell, it has nothing to do with (left) join but the way you create the download link. It should NOT be the BLOB column name, but this:
dbms_lob.getlength(ac.file_data) download
Or, applied to your query,
SELECT CELLS.ID,
CELLS.NAME,
CELLS.NUM,
CELLS.AC_ID,
AC.SERIAL,
AC.FILE_NAME,
--
dbms_lob.getlength(AC.FILE_DATA) download --> this
FROM CELLS
LEFT JOIN AC
ON CELLS.AC_ID = AC.ID
WHERE CELLS.AC_ID IS NOT NULL
ORDER BY CELLS.NUM
"Download" column settings:
Type: Download BLOB
Table name: AC
BLOB Column: FILE_DATA
Primary Key Column 1: ID
Save, Run - should be just fine.