This repository has been archived on 2021-11-25. You can view files and clone it, but cannot push or open issues/pull-requests.
BigDataProject/2.2.cpp

124 lines
3.4 KiB
C++

#include "MySQLWrapper.h"
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <fstream>
using namespace std;
#include "local_db_account.h"
#include "util.hpp"
int main()
{
MySQLConn conn;
conn.connect("127.0.0.1",db_user,db_passwd,"bigdata3",3306);
conn.exec("set names gbk",nullptr);
/// 2.2 不同领域的平均薪资
vector<vector<string>> keywdvec
{
{
"大数据"
},
{
"程序开发","程序设计","开发"
},
{
"运维","运行维护","维护"
},
{
"测试"
}
};
map<string,vector<cash_pack>> mp;
int szArea=keywdvec.size();
for(int i=0;i<szArea;i++)
{
int szAlias=keywdvec[i].size();
for(int j=0;j<szAlias;j++)
{
ostringstream ostr;
ostr<<"select salary from lagou_job where ((title like '%"
<<keywdvec[i][j]<<"%') or (tags like '%"
<<keywdvec[i][j]<<"%'))";
conn.exec("use bigdata3",nullptr);
string sqlcmd=ostr.str();
conn.exec(sqlcmd,[&](MySQLResult& res)
{
res.stepRow([&](char** val,unsigned long* len)
{
cash_pack pk=ParseCash(val[0]);
mp[keywdvec[i][j]].push_back(pk);
});
});
ostr.str("");
ostr<<"select salary from pages where ((title like '%"
<<keywdvec[i][j]<<"%') or (tags like '%"
<<keywdvec[i][j]<<"%'))";
sqlcmd=ostr.str();
conn.exec("use bigdata5",nullptr);
conn.exec(sqlcmd,[&](MySQLResult& res)
{
res.stepRow([&](char** val,unsigned long* len)
{
cash_pack pk=ParseCashX(val[0]);
mp[keywdvec[i][j]].push_back(pk);
});
});
ostr.str("");
ostr<<"select salary from pages where title like '%"
<<keywdvec[i][j]<<"%' ";
sqlcmd=ostr.str();
conn.exec("use bigdata2",nullptr);
conn.exec(sqlcmd,[&](MySQLResult& res)
{
res.stepRow([&](char** val,unsigned long* len)
{
cash_pack pk=ParseCashX(val[0]);
mp[keywdvec[i][j]].push_back(pk);
});
});
}
}
ofstream ofs("result/2.2.csv");
#define cout ofs
cout<<"行业,最低工资(平均),最高工资(平均),平均工资,数量"<<endl;
for(auto& pr:mp)
{
int sum=0;
int lowsum=0;
int highsum=-1;
int szVec=pr.second.size();
for(auto& pk:pr.second)
{
sum+=pk.ave();
lowsum+=pk.low;
highsum+=pk.high;
}
sum/=szVec;
lowsum/=szVec;
highsum/=szVec;
cout<<pr.first<<","<<lowsum<<","<<highsum<<","<<sum<<","<<szVec<<endl;
}
#undef cout
return 0;
}