Writing to KDB using qJava
I'm not very good with KDB (in case the question sounds silly). I am trying to use kdb (disk not memory) to load all my data from the database. I already asked a question about upserts and I figured out how to take off from console and save to disk
q)dsPricing:([id:`int$(); date:`date$()] open:`float$();close:`float$();high:`float$();low:`float$();volume:`int$())
q)dsPricing:([id:`int$(); date:`date$()] open:`float$();close:`float$();high:`float$();low:`float$();volume:`int$())
q)`dsPricing insert(123;2003.03.23;1.0;3.0;4.0;2.0;1000)
q)`dsPricing insert(123;2003.03.24;1.0;3.0;4.0;2.0;2000)
q)save `:dsPricing
q)`:dsPricing upsert(123;2003.03.25;1.0;3.0;4.0;2.0;1500)
Now I am trying to do it in Java and have the following code
public class LoadDS {
SqlSession session;
private DataStreamMapper mapper ;
public static void main(String args[]){
final QConnection q = new QBasicConnection(args.length >= 1 ? args[0] : "localhost", args.length >= 2 ? Integer.parseInt(args[1]) : 5001, "user",
"pwd");
LoadDS l=new LoadDS();
l.session = MyBatisConnectionFactory.getSqlSessionFactory("SMALLS").openSession();
l.mapper = l.session.getMapper(DataStreamMapper.class);
List<DataStream> prices = l.mapper.selectHistoricalPrices(1);
try {
q.open();
q.sync("upsert", "'dsPricing", l.getData(prices));
} catch (QException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// dsPricing:([id:`int$(); date:`date$()] open:`float$();close:`float$();high:`float$();low:`float$();volume:`int$())
private Object[] getData(List<DataStream> prices) {
final Object[] data = new Object[] {new int[prices.size()], new QDate[prices.size()],
new float[prices.size()], new float[prices.size()],
new float[prices.size()],new float[prices.size()],
new int[prices.size()]
};
for ( int i = 0; i < prices.size(); i++ ) {
((int[]) data[0])[i] = prices.get(i).getInfoCode();
((QDate[]) data[1])[i] = new QDate(prices.get(i).getMarketDate());
((float[]) data[2])[i] = (float)prices.get(i).getOpen_();
((float[]) data[3])[i] = (float)prices.get(i).getClose_();
((float[]) data[4])[i] = (float)prices.get(i).getHigh();
((float[]) data[5])[i] = (float)prices.get(i).getLow();
((int[]) data[6])[i] = (int)prices.get(i).getVolume();
}
return data;
}
}
Can anyone tell me what I am doing wrong? No data is saved and I tried several options. I'd rather just load the data from SQL and save it to disk for bootstrapping.
You probably want to replace "dsPricing" (note the extra apostles) with "dsPricing" or perhaps ": dsPricing". qJava converts strings to characters, so "dsPricing" is sent as what you get in q by writing "$" "dsPricing".
Thanks to Charles Skelton of Kx Systems
public static void main(String[]args){
try{
c c=new c("",5001);
c.k("addData",new Object[]{new int[]{1,1},
new Date[]{Date.valueOf("1994-2-17"),Date.valueOf("1994-2-16")},
new double[]{73.,76.},
new double[]{73.,76.},
new double[]{76.,77.899994},
new double[]{73.,75.},
new int[]{2223000,3167000}});
c.close();
}
catch(Exception e){
e.printStackTrace();
}
}
then confirm data availability
q)addData:{`:dsPricing/ upsert flip `id`date`open`close`high`low`volume!x;}
q)select from `:dsPricing
id date | open close high low volume
-------------| -------------------------------
1 1994.02.17| 73 73 76 73 2223000
1 1994.02.16| 76 76 77.89999 75 3167000