• csv 过滤及汇总计算


    【问题】

    In Bookings.csv file each line contains a name, surname, roomtype, check-in date, check-out date separated by semicolons.

    1. Name;Surname;roomtype1.2;2015-03-24;2015-03-26
    2. Paul;Smith;roomtype1.1;2015-03-21;2015-03-23
    3. Romas;Babajus;roomtype2.1;2015-03-26;2015-03-28
    4. Bob;Alfredo;roomtype3.1;2015-03-24;2015-03-26
    5. Edvard;Jogn;roomtype2.2.;2015-03-04;2015-03-25
    6. Jonas;Amberto;roomtype3.2;2015-03-20;2015-03-23 

    in roomtype1.* , roomtype2.* , roomtype3.* the “*” indicates a room that is considered to be roomtype1.

    When there’s a new booking for roomtype1 the program should find the check-out dates of rooms which are roomtype1 (roomtype1.1 and roomtype1.2) and compare each check-out date in order find a room that has the closest check-out date to the new booking date.

    So far a was only able to read the whole dates stored in Bookings.csv without knowing to which roomtype those dates belong.

    How would you suggest to read only roomtype1 check-out dates from a csv file? Would it be the best way to use a two-dimensional array and loop the file?

    So far my code looks like this if that helps.

    1. publicclassBookings{
    2. staticlongdifv;
    3. publicstaticvoidmain(String\[\] args) throws Exception{
    4. SimpleDateFormatft = newSimpleDateFormat("yyyy-MM-dd");
    5. DatecheckIn = null;
    6. DatecheckOut = null;
    7. Datetest = ft.parse("2015-03-30");
    8. StringfileName = "Bookings.csv";
    9. Filefile = newFile(fileName);
    10. try{
    11. ScannerinputStream = newScanner(file);
    12. while(inputStream.hasNext()) {
    13. Stringdata = inputStream.next();
    14. String\[\] values = data.split(";");
    15. checkIn = ft.parse(values\[3\]);
    16. checkOut = ft.parse(values\[4\]);
    17. // System.out.println("Check in date");
    18. // System.out.println(checkIn);
    19. // System.out.println("Check out date");
    20. // System.out.println(checkOut);
    21. }
    22. inputStream.close();
    23. //interval(checkOut, test, TimeUnit.HOURS);
    24. // System.out.println("the difv is " + difv);
    25. // if (checkOut.compareTo(test) <= 0) { // or equal
    26. // System.out.println("Date1 is after or equal to Date2");
    27. // } else if (checkOut.compareTo(test) < 0) {
    28. // System.out.println("Date1 is before Date2");
    29. // } else if (checkOut.compareTo(test) == 0) {
    30. // System.out.println("Date1 is equal to Date2");
    31. // } else {
    32. // System.out.println("How to get here?");
    33. //
    34. // }
    35. } catch(FileNotFoundExceptione) {
    36. e.printStackTrace();
    37. }
    38. }
    39. publicstaticlonginterval(DatecheckOut, Datetest, TimeUnittimeunit) {
    40. longdiff = test.getTime() - checkOut.getTime();
    41. difv = timeunit.convert(diff,TimeUnit.MILLISECONDS);
    42. returndifv;
    43. }
    44. }

    Thank you in advance for your help!

    【回答】

    本问题需要进行字段查询、求最大值,这些都是结构化数据的基本运算,但 JAVA 缺乏相关的类库,实现过程复杂,代码可读性差。这种情况下可以用 SPL 辅助实现,代码更直观易懂:

    A
    1=file("Bookings.csv").import@tc(;,";")
    2=A1.select(like(roomtype,argtype+".*"))
    3=A2.maxp(interval('check-out',argDate))

    A1: 读入文件,分割符是分号,将第一行读为字段名。

    A2: 查询 roomtype 字段,argtype 是查询参数,* 是通配符。

    A3: 计算最大值,排序规则是 check-out 字段和输入参数 argDate 的间隔。

    集算器提供了易于集成的 JDBC 接口,结合集算器可以大大简化 JAVA 中的结构化计算,详细可参考:

    Java 如何调用 SPL 脚本

    集算器协助 java 处理结构化文本

     

  • 相关阅读:
    Go语法的特殊之处
    SpringBoot3整合RabbitMQ之一_消息生产者与消息消费者服务搭建
    MySQL大小版本升级步骤
    【Linux】计算机的软硬件体系结构
    【二叉树】最大二叉树
    Python 网络爬虫:深入解析 Scrapy
    工厂设备扫码使用售卖联网开发需要怎么开发开源代码?
    设计模式——结构性设计模式
    vue的axios是干什么的
    Linux下vim的简单使用方式
  • 原文地址:https://blog.csdn.net/raqsoft/article/details/126783190