Exemplo n.º 1
0
    //Upload
    public static async Task <bool> UploadFile(string hadoopURL, string filePath, IEnumerable <KeyValuePair <string, string> > URLSwitch, byte[] datas)
    {
        HDFSResponse.Redirection createFile = await HadoopAPI.CallHadoop <HDFSResponse.Redirection>("PUT", $"{hadoopURL}webhdfs/v1/{filePath}?op=CREATE&overwrite=true&noredirect=true", null, HttpStatusCode.OK);

        if (createFile != null)
        {
            if (URLSwitch != null)
            {
                createFile.URLSwitch(URLSwitch);
            }

            string responseContent = "";

            HttpStatusCode responseHttpStatusCode = HttpStatusCode.NotFound;
            await CallHadoop("PUT", createFile.Location, request => {
                request.Content = new ByteArrayContent(datas);
            }, response => {
                responseHttpStatusCode = response.StatusCode;
                responseContent        = response.Content.ReadAsStringAsync().Result;
            });

#if DEBUG
            Console.WriteLine("responseHttpStatusCode : " + responseHttpStatusCode);
#endif

            switch (responseHttpStatusCode)
            {
            //OK
            case HttpStatusCode.Created: return(true);

            //case HttpStatusCode.Forbidden:
            default:
            {
#if DEBUG
                Console.WriteLine("Debug Response Content");
                Console.WriteLine(responseContent);
#endif
            }
            break;
            }
        }

        return(false);
    }
Exemplo n.º 2
0
    //Checksum
    public static async Task <HDFSResponse.FileChecksum> FileChecksum(string hadoopURL, string filePath)
    {
        HDFSResponse.FileChecksum fileChecksum = null;

        HDFSResponse.Redirection createFile = await HadoopAPI.CallHadoop <HDFSResponse.Redirection>("GET", $"{hadoopURL}webhdfs/v1/{filePath}?op=GETFILECHECKSUM&noredirect=true", null, HttpStatusCode.OK);

        if (createFile != null)
        {
            await CallHadoop("GET", createFile.Location, null, response => {
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    string jsonResponse = response.Content.ReadAsStringAsync().Result;
                    fileChecksum        = JsonConvert.DeserializeObject <HDFSResponse.FileChecksum>(jsonResponse);
                }
            });
        }

        return(fileChecksum);
    }
Exemplo n.º 3
0
    //Read
    public static async Task <byte[]> OpenRead(string hadoopURL, string filePath, IEnumerable <KeyValuePair <string, string> > URLSwitch)
    {
        byte[] byteRet = null;

        HDFSResponse.Redirection createFile = await HadoopAPI.CallHadoop <HDFSResponse.Redirection>("GET", $"{hadoopURL}webhdfs/v1/{filePath}?op=OPEN&noredirect=true", null, HttpStatusCode.OK);

        if (createFile != null)
        {
            if (URLSwitch != null)
            {
                createFile.URLSwitch(URLSwitch);
            }

            await CallHadoop("GET", createFile.Location, null, response => {
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    using (Stream stream = response.Content.ReadAsStreamAsync().Result)
                    {
                        //using(MemoryStream memoryStream = new MemoryStream())
                        {
                            //stream.CopyToAsync(memoryStream);

                            using (BinaryReader reader = new BinaryReader(stream))
                            {
                                //limit 4GB
                                //แต่คิดว่าไม่น่าจะมีปัญหาเนื่องจากเราไม่ได้ใช้ในการอ่านไฟล์ขนาดใหญ่อยู่แล้ว
                                byteRet = reader.ReadBytes((int)stream.Length);

                                // Console.WriteLine("");
                                // for(int i = 0 ; i < stream.Length ; ++i)
                                // {
                                //     Console.Write(reader.ReadByte().ToString("X2"));
                                // }
                            }
                        }
                    }
                }
            });
        }

        return(byteRet);
    }