1)首先要确保正确安装CouchDB软件(Windows系统)
2)pom.xml
<!-- couchdb --><dependency><groupId>org.ektorp</groupId><artifactId>org.ektorp</artifactId><version>1.5.0</version></dependency>
3)application.yml
# CouchDB非关系型数据库,与MongoDB类似
couchdb:host: http://127.0.0.1port: 5984username: adminpassword: wz123456database: test
4)配置文件CouchDBConfig.class
@Component
@Data
public class CouchDBConfig {@Value(value = "${couchdb.host}")private String host;@Value(value = "${couchdb.port}")private String port;@Value(value = "${couchdb.database}")private String database;@Value(value = "${couchdb.username}")private String username;@Value(value = "${couchdb.password}")private String password;@Bean(name = "CouchDbConnector")public CouchDbConnector couchDbConnector() throws Exception {HttpClient httpClient = new StdHttpClient.Builder().url(host + ":"+ port).username(username).connectionTimeout(10000).socketTimeout(1000000).password(password).build();CouchDbInstance couchDbInstance = new StdCouchDbInstance(httpClient);CouchDbConnector couchDbConnector = new StdCouchDbConnector(database, couchDbInstance);couchDbConnector.createDatabaseIfNotExists();System.out.println("注入成功");return couchDbConnector;}// 根据传入的数据库名称创建新的 CouchDbConnector 实例public CouchDbConnector getConnectorForDatabase(String databaseName) throws Exception {HttpClient httpClient = new StdHttpClient.Builder().url(host + ":" + port).username(username).connectionTimeout(10000).socketTimeout(1000000).password(password).build();CouchDbInstance couchDbInstance = new StdCouchDbInstance(httpClient);CouchDbConnector couchDbConnector = new StdCouchDbConnector(databaseName, couchDbInstance);couchDbConnector.createDatabaseIfNotExists(); // 确保目标数据库存在return couchDbConnector;}
}
5)定时启动服务,RandomlyGeneratEntityCongfig.GenerateEntity(12000);为数据随机生成,可自定义初始化
@Component
public class CouchDBDataReader {private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");@Autowiredprivate CouchDBConfig couchDBConfig;@Autowiredprivate CouchDbConnector couchDbConnector;/*** 定时任务启动,每隔5s,统一在AllAutoReader类中启动*/
// @Scheduled(fixedRate = 10000)public void readCouchDBData(){String currentTime = LocalDateTime.now().format(formatter);System.out.println(currentTime+"readCouchDB操作开始");// insertDevPointFiveToCouchDBData();
// getLimitedDocuments();
// countAllDocuments();insertDevToCouchDB(1000);insertDevToCouchDB(3000);insertDevToCouchDB(5000);
// insertDevToCouchDB(10000);String currentTime2 = LocalDateTime.now().format(formatter);System.out.println(currentTime2+", CouchDB操作完成");}
/*** 获取CouchDB数据库指定test数据库中的所有文档*/public long countAllDocuments(){// 方式一:获取所有文档的 ID 列表
// List<String> allDocIds = couchDbConnector.getAllDocIds();
// System.out.println("CouchDB中总文档数量:" + allDocIds.size());// 方式二:获取数据库信息// 定义 CouchDB 数据库 URLString database= "test";String dbUrl = "http://localhost:5984/"+database;// 设置认证信息String username = "admin";String password = "wz123456";CustomDbInfo customDbInfo = getDBInfo(dbUrl,username,password);
// System.out.println("CouchDB中当前数据库:"+customDbInfo.getDbName()+" ,总文档数量: "+ customDbInfo.getDocCount());return customDbInfo.getDocCount();}
/*** 将随机生成的数据插入到CouchDB中* 数量自定义设置*/
public void insertDevPointFiveToCouchDBData(){try{List<DevPointFiveSeDayShard> devPointFiveList = RandomlyGeneratEntityCongfig.GenerateEntity(12000);
// System.out.println("生成数据量大小"+devPointFiveList.size());
// System.out.println("生成数据"+devPointFiveList);List<DevPointForCouchDB> devPointForCouchDBList = devPointFiveList.stream().map(DevPointForCouchDB::new).collect(Collectors.toList());
// System.out.println("转换后数据量"+devPointForCouchDBList);long startTime = System.currentTimeMillis();// 选择源数据库 直接选择默认配置的数据库testcouchDbConnector.executeBulk(devPointForCouchDBList);
// // 选择目标数据库 指定数据库名targetDbName
// // 根据传入的数据库名称获取相应的 CouchDbConnector
// String targetDbName = "devpoint";
// CouchDbConnector targetDbConnector = couchDBConfig.getConnectorForDatabase(targetDbName);
// targetDbConnector.executeBulk(devPointForCouchDBList);long endTime = System.currentTimeMillis();System.out.println(LocalDateTime.now().format(formatter)+"存储CouchDB数据耗时:" + (endTime - startTime) + "ms"+"存储量:"+devPointForCouchDBList.size());long documentSize = countAllDocuments();System.out.println("CouchDB中总文档数量:" + documentSize);// 准备要写入的日志内容String logMessage = String.format("%s 存储CouchDB耗时: %d 毫秒, 数据量: %d , 数据总量达到:%d",LocalDateTime.now().format(formatter),(endTime - startTime), devPointFiveList.size(),documentSize);// 写入文件try (BufferedWriter writer = new BufferedWriter(new FileWriter("logs/insertCouchDBDB30000_log.txt", true))) {writer.write(logMessage);writer.newLine(); // 换行} catch (IOException e) {e.printStackTrace(); // 处理异常}}catch (Exception e){e.printStackTrace();}}
}
6)实体类DevPointFiveSeDayShard和DevPointForCouchDB
@Data
@Document()
@AllArgsConstructor
@NoArgsConstructor
public class DevPointFiveSeDayShard {/*** 设备id*/@Indexedprivate String productId;/*** 变量id*/@Indexedprivate String variableId;/*** 计算后的变量值,为Double.NaN此值为异常值*/private Double calcValue = Double.NaN;/*** 异常类型(0--正常 1--值异常 2--采集异常(取不到数据) 3--通讯异常 5--换表 6--清零)*/private int errType = 0;/*** 整条数据上报时间* yyyy-MM-dd HH:mm:ss*/@Indexedprivate Long reportDateTime;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class DevPointForCouchDB {@JsonProperty("_id")private String id; // CouchDB 文档 ID@JsonProperty("_rev")private String rev; // CouchDB 文档版本private DevPointFiveSeDayShard data; // 原始数据// 构造函数,生成唯一 ID,并封装数据public DevPointForCouchDB(DevPointFiveSeDayShard data) {this.id = UUID.randomUUID().toString(); // 自动生成唯一 IDthis.data = data;}
}