0

我创建了一个自定义 Magic 命令,旨在以编程方式生成 spark 查询。这是我的类中实现 MagicCommandFunctionality 的相关部分:

MagicCommandOutcomeItem execute(MagicCommandExecutionParam magicCommandExecutionParam) {    

    // get the string that was entered:
    String input = magicCommandExecutionParam.command.substring(MAGIC.length())

    // use the input to generate a query
    String generatedQuery =  Interpreter.interpret(input)

    MIMEContainer result = Text(generatedQuery);
    return new MagicCommandOutput(MagicCommandOutcomeItem.Status.OK, result.getData().toString());
}

这非常有效。它返回我生成的命令。(作为文本)

我的问题是——我如何强制笔记本评估单元格中的值? 我的猜测是涉及 SimpleEvaluationObject 和 TryResult,但我找不到任何使用它们的示例

我可能希望内核为我创建一个,而不是创建 MagicCommandOutput。我看到 KernelMagicCommand 有一个执行方法可以做到这一点。有人有想法么?

4

1 回答 1

0

好的,我找到了一种方法。这是我的解决方案:
您可以向当前的 kernelManager 询问您感兴趣的内核,
然后调用 PythonEntryPoint.evaluate。它似乎完成了这项工作!

@Override MagicCommandOutcomeItem 执行(MagicCommandExecutionParam magicCommandExecutionParam){

String input = magicCommandExecutionParam.command.substring(MAGIC.length() + 1)
// this is the Scala code I want to evaluate:
String codeToExecute = <your code here>

KernelFunctionality kernel = KernelManager.get()
PythonEntryPoint pep = kernel.getPythonEntryPoint(SCALA_KERNEL)
pep.evaluate(codeToExecute)
pep.getShellMsg()

List<Message> messages = new ArrayList<>()
//until there are messages on iopub channel available collect them into response
    while (true) {
        String iopubMsg = pep.getIopubMsg()
        if (iopubMsg == "null") break
        try {
            Message msg = parseMessage(iopubMsg) //(I didn't show this part)
            messages.add(msg)
            String commId = (String) msg.getContent().get("comm_id")
            if (commId != null) {
                kernel.addCommIdManagerMapping(commId, SCALA_KERNEL)
            }
        } catch (IOException e) {
            log.error("There was an error: ${e.getMessage()}")
            return new MagicKernelResponse(MagicCommandOutcomeItem.Status.ERROR, messages)
        }
    }
    return new MagicKernelResponse(MagicCommandOutcomeItem.Status.OK, messages)
}
于 2018-07-31T20:33:54.743 回答