kdb+q一个入库和删除的小demo


//什么都别说,先看我的入库代码!
//下面这句是导入表,有了这句就可以select啦
system "l /mnt/kdb_data1/SpotDB"
//下面这句是入库的表的物理路径
dir:`:/mnt/kdb_data1/SpotDB;
//下面这句是包含很多csv的根目录
filePath:`:/home/data/prod_script/daily/zjhscript/egg/eggSpotDataTool/;
/ filePath:`:/home/data/prod_script/daily/zjhscript/egg/eggSpotDataTool/egg_index_20160803.csv;
//下面这句是列举出目录下的所有的文件
pp:key filePath;
//下面这句从目录列举文件中选出符合条件的csv
pp:pp where (string pp) like "egg_index_*.csv";
//下面这句是把根目录和文件名拼接成绝对路径注意,sv前面有个空格才是`,最后转化为·list
filePathList:raze {` sv filePath, x } each pp;
     
//day:raze {"D"$8#-12#string x} each pp;
//joint the table in tb
//tb: ("dsiif"; enlist ",") 0:filePath
//下面这句是逐一读取csv拼成tbl
tb: raze {("dsiif"; enlist ",") 0: x} each filePathList;
//下面这句是sql语句,选出几列
tb: select TRADE_DT:date,Area:area,IsProductionArea:isProductionArea,IsRedBigEgg:isRedBigEgg,PriceAvg:priceAvg from tb;
//下面这句是选出时间
tradingDays:exec distinct TRADE_DT from tb;

savekdb:{[day] 
// 输出常量day这个字符串
    1 "day:";
// 输出变量day
    0N! day;
// sv拼接symbol,最后一一个`$""代表/
    dbdir:` sv dir,`$string day,`EggSpotIndex,`$"";
    1 "dbdir:";
    0N! dbdir;
    dtb:select from tb where TRADE_DT=day;
// 入库
    result: dbdir set .Q.en[hsym dir] update `p#Area from `Area`TRADE_DT xasc dtb;
    } each tradingDays;
   
select count i by TRADE_DT from EggSpotIndex
bb:select from EggSpotIndex






好了,这就是今天写的一点q代码,可以做一个小demo,进行参考




下面一个删除数据的demosystem "l /mnt/kdb_data1/ModelAnalysisTemp";

/ meta SimFutures
/ select from SimFutures where date=2016.07.07;
/ L:exec date from select distinct date from SimFutures;
/ select count  i from SimFutures where date=2016.06.08, day_night = 0;

/ kss:{
/    d:L x;
/    data: select from SimFutures where date=d, strategy<>`hi31;
/    data2:select from  SimFutures where date=d, day_night=1,strategy=`hi31,product=`dli;
/    data,:data2;
/    data:delete date from data;
/    data:`exch`product xasc data;
/    data:update `p#exch,`p#product from data;
/    dbdir:` sv `:/mnt/kdb_data1/ModelAnalysisTemp,(`$string d),`SimFutures,`$"";
/    dbdir set .Q.en[`:/mnt/kdb_data1/ModelAnalysisTemp]   data;
/    show d;
/    };


select distinct exch from SimFutures

   
deleteNightData:{
    [d]
    0N! "date:", (string d);
// 选出那一天的所有数据
    data: select from SimFutures where date=d;
//选出那一天需要删除的数据后剩下的数据,统计一下是不是对的
    data: delete from data where day_night = 0,exch=`shfe;
    data: delete date from data;
    data:`exch`product xasc data;
    data:update `p#exch,`p#product from data;
    0N! count data;
    dbdir:` sv `:/mnt/kdb_data1/ModelAnalysisTemp,(`$string d),`SimFutures,`$"";
    dbdir set .Q.en[`:/mnt/kdb_data1/ModelAnalysisTemp]   data;
    show d
    };

TD:(2016.08.02);

deleteNightData each TD;


select count i from SimFutures where date=2016.08.02,day_night=0,exch=`shfe

data1:select from SimFutures where date=2016.07.22,day_night=0;
10#data1

data2:select from SimFutures where date=2016.04.29,day_night=0;
-10#data2





你可能感兴趣的:(kdb,kdb+q,量化)