首页 \ 问答 \ Spring Batch ItemReader处理Tree结构化文件(Spring Batch ItemReader to process Tree structured File)

Spring Batch ItemReader处理Tree结构化文件(Spring Batch ItemReader to process Tree structured File)

我正在尝试读取一个固定长度的文本文件,并使用spring批处理将其寄存器导入数据库,但输入文件的总体布局是嵌套的,例如(为了简化可视化而添加的列表):

FILE HEADER
    ENTERPISE1 HEADER
        DEPARTMENT1 HEADER
            WORKER1
            WORKER2
        DEPARTMENT1 FOOTER
        DEPARTMENT2 HEADER
            WORKER3
        DEPARTMENT2 FOOTER
    ENTERPRISE1 FOOTER  
    ENTERPRISE2 HEADER
        DEPARTMENT3 HEADER
            WORKER4
        DEPARTMENT3 FOOTER
    ENTERPRISE2 FOOTER
             .
             .
             .
FILE FOOTER

必须将文件导入到具有表和关系的数据库中。

TABLE       RELATION   TABLE 

ENTERPRISE (ONETOMANY) DEPARTMENTS
DEPARTMENT (ONETOMANY) WORKERS

在我实施的其他春季批量项目中,我们使用了具有相同布局规则的文件,其中的字段引用了它们所属的关系。 在这种情况下,寄存器的类型由行的第一列上的“id”和“树”结构的关系来标识。 是否有任何弹簧开箱即用的ItemReader可以帮助完成这项任务? 我得到的解决方案是构建一个自定义ItemReader,它读取企业注册表并继续阅读构建部门和工作对象的行,并返回一个Enterprise对象,所有部门和工作人员立即写入数据库,但我不知道是否当工作人员和部门的数量太高时(使用JPA写入数据库),将进行扩展。

在此先感谢您的帮助。


I'm trying to read a fixed-length text file and import its registers to a database using spring batch but the input file general layout is nested, something like (tabulation added just to simplify visualization):

FILE HEADER
    ENTERPISE1 HEADER
        DEPARTMENT1 HEADER
            WORKER1
            WORKER2
        DEPARTMENT1 FOOTER
        DEPARTMENT2 HEADER
            WORKER3
        DEPARTMENT2 FOOTER
    ENTERPRISE1 FOOTER  
    ENTERPRISE2 HEADER
        DEPARTMENT3 HEADER
            WORKER4
        DEPARTMENT3 FOOTER
    ENTERPRISE2 FOOTER
             .
             .
             .
FILE FOOTER

the file has to be imported to a database with the tables and relations like.

TABLE       RELATION   TABLE 

ENTERPRISE (ONETOMANY) DEPARTMENTS
DEPARTMENT (ONETOMANY) WORKERS

In other spring batch projects i had implemented, we used files with same-layout-regiters with fields that refers to the relation that they belong. In this case the type of the register is identified by an "id" on the first column of the line and the relation by the "tree" structure. Is there any spring out-of-the-box ItemReader that can help on this task? The solution I get is to build a custom ItemReader that reads the enterprise register and keep reading lines building department and worker objects and returning an Enterprise object with all departments and workers to be write into the database at once but I don't know if that would scale when the number of workers and departments are too high (using JPA to write to the database).

Thanks in advance for any help.


原文:https://stackoverflow.com/questions/35800549
更新时间:2023-06-11 16:06

最满意答案

我认为你所尝试的是不可能的。 它在调试时看到你的原因是VS正在运行该站点,并在您的用户名下运行。

当您将应用程序部署到服务器时,在那里运行的代码无法访问用户的计算机 - 因此无法找到用户名。

您可以在用户的​​计算机上运行一些Javascript,但我认为不可能从中获取本地计算机用户名。

更新:好的,如果用户以某种方式登录到网站,那就不同了 - 我误解了这个问题。 您可以尝试从以下位置获取:

System.Threading.Thread.CurrentPrincipal.Identity.Name


So this turned out to be a simple fix using one of the methods I had shown before.

string UserName = System.Threading.Thread.CurrentPrincipal.Identity.Name;

The tricky part was in the IIS Configuration. Once we figured out that Anonymous Authentication being disabled forces the Users log in ID to come accross It worked fine.

相关问答

更多