2014-09-10 18 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