1

I'm on the AWS personalize documentation and it says access node.js code here.

More code examples (in Node.js, Python, and Go) and AWS Lambda usage guidelines are found here.

enter image description here

Where is here??? I'm trying to use nodejs without using lambda. How do I send the equivalent from my node server without lambda, is it even possible?

Here's the Java code.

package example;

  public class ProcessKinesisRecords implements RequestHandler<KinesisEvent, Void>{
  
      private static final String TRACKING_ID = be5aa88c-05dd-4c5e-9372-15ffa6a846b9;
      private EndpointConfiguration endpointConfiguration = new EndpointConfiguration("http://concierge-gamma-events.us-west-2.amazonaws.com", "us-west-2");
      private AWSConciergeEventTracking client = AWSConciergeEventTrackingClientBuilder.standard()
                  .withEndpointConfiguration(endpointConfiguration).build();
      private final CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder();
  
      @Override
      public Void recordHandler(KinesisEvent event, Context context)
      {
          for(KinesisEventRecord rec : event.getRecords()) {
              try {
                  // Kinesis data is Base64 encoded and needs to be decoded before being used.
                  String data = decoder.decode(record.getKinesis().getData()).toString();
  
                  JSONParser parser = new JSONParser();
                  JSONObject jsonObject = (JSONObject) parser.parse(data);
                  String userId = (String) jsonObject.get(USER_ID);
                  String sessionId = (String) jsonObject.get(SESSION_ID);
                  String eventType = (String) jsonObject.get(EVENT_TYPE);
                  long timestamp= new Date().getTime();
                  ByteBuffer properties = jsonObject.get(EVENT_TYPE).getBytes("UTF-8");
  
                  List<Event> eventList = new ArrayList<>();
                  eventList.add(new Event().withProperties(properties).withType(eventType));
                  TrackRequest request = new TrackRequest()
                          .withTrackingId(EVENT_TRACKER_ARN)
                          .withChannel("server")
                          .withUserId(userId)
                          .withSessionId(sessionId)
                          .withEventList(eventList);
                  client.track(request);
              } catch (CharacterCodingException e) {
                  log.error("Error decoding data in the kinesis stream. ", e);
                  throw new IllegalArgumentException(e.getMessage());
              } catch (ParseException e) {
                  log.error("Error parsing JSON input from the kinesis stream. ", e);
                  throw new IllegalArgumentException(e.getMessage());
              } catch (Exception e) {
                  log.error("Error processing record. ", e);
                  throw new IllegalArgumentException(e.getMessage());
              }
          return null;
      }
  }
John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
Harry
  • 52,711
  • 71
  • 177
  • 261

1 Answers1

0

Actually, all is provided in AWS SDK for Node environment. After some research I have come up with the following code.

This is just a draft, and shows it works.

let AWS = require ('aws-sdk');

AWS.config.update({
    'region': 'eu-central-1', // process.env.REGION,
    'accessKeyId': 'XXXX', // process.env.AWS_ACCESS_KEY_ID,
    'secretAccessKey': 'XXXXXXX' // process.env.AWS_SECRET_ACCESS_KEY
});

let personalizeEvents = new AWS.PersonalizeEvents();

var params = {
    eventList: [ /* required */
        {
            eventType: 'click', /* required */
            sentAt: new Date || 'Wed Dec 31 1969 16:00:00 GMT-0800 (PST)' || 123456789, /* required */
            eventId: 'STRING_VALUE',
            eventValue: 11,
            impression: [
                'STRING_VALUE',
                /* more items */
            ],
            itemId: '1',
            //properties: any /* This value will be JSON encoded on your behalf with JSON.stringify() */,
            recommendationId: 'STRING_VALUE'
        },
    ],
    sessionId: 'STRING_VALUE', /* required */
    trackingId: 'trackerid', /* required */
    userId: '666'
};

personalizeEvents.putEvents(params, function (err, data) {
    if (err) console.log(err, err.stack); // an error occurred
    else     console.log(data);           // successful response
});

Please read this first, AWS Official documentation to understand.

And then check the API documentation.

Amiga500
  • 5,874
  • 10
  • 64
  • 117