将数据从Java对象保存到H2O Frame

时间:2016-04-07 17:22:31

标签: java h2o

此问题类似于此Converting R dataframe to H2O Frame without writing to disk 除了适用于Java对象

我的数据在Java应用程序中生成,然后保存为文本并传递给H2O(通过R或Flow)。如果我在应用程序中动态创建(并保存)H2O DataFrames,我想我可以避免一些开销。我怀疑这很简单,但快速查看docs并没有给出一个简单的(SO风格)答案

2 个答案:

答案 0 :(得分:1)

您可以使用REST API从Java应用程序中驱动H2O实例,但这仍然需要您保存数据,然后调用ImportFiles(http://h2o-release.s3.amazonaws.com/h2o/rel-turchin/2/docs-website/h2o-docs/index.html#route-%2F3%2FImportFiles)之类的内容。

如果我正确理解了您的问题,目前无法按照您的建议将数据从您的应用“流式传输”到H2O实例。你可以这样做,如果你直接将H2O捆绑到你的应用程序,但我不确定这是你想要的。

答案 1 :(得分:1)

我花了很长时间尝试解决同一问题,因为这在任何地方都没有记录。也就是说,如何创建H2O框架而不会(缓慢地)往返磁盘。

这是我的内存中Java解决方案,用于将Guava表转换为H2O数据帧。应该很容易适应您的数据结构。

不过,我还没有弄清楚如何将其拆分为大块(或者不这样做会对性能产生什么影响)。也许更了解H2O的人可以对此发表评论??

/**
 * Converts a Guava Table to an H2O Frame (and registers it in the Distributed
 * Key/Value store) using only in-memory operations.
 * 
 * TODO everything is contained in a single chunk.  Not sure of performance implications of this...
 * 
 * @param t
 *            the guava table
 * @param tableKey
 *            a unique key to register the table in h2o under
 * @return an H2O Frame
 * @throws IOException
 */
public static Frame tableToFrame(LinkedHashBasedTable<Integer, String, Double> t, String tableKey) throws IOException {

    Set<String> cols = t.columnKeySet();
    List<Vec> vecs = new ArrayList<>();

    VectorGroup group = new VectorGroup(); //make a common group to contain vector keys.  This has something to do with Chunk distribution among nodes for parallel processing.
    for (String col : cols) {
        double[] vals = toDoubleArray(t.column(col).values());
        Key<Vec> key = group.addVec(); 
        Vec v = Vec.makeVec(vals, key);  
        vecs.add(v);
    }

    String[] names = cols.toArray(new String[cols.size()]);
    Vec[] vecArr = vecs.toArray(new Vec[vecs.size()]);

    Key<Frame> frameKey = Key.make(tableKey);
    Frame frame = new Frame(frameKey, names, vecArr);
    DKV.put(frameKey, frame); //register the new Frame in the DKV, so h2o jobs can find it.

    logger.info("Converted Table to Frame with "+frame.numRows()+" rows and "+frame.numCols()+" cols");

    return frame;
}