简体   繁体   中英

How to append data to a parquet file in Azure blob using Azure function

I am trying to append to a parquet file which is in Azure blob using Azure function c# script.

I have been able to append to a locally created parquet file using Parquet.net package. However, while I am trying to execute the code to append to a parquet file which is in Azure, I am getting errors.

Below code works for a local parquet file append.

var ds = new DataSet(new DataField<int>("id"),new DataField<string>("city"));
ds.Add(1, "London");
using (Stream fileStream = File.Open(file, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
   ParquetWriter.Write(ds, fileStream,CompressionMethod.None,null,null, true);
   Console.Write("File writing completed successfully\n");
}

However the code below does not work for an Azure parquet file append

var ds = new DataSet(new DataField<int>("id"),new DataField<string>("city"));
ds.Add(1, "London");
Stream stream = new MemoryStream();
ParquetWriter.Write(ds, stream,CompressionMethod.None,null,null, false);
parquetBlob.AppendBlock(stream); //this line fails with error

I get following error:

2018-07-05T05:07:14.479 [Info] Parquet file writing started
2018-07-05T05:07:14.667 [Info] Parquet file writing : successfully written to memory stream
2018-07-05T05:07:14.686 [Info] Exception  while appending to parquet file: Microsoft.WindowsAzure.Storage.StorageException: The remote server returned an error: (400) Bad Request. ---> System.Net.WebException: The remote server returned an error: (400) Bad Request.
   at System.Net.HttpWebRequest.GetResponse()
   at Microsoft.WindowsAzure.Storage.Core.Executor.Executor.ExecuteSync[T](RESTCommand`1 cmd, IRetryPolicy policy, OperationContext operationContext) in c:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\ClassLibraryCommon\Core\Executor\Executor.cs:line 677
   --- End of inner exception stack trace ---
   at Microsoft.WindowsAzure.Storage.Core.Executor.Executor.ExecuteSync[T](RESTCommand`1 cmd, IRetryPolicy policy, OperationContext operationContext) in c:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\ClassLibraryCommon\Core\Executor\Executor.cs:line 604
   at Microsoft.WindowsAzure.Storage.Blob.CloudAppendBlob.AppendBlock(Stream blockData, String contentMD5, AccessCondition accessCondition, BlobRequestOptions options, OperationContext operationContext) in c:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\ClassLibraryCommon\Blob\CloudAppendBlob.cs:line 2145
   at Submission#0.writeParquet(String data, CloudAppendBlob parquetBlob, TraceWriter log) in D:\home\site\wwwroot\EventHubTriggerCSharp2\run.csx:line 189
   at Submission#0.WriteToBlob(String fileName, String data, TraceWriter log) in D:\home\site\wwwroot\EventHubTriggerCSharp2\run.csx:line 158
Request Information
RequestID:45299e54-001e-009d-7d1e-143e91000000
RequestDate:Thu, 05 Jul 2018 05:07:13 GMT
StatusMessage:The value for one of the HTTP headers is not in the correct format.
ErrorCode:InvalidHeaderValue

Any help will be highly appreciated.

@Gaurav showed us the way.

The value for one of the HTTP headers is not in the correct format. If we check the RequestInformation, we will find Content-Length is 0.

You need to seek stream back to the begin before appending it to the blob.

Add stream.Position = 0 before parquetBlob.AppendBlock(stream);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM