我正在使用适用于 .NET 的 Azure SDK 来操作数据湖 (Gen2) 上的文件。 在 Azure 函数中,我想向存储在数据湖上的 csv 文件添加一些数据。
我想出了这个方法,应该按照文档工作(或者我没有完全理解它)。
问题是数据没有“刷新”到文件中。它仍然是原始内容。 恐怕无法弄清楚这里发生了什么:-(
有什么提示吗?
问候, 斯文·皮特斯
PS:我必须增量添加数据,否则内存消耗可能会成为一个问题。
public void AddFileContents(string fullPath, string content, string leaseId = null)
{
DataLakeFileClient dataLakeFileClient = GetFileSystemClient().GetFileClient(fullPath);
dataLakeFileClient.CreateIfNotExists();
long currentLength = dataLakeFileClient.GetProperties().Value.ContentLength;
byte[] byteArray = Encoding.UTF8.GetBytes(content);
MemoryStream mStream = new MemoryStream(byteArray);
long fileSize = mStream.Length;
dataLakeFileClient.Append(mStream, currentLength, leaseId: leaseId);
dataLakeFileClient.Flush(position: currentLength, close: true, conditions: new DataLakeRequestConditions() { LeaseId = leaseId });
}
答案 0 :(得分:0)
根据 API documentation,您应该将 position: currentLength
方法中的 position: currentLength + fileSize
更改为 Flush
。位置参数应该等于你追加后的文件长度。
要刷新,之前上传的数据必须是连续的, 必须指定位置参数并等于其长度 写入所有数据后的文件,并且不能有请求 包含在请求中的实体正文。
代码:
public static void AddFileContents(string fullPath, string content, string leaseId = null)
{
DataLakeFileClient dataLakeFileClient = GetFileSystemClient().GetFileClient(fullPath);
dataLakeFileClient.CreateIfNotExists();
long currentLength = dataLakeFileClient.GetProperties().Value.ContentLength;
byte[] byteArray = Encoding.UTF8.GetBytes(content);
MemoryStream mStream = new MemoryStream(byteArray);
long fileSize = mStream.Length;
dataLakeFileClient.Append(mStream, currentLength, leaseId: leaseId);
dataLakeFileClient.Flush(position: currentLength + fileSize, close: true, conditions: new DataLakeRequestConditions() { LeaseId = leaseId });
}