I have asked similar kind of question before so please excuse if you find it more as repetition though the code here is different which I am trying to fix.
Below is the code where I am reading a JSON file and copying the contents in a Azure Tabular Storage. Json files are being read from a blob storage. Right now I am reading from memory and passing the content to be copied. But considering large Json files might give me a memory exception, I would like to read this as a stream and not store it as memory. How should I do that?
sample Json to read>
{"PartitionKey": "test","RowKey": "7tttt","IdPit": 653,"Class": "A76","Power": 323,"Time": "04/23/2012 18:25:43","bits": "test"}
{"PartitionKey": "test","RowKey": "itttt","IdPit": 432,"Class": "B65","Power": 23,"Time": "04/22/2012 18:25:43","bits": "Ttest"}
Code for reading Json:
List<string> lines = new List<string>();
foreach (var files in recFiles)
{
Stream data = await DownloadBlob(containerName, fileName, connectionString);
StreamReader reader = new StreamReader(data, Encoding.UTF8);
string dataContents = reader.ReadToEnd();
lines.Add(dataContents);
}
await PopulateTable(lines);
DownloadBlob:
public async Task<Stream> DownloadBlob(string containerName, string fileName, string connectionString)
{
Microsoft.Azure.Storage.CloudStorageAccount storageAccount = Microsoft.Azure.Storage.CloudStorageAccount.Parse(connectionString);
CloudBlobClient serviceClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = serviceClient.GetContainerReference(containerName);
CloudBlockBlob blob = container.GetBlockBlobReference(fileName);
if (!blob.Exists())
{
throw new Exception("Blob Not found");
}
return await blob.OpenReadAsync();
}
Reading and uploading Json :
public async Task<List<DynamicTableEntity>> PopulateTable(IEnumerable<string> lines)
{
var validator = new JsonSchemaValidator();
var tableData = new List<JObject>();
// Validate all entries
foreach (var line in lines)
{
if (string.IsNullOrWhiteSpace(line))
continue;
var data = JsonConvert.DeserializeObject<JObject>(line);
...... // adding to table
}
}