From e349ae85fd1175fedebec85c3e4d906625b8b921 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BD=98=E5=BB=BA=E4=B8=9C?= <617601767@qq.com> Date: Fri, 8 Mar 2024 16:37:21 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=BB=98=E8=AE=A4=E5=80=BC?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/HadoopController.cs | 158 +++++++++++++++--- Hadoop/ZKLT.Hadoop.API/ZKLT.Hadoop.API.csproj | 4 + Hadoop/ZKLT.Hadoop.Model/HDP_CommandAction.cs | 35 ++-- Hadoop/ZKLT.Hadoop.Model/HDP_Table.cs | 4 +- Hadoop/ZKLT.Hadoop/HadoopService.cs | 94 +++++------ Hadoop/ZKLT.Hadoop/TableService.cs | 43 ++--- 6 files changed, 219 insertions(+), 119 deletions(-) diff --git a/Hadoop/ZKLT.Hadoop.API/Controllers/HadoopController.cs b/Hadoop/ZKLT.Hadoop.API/Controllers/HadoopController.cs index facde84..228cfdc 100644 --- a/Hadoop/ZKLT.Hadoop.API/Controllers/HadoopController.cs +++ b/Hadoop/ZKLT.Hadoop.API/Controllers/HadoopController.cs @@ -126,7 +126,8 @@ namespace ZKLT.Hadoop.API.Controllers } [HttpGet("deletetable")] - public ActionResult DeleteTable([FromQuery] string tableId) { + public ActionResult DeleteTable([FromQuery] string tableId) + { try { return Ok(_HadoopService.DeleteTable(tableId)); @@ -138,7 +139,8 @@ namespace ZKLT.Hadoop.API.Controllers } [HttpPost("querytable")] - public ActionResult QueryTable(HDP_Command command) { + public ActionResult QueryTable(HDP_Command command) + { try { return Ok(_HadoopService.QueryTable(command)); @@ -149,24 +151,140 @@ namespace ZKLT.Hadoop.API.Controllers } } - [HttpPost("insertfile")] - public ActionResult InsertFile(IFormFile file) - { - if(file == null) { - return BadRequest("文件不存在"); - } + //[HttpPost("insertfile")] + //public ActionResult InsertFile(IFormFile file, [FromQuery] string id = "") + //{ + // if (file == null) + // { + // return BadRequest("文件不存在"); + // } + // try + // { + // var _file = new HDP_File(); + // if (!string.IsNullOrEmpty(id)) + // { + // _file.Id = id; + // } + // else { + // _file.Id = Guid.NewGuid().ToString(); + // } + // _file.FileName = file.FileName; + // _file.ContentType = file.ContentType; + // _file.Data = new byte[file.Length]; + // file.OpenReadStream().Read(_file.Data, 0, (int)file.Length); + // _file.FileSize = (int)file.Length; + // if (_HadoopService.Insert(new HDP_Command + // { + // TableId = "HDP_File", + // Data = HDP_Table.Class2Dictionary(_file) + // })) + // { + // return Ok(_file.Id); + // } + // else { + // return Ok(false); + // } + + // } + // catch (Exception e) + // { + // return BadRequest(e.Message); + // } + //} + + //[HttpGet("deletefile")] + //public ActionResult DeleteFile([FromQuery] string fileId) + //{ + // try + // { + // return Ok(_HadoopService.Delete(new HDP_Command + // { + // TableId = "HDP_File", + // Where = new Dictionary { + // { "Id","="} + // }, + // Data = new Dictionary { + // {"Id",fileId } + // } + // })); + // } + // catch (Exception e) + // { + // return BadRequest(e.Message); + // } + //} + + //[HttpGet("getfile")] + //public ActionResult GetFile([FromQuery] string fileId) + //{ + // try + // { + // var _file = _HadoopService.QuerySingle(new HDP_Command + // { + // TableId = "HDP_File", + // Where = new Dictionary + // { + // {"Id","=" } + // }, + // Data = new Dictionary + // { + // {"Id",fileId } + // } + // }); + // if (_file != null) + // { + // return File(_file.Data!, _file.ContentType!); + // } + // else { + // return NoContent(); + // } + // } + // catch (Exception e) + // { + // return BadRequest(e.Message); + // } + //} + + //[HttpPost("queryfile")] + //public ActionResult QueryFile(HDP_Command command) + //{ + // try + // { + // var _files = _HadoopService.Query(command); + // for (var i = 0; i < _files.Length; i++) { + // _files[i].Data = null; + // } + // return Ok(_files); + // } + // catch (Exception e) + // { + // return BadRequest(e.Message); + // } + //} + + //[HttpPost("pagefile")] + //public ActionResult PageFile(HDP_Command command) + //{ + // try + // { + // var _filesPage = _HadoopService.Page(command); + // for (var i = 0; i < _filesPage.Data!.Length; i++) + // { + // _filesPage.Data[i]!.Data = null; + // } + // return Ok(_filesPage); + // } + // catch (Exception e) + // { + // return BadRequest(e.Message); + // } + //} + - var _file = new HDP_File(); - _file.FileName = file.FileName; - _file.ContentType = file.ContentType; - _file.Data = new byte[file.Length]; - file.OpenReadStream().Read(_file.Data, 0, (int)file.Length); - _file.FileSize = (int)file.Length; - return Ok(_file); - } [HttpPost("insert")] - public ActionResult Insert(HDP_Command command) { + public ActionResult Insert(HDP_Command command) + { try { return Ok(_HadoopService.Insert(command)); @@ -230,7 +348,8 @@ namespace ZKLT.Hadoop.API.Controllers } [HttpPost("page")] - public ActionResult Page(HDP_Command command) { + public ActionResult Page(HDP_Command command) + { try { return Ok(_HadoopService.Page(command)); @@ -242,7 +361,8 @@ namespace ZKLT.Hadoop.API.Controllers } [HttpPost("patch")] - public ActionResult PatchCommand(HDP_Command[] command) { + public ActionResult PatchCommand(HDP_Command[] command) + { try { return Ok(_HadoopService.PatchCommand(command)); diff --git a/Hadoop/ZKLT.Hadoop.API/ZKLT.Hadoop.API.csproj b/Hadoop/ZKLT.Hadoop.API/ZKLT.Hadoop.API.csproj index 7fb3a8b..88ec2af 100644 --- a/Hadoop/ZKLT.Hadoop.API/ZKLT.Hadoop.API.csproj +++ b/Hadoop/ZKLT.Hadoop.API/ZKLT.Hadoop.API.csproj @@ -9,6 +9,10 @@ ..\.. + + + + diff --git a/Hadoop/ZKLT.Hadoop.Model/HDP_CommandAction.cs b/Hadoop/ZKLT.Hadoop.Model/HDP_CommandAction.cs index 8e78e3f..7480205 100644 --- a/Hadoop/ZKLT.Hadoop.Model/HDP_CommandAction.cs +++ b/Hadoop/ZKLT.Hadoop.Model/HDP_CommandAction.cs @@ -24,37 +24,22 @@ namespace ZKLT.Hadoop.Model /// /// 转换命令 /// - /// 命令 + /// 函数 /// 参数 /// 命令 - public static string ConvertCommand(string command, Dictionary param) + public static object Convert(string action, Dictionary param) { - //日期转换 - command = command.Replace(DATENOW,@$"'{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}'"); - - command = command.Replace(UUID, @$"'{Guid.NewGuid().ToString()}'"); - return command; - } - - /// - /// 判断是否方法 - /// - /// 方法 - /// 是否包含 - public static bool IsAction(string action) - { - var _Result = false; - - if (action == DATENOW) { - return true; + if (action == DATENOW) + { + return DateTime.Now; } - - if(action == UUID) + else if (action == UUID) { - return true; + return Guid.NewGuid().ToString(); + } + else { + return action; } - - return _Result; } } } diff --git a/Hadoop/ZKLT.Hadoop.Model/HDP_Table.cs b/Hadoop/ZKLT.Hadoop.Model/HDP_Table.cs index 9c62ad3..4847fb3 100644 --- a/Hadoop/ZKLT.Hadoop.Model/HDP_Table.cs +++ b/Hadoop/ZKLT.Hadoop.Model/HDP_Table.cs @@ -131,7 +131,9 @@ namespace ZKLT.Hadoop.Model foreach (var _property in _properties) { - _result.Add(_property.Name, _property.GetValue(data)!); + if (_property.GetValue(data) != null) { + _result.Add(_property.Name, _property.GetValue(data)!); + } } return _result; diff --git a/Hadoop/ZKLT.Hadoop/HadoopService.cs b/Hadoop/ZKLT.Hadoop/HadoopService.cs index c021ac3..d1cd93d 100644 --- a/Hadoop/ZKLT.Hadoop/HadoopService.cs +++ b/Hadoop/ZKLT.Hadoop/HadoopService.cs @@ -128,12 +128,12 @@ namespace ZKLT.Hadoop throw new Exception("初始化数据列失败"); } - var _file = HDP_Table.Class2Table(); - _Tables.Add(_file); - if (!_TableService.InitStruct(_Source, _file)) - { - throw new Exception("初始化文件失败"); - } + //var _file = HDP_Table.Class2Table(); + //_Tables.Add(_file); + //if (!_TableService.InitStruct(_Source, _file)) + //{ + // throw new Exception("初始化文件失败"); + //} } /// @@ -462,47 +462,47 @@ namespace ZKLT.Hadoop return _TableService.Query(_Source, GetTable("HDP_Table")!, command.Where!, command.Data!, command.Order!); } - /// - /// 插入文件 - /// - /// 文件 - /// 是否成功 - public bool InsertFile(HDP_File file) - { - if (!string.IsNullOrEmpty(file.Id) && _TableService.QuerySingle(_Source, GetTable("HDP_File")!, new Dictionary { - { "Id","=" } - }, new Dictionary { - {"Id",file.Id } - }) != null) - { - throw new ArgumentException("文件编号已存在"); - } - - return _TableService.Insert(_Source, GetTable("HDP_File")!, HDP_Table.Class2Dictionary(file)); - } - - /// - /// 删除文件 - /// - /// 文件编号 - /// 是否成功 - public bool DeleteFile(string fileId) - { - if (string.IsNullOrEmpty(fileId) || _TableService.QuerySingle(_Source, GetTable("HDP_File")!, new Dictionary { - { "Id","=" } - }, new Dictionary { - {"Id",fileId } - }) == null) - { - throw new ArgumentException("文件编号不存在"); - } - - return _TableService.Delete(_Source, GetTable("HDP_File")!, new Dictionary { - {"Id","=" } - }, new Dictionary { - {"Id",fileId } - }); - } + ///// + ///// 插入文件 + ///// + ///// 文件 + ///// 是否成功 + //public bool InsertFile(HDP_File file) + //{ + // if (!string.IsNullOrEmpty(file.Id) && _TableService.QuerySingle(_Source, GetTable("HDP_File")!, new Dictionary { + // { "Id","=" } + // }, new Dictionary { + // {"Id",file.Id } + // }) != null) + // { + // throw new ArgumentException("文件编号已存在"); + // } + + // return _TableService.Insert(_Source, GetTable("HDP_File")!, HDP_Table.Class2Dictionary(file)); + //} + + ///// + ///// 删除文件 + ///// + ///// 文件编号 + ///// 是否成功 + //public bool DeleteFile(string fileId) + //{ + // if (string.IsNullOrEmpty(fileId) || _TableService.QuerySingle(_Source, GetTable("HDP_File")!, new Dictionary { + // { "Id","=" } + // }, new Dictionary { + // {"Id",fileId } + // }) == null) + // { + // throw new ArgumentException("文件编号不存在"); + // } + + // return _TableService.Delete(_Source, GetTable("HDP_File")!, new Dictionary { + // {"Id","=" } + // }, new Dictionary { + // {"Id",fileId } + // }); + //} /// /// 插入数据 diff --git a/Hadoop/ZKLT.Hadoop/TableService.cs b/Hadoop/ZKLT.Hadoop/TableService.cs index e1f7afd..9c28e6f 100644 --- a/Hadoop/ZKLT.Hadoop/TableService.cs +++ b/Hadoop/ZKLT.Hadoop/TableService.cs @@ -301,7 +301,11 @@ namespace ZKLT.Hadoop for (var i = 0; i < _primarys.Length; i++) { var _primary = _primarys[i]; - if (row[_primary.Key!] == null || string.IsNullOrEmpty(row[_primary.Key!].ToString())) + if (!string.IsNullOrEmpty(_primary.InsertDefault) || (row.ContainsKey(_primary.Key!) && row[_primary.Key!] != null)) + { + continue; + } + else { throw new ArgumentException($@"主键{_primary.Key}值无效"); } @@ -323,17 +327,9 @@ namespace ZKLT.Hadoop } else if (!string.IsNullOrEmpty(_column.InsertDefault)) { - if (HDP_CommandAction.IsAction(_column.InsertDefault)) - { - _colstr.Append($@"`{_column.Key!}`,"); - _parmstr.Append($"{_column.InsertDefault},"); - } - else - { - _colstr.Append($@"`{_column.Key!}`,"); - _parmstr.Append($@"@{_column.Key},"); - _params.Add(_column.Key!, _column.InsertDefault); - } + _colstr.Append($@"`{_column.Key!}`,"); + _parmstr.Append($@"@{_column.Key},"); + _params.Add(_column.Key!, HDP_CommandAction.Convert(_column.InsertDefault, row)); } } if (_colstr[_colstr.Length - 1] == ',') @@ -348,7 +344,7 @@ namespace ZKLT.Hadoop _command.AppendLine(") VALUES ("); _command.AppendLine(_parmstr.ToString()); _command.AppendLine(")"); - var _result = _connection.Execute(HDP_CommandAction.ConvertCommand(_command.ToString(), row), _params); + var _result = _connection.Execute(_command.ToString(), _params); _connection.Close(); if (_result > 0) { @@ -416,15 +412,8 @@ namespace ZKLT.Hadoop } else if (!string.IsNullOrEmpty(_column.UpdateDefault)) { - if (HDP_CommandAction.IsAction(_column.UpdateDefault)) - { - _colstr.Append($@"`{_column.Key!}`={_column.UpdateDefault}"); - } - else - { - _colstr.Append($@"`{_column.Key!}`=@{_column.Key!},"); - _params.Add(_column.Key!, _column.UpdateDefault); - } + _colstr.Append($@"`{_column.Key!}`=@{_column.Key!},"); + _params.Add(_column.Key!, HDP_CommandAction.Convert(_column.UpdateDefault, row)); } } if (_colstr[_colstr.Length - 1] == ',') @@ -436,7 +425,7 @@ namespace ZKLT.Hadoop //执行条件 MergeWhere(table, where, row, _command, _params); - var _result = _connection.Execute(HDP_CommandAction.ConvertCommand(_command.ToString(), row), _params); + var _result = _connection.Execute(_command.ToString(), _params); _connection.Close(); if (_result > 0) { @@ -494,7 +483,7 @@ namespace ZKLT.Hadoop MergeWhere(table, where, row, _command, _params); - var _result = _connection.Execute(HDP_CommandAction.ConvertCommand(_command.ToString(), row), _params); + var _result = _connection.Execute(_command.ToString(), _params); _connection.Close(); if (_result > 0) { @@ -553,7 +542,7 @@ namespace ZKLT.Hadoop //执行条件 MergeWhere(table, where, row, _command, _params); - var _result = _connection.Query(HDP_CommandAction.ConvertCommand(_command.ToString(), row), _params).ToArray(); + var _result = _connection.Query(_command.ToString(), _params).ToArray(); _connection.Close(); if (_result.Length > 0) { @@ -632,7 +621,7 @@ namespace ZKLT.Hadoop } _command.AppendLine(_orderstr.ToString()); } - var _result = _connection.Query(HDP_CommandAction.ConvertCommand(_command.ToString(), row), _params); + var _result = _connection.Query(_command.ToString(), _params); _connection.Close(); return _result.ToArray(); } @@ -717,7 +706,7 @@ namespace ZKLT.Hadoop _pagestr.AppendLine(_command.ToString()); _pagestr.AppendLine(") AS Temp"); _pagestr.AppendLine(@$"LIMIT {(_result.PageIndex - 1) * _result.PageSize},{_result.PageSize}"); - _result.Data = _connection.Query(HDP_CommandAction.ConvertCommand(_pagestr.ToString(), row), _params).ToArray(); + _result.Data = _connection.Query(_pagestr.ToString(), _params).ToArray(); _connection.Close(); return _result; }