Error in query Athena using a Lambda function

0

Hi. I have created a java lambda funtion:

package aws_lambda;

import com.amazonaws.athena.connector.lambda.handlers.UserDefinedFunctionHandler;
import java.io.IOException;

public class ip_to_long extends UserDefinedFunctionHandler {
    
    private static final String SOURCE_TYPE = "ip_to_long_athena_udf_handler";
    
    public ip_to_long() throws IOException {
        super(SOURCE_TYPE);
    }

    public Long evaluate(String s) {
        final Long ip_long ;
        if (s == null) { 
            return null; 
        }

        String s_to_text = s;
        
        if (!s_to_text.contains(".")) {
            return null; 
        }

        String[] parts = s_to_text.split("\\.");
        int up_value = 256;
        if(parts.length != 4){
            return null; 
        }

        ip_long =   Long.parseLong(parts[0]) * (new Double(Math.pow(up_value, 3))).longValue() + 
                    Long.parseLong(parts[1]) * (new Double(Math.pow(up_value, 2))).longValue() + 
                    Long.parseLong(parts[2]) * (new Double(Math.pow(up_value, 1))).longValue() + 
                    Long.parseLong(parts[3]);
        
        return ip_long;
    }
}

In AWS lambda console it is OK, i pass a IP in string format and lambda returns a LONG but in athena query return a json parsing error:

USING EXTERNAL FUNCTION evaluate(s VARCHAR)
RETURNS BIGINT 
LAMBDA 'ip_to_long'
select evaluate('192.168.1.1')
SQL Error [100071] [HY000]: [Simba][AthenaJDBC](100071) An error has been thrown from the AWS Athena client. GENERIC_USER_ERROR: io.trino.spi.TrinoException: Encountered an exception[java.lang.RuntimeException] from your LambdaFunction[ip_to_long] executed in context[ping] with message[An error occurred during JSON parsing] [Execution ID: 20fbe678-1a12-42be-ae81-061ad72f4409]

thanks

asked a year ago555 views
2 Answers
0

Hi,

I have seen in past that string may give troubles in being passed to Lambda function. Can you try creating a n object holding the input, such as a POJO or just a Map<String, Object>?

Hope it helps ;)

profile picture
EXPERT
answered a year ago
  • Thanks for all alatech. In AWS console it is OK but in athena query no, i dont know if something is missing in configuration to accept athena queries and string encoding. Thanks

0

Hi alatech I try this

public class ip_to_long implements RequestHandler<String, String>{
  @Override
  /*
   * Takes a String as input, and converts all characters to lowercase.
   */
  public String handleRequest(String event, Context context)
  {
    LambdaLogger logger = context.getLogger();
    logger.log("EVENT TYPE: " + event.getClass().toString());
    return event.toLowerCase();
  }
}

In the AWS console is OK, but in athen query nothing :(

USING EXTERNAL FUNCTION handleRequest(s VARCHAR)
RETURNS VARCHAR 
LAMBDA 'ip_to_long'
select handleRequest('test')

I dont know if i something is missing to accept in athena queries

thanks for all

answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions