gpt4 book ai didi

java - 从 mongodb 检索数据并显示在 csv 文件中时出现意外输出?

转载 作者:行者123 更新时间:2023-12-02 12:06:47 25 4
gpt4 key购买 nike

问题

我正在从 mongodb 检索数据并以文本/csv 格式生成输出。虽然用户记录对用户的字段兴趣没有任何值(value),但我仍然获得给定用户的字段兴趣的值(value)。

我的分析:

在调试时,我得到了与我的 mongodb 数据匹配的结果,但生成的 text/csv 数据是错误的。

59e75753a7986c0a86c758ffCOOKING
[COOKING]
59e759c6a7986c0a86c75901READING
[READING]
59e75f0ea7986c0c4b003e24READING
[READING]
59e7926ea7986c0fc809cbd9CRICKET
[CRICKET]

Can anyone guide what I am doing wrong.Thankx in advance.

下面是我编写的代码

@RestController
@RequestMapping("/report")
public class ReportController {
@Autowired
private UserService userService;
@RequestMapping(value = "/endcustomer", method = RequestMethod.GET, produces = "text/csv")
public void endCustomerReport(HttpServletResponse response) {
ICsvBeanWriter csvWriter=null;
int pageSize=2000;
Page<User> users=null;
int page=0;
String csvFileName = "End-customer.csv";
List<String> headerList=null;
Field[] declaredFields=null;
UserReport userReport=new UserReport();
try {
response.setHeader("Content-Disposition", String.format("attachment; filename=\"%s\"", csvFileName));
headerList = new ArrayList<>();
declaredFields = UserReport.class.getDeclaredFields();
for (Field declaredField : declaredFields) {
headerList.add(declaredField.getName());
}
csvWriter = new CsvBeanWriter(response.getWriter(),
CsvPreference.STANDARD_PREFERENCE);

String[] header = headerList.toArray(new String[]{});
csvWriter.writeHeader(header);
/* CsvPreference.STANDARD_PREFERENCE
Ready to use configuration that should cover 99% of all usages.*/


do {
users = userService.getAllUsers(new PageRequest(page,pageSize));
for (User user : users) {
List interests=new ArrayList();
userReport.setName(user.getName());
userReport.setUsername(user.getUsername());
userReport.setContact(user.getContact());
userReport.setAddress(user.getAddress());
userReport.setEmail(user.getEmail());
userReport.setConnections(user.getConnections()); //See it's data
userReport.setPending(user.getPending());//See it's data
INTERESTS intrests[]=user.getInterests();
if(intrests!=null) {
for (INTERESTS interest : intrests) {
System.out.println(user.getId()+""+interest);
interests.add(interest);
}
System.out.println(interests);
userReport.setInterests(interests);
}
//See it's data
userReport.setOccupation(user.getOccupation());
userReport.setGender(user.getGender());
csvWriter.write(userReport, header);
}
page++;
}while (users.hasNext());
}
catch (IOException e){
e.printStackTrace();
}
finally{
if(csvWriter!=null){
try {
csvWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

}

我的用户文档

{ 
"_id" : ObjectId("59e75753a7986c0a86c758ff"),
"_class" : "com.ideationz.client.models.User",
"contact" : "9811164864",
"email" : "jalaj.chawla93@gmail.com",
"address" : "44HakikatNagar",
"gender" : "MALE",
"name" : "jalaj",
"dob" : ISODate("1993-02-10T18:30:00.000+0000"),
"username" : "jalaj",
"country" : "india",
"designation" : "engineer",
"imid" : "1",
"interests" : "COOKING"
}
{
"_id" : ObjectId("59e7583da7986c0a86c75900"),
"_class" : "com.ideationz.client.models.User",
"contact" : "9811164865",
"email" : "jalaj.chawla95@gmail.com",
"address" : "44HakikatNagar",
"gender" : "MALE",
"name" : "jalaj5",
"dob" : ISODate("1994-02-10T18:30:00.000+0000"),
"username" : "jalaj5",
"country" : "india",
"designation" : "engineer",
"imid" : "2"
}
{
"_id" : ObjectId("59e759c6a7986c0a86c75901"),
"_class" : "com.ideationz.client.models.User",
"contact" : "9811164866",
"email" : "jalaj.chawla96@gmail.com",
"address" : "44HakikatNagar",
"gender" : "MALE",
"name" : "jalaj6",
"dob" : ISODate("1994-02-10T18:30:00.000+0000"),
"username" : "jalaj6",
"country" : "india",
"designation" : "engineer",
"imid" : "3",
"interests" : "READING"
}
{
"_id" : ObjectId("59e75f0ea7986c0c4b003e24"),
"_class" : "com.ideationz.client.models.User",
"interests" : "READING",
"contact" : "9811164867",
"email" : "jalaj.chawla97@gmail.com",
"address" : "44HakikatNagar",
"gender" : "FEMALE",
"name" : "jalaj7",
"dob" : ISODate("1994-02-10T18:30:00.000+0000"),
"username" : "jalaj7",
"country" : "india",
"designation" : "engineer",
"imid" : "4"
}
{
"_id" : ObjectId("59e7926ea7986c0fc809cbd9"),
"_class" : "com.ideationz.client.models.User",
"contact" : "9811164869",
"email" : "jalaj.chawla99@gmail.com",
"address" : "44HakikatNagar",
"gender" : "MALE",
"name" : "jalaj9",
"dob" : ISODate("1993-11-13T18:30:00.000+0000"),
"username" : "jalaj9",
"country" : "india",
"designation" : "engineer",
"imid" : "9",
"interests" : "CRICKET"
}

预期输出

name    username    contact     address      email                   connections     pending  interests   occupation    gender  region
jalaj jalaj 9811164864 44HakikatNagar jalaj.chawla93@gmail.com [COOKING] MALE
jalaj5 jalaj5 9811164865 44HakikatNagar jalaj.chawla95@gmail.com [] MALE //Change here
jalaj6 jalaj6 9811164866 44HakikatNagar jalaj.chawla96@gmail.com [READING] MALE
jalaj7 jalaj7 9811164867 44HakikatNagar jalaj.chawla97@gmail.com [READING] FEMALE
jalaj9 jalaj9 9811164869 44HakikatNagar jalaj.chawla99@gmail.com [CRICKET] MALE

显示输出

name    username  contact   address email   connections pending interests   occupation  gender  region
jalaj jalaj 9811164864 44HakikatNagar jalaj.chawla93@gmail.com [COOKING] MALE
jalaj5 jalaj5 9811164865 44HakikatNagar jalaj.chawla95@gmail.com [COOKING] MALE
jalaj6 jalaj6 9811164866 44HakikatNagar jalaj.chawla96@gmail.com [READING] MALE
jalaj7 jalaj7 9811164867 44HakikatNagar jalaj.chawla97@gmail.com [READING] FEMALE
jalaj9 jalaj9 9811164869 44HakikatNagar jalaj.chawla99@gmail.com [CRICKET] MALE

最佳答案

上一次迭代中保留

您必须在 for 循环中初始化 userReport (UserReport userReport=new UserReport();) 或执行 setInterests(null).

关于java - 从 mongodb 检索数据并显示在 csv 文件中时出现意外输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46851455/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com