Forráskód Böngészése

feat:优化了文件上传

DingXiaoYu 3 éve
szülő
commit
3693070e4b

+ 0 - 6
java_compile/a.csv

@@ -1,6 +0,0 @@
- score,label
-1,1
-2,1
-3,0
-4,0
-5,0

+ 1 - 1
java_compile/src/main/java/com/example/controller/FileController.java

@@ -39,7 +39,7 @@ public class FileController {
             return rsl != 0 ? new NormalRes(rsl) : new ErrorRes(40002, "Server error.");
         } catch (Exception e) {
             log.info("error fileLoader");
-            return new ErrorRes();
+            return new ErrorRes(40002,e.getMessage());
         }
     }
 

+ 1 - 1
java_compile/src/main/java/com/example/entity/HeaderInfo.java

@@ -16,7 +16,7 @@ import javax.persistence.*;
 @Table(name = "header_info")
 public class HeaderInfo {
     @Id
-    @GeneratedValue(strategy = GenerationType.AUTO)
+    @GeneratedValue(strategy = GenerationType.IDENTITY)
     private int id;
     private int fileInfoId;  //文件id
     private String fieldName;   //文件头的字段信息

+ 13 - 4
java_compile/src/main/java/com/example/services/FileService.java

@@ -64,10 +64,19 @@ public class FileService {
         String fileType = FileHelper.getFileType(filename);
         File file = FileHelper.multipartToFile(inFile);
         log.info("server file location: " + file.getAbsolutePath());
-        int rsl = "csv".equals(fileType) ? csvLoader.loadCsv(file.getAbsolutePath(), userId) :
-                sasLoader.loadSas(file.getAbsolutePath(), userId);
-        file.delete();
-        return rsl;
+        try{
+//          return  "csv".equals(fileType) ? csvLoader.loadCsv(file.getAbsolutePath(), userId) :
+//                    sasLoader.loadSas(file.getAbsolutePath(), userId);
+            //数据库中不能用同名文件
+            if (fileInfoDao.findByUserIdAndFilename(userId,filename)!=null){
+                throw new Exception("文件已经存在了。");
+            }
+            //暂时只接受csv
+            return  "csv".equals(fileType) ? csvAdapter.Csv2Parquet(file.getAbsolutePath(), filename, userId) : 0;
+        }finally {
+            //上传失败也必须将文件在本地删除,否则会堆积
+            file.delete();
+        }
     }
 
     public void deleteFile(int fileId) throws Exception {

+ 1 - 6
java_compile/src/main/resources/script.sql

@@ -49,7 +49,7 @@ create table func_info
 
 create table header_info
 (
-    id           int          not null
+    id              int auto_increment
         primary key,
     alias_name   varchar(255) null,
     con_or_dis   int          not null,
@@ -61,11 +61,6 @@ create table header_info
     value_info   varchar(255) null
 );
 
-create table hibernate_sequence
-(
-    next_val bigint null
-);
-
 create table layer_argument
 (
     id          int          not null

+ 1 - 1
java_compile/src/main/scala/com/example/data/CsvAdapter.scala

@@ -96,7 +96,7 @@ class CsvAdapter extends SparkConnect{
       headerInfoDao.save(new HeaderInfo(fileId, field.name, field.dataType.catalogString))
     })
     //7. 分析文件
-     fileService.analysis(fileId);
+    fileService.analysis(fileId);
     fileId
   }
 }