无法弄清楚如何将数据添加到现有数据湖文件
Unable to figure out how to add data to existing data lake file
我正在使用适用于 .NET 的 Azure SDK 来操作数据湖 (Gen2) 上的文件。
在 Azure 函数中,我想将一些数据添加到存储在数据湖中的 csv 文件中。
这个方法是我自己想出来的,按照文档应该是行得通的(或者我没看懂)
问题是数据没有'flushed'到文件。它仍然是原来的内容。
恐怕无法弄清楚这里发生了什么:-(
有什么建议吗?
此致,
斯文·皮特斯
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 });
}
根据APIdocumentation,您应该在Flush
方法中将position: currentLength
更改为position: currentLength + fileSize
。 position 参数应等于追加后的文件长度。
To flush, the previously uploaded data must be contiguous, the
position parameter must be specified and equal to the length of the
file after all data has been written, and there must not be a request
entity body included with the request.
代码:
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 });
}
我正在使用适用于 .NET 的 Azure SDK 来操作数据湖 (Gen2) 上的文件。 在 Azure 函数中,我想将一些数据添加到存储在数据湖中的 csv 文件中。
这个方法是我自己想出来的,按照文档应该是行得通的(或者我没看懂)
问题是数据没有'flushed'到文件。它仍然是原来的内容。 恐怕无法弄清楚这里发生了什么:-(
有什么建议吗?
此致, 斯文·皮特斯
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 });
}
根据APIdocumentation,您应该在Flush
方法中将position: currentLength
更改为position: currentLength + fileSize
。 position 参数应等于追加后的文件长度。
To flush, the previously uploaded data must be contiguous, the position parameter must be specified and equal to the length of the file after all data has been written, and there must not be a request entity body included with the request.
代码:
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 });
}