利用Python随机生成一个1000行的csv文件
import csv
import random
from datetime import datetime, timedelta
from random import randint, choice
monitor_objects = ['Server1', 'Server2', 'Server3', 'DB1']
metric_names = ['CPUUsage', 'MemoryUsage', 'DiskSpace', 'NetworkTraffic']
start_date = datetime.now() - timedelta(days=365)
end_date = datetime.now()
filename = 'random_data.csv'
with open(filename, mode='w', newline='', encoding='utf-8') as csvfile:fieldnames = ['dates', 'monitorObject', 'timeStamp', 'metricName', 'value']writer = csv.DictWriter(csvfile, fieldnames=fieldnames)writer.writeheader()for _ in range(10000000):random_date = start_date + (end_date - start_date) * random.random()date_str = random_date.strftime('%Y-%m-%d')time_stamp = int(random_date.timestamp())monitor_object = choice(monitor_objects)metric_name = choice(metric_names)value = round(random.uniform(0, 100), 2) writer.writerow({'dates': date_str,'monitorObject': monitor_object,'timeStamp': time_stamp,'metricName': metric_name,'value': value})print(f"CSV文件已成功生成,文件名为: {filename}")
利用Java写入inluxdb
package org.example;import com.csvreader.CsvReader;
import com.influxdb.client.InfluxDBClient;
import com.influxdb.client.InfluxDBClientFactory;
import com.influxdb.client.WriteApiBlocking;
import com.influxdb.client.domain.WritePrecision;
import com.influxdb.client.write.Point;import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;public class Main {static String token = "ZgpSCp3H_9liDB6v5POacJ8MOqnKOUR9YUlJjkIvLtFbYVyZr5Rkn-YKpUNdPqWpRKY_7Fqdwv6GN9r4A8BwcQ==";static String bucket = "data";static String org = "test";static InfluxDBClient client = InfluxDBClientFactory.create("http://localhost:8086", token.toCharArray());public static void main(String[] args) throws Exception {String CSV_FILE_PATH = "D:\\JAVA_dataAnalyze\\dataAnalyze\\src\\main\\java\\org\\example\\data\\random_data.csv";long startTime = System.currentTimeMillis();readCsv(CSV_FILE_PATH); long endTime = System.currentTimeMillis();System.out.println("excute time: " + (endTime - startTime) + "ms");startTime = System.currentTimeMillis();readCSVByMemory(CSV_FILE_PATH); endTime = System.currentTimeMillis();System.out.println("excute time: " + (endTime - startTime) + "ms");}public static void readCsv(String CSV_FILE_PATH) throws Exception {CsvReader CSVReader = new CsvReader(CSV_FILE_PATH, ',', StandardCharsets.UTF_8);CSVReader.readHeaders();int rowNumber = 0;long startTime = System.currentTimeMillis();while (CSVReader.readRecord()) {String content = CSVReader.getRawRecord();Point lineProtocol = row2InfluxPoint(content);point2InluxDB(lineProtocol);rowNumber++;if(rowNumber % 10000 == 0){long endTime = System.currentTimeMillis();System.out.println("rowNumber: " + rowNumber + "excute time: " + (endTime - startTime) + "ms");startTime = endTime;}}CSVReader.close();}public static void readCSVByMemory(String CSV_FILE_PATH) throws Exception {CsvReader CSVReader = new CsvReader(CSV_FILE_PATH, ',', StandardCharsets.UTF_8);CSVReader.readHeaders();int rowNumber = 0;long startTime = System.currentTimeMillis();List<String> contentList = new ArrayList<String>();while (CSVReader.readRecord()) {contentList.add(CSVReader.getRawRecord());}contentList.stream().parallel().forEach(content -> {point2InluxDB(row2InfluxPoint(content));});
}public static Point row2InfluxPoint(String content) {String[] contentArray = content.split(",");Point point = Point.measurement("mem_data1T").addTag("monitorObject", contentArray[1]).addField(contentArray[3], Float.parseFloat(contentArray[4])).time(Long.parseLong(contentArray[2]), WritePrecision.S);return point;}public static void point2InluxDB(Point point){WriteApiBlocking writeApi = client.getWriteApiBlocking();writeApi.writePoint(bucket, org, point);}}
<dependencies><dependency><groupId>net.sourceforge.javacsv</groupId><artifactId>javacsv</artifactId><version>2.0</version></dependency><dependency><groupId>com.influxdb</groupId><artifactId>influxdb-client-java</artifactId><version>6.6.0</version></dependency></dependencies>