2014-09-10 55 views
2

我對KDB不太好(如果問題聽起來很愚蠢)。我正在嘗試使用kdb(磁盤不是內存)從數據庫加載所有數據。我已經問upserts的問題,我想通了如何從控制檯UPSERT並保存到磁盤使用qJava寫入KDB

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) 

現在我試圖做到這一點在Java中,並具有以下代碼

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; 
    } 

}

誰能告訴我我做錯了什麼?數據沒有得到保存,我嘗試了多種變化。我寧願只從SQL加載數據並將其保存到磁盤以進行初始加載。

+0

您是否嘗試過使用消息處理程序向KDB一旁看到這是怎麼回事?在kdb端設置.z.po:{show「connected」},這樣你就可以確定你已經連接了。在kdb一側設置.z.pg:{0N!x}以準確查看您發送的內容 – terrylynch 2014-09-11 12:40:10

+0

您是否確實意指''dsPricing'中的''而不是'\'(很難看到這個字體中的不同,意味着反擊與反轉!) – 2014-09-16 14:33:18

回答

1

您可能想用「dsPricing」或者「:dsPricing」替代「'dsPricing」(注意額外的分詞)。 qJava將字符串轉換爲符號,因此「'dsPricing'」將以您通過編寫'$''dsPricing'獲得的內容發送。

0

感謝來自Kx系統

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(); 
    } 
} 

查爾斯·斯凱爾頓然後確認數據是否存在

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