【问题标题】:Split an avro file and upload to REST拆分 avro 文件并上传到 REST
【发布时间】: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


    【解决方案1】:

    我现在发现了问题。问题是因为 MultipartFormDataContent。当一个 avro 文件与它一起上传时,它会添加额外的文本,如内容类型等,同时删除许多行(我不知道为什么)。

    因此,解决方案是将内容作为“ByteArrayContent”本身上传,而不是像我之前所做的那样将其添加到 MultipartFormDataContent。

    这里是sn-p,与问题中的几乎相似,只是我不再使用MultipartFormDataContent

                string filenamefullyqualified = path + filename;
                Stream stream = System.IO.File.Open(filenamefullyqualified, FileMode.Open, FileAccess.Read, FileShare.None);
                //content.Add(CreateFileContent(fs, path, filename, "text/plain"));
    
    
                long? position = 0;
    
                byte[] buffer = new byte[(20 * 1024 * 1024) + 100];
                long numBytesToRead = stream.Length;
                int numBytesRead = 0;
    
    
                //while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 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);
                    //byteContent.Headers.ContentType= new MediaTypeHeaderValue("text/plain");
                    //content.Add(byteContent);
    
    
                    method = new HttpMethod("PATCH");
    
                    //request = new HttpRequestMessage(method, url)
                    //{
                    //    Content = content
                    //};
    
    
                    request = new HttpRequestMessage(method, url)
                    {
                        Content = byteContent
                    };
    
                    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();
    

    【讨论】:

      猜你喜欢
      • 2012-07-27
      • 2011-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-23
      • 2019-08-26
      • 2019-07-03
      • 2017-09-21
      相关资源
      最近更新 更多