美文网首页
Common-csv来读取和写入csv

Common-csv来读取和写入csv

作者: 早点起床晒太阳 | 来源:发表于2020-09-27 20:26 被阅读0次

参考资料:https://commons.apache.org/proper/commons-csv/user-guide.html

背景

我们写CSV的时候不要自己用逗号去拼接 因为会有一堆的问题,比如说字段中有逗号的,字段中有特殊字符的 ,所以我们最好使用现成的包去写入或者读取CSV

maven 依赖

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-csv</artifactId>
    <version>1.8</version>
</dependency>

支持的csv格式

支持的csv格式有很多种,DEFAULT 是支持的标准的csv格式

读取csv

这块其实很简单 我直接上写好的代码

   /**
     * 按照自己定义的数组表头读取
     * @param path
     * @throws IOException
     */
    public static  void  readByArr (String path) throws IOException {
        Reader in = new FileReader(path);
        String[] header = {"id", "name"};
        Iterable<CSVRecord> records = CSVFormat.DEFAULT.withHeader(header).parse(in);
        for (CSVRecord record : records) {
            String lastName = record.get("id");
            String firstName = record.get("name");
            System.out.println(lastName);
            System.out.println(firstName);
        }
    }

    /**
     * 按照csv的第一行读取
     * @param path
     * @throws IOException
     */
    public static void readByAuto(String path) throws IOException {
        Reader in = new FileReader(path);
        Iterable<CSVRecord> records = CSVFormat.DEFAULT.withFirstRecordAsHeader().parse(in);
        for (CSVRecord record : records) {
            String lastName = record.get("id");
            String firstName = record.get("name");
            System.out.println(lastName);
            System.out.println(firstName);
        }
    }

写入csv

我这边列举了一个jdbc读取的数据写入到hdfs的操作

   /**
     * 将JDBC读取的数据写CSV到HDFS
     * @throws IOException
     */
    public static void  wirteToHdfs() throws IOException {

        FileSystem fs = HadoopUtils.getFileSystem();
        FSDataOutputStream fos = fs.create(new Path("/user/zgh/cdmdkdk"));

        CSVPrinter printer = new CSVPrinter(new OutputStreamWriter(fos), CSVFormat.DEFAULT.withHeader());

        Connection connection = JdbcTemplate.getConnection();
        Statement statement = null;
        try {
            statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("select * from vbapff3dddbd28479f2c3e0c37f7_parquet limit 10");
            printer.printRecords(resultSet);
            printer.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

相关文章

网友评论

      本文标题:Common-csv来读取和写入csv

      本文链接:https://www.haomeiwen.com/subject/wmgluktx.html