Bardiya Choupani
Bardiya Choupani

Reputation: 165

Writing to KDB using qJava

I'm not very good with KDB ( in case the question sounds stupid). I'm trying to use kdb ( disk not memory) to load all my data from a database. I've already asked the question about upserts and I figured how to upsert 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'm trying to do this 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'm doing wrong? The data is not getting saved and I've tried multiple variations. I'd prefer to just load the data from SQL and save it to the disk for my initial load.

Upvotes: 3

Views: 1559

Answers (2)

Bardiya Choupani
Bardiya Choupani

Reputation: 165

Thanks to Charles Skelton from 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 is present

  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

Upvotes: 0

Slawomir K.
Slawomir K.

Reputation: 181

You probably want to replace "'dsPricing" (note the extra apostrohe) by "dsPricing" or perhaps ":dsPricing". qJava converts strings to symbols, so "'dsPricing" gets sent as what would you get in q by writing `$"'dsPricing".

Upvotes: 2

Related Questions