//上传文件 public bool Create(ThriftHadoopFileSystem.Client client, string localPath, string path) { bool result = false; if (client != null) { ThriftHandle th = null; FileStream fs = null; try { //创建一个文件 //th = client.createFile(new Pathname() { pathname = path }, 1, true, 1024, ConfigHelper.HDFSREPLICATION, 1024*1024*64); th = client.createFile(new Pathname() { pathname = path }, 1, true, 1024, 1, 1024 * 1024 * 64); UTF8Encoding utf8 = new UTF8Encoding(false,true); fs = new FileStream(localPath, FileMode.Open, FileAccess.Read); byte[] fileBuffer = new byte[1024 * 1024]; // 每次传1MB int bytesRead; while ((bytesRead = fs.Read(fileBuffer, 0, fileBuffer.Length)) > 0) { byte[] realBuffer = new byte[bytesRead]; Array.Copy(fileBuffer, realBuffer, bytesRead); //将utf8转为可存储编码 byte[] buf = Encoding.Convert(Encoding.GetEncoding("iso-8859-1"), utf8, realBuffer); //发送 client.write(th,buf); //清仓缓存 Array.Clear(fileBuffer, 0, fileBuffer.Length); } result = true; } catch (Exception ex) { throw ex; } finally { if (th != null) client.close(th); if (fs != null) fs.Close(); } } return result; }
//上传文件 public bool Create(ThriftHadoopFileSystem.Client client, byte[] data, string path) { bool result = false; if (client != null) { ThriftHandle th = null; try { //创建一个文件 //th = client.createFile(new Pathname() { pathname = path }, 1, true, 1024, ConfigHelper.HDFSREPLICATION, 1024*1024*64); th = client.createFile(new Pathname() { pathname = path }, 1, true, 1024, 1, 1024 * 1024 * 64); UTF8Encoding utf8 = new UTF8Encoding(false, true); byte[] buf = Encoding.Convert(Encoding.GetEncoding("iso-8859-1"), utf8, data); //发送 client.write(th, buf); result = true; } catch (Exception ex) { throw ex; } finally { if (th != null) client.close(th); } } return result; }