使用Java将Json文件转换成CSV文件

本文介绍了如何使用Fastjson库将复杂的JSON文件转换为CSV格式,以便于ClickHouse的高效导入,涉及JSON解析、对象提取及文件操作步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

将JSON文件数据导入ClickHouse的时候,由于JSON数据格式相对复杂,不是简单的K-V格式,所以将JSON数据进行处理,转换成CSV格式,代码如下:

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @program: ClickhouseSink
 * @ClassName Json2CSV
 * @description:
 * @author:
 * @create: 2022-04-13 09:06
 * @Version 1.0
 **/
public class Json2CSV {
    public static void main(String[] args) {
        File file = new File("D:\\Code\\IDEA\\ClickhouseSink\\input\\aaa.json");//待读取文件
        String content;
        if(file.exists()){
            System.out.println("文件存在。不用创建");
            try{
                String str;
                StringBuffer stringBuffer=new StringBuffer();
                // 读取文件的内容
                FileInputStream fileInputStream=new FileInputStream(file);
                InputStreamReader inputStreamReader=new InputStreamReader(fileInputStream,"UTF-8");
                BufferedReader bufferedReader=new BufferedReader(inputStreamReader);
                while ((content=bufferedReader.readLine())!=null){

                    //System.out.println(content);

                    JSONObject jsonObject = JSON.parseObject(content);

                    String id = jsonObject.getString("_id");

                    JSONObject source = jsonObject.getJSONObject("_source");
                    Integer broadcast = source.getInteger("broadcast");
                    String date = source.getString("date");
                    Integer drop = source.getInteger("drop");
                    Long timestamp = source.getLong("timestamp");

                    Date date1 = new Date();
                    SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String upload_time = sdf.format(date1);

                    stringBuffer.append(id).append(",")
                            .append(broadcast).append(",")
                            .append(date).append(",")
                            .append(drop).append(",")
                            .append(link_region).append(",")
                            .append(timestamp).append(",")
                            .append(upload_time)
                            .append("\n");
                }
                bufferedReader.close();
                inputStreamReader.close();
                fileInputStream.close();
                //向文件写入内容
                File newfile=new File("D:\\Code\\IDEA\\ClickhouseSink\\input\\bbb.csv");//待写入文件
                BufferedWriter bufferedWriter=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(newfile),"UTF-8"));//顺便练习下匿名类的使用

                bufferedWriter.write(stringBuffer.toString());
                bufferedWriter.close();
                System.out.println("文件写入内容完成");
            }catch (FileNotFoundException e){
                e.printStackTrace();
            }catch (UnsupportedEncodingException e){
                e.printStackTrace();
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值