加入两个.csv文件

时间:2017-02-20 10:34:47

标签: java database csv join merge

我想以下面显示的方式加入两个CSV文件。 文件总是具有相同的结构(例如,购物车中总有三种产品)。

"数据库":

ProductId, Name, Price
123, "My Product 1", 99
223, "My Product 2", 42
323. "My Product 3", 19

"购物-车":

 CustomerId, Product1, Product2, Product3
 111, 123, 223, 323
 112, 223, 223, 123

通缉输出:

CustomerId, Product1, Product1Name, Product1Price, Product2, Product2Name, Product2Price, Product3, Product3Name, Product3Price
111, 123, "My Product1", 99, 223, "My Product 2", 42, 323, "My Product 3", 19
112, 223, "My Product2", 41, 223, "My Product 2", 42, 123, "My Product 1", 99

我想过用一个小型Java-Programm使用CSV库来解决这个问题,而不是手动处理这些文件。但我想知道是否有更好的方法来做到这一点。

2 个答案:

答案 0 :(得分:1)

如果您想在不使用中间步骤的数据库的情况下处理CSV文件(在这种情况下无论如何都是更好的解决方案),您可以使用CSV file JDBC driver

在下面找到一个显示原理的片段。 (注意:除了准备片段之外,没有进行进一步的调查。)

假设以下目录和文件

bin/
csv.directory/database.csv
csv.directory/shopping_cart.csv
lib/csvjdbc-1.0-31.jar
src/sub/optimal/Main.java

<强> database.csv

ProductId, Name, Price
123,"My Product 1",99
223,"My Product 2",42
323,"My Product 3",19

<强> shopping_cart.csv

111,123,223,323
112,223,223,123

<强> Main.java

import java.sql.*;
import org.relique.jdbc.csv.CsvDriver;

public class Main {

    public static void main(String[] args) throws SQLException {
        String csvDirectory = "csv.directory";
        String connURL = "jdbc:relique:csv:";
        Connection conn = DriverManager.getConnection(connURL + csvDirectory);
        try (Statement stmt = conn.createStatement()) {
            String sql;
            sql = "SELECT c.CustomerId,"
                    + "       c.Product1,"
                    + "       (SELECT name"
                    + "        FROM database AS d"
                    + "        WHERE d.ProductId = c.Product1"
                    + "       ) AS Product1Name,"
                    + "       (SELECT price"
                    + "        FROM database AS d"
                    + "        WHERE d.ProductId = c.Product1"
                    + "       ) AS Product1Price"
                    + " FROM shopping_cart AS c";
            ResultSet results = stmt.executeQuery(sql);

            boolean append = true;
            CsvDriver.writeToCsv(results, System.out, append);
        }
    }
}

<强>编译

javac -d bin/ -cp lib/* src/sub/optimal/Main.java

生成

java -cp bin:lib/* sub.optimal.Main

<强>输出

CustomerId,Product1,PRODUCT1NAME,PRODUCT1PRICE
111,123,My Product 1,99
112,223,My Product 2,42

答案 1 :(得分:0)

连接表是DBMS - 数据库管理系统 - 通常所做的。虽然您可以自己编写,但使用自由软件DBMS(例如MonetDBMariaDBPostgreSQL)可能更简单,更容易:将两个CSV文件加载到表中DBMS;写一个连接查询:

SELECT cart_with_product_info AS
  c.CustomerId,
  c.Product1,
  p1.Name AS Product1Name,
  p1.Price AS Product1Price,
  c.Product2,
  p2.Price AS Product2Name,
  p2.Price AS Product2Price,
  c.Product3,
  p3.Name AS Product3Name,
  p3.Price Product3Price
FROM 
  shopping_cart AS c,
  products AS p1,
  products AS p2,
  products AS p3
WHERE
  p1.ProductId = c.Product1
  AND p2.ProductId = c.Product2
  AND p3.ProductId = c.Product3;

运行它并以CSV形式获得结果(通过多种方式执行此操作,例如以编程方式或使用命令行客户端)。