package com.rootcloud.bfcec.util;
import com.alibaba.fastjson.JSON;
import java.io.;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.;
import java.util.concurrent.TimeUnit;
public class CsvUtil {
/**
* CSV文件生成方法
* @param head
* @param dataList
* @param outPutPath
* @param filename
* @return
*/
public static File createCSVFile(List<String> head, List<List<String>> dataList,
String outPutPath, String filename) {
File csvFile = null;
BufferedWriter csvWtriter = null;
try {
csvFile = new File(outPutPath + File.separator + filename + ".csv");
File parent = csvFile.getParentFile();
if (parent != null && !parent.exists()) {
parent.mkdirs();
}
csvFile.createNewFile();
// GB2312使正确读取分隔符","
csvWtriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(
csvFile), "GBK"), 1024);
// 写入文件头部
writeRow(head, csvWtriter);
// 写入文件内容
for (List<String> row : dataList) {
writeRow(row, csvWtriter);
}
csvWtriter.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
csvWtriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return csvFile;
}
private static void writeRow(List<String> row, BufferedWriter csvWriter) throws IOException {
// 写入文件头部
for (Object data : row) {
StringBuffer sb = new StringBuffer();
String rowStr = sb.append("\"").append(data).append("\",").toString();
csvWriter.write(rowStr);
}
csvWriter.newLine();
}
public static List<String> getContent(String path){
List<String> allString = new ArrayList<>();
File csv = new File(path); // CSV文件路径
BufferedReader br = null;
try
{
br = new BufferedReader(new FileReader(csv));
} catch (FileNotFoundException e)
{
e.printStackTrace();
}
String line = "";
String everyLine = "";
try {
while ((line = br.readLine()) != null) //读取到的内容给line变量
{
everyLine = line;
System.out.println(everyLine);
allString.add(everyLine);
}
System.out.println("csv表格中所有行数:"+allString.size());
} catch (IOException e)
{
e.printStackTrace();
}
return allString;
}
public static List<Map<String,Object>> getContentJson(String path){
List<Map<String,Object>> allString = new ArrayList<>();
File csv = new File(path); // CSV文件路径
BufferedReader br = null;
try
{
br = new BufferedReader(new FileReader(csv));
} catch (FileNotFoundException e)
{
e.printStackTrace();
}
String line = "";
try {
int c = 0;
while ((line = br.readLine()) != null) //读取到的内容给line变量
{
if(c!=0){
Map<String,Object> everyLine = new HashMap<>();
String[] lines = line.split(",");
for(int i=0;i<lines.length;i++){
if(i==1){
everyLine.put("t",lines[i]);
}
if(i==2){
everyLine.put("o",lines[i]);
}
if(i==3){
everyLine.put("a",lines[i]);
}
//String item = lines[i];
}
//System.out.println(everyLine);
allString.add(everyLine);
}
c++;
}
System.out.println("csv表格中所有行数:"+allString.size());
} catch (IOException e)
{
e.printStackTrace();
}
return allString;
}
/**
* 压装
* @param path
* @return
*/
public static List<Map<String,Object>> getContentJson2(String path){
List<Map<String,Object>> allString = new ArrayList<>();
File csv = new File(path); // CSV文件路径
BufferedReader br = null;
try
{
br = new BufferedReader(new FileReader(csv));
} catch (FileNotFoundException e)
{
e.printStackTrace();
}
String line = "";
try {
int c = 0;
while ((line = br.readLine()) != null) //读取到的内容给line变量
{
if(c!=0){
Map<String,Object> everyLine = new HashMap<>();
String[] lines = line.split(",");
for(int i=0;i<lines.length;i++){
if(i==1){
everyLine.put("t",lines[i]);
}
if(i==2){
everyLine.put("d",lines[i]);
}
if(i==3){
everyLine.put("p",lines[i]);
}
//String item = lines[i];
}
//System.out.println(everyLine);
allString.add(everyLine);
}
c++;
}
System.out.println("csv表格中所有行数:"+allString.size());
} catch (IOException e)
{
e.printStackTrace();
}
return allString;
}
public static void testData(){
String catalogPath ="D:\\work\\data\\bfcec\\list.xlsx";
}
public static void main(String[] args) {
String path = "D:\\work\\rootcloud\\tmp\\1.csv";
List<String> allString = CsvUtil.getContent(path);
String heads = allString.get(0);
String[] head = heads.split(",");
InfluxDBConnection influxDBConnection = new InfluxDBConnection("admin", "admin", "http://127.0.0.1:8086", "mydb", "autogen");
for(int i=1;i<allString.size();i++){
String contents = allString.get(i);
String[] content = contents.split(",");
Map<String, String> tags = new HashMap<>();
Map<String, Object> fields = new HashMap<>();
for(int j=0;j<head.length;j++){
if(head[j].equals("index")){
tags.put(head[j], content[j]);
}else{
Object v = content[j];
fields.put(head[j], v);
}
System.out.println(head[j]+"="+content[j]);
}
System.out.println("--tags="+ JSON.toJSONString(tags)+" fields="+JSON.toJSONString(fields));
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
Long time = null;
try{
String strT = fields.get("time").toString();
Date date = sdf.parse(strT);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
Timestamp ts = Timestamp.valueOf(fields.get("time").toString());
time = ts.getTime();
String lastTime = strT.substring(strT.lastIndexOf(".")+1);
String strTime = date.getTime()/1000+""+lastTime;
time = Long.parseLong(strTime);
System.out.println(" ----time="+fields.get("time").toString());
System.out.println(" ----strTime="+strTime);
System.out.println(" ----time="+ts.getTime());
}catch (Exception e){
e.printStackTrace();
}
influxDBConnection.insert("bfcec",tags,fields,time,TimeUnit.NANOSECONDS);
}
}
}