#include <pthread.h>
#include <sys/time.h>
#include "decode.h"
//#include "../include/clickhouse.h"
__thread struct Student_pool_ student_pool;
#define INSERT_STD_CNT_EVERY_TIME 50000
static void forge_infomation(struct Student_* std)
{
srand((unsigned int)time(0));
// 1.学号
std->sno = rand() % 1000000 + 1000000;
// 2.姓名
memcpy(std->name, "xiaom", 5);
// 3.性别
if(std->sno % 2) {
memcpy(std->sex, "male", 4);
} else {
memcpy(std->sex, "female", 6);
}
// 4.出生日期
std->timestamp = 1649232299;
// 5.年龄
std->age = 25;
// 6.家庭住址
memcpy(std->address, "RenminRoad", 11);
// 7.监护人姓名
memcpy(std->guardian, "xiaow", 5);
// 8.监护人电话
memcpy(std->guardian_tel, "16312345678", 11);
}
__thread void* clickhouse_cli;
static void* decode(void* data) {
(void)data;
int iret = 0;
student_pool.student = malloc(INSERT_STD_CNT_EVERY_TIME * sizeof(struct Student_));
memset(student_pool.student, 0, INSERT_STD_CNT_EVERY_TIME * sizeof(struct Student_));
student_pool.student_cnt = 0;
struct Student_ std;
// 链接到clickhouse服务端
//clickhouse_cli = DatabaseInit();
//if(clickhouse_cli == NULL) {
// printf("DatebaseInit Failed.\n");
//}
pthread_barrier_wait(&barrier);
while(!g_force_quit) {
memset(&std, 0, sizeof(struct Student_));
// 1.伪造一份学生信息
forge_infomation(&std);
// 2.判断是否写满5W个学生信息
if(student_pool.student_cnt < INSERT_STD_CNT_EVERY_TIME) {
// 2.1 没有写满接着写
memcpy(&(student_pool.student[student_pool.student_cnt]), &std, sizeof(struct Student_));
student_pool.student_cnt++;
} else {
// 2.2 写满了则调用存储接口
//iret = DataInsertClickhouse(clickhouse_cli, (char*)student_pool.student, student_pool.student_cnt);
//sleep(20);
// 2.3 清除缓存区
memset(student_pool.student, 0, INSERT_STD_CNT_EVERY_TIME * sizeof(struct Student_));
student_pool.student_cnt = 0;
// 2.4 将这一次的写入到缓存区
memcpy(&(student_pool.student[student_pool.student_cnt]), &std, sizeof(struct Student_));
student_pool.student_cnt++;
// 2.5 判断如果写入失败了,则重新连接数据库
if (iret) {
// 删除之前的连接
// DatabaseDeinit(clickhouse_cli);
// 建立新的连接
// clickhouse_cli = DatabaseInit();
}
}
}
return NULL;
}
int decode_thread(pthread_t* thread_pid) {
int iret = 0;
for(int i = 0; i < 5; i++) {
iret = pthread_create(&thread_pid[i], NULL, decode, NULL);
if(iret) {
return iret;
}
}
return iret;
}
decode 加入clickhouse库之前代码
需积分: 5 74 浏览量
2022-04-07
14:37:15
上传
评论
收藏 7MB GZ 举报
LFTF
- 粉丝: 91
- 资源: 8
评论0