【发布时间】:2019-07-02 21:05:35
【问题描述】:
我创建了一些 avro 文件。我可以使用以下命令将它们转换为json,只是为了检查文件是否正常
java -jar avro-tools-1.8.2.jar tojson FileName.avro>outputfilename.json
现在,我有一些大的 avro 文件,并且我尝试上传到的 REST API 有大小限制,因此我尝试使用流将其分块上传。
以下示例只是从原始文件中读取块并复制到另一个 avro 文件中,完美地创建了文件
using System;
using System.IO;
class Test
{
public static void Main()
{
// Specify a file to read from and to create.
string pathSource = @"D:\BDS\AVRO\filename.avro";
string pathNew = @"D:\BDS\AVRO\test\filenamenew.avro";
try
{
using (FileStream fsSource = new FileStream(pathSource,
FileMode.Open, FileAccess.Read))
{
byte[] buffer = new byte[(20 * 1024 * 1024) + 100];
long numBytesToRead = (int)fsSource.Length;
int numBytesRead = 0;
using (FileStream fsNew = new FileStream(pathNew,
FileMode.Append, FileAccess.Write))
{
// Read the source file into a byte array.
//byte[] bytes = new byte[fsSource.Length];
//int numBytesToRead = (int)fsSource.Length;
//int numBytesRead = 0;
while (numBytesToRead > 0)
{
int bytesRead = fsSource.Read(buffer, 0, buffer.Length);
byte[] actualbytes = new byte[bytesRead];
Array.Copy(buffer, actualbytes, bytesRead);
// Read may return anything from 0 to numBytesToRead.
// Break when the end of the file is reached.
if (bytesRead == 0)
break;
numBytesRead += bytesRead;
numBytesToRead -= bytesRead;
fsNew.Write(actualbytes, 0, actualbytes.Length);
}
}
}
// Write the byte array to the other FileStream.
}
catch (FileNotFoundException ioEx)
{
Console.WriteLine(ioEx.Message);
}
}
}
我怎么知道这会创建一个好的 avro。因为之前转换为 json 的命令再次起作用,即
java -jar avro-tools-1.8.2.jar tojson filenamenew.avro>outputfilename.json
但是,当我使用相同的代码,而不是复制到另一个文件时,只需调用一个 rest api,文件就会被上传,但是在从服务器下载相同的文件并运行上面的命令转换为 json 时说 - “不是数据文件”。
所以,显然有些东西被破坏了,我正在努力弄清楚是什么。
这是sn-p
string filenamefullyqualified = path + filename;
Stream stream = System.IO.File.Open(filenamefullyqualified, FileMode.Open, FileAccess.Read, FileShare.None);
long? position = 0;
byte[] buffer = new byte[(20 * 1024 * 1024) + 100];
long numBytesToRead = stream.Length;
int numBytesRead = 0;
do
{
var content = new MultipartFormDataContent();
int bytesRead = stream.Read(buffer, 0, buffer.Length);
byte[] actualbytes = new byte[bytesRead];
Array.Copy(buffer, actualbytes, bytesRead);
if (bytesRead == 0)
break;
//Append Data
url = String.Format("https://{0}.dfs.core.windows.net/raw/datawarehouse/{1}/{2}/{3}/{4}/{5}?action=append&position={6}", datalakeName, filename.Substring(0, filename.IndexOf("_")), year, month, day, filename, position.ToString());
numBytesRead += bytesRead;
numBytesToRead -= bytesRead;
ByteArrayContent byteContent = new ByteArrayContent(actualbytes);
content.Add(byteContent);
method = new HttpMethod("PATCH");
request = new HttpRequestMessage(method, url)
{
Content = content
};
request.Headers.Add("Authorization", "Bearer " + accesstoken);
var response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
position = position + request.Content.Headers.ContentLength;
Array.Clear(buffer, 0, buffer.Length);
} while (numBytesToRead > 0);
stream.Close();
我浏览了论坛主题,但没有遇到任何涉及拆分 avro 文件的内容。
我有一种预感,我的 http 请求“内容”不正确。我错过了什么?
如果您需要更多详细信息,我很乐意提供。
【问题讨论】:
标签: c# httpclient filestream avro