【问题标题】:JSON file reading from Hadoop HDFS using C#.NET使用 C#.NET 从 Hadoop HDFS 读取 JSON 文件
【发布时间】:2014-08-07 13:23:32
【问题描述】:

我正在尝试将 json 文件从 HDFS 读取到文件流,但出现异常。

private string ReadHadoopJasonFiles(){//设置变量 string destFolderName = "/demo/ufo/in"; string destFileName = "admingroups_metadata.json";

        //connect to hadoop cluster
        Uri myUri = new Uri("http://localhost:50070");
        string userName = "hadoop";
        string srcFilename = destFolderName + "/" + destFileName;
        //string srcFilename = @"C:\FilesForHadoopJason\admingroups_metadata.xml";
        WebHDFSClient myClient = new WebHDFSClient(Convert.ToString(myUri), userName);

        FileStream fs = new FileStream(srcFilename, FileMode.Open, FileAccess.Read);
        StreamReader sr = new StreamReader(fs);
        string json;
        try
        {
            json = sr.ReadToEnd();
            string repalcedjson = json.Replace("\"", "'");
            return repalcedjson;

        }
        catch (Exception)
        {
            return null;
        }
        finally
        {
            sr.Close();
            fs.Dispose();
        }
    }

例外: System.IO.DirectoryNotFoundException 未处理 H结果=-2147024893 消息=找不到路径“C:\user\projects\PoC_\challengeOpenDump20140701160001_admingroups.json”的一部分。 源=mscorlib 堆栈跟踪: 在 System.IO.__Error.WinIOError(Int32 错误代码,字符串可能全路径) 在 System.IO.FileStream.Init(字符串路径、FileMode 模式、FileAccess 访问、Int32 权限、Boolean useRights、FileShare 共享、Int32 bufferSize、FileOptions 选项、SECURITY_ATTRIBUTES secAttrs、String msgPath、Boolean bFromProxy、Boolean useLongPath、Boolean checkHost) 在 System.IO.FileStream..ctor(字符串路径,FileMode 模式,FileAccess 访问) 在 c:\Users\DXG8488\Documents\Visual Studio 2012\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs:line 111 中的 ConsoleApplication1.HadoopJsonToRdbms.ReadHadoopJasonFiles() 在 c:\Users\DXG8488\Documents\Visual Studio 2012\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs:line 29 中的 ConsoleApplication1.HadoopJsonToRdbms.JsonToRDBMSControler() 在 c:\Users\DXG8488\Documents\Visual Studio 2012\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs:line 24 中的 ConsoleApplication1.HadoopJsonToRdbms.Main(String[] args) 在 System.AppDomain._nExecuteAssembly(RuntimeAssembly 程序集,字符串 [] 参数) 在 System.AppDomain.ExecuteAssembly(字符串 assemblyFile,证据 assemblySecurity,String [] args) 在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 在 System.Threading.ThreadHelper.ThreadStart_Context(对象状态) 在 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext,ContextCallback 回调,对象状态,布尔值 preserveSyncCtx) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback 回调,对象状态,布尔值 preserveSyncCtx) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback 回调,对象状态) 在 System.Threading.ThreadHelper.ThreadStart() 内部异常:

【问题讨论】:

  • File not found 异常即将到来。对此有什么想法吗?
  • 请编辑您的问题以添加异常、异常的来源和问题本身。

标签: json hadoop


【解决方案1】:

我使用Sharphadoop 这是一个读取 Json Hadoop REST API 的示例

HttpWebRequest req = WebRequest.Create(url_path) as HttpWebRequest;
req.Method = WebRequestMethods.Http.Get; // Get method            
req.Accept = "application/json"; // accept json
HttpWebResponse resp = req.GetResponse() as HttpWebResponse;
StreamReader reader = new StreamReader(resp.GetResponseStream());

string result = reader.ReadToEnd();
return result;

【讨论】:

猜你喜欢
  • 2020-02-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多