I am trying to convert unix timestamp(string) coming from my dynamo db by using lambda function to date format to visualize in kibana

'use strict';

var moment = require('moment');

//var Elasticsearch = require('Elasticsearch');

//var client = new Elasticsearch.Clinet({

// host: 'https://.us-east-2.es.amazonaws.com',

// log: 'error'

//});

const { Client } = require('Elasticsearch')

const client = new Client({

node: 'https://.us-east-2.es.amazonaws.com',

log: 'error'

})

module.exports.es = async (event, context) => {

for (var i = 0; i < event.Records.length; i++) {

var record = event.Records[i];

try {

 if (record.eventName == "INSERT") {

// const date = moment(record.dynamodb.NewImage?.timestampResult?.N, 'X').format('yyyyMMdd'T'HHmmss.SSSZ')

    let date

     try{

          date = new Date(record.dynamodb.NewImage?.timestampResult?.N * 1000).toISOString()

        }

      catch(e)

      {

          date = record.dynamodb.NewImage?.timestampResult?.N

      }

   var result = await client.create({

     index: 'uuidtestkit-index',

     type: 'Testkits-test',

// id: 'uuidTestkit',

     id: record.dynamodb.NewImage.uuidTestkit.S,

     body: {

      uuidTestkit: record.dynamodb.NewImage.uuidTestkit.S,

      timestampAgent: record.dynamodb.NewImage?.timestampAgent?.N,

      timestampResult: date,

      testResult: record.dynamodb.NewImage?.testResult?.S,

      timestampUser: record.dynamodb.NewImage?.timestampUser?.N,

      txLink: record.dynamodb.NewImage?.txLink?.S,

      profile_name: record.dynamodb.NewImage?.profile_name?.S,

      profile_picture: record.dynamodb.NewImage?.profile_picture?.S,

      profile_clientID: record.dynamodb.NewImage?.profile_clientID?.S,

      profile_lastname: record.dynamodb.NewImage?.profile_lastname?.S,

      Leaves: record.dynamodb.NewImage?.Leaves?.S,

      userId: record.dynamodb.NewImage?.userId?.S,

      urlVpToken : record.dynamodb.NewImage?.urlVpToken?.S,

      VcToken: record.dynamodb.NewImage?.VcToken?.S,

      VpToken: record.dynamodb.NewImage?.VpToken?.S,

      agentId: record.dynamodb.NewImage?.agentId?.S,

     }

   });

   console.log("=== completed ===");

   console.log(result);

 }

}

catch (err) {

 console.log(err);

}

}

return 'Successfully processed: ${event.Records.length} records.';

};

I am using above node js code for lambda function.
I can able to convert the unix timestamp to date. But it is still converting as a string and not allowing me to use data range or date histogram options in kibana.

Any help can be appreciated.

What is your mapping for that field in that index? It needs to be a date if you want to perform functions in Kibana as a date.

I am not sure how to give format as date in my above code.

But i tried try{

      date = new Date(record.dynamodb.NewImage?.timestampResult?.N * 1000).toISOString()

    }

  catch(e)

  {

      date = record.dynamodb.NewImage?.timestampResult?.N

  }

and in body i gave .D as well for respective field.

And even if i pass a number it is somehow converting as a string in kibana.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.