(利用索引)大数据查询
来源:互联网 发布:苹果电脑怎么更新软件 编辑:程序博客网 时间:2024/05/20 14:40
编译环境:QT5(创建时选择的是widget)
主要是将一个大文件拆成多个小文件,这样子就能够大大地节省了搜索的时间。
主要代码如下:
main.cpp
#include "widget.h"#include <QApplication>int main(int argc, char *argv[]){ QApplication a(argc, argv); Widget w; w.resize(600,400); w.show(); return a.exec();}
widget.cpp
#include "widget.h"#include <QHBoxLayout>#include <QVBoxLayout>#include <QMessageBox>#include <QTextCodec> //ASCII转UTF-8#include <QCursor>#include <time.h>#include <stdio.h>#include <stdlib.h>#include <string.h>Widget::Widget(QWidget *parent) : QWidget(parent){ setWindowTitle("酒店入住记录查询"); label1=new QLabel(); label1->setText("文件大小1.27GB\n记录数20151574"); label2=new QLabel(); label2->setText("请输入要查询的姓名:"); btn1=new QPushButton(); btn1->setText("查询"); edit1=new QLineEdit(); text1=new QTextBrowser(); QHBoxLayout *layout1=new QHBoxLayout(); layout1->addWidget(label2); layout1->addWidget(edit1); layout1->addWidget(btn1); QVBoxLayout *layout2=new QVBoxLayout(this); layout2->addWidget(label1); layout2->addLayout(layout1); layout2->addWidget(text1); connect(btn1,SIGNAL(clicked()),this,SLOT(on_clicked())); //将btn1与on_clicked()关联}Widget::~Widget(){}void Widget::select(int n) //n==1代表查询原始数据文件 n==2查询索引文件{ clock_t start=clock(); //执行效率计时开始 QTextCodec *codec=QTextCodec::codecForName("GBK"); char key[2048]={0}; char content[2048]={0}; const char*s= codec->fromUnicode(edit1->text().trimmed()).data();//将edit1中的内容从QString转化为const char* strcpy(key,s); if(strlen(key)<4) { QMessageBox::information(this,"错误","输入不正确,请重新输入"); return; } key[strlen(key)]=','; char path[1024]={0}; if(n == 1) strcpy(path,"D:/Documents/code/hotel/db/kaifang_noBomb.txt"); else if(n ==2) { int index=((unsigned char)key[0]+(unsigned char)key[1]+(unsigned char)key[2]+(unsigned char)key[3])%256; sprintf(path,"D:/Documents/code/hotel/db/index/%d.db",index); } FILE *p=fopen(path,"r"); if(!p) { QString str=path; str+="打开失败"; QMessageBox::information(this,"错误",str); return ; } this->setCursor(Qt::WaitCursor); text1->clear(); unsigned int count=0; //计数器 while(!feof(p)) { memset(content,0,sizeof(content)); fgets(content,sizeof(content),p); if(content[strlen(content)-1] == '\n') content[strlen(content)-1]=0; if(strncmp(content,key,strlen(key))== 0) { text1->append(codec->toUnicode(content)); count++; } } clock_t end=clock(); this->setCursor(Qt::ArrowCursor); fclose(p); QString res="找到"+QString::number(count)+"条记录,用时"+QString::number(end-start)+"毫秒"; QMessageBox::information(this,"完成",res);}void Widget::create_index(){ clock_t start=clock(); char path[1024]={0}; strcpy(path,"D:/Documents/code/hotel/db/kaifang_noBomb.txt"); FILE *p=fopen(path,"r"); if(!p) { QString str=path; str+="打开失败"; QMessageBox::information(this,"错误",str); return ; } FILE *pt[256]={NULL}; //建立256个索引文件并打开 for(int i=0;i<256;i++) { memset(path,0,sizeof(path)); sprintf(path,"D:/Documents/code/hotel/db/index/%d.db",i); pt[i]=fopen(path,"w"); } this->setCursor(Qt::WaitCursor); char buf[1024]={0}; int index=0; //索引值 while(!feof(p)) { memset(buf,0,sizeof(buf)); fgets(buf,sizeof(buf),p); if(buf[strlen(buf)-1] == '\n') buf[strlen(buf)-1]=0; //根据关键字求索引键值 index=((unsigned char)buf[0]+(unsigned char)buf[1]+(unsigned char)buf[2]+(unsigned char)buf[3])%256; fprintf(pt[index],"%s\n",buf); } fclose(p); for(int i=0;i<256;i++) { fclose(pt[i]); } clock_t end=clock(); this->setCursor(Qt::ArrowCursor); QString res="用时"+QString::number(end-start)+"毫秒"; QMessageBox::information(this,"完成",res);}void Widget::on_clicked(){ //QMessageBox::information(this,"提示","按钮被按了"); select(2);}
效果图:
阅读全文
0 0
- (利用索引)大数据查询
- sql大数据多条件查询索引优化
- mongodb nosql 大数据提高查询速度-索引
- 查询、索引和数据
- 大数据索引
- 大数据索引
- 大数据索引
- 利用索引提高查询效率
- 利用索引优化查询,提高效率
- 利用解决大数据查找死锁问题索引的三种方法
- 大数据查询
- 文本文件大数据查询
- Solr 千万级大数据索引查询导致服务器崩溃的原因和解决
- 提高mysql千万级大数据SQL查询优化30条经验(Mysql索引优化注意
- 【数据库,索引,大批数据查询】
- Oracle大数据and索引
- 大数据计算文章索引
- 大索引技术,大数据的未来
- H5项目笔记
- js 实现复选框只能选择一项
- PyCharm3.0默认快捷键
- 分治与动态规划之Burst Balloons问题
- spring-boot-admin源码分析及单机监控spring-boot-monitor的实现(三)
- (利用索引)大数据查询
- 获取当前时间戳
- MFC在鼠标自定义的一定的范围内实现拖动窗口移动、错误:不支持尝试执行的操作、实现进度条颜色的渐变或者背景和前景色的改变
- 关于eclipse左边文件栏消失
- Netty4实战第九章:启动Netty应用
- Paragon ntfs for mac 15新增了哪些功能
- 华硕g550jk4700笔记本bios设置u盘启动呢?
- 服务端编程介绍
- windows下编译caffe出错时解决办法