Java uniVocity-按位置转换为列表

Java uniVocity-按位置转换为列表,java,csv,univocity,Java,Csv,Univocity,我正在尝试从csv文件中的特定位置创建一个列表,而不是用括号 class Person {// @Parsed private String name; @?? private Address address; @Convert(conversionClass = Emails2.class, args = { "," , "5,6" }) @Parsed private List<String> emails; } 我需要阅读csv文件并创建电子邮件列表和地址对象。 我想用@Co

我正在尝试从csv文件中的特定位置创建一个列表,而不是用括号

class Person {//
@Parsed
private String name;
@??
private Address address;
@Convert(conversionClass = Emails2.class, args = { "," , "5,6" })
@Parsed
private List<String> emails;
}
我需要阅读csv文件并创建电子邮件列表和地址对象。 我想用@Convert

public class Emails2 implements Conversion<String, Set<String>> {

    private final String separator;
    private final Set<String> positions;

    public Emails2(String... args) {
        String separator = ",";
        Set<String> positions = null;

        if (args.length == 1) {
            separator = args[0];
        }

        if (args.length == 2) {
            positions = new HashSet<String>();
            String[] posi = args[1].split(",");
            for (String string : posi) {
                positions.add(string);
            }
        }
        this.separator = separator;
        this.positions = positions;
    }

    public Emails2(String separator, Set<String> positions) {
        this.separator = separator;
        this.positions = positions;
    }
//this method is not called, I don't know why
    public Set<String> execute(String input) { //I would like add the list and Address object inside this method to get it done in beanProcessed(...)
        if (input == null) {
            return Collections.emptySet();
        }

        Set<String> out = new TreeSet<String>();
        for (String token : input.split(separator)) {
            for (String word : token.trim().split("\\s")) {
                out.add(word.trim());
            }
        }
        return out;
    }

    public String revert(Set<String> input) {
        return revert(input);
    }

}
public类Emails2实现转换{
专用最终字符串分隔符;
私人最终设定位置;
公共电子邮件2(字符串…参数){
字符串分隔符=“,”;
设置位置=空;
如果(args.length==1){
分隔符=args[0];
}
如果(args.length==2){
positions=newhashset();
字符串[]posi=args[1]。拆分(“,”;
for(字符串:posi){
位置。添加(字符串);
}
}
this.separator=分隔符;
这个位置=位置;
}
公用电子邮件2(字符串分隔符,设置位置){
this.separator=分隔符;
这个位置=位置;
}
//这个方法没有被调用,我不知道为什么
publicsetexecute(stringinput){//我想在这个方法中添加list和Address对象,以便在beanProcessed(…)中完成它
如果(输入==null){
返回集合;
}
放样=新树集();
for(字符串标记:input.split(分隔符)){
对于(字符串字:token.trim().split(\\s))){
out.add(word.trim());
}
}
返回;
}
公共字符串还原(设置输入){
返回还原(输入);
}
}
我现在怎么样

    public static void main(String[] args) {

            BeanProcessor<Person> rowProcessor = new BeanProcessor<Person>(Person.class) {
                @Override
                public void beanProcessed(Person c, ParsingContext context) {
                    System.out.println(c.getName());

                    String[] positions = context.currentParsedContent().split(","); 
                    System.out.println(positions[5]);
//I'm using fixed position here, I'd like get the position from @Convert or another way by configuration
                    System.out.println(positions[6]);
                    List<String> list = new ArrayList<>();
                    list.add(positions[5]);
                    list.add(positions[6]);
                    c.setEmails(list);
                }

            };

            CsvParserSettings parserSettings = new CsvParserSettings();
            parserSettings.setRowProcessor(rowProcessor);
            parserSettings.setHeaderExtractionEnabled(true);
            CsvParser parser2 = new CsvParser(parserSettings);
            parser2.parse(getReader("/var/lib/cob/test2.csv"));

        }
publicstaticvoidmain(字符串[]args){
BeanProcessor rowProcessor=新BeanProcessor(Person.class){
@凌驾
已处理公共作废(人员c,ParsingContext){
System.out.println(c.getName());
String[]positions=context.currentParsedContent().split(“,”);
系统输出打印项次(位置[5]);
//我在这里使用的是固定位置,我希望通过@Convert或其他方式通过配置获得位置
系统输出打印LN(位置[6]);
列表=新的ArrayList();
增加(职位[5]);
增加(职位[6]);
c、 设置电子邮件(列表);
}
};
CsvParserSettings parserSettings=新的CsvParserSettings();
setRowProcessor(rowProcessor);
parserSettings.setHeaderExtractionEnabled(true);
CsvParser parser2=新的CsvParser(parserSettings);
parse(getReader(“/var/lib/cob/test2.csv”);
}

您试图通过自定义转换将多个列(
email1
email2
email3
)的值分配到单个属性(
Person.emails
)中,但这不受支持,因为转换的目标是单个字段(或输入的列),而不是整行

此外,自定义转换中的
execute
方法不会被调用,因为输入中没有
emails
标题。如果使用
@Parsed
注释对属性进行注释,但不提供要匹配的标题名称,则将使用属性名称本身。如果将属性从
emails
重命名为
email1
,您将看到转换类将被调用,或者如果您只是在注释中提供字段名,如下所示:

@Parsed(field = "email1")
private List<String> emails;
@Parsed(field=“email1”)
私人名单电子邮件;
它也将被称为

我明白了您在这里试图做什么,并打开了为此类需求提供更好支持的窗口


希望这能有所帮助。

您正试图通过自定义转换将多个列(
email1
email2
email3
)的值分配到单个属性(
Person.emails
)中,但这不受支持,因为转换的目标是单个字段(或输入的列)而不是整行

此外,自定义转换中的
execute
方法不会被调用,因为输入中没有
emails
标题。如果使用
@Parsed
注释对属性进行注释,但不提供要匹配的标题名称,则将使用属性名称本身。如果将属性从
emails
重命名为
email1
,您将看到转换类将被调用,或者如果您只是在注释中提供字段名,如下所示:

@Parsed(field = "email1")
private List<String> emails;
@Parsed(field=“email1”)
私人名单电子邮件;
它也将被称为

我明白了您在这里试图做什么,并打开了为此类需求提供更好支持的窗口


希望这能有所帮助。

我用另一种方式实现了我想要的,不是我想要的,但它奏效了。我使用MultiBeanProcessor并创建了虚拟类(我的老板不会喜欢)来表示我的csv位置

我的真实csv有如下标题:

email1,email2,email3,dddcel1,dddcel2,dddcel3,dddcel4,dddtel1,dddtel2,dddtel3,名字,文件,v_atu,ofe_cc,ignore1,银行,机构,ignore2,发票号码,联系电话,地址,邻居,邮编,城市,州

和多处理器

 MultiBeanProcessor processor = new MultiBeanProcessor(Customer.class, Address.class, Email.class, Mobile.class, Phone.class) {
            private Customer customer;
            private Address address;
            private Email email;
            private Mobile mobile;
            private Phone phone;

            @Override
            public void beanProcessed(Class<?> beanType, Object beanInstance, ParsingContext context) {

                if (beanType == Customer.class) {
                    customer = (Customer) beanInstance;
                    customer.set_id(UUID.randomUUID().toString());
                    customer.setStatus(CustomerStatusType.PENDING);
                    customer.setDiscountValue(customer.getInvoiceValue() - customer.getTotalValue());
                    customer.setToken(UUID.randomUUID().toString());
                    try {
                        customer.setCreated(new java.text.SimpleDateFormat("yyyy-MM-dd")
                                .parse((new java.text.SimpleDateFormat("yyyy-MM-dd").format(new java.util.Date()))));
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }

                    customer.setDomain("???????????");
                    Map<String, String> map = new HashMap<String, String>();
                    map.put("banco", customer.getBank());
                    map.put("agencia", customer.getAgency());
                    customer.setInvoiceDetail(map);

                }

                if (beanType == Address.class) {
                    address = (Address) beanInstance;
                    customer.setAddress(address);
                }

                if (beanType == Email.class) {
                    email = (Email) beanInstance;
                    customer.setEmails(Arrays.asList(email.getEmail1(), email.getEmail2(), email.getEmail3()));
                }

                if (beanType == Mobile.class) {
                    mobile = (Mobile) beanInstance;
                    customer.setMobiles(Arrays.asList(mobile.getDddcel1(), mobile.getDddcel2(), mobile.getDddcel3(), mobile.getDddcel4()));

                }

                if (beanType == Phone.class) {
                    phone = (Phone) beanInstance;
                    customer.setPhones(Arrays.asList(phone.getDddtel1(), phone.getDddtel2(), phone.getDddtel3()));
                }

                if (customer != null && address != null && email != null && mobile != null & phone != null) {
                    System.out.println(customer);//save customer object
                    customer = null;
                    address = null;
                    email = null;
                    phone = null;
                    mobile = null;
                }

            }
        };
MultiBean处理器=新的MultiBean处理器(Customer.class、Address.class、Email.class、Mobile.class、Phone.class){
私人客户;
私人地址;
私人电子邮件;
私人流动电话;
私人电话;
@凌驾
public void beanProcessed(类beanType、对象beanInstance、ParsingContext上下文){
if(beanType==Customer.class){
客户=(客户)beanInstance;
customer.set_id(UUID.randomuid().toString());
客户设置状态(客户)
 MultiBeanProcessor processor = new MultiBeanProcessor(Customer.class, Address.class, Email.class, Mobile.class, Phone.class) {
            private Customer customer;
            private Address address;
            private Email email;
            private Mobile mobile;
            private Phone phone;

            @Override
            public void beanProcessed(Class<?> beanType, Object beanInstance, ParsingContext context) {

                if (beanType == Customer.class) {
                    customer = (Customer) beanInstance;
                    customer.set_id(UUID.randomUUID().toString());
                    customer.setStatus(CustomerStatusType.PENDING);
                    customer.setDiscountValue(customer.getInvoiceValue() - customer.getTotalValue());
                    customer.setToken(UUID.randomUUID().toString());
                    try {
                        customer.setCreated(new java.text.SimpleDateFormat("yyyy-MM-dd")
                                .parse((new java.text.SimpleDateFormat("yyyy-MM-dd").format(new java.util.Date()))));
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }

                    customer.setDomain("???????????");
                    Map<String, String> map = new HashMap<String, String>();
                    map.put("banco", customer.getBank());
                    map.put("agencia", customer.getAgency());
                    customer.setInvoiceDetail(map);

                }

                if (beanType == Address.class) {
                    address = (Address) beanInstance;
                    customer.setAddress(address);
                }

                if (beanType == Email.class) {
                    email = (Email) beanInstance;
                    customer.setEmails(Arrays.asList(email.getEmail1(), email.getEmail2(), email.getEmail3()));
                }

                if (beanType == Mobile.class) {
                    mobile = (Mobile) beanInstance;
                    customer.setMobiles(Arrays.asList(mobile.getDddcel1(), mobile.getDddcel2(), mobile.getDddcel3(), mobile.getDddcel4()));

                }

                if (beanType == Phone.class) {
                    phone = (Phone) beanInstance;
                    customer.setPhones(Arrays.asList(phone.getDddtel1(), phone.getDddtel2(), phone.getDddtel3()));
                }

                if (customer != null && address != null && email != null && mobile != null & phone != null) {
                    System.out.println(customer);//save customer object
                    customer = null;
                    address = null;
                    email = null;
                    phone = null;
                    mobile = null;
                }

            }
        };