/// <summary> /// Unmarshaller the response from the service to the response class. /// </summary> /// <param name="context"></param> /// <returns></returns> public override AmazonWebServiceResponse Unmarshall(JsonUnmarshallerContext context) { PutRecordResponse response = new PutRecordResponse(); context.Read(); int targetDepth = context.CurrentDepth; while (context.ReadAtDepth(targetDepth)) { if (context.TestExpression("EncryptionType", targetDepth)) { var unmarshaller = StringUnmarshaller.Instance; response.EncryptionType = unmarshaller.Unmarshall(context); continue; } if (context.TestExpression("SequenceNumber", targetDepth)) { var unmarshaller = StringUnmarshaller.Instance; response.SequenceNumber = unmarshaller.Unmarshall(context); continue; } if (context.TestExpression("ShardId", targetDepth)) { var unmarshaller = StringUnmarshaller.Instance; response.ShardId = unmarshaller.Unmarshall(context); continue; } } return(response); }
public async Task <bool> RecordAsync <T>(T @event, string streamName) { string objAsJson = _serializer.Serialize(@event); byte[] objAsBytes = Encoding.UTF8.GetBytes(objAsJson + "\n"); using (var ms = new MemoryStream(objAsBytes)) { var record = new Record { Data = ms }; var request = new PutRecordRequest { DeliveryStreamName = streamName, Record = record }; PutRecordResponse response = await _client.PutRecordAsync(request); if (response.HttpStatusCode != HttpStatusCode.OK) { throw new System.Exception($"Error sending message. HttpStatusCode: {response.HttpStatusCode}"); } } return(true); }
/// <summary> /// Unmarshaller the response from the service to the response class. /// </summary> /// <param name="context"></param> /// <returns></returns> public override AmazonWebServiceResponse Unmarshall(JsonUnmarshallerContext context) { PutRecordResponse response = new PutRecordResponse(); return(response); }
private static void PublishDeviceDataToKinesis(List <DeviceData> dataList) { AmazonKinesisConfig config = new AmazonKinesisConfig(); config.RegionEndpoint = _regionEndpoint; AmazonKinesisClient kinesisClient = new AmazonKinesisClient(config); foreach (DeviceData data in dataList) { string dataAsJson = JsonConvert.SerializeObject(data); byte[] dataAsBytes = Encoding.UTF8.GetBytes(dataAsJson); using (MemoryStream memoryStream = new MemoryStream(dataAsBytes)) { try { PutRecordRequest requestRecord = new PutRecordRequest(); requestRecord.StreamName = _kinesisStreamName; requestRecord.PartitionKey = data.DeviceId; requestRecord.Data = memoryStream; PutRecordResponse responseRecord = kinesisClient.PutRecord(requestRecord); Console.WriteLine("Successfully published. Record:{0},{1},{2} Seq:{3}", data.DeviceId, data.Humidity, data.Temperature, responseRecord.SequenceNumber); } catch (Exception ex) { Console.WriteLine("Failed to publish. Exception: {0}", ex.Message); } } } }
private static void UnmarshallResult(JsonUnmarshallerContext context, PutRecordResponse response) { int originalDepth = context.CurrentDepth; int targetDepth = originalDepth + 1; while (context.Read()) { if (context.TestExpression("ShardId", targetDepth)) { context.Read(); response.ShardId = StringUnmarshaller.GetInstance().Unmarshall(context); continue; } if (context.TestExpression("SequenceNumber", targetDepth)) { context.Read(); response.SequenceNumber = StringUnmarshaller.GetInstance().Unmarshall(context); continue; } if (context.CurrentDepth <= originalDepth) { return; } } return; }
private async Task WriteToStream() { const string myStreamName = "myTestStream"; const int myStreamSize = 1; try { var createStreamRequest = new CreateStreamRequest(); createStreamRequest.StreamName = myStreamName; createStreamRequest.ShardCount = myStreamSize; var createStreamReq = createStreamRequest; var existingStreams = await kinesisClient.ListStreamsAsync(); if (!existingStreams.StreamNames.Contains(myStreamName)) { var CreateStreamResponse = await kinesisClient.CreateStreamAsync(createStreamReq); Console.WriteLine("Created Stream : " + myStreamName); } } catch (ResourceInUseException) { Console.Error.WriteLine("Producer is quitting without creating stream " + myStreamName + " to put records into as a stream of the same name already exists."); Environment.Exit(1); } await WaitForStreamToBecomeAvailableAsync(myStreamName); Console.Error.WriteLine("Putting records in stream : " + myStreamName); // Write 10 UTF-8 encoded records to the stream. for (int j = 0; j < 10; ++j) { byte[] dataAsBytes = Encoding.UTF8.GetBytes("testdata-" + j); using (MemoryStream memoryStream = new MemoryStream(dataAsBytes)) { try { PutRecordRequest requestRecord = new PutRecordRequest(); requestRecord.StreamName = myStreamName; requestRecord.PartitionKey = "url-response-times"; requestRecord.Data = memoryStream; PutRecordResponse responseRecord = await kinesisClient.PutRecordAsync(requestRecord); Console.WriteLine("Successfully sent record to Kinesis. Sequence number: {0}", responseRecord.SequenceNumber); } catch (Exception ex) { Console.WriteLine("Failed to send record to Kinesis. Exception: {0}", ex.Message); } } } Console.ReadLine(); }
public void LogRecord(Record record) { byte[] dataAsBytes = Encoding.UTF8.GetBytes(record.ToJson()); using (MemoryStream memoryStream = new MemoryStream(dataAsBytes)) { PutRecordRequest requestRecord = new PutRecordRequest(); requestRecord.StreamName = StreamName; requestRecord.PartitionKey = PartitionKey; requestRecord.Data = memoryStream; PutRecordResponse responseRecord = KinesisClient.PutRecord(requestRecord); } }
//FPWebLog //This function will push to kinesis with details From Request and Lambda will process accordingly public static void LogFPRequestDetailsIntoKinesis(object state) { try { var objArray = state as object[]; var fpCode = objArray[0] as string; var ip = objArray[1] as string; var agent = objArray[2] as string; var url = objArray[3] as string; var isCrawler = objArray[4] as string; if (!String.IsNullOrEmpty(fpCode)) { if (kinesisClient == null) { InitiateAWSClient(); } string record = String.Format("\"FPCode\":\"{0}\", \"IP\":\"{1}\", \"UserAgent\":\"{2}\", \"URL\":\"\", \"IsCrawler\":\"\"", fpCode, ip, agent, url, isCrawler); byte[] dataAsBytes = Encoding.UTF8.GetBytes("{" + record + "}"); string sequenceNumber = string.Empty; using (MemoryStream memoryStream = new MemoryStream(dataAsBytes)) { try { PutRecordRequest requestRecord = new PutRecordRequest(); requestRecord.StreamName = FPWebLogStreamName; requestRecord.PartitionKey = "FPWebLog-Stream"; requestRecord.Data = memoryStream; PutRecordResponse responseRecord = Task.Run(() => kinesisClient.PutRecordAsync(requestRecord)).Result; sequenceNumber = responseRecord.SequenceNumber; } catch (Exception ex) { //EventLogger.Write(ex, "FlowLayoutManager Exception occured while processing the request LogFPRequestDetailsIntoKinesis"); throw ex; } } } } catch (Exception ex) { //EventLogger.Write(ex, "FlowLayoutManager Exception occured while processing the request LogFPRequestDetailsIntoKinesis"); } }
/// <summary> /// /// </summary> private static void PutRecords() { Console.Error.WriteLine("Putting records in stream : " + myStreamName); // Write 10 UTF-8 encoded records to the stream. for (int j = 0; j < 10; ++j) { PutRecordRequest requestRecord = new PutRecordRequest(); requestRecord.StreamName = myStreamName; requestRecord.Data = new MemoryStream(Encoding.UTF8.GetBytes("testData-" + j)); requestRecord.PartitionKey = "partitionKey-" + j; PutRecordResponse putResult = oClient.PutRecord(requestRecord); Console.Error.WriteLine( String.Format("Successfully putrecord {0}:\n\t partition key = {1,15}, shard ID = {2}", j, requestRecord.PartitionKey, putResult.ShardId)); } }
private static async Task <tResult> WriteStream(tRevertCommand oRevertCommand, ILambdaContext context) { const string myStreamName = "CloudEcoPlus"; string strInput; string strEncoded; tResult oResult = new tResult(); try { context.Logger.LogLine("Putting records in stream : " + myStreamName); // Write 10 UTF-8 encoded records to the stream. PutRecordRequest requestRecord = new PutRecordRequest(); requestRecord.StreamName = myStreamName; strInput = JsonSerializer.Serialize(oRevertCommand); requestRecord.Data = new MemoryStream(Encoding.UTF8.GetBytes(strInput)); context.Logger.LogLine("Putting records in stream 1 : " + myStreamName); strEncoded = Base64Encode(strInput); // just for debug base64 encoding // context.Logger.LogLine("Putting records in stream 2 : " + myStreamName); requestRecord.PartitionKey = "partitionKey"; context.Logger.LogLine("Putting records in stream 3 : " + myStreamName); PutRecordResponse PutRecordResult = await kinesisClient.PutRecordAsync(requestRecord); context.Logger.LogLine("PutRecordResult ok" + PutRecordResult.HttpStatusCode); } catch (Exception ex) { oResult.Ok = false; oResult.Info = ex.Message; context.Logger.LogLine("Error WriteStream "); } context.Logger.LogLine("Putting records in stream 4"); return(oResult); }
internal static string LogRecord <T>(T data, string streamName) { try { if (data != null && !String.IsNullOrEmpty(streamName)) { if (kinesisClient == null) { InitiateAWSClient(); } byte[] dataAsBytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(data)); string sequenceNumber = string.Empty; using (MemoryStream memoryStream = new MemoryStream(dataAsBytes)) { try { PutRecordRequest requestRecord = new PutRecordRequest(); requestRecord.StreamName = streamName; requestRecord.PartitionKey = "KitsuneAPI"; requestRecord.Data = memoryStream; PutRecordResponse responseRecord = kinesisClient.PutRecordAsync(requestRecord).Result; sequenceNumber = responseRecord.SequenceNumber; return(sequenceNumber); } catch (Exception ex) { //EventLogger.Write(ex, "FlowLayoutManager Exception occured while processing the request LogFPRequestDetailsIntoKinesis"); throw ex; } } } } catch (Exception ex) { //Log exception; //EventLogger.Write(ex, "FlowLayoutManager Exception occured while processing the request LogFPRequestDetailsIntoKinesis"); } return(null); }
private async Task PushStreamAsync(string partitionKey, MemoryStream stream) { try { var request = new PutRecordRequest { Data = stream, PartitionKey = partitionKey, StreamName = _streamName }; PutRecordResponse result = await _client.PutRecordAsync(request, CancellationToken.None); _logger.LogInformation("Put Sequence Number: {0}. ShardId: {1}", result.SequenceNumber, result.ShardId); } catch (Exception ex) { _logger.LogError(ex, "Failed to push to Kinesis stream"); // Sink. } }
static void Main(string[] args) { AmazonKinesisClient client = new AmazonKinesisClient(Amazon.RegionEndpoint.USEast1); for (int i = 0; i <= 9; i++) { string message = "this is input number: " + i; byte[] bytes = Encoding.ASCII.GetBytes(message); using (MemoryStream ms = new MemoryStream(bytes)) { PutRecordRequest requestRecord = new PutRecordRequest { StreamName = "LogsStream", PartitionKey = i.ToString(), Data = ms }; PutRecordResponse responseRecord = client.PutRecord(requestRecord); Thread.Sleep(1000); } } }
/// <summary> /// A simple function that takes a string and does a ToUpper /// </summary> /// <param name="input"></param> /// <param name="context"></param> /// <returns></returns> public APIGatewayProxyResponse FunctionHandler(APIGatewayProxyRequest request, ILambdaContext context) { APIGatewayProxyResponse APIResponse = new APIGatewayProxyResponse(); try { bool EVSendToFirehose = Environment.GetEnvironmentVariable("PUSH_TO_FIREHOSE").ToUpper() == "TRUE"; if (EVSendToFirehose) // Only Processes if SentToFirehose==True { /*Json to Telemetry List*/ TelemetryList telemetryList = JsonConvert.DeserializeObject <TelemetryList>(request.Body); int ItemCount = telemetryList.Telemetry.Count; /*Get Environment Vars*/ string EVDeliveryStream = Environment.GetEnvironmentVariable("FIREHOSE_DELIVERYSTREAM"); string EVRegion = Environment.GetEnvironmentVariable("REGION"); byte[] oByte; Amazon.RegionEndpoint Region = Amazon.RegionEndpoint.GetBySystemName(EVRegion); foreach (Telemetry telemetry in telemetryList.Telemetry) { telemetry.Instant = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture); oByte = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(telemetry)); using (MemoryStream ms = new MemoryStream(oByte)) { Record RecordToSend = new Record { Data = ms }; PutRecordRequest DeliveryRequest = new PutRecordRequest { DeliveryStreamName = EVDeliveryStream, //DigitalPlatformTelemetry Record = RecordToSend }; AmazonKinesisFirehoseClient FirehoseClient = new AmazonKinesisFirehoseClient(Region); PutRecordResponse Response = FirehoseClient.PutRecord(DeliveryRequest); } } APIResponse.StatusCode = 200; APIResponse.Body = "Sucess, processed " + ItemCount + " items " + DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture); return(APIResponse); } APIResponse.StatusCode = 200; APIResponse.Body = APIResponse.Body = "Sucess, no entries processed ( PUSH_TO_FIREHOSE = False ). " + DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture); return(APIResponse); } catch (InvalidCastException e) { APIResponse.StatusCode = 500; APIResponse.Body = e.Message; return(APIResponse); } }
private async void SubmitButton_Click(object sender, System.EventArgs e) { //Disable the submit button so the user can't accidentally submit // the registration twice FindViewById <Button>(Resource.Id.button1).Enabled = false; //reset all of the error flags onClick isError = false; TextView errorFirstName = FindViewById <TextView>(Resource.Id.errorFirstName); errorFirstName.Text = ""; TextView errorLastName = FindViewById <TextView>(Resource.Id.errorLastName); errorLastName.Text = ""; TextView errorEmail = FindViewById <TextView>(Resource.Id.errorEmail); errorEmail.Text = ""; TextView errorPhone = FindViewById <TextView>(Resource.Id.errorPhone); errorPhone.Text = ""; EditText firstname = FindViewById <EditText>(Resource.Id.firstName); //check that the information in the field is valid //and that it isn't blank if (firstname.Text != "" || Regex.IsMatch(firstname.Text, "^[A-Za-z]$")) { currentStudent.FirstName = firstname.Text; } else { errorFirstName.Text = "Please enter your first name"; isError = true; } EditText lastname = FindViewById <EditText>(Resource.Id.lastName); //check that the information in the field is valid //and that it isn't blank if (lastname.Text != "" || Regex.IsMatch(lastname.Text, "^[A-Za-z]$")) { currentStudent.LastName = lastname.Text; } else { errorLastName.Text = "Please enter your last name"; isError = true; } EditText email = FindViewById <EditText>(Resource.Id.email); //check that the information in the field is valid //and that it isn't blank if (email.Text != "" || Regex.IsMatch(email.Text, @"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$")) { currentStudent.Email = email.Text; } else { errorEmail.Text = "Please enter your email"; isError = true; } EditText phone = FindViewById <EditText>(Resource.Id.phoneNo); //check that the information in the field is valid //and that it isn't blank if (phone.Text != "" || Regex.IsMatch(phone.Text, @"^\(? (\d{ 3})\)?[\s\-]? (\d{3})\-? (\d{4})$")) { currentStudent.Phone = phone.Text; } else { errorPhone.Text = "Please enter your phone number"; isError = true; } if (isError) { //Enable the submit button again so the user can press it // since the validation failed FindViewById <Button>(Resource.Id.button1).Enabled = true; } else { //add data to shared preferences ISharedPreferencesEditor editor = prefs.Edit(); editor.PutString("firstname", currentStudent.FirstName); editor.PutString("lastname", currentStudent.LastName); editor.PutString("email", currentStudent.Email); editor.PutString("school", currentStudent.School); editor.PutString("phone", currentStudent.Phone); DateTime time = DateTime.Now; editor.PutString("timestamp", time.ToString("T")); //write to SP editor.Apply(); //information needs to be added to the database using the AWS Lambda function var awsCredentials = new Amazon.Runtime.BasicAWSCredentials(WebApp.AWSKey, WebApp.AWSSecret); try { string dataAsJson = JsonConvert.SerializeObject(currentStudent); byte[] dataAsBytes = Encoding.UTF8.GetBytes(dataAsJson); using (MemoryStream memoryStream = new MemoryStream(dataAsBytes)) { //create config that points to AWS region AmazonKinesisConfig config = new AmazonKinesisConfig(); config.RegionEndpoint = Amazon.RegionEndpoint.USEast1; //create client that pulls creds from config AmazonKinesisClient kinesisClient = new AmazonKinesisClient(awsCredentials, Amazon.RegionEndpoint.USEast1); //create put request PutRecordRequest requestRecord = new PutRecordRequest(); requestRecord.StreamName = "exploreOTCData"; //give partition key that is used to place record in particular shard requestRecord.PartitionKey = "registration"; //add record as memory stream requestRecord.Data = memoryStream; //PUT the record to Kinesis Toast.MakeText(this, "Processing registration...", ToastLength.Long); PutRecordResponse response = await kinesisClient.PutRecordAsync(requestRecord); Console.WriteLine(response); } } catch (Exception ex) { Console.Write(ex.Message); } //close this activity Finish(); } }