-
-
Save ThomasAnnerel/9e15aa8a9e844a1834ec08d530fd1a36 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using Microsoft.Hadoop.Avro; | |
using Microsoft.Hadoop.Avro.Container; | |
using Microsoft.ServiceBus.Messaging; | |
namespace EventHubTestClient | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var eventHubClient = | |
EventHubClient.CreateFromConnectionString( | |
"Endpoint=sb://xxxxxxxxxx.servicebus.windows.net/;SharedAccessKeyName=xxxxxxx;SharedAccessKey=xxxxxxxx", | |
"eventtest"); | |
string avroSchema; | |
using (StreamReader sr = new StreamReader("webview.avsc")) | |
{ | |
// Read the stream to a string, and write the string to the console. | |
avroSchema = sr.ReadToEnd(); | |
Console.WriteLine("schema read: " + avroSchema); | |
} | |
var serializer = AvroSerializer.CreateGeneric(avroSchema); | |
for (int i = 0; i < 100; i++) | |
{ | |
AvroRecord record = new AvroRecord(serializer.WriterSchema); | |
record["cookieId"] = Guid.NewGuid().ToString(); | |
record["sessionId"] = Guid.NewGuid().ToString(); | |
record["viewId"] = Guid.NewGuid().ToString(); | |
record["timestamp"] = DateTime.Now.Ticks; | |
record["viewSequence"] = i; | |
record["brand"] = "nb"; | |
record["pageUrl"] = "http://www.nieuwsblad.be/" + i; | |
byte[] avroMessage; | |
using (var memoryStream = new MemoryStream()) | |
using (var avroWriter = AvroContainer.CreateGenericWriter(avroSchema, memoryStream, Codec.Null)) | |
using (var seqWriter = new SequentialWriter<object>(avroWriter, 1)) | |
{ | |
seqWriter.Write(record); | |
avroMessage = memoryStream.ToArray(); | |
} | |
eventHubClient.Send(new EventData(avroMessage)); | |
Console.WriteLine("Sending message :" + i); | |
Console.WriteLine(Encoding.UTF8.GetString(avroMessage)); | |
} | |
Console.ReadLine(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment