image.png
image.png
case

    def test_dmc_dataflow_export_model(self):
        t1 = Table.from_lines([
            "utf8100100,value,汉字, str",
            "n1,1,一," + str(time.time()),
            "n2,0,二" + str(time.time()),
        ], 1)

        t1.wait_for_ok()
        df = Dataflow(dmc_request=1)
        base_node = df.create_base_node(t1)

        selectNode = df.create_select_node([base_node.key], status=0)
        selectNode.add_select_fields(["value"])
        df.add_node(selectNode)

        df.create_output_node([selectNode.key])
        model_name = 'model_import_or_export_' + str(time.time()).split('.')[0]
        df.save_dataflow(model_name)

        if os.path.exists(bdpconf.DOWNLOAD_DIR + model_name + '.model'):
            os.remove(bdpconf.DOWNLOAD_DIR + model_name + '.model')


        df.export_dataflow(df.dataflow_id)
        assert os.path.exists(bdpconf.DOWNLOAD_DIR + model_name + '.model')

        im_df = df.import_dataflow(bdpconf.DOWNLOAD_DIR + model_name + '.model')
        assert im_df["name"] == model_name
        im_df_id = im_df["dataflow_id"]

        # 清空数据
        df.delete()
        BdpSDK.instance().call_method_and_succ("delete_dataflow", im_df_id, 1)

调用

 def import_dataflow(self, file):
        return BdpSDK.instance().call_method_and_succ("import_dataflow", file, self.dmc_request, ret_expr='["result"]')

接口定义

    def import_dataflow(self, file, dmc_request=None):
        if hasattr(file, "read"):
            filename = file.filename
            fp = file
        else:
            filename = os.path.basename(file)
            fp = open(file)
        boundary = ''.join(random.sample('abcdefgABCDEFG1234567', 16))

        data = list()
        data.append("--%s" % boundary)
        data.append('Content-Disposition: form-data; name="dmc_request"\r\n')
        data.append(str(0 if dmc_request == None else dmc_request))
        data.append("--%s" % boundary)
        data.append('Content-Disposition: form-data; name="data"; filename="%s"' % filename)
        data.append('Content-Type: application/octet-stream\r\n')
        data.append(fp.read())
        fp.close()
        data.append("--%s--\r\n" % boundary)

        self.raw_data = self.http_request.post(self.build_url(BdpSDK.api_dataflow_import), '\r\n'.join(data),
                                               headers={
                                                   "Content-Type": 'multipart/form-data; boundary=%s' % boundary}).read()
 @dec_log()
    def export_dataflow(self, dataflow_id, dmc_request):
        req_param = {
            "dataflow_id": dataflow_id,
            "dmc_request": 0 if dmc_request == None else dmc_request
        }

        req_url = self.build_url(self.api_dataflow_export, req_param)
        raw_resp = self.http_request.get(req_url)

        content_len = int(raw_resp.headers["Content-Length"])
        content_disp = raw_resp.headers["Content-Disposition"]
        content_dict = {}
        for i in content_disp.split(';'):
            try:
                k, v = i.split('=')
            except:
                k, v = "", ""

            if k:
                content_dict[k.strip()] = v.strip().strip('"')

        local_file = content_dict["filename"]

        fp = open("%s/%s" % (bdpconf.DOWNLOAD_DIR, local_file), "wb")

        for i in raw_resp.iter_content(chunk_size=4096):
            fp.write(i)

        assert fp.tell() == content_len

        fp.close()

Feng
9 声望1 粉丝

渣级菜鸟


« 上一篇
docker入门
下一篇 »
selenium-js