Skip to content

Instantly share code, notes, and snippets.

@ThomasAnnerel
Created May 3, 2016 20:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ThomasAnnerel/9e15aa8a9e844a1834ec08d530fd1a36 to your computer and use it in GitHub Desktop.
Save ThomasAnnerel/9e15aa8a9e844a1834ec08d530fd1a36 to your computer and use it in GitHub Desktop.
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