Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将json对象插入数据湖_Python_Azure_Stream_Azure Data Lake - Fatal编程技术网

Python 将json对象插入数据湖

Python 将json对象插入数据湖,python,azure,stream,azure-data-lake,Python,Azure,Stream,Azure Data Lake,我几乎没有python api端点可以在请求体中获取数据。每次api调用任何想法时,我都想将此数据插入/添加到azure datalake 示例api端点 @main.route("/order/add", methods=["POST"]) def post_add_new_order(): data = request.json for key in data: if not typesModule.key_t

我几乎没有python api端点可以在请求体中获取数据。每次api调用任何想法时,我都想将此数据插入/添加到azure datalake

示例api端点

 @main.route("/order/add", methods=["POST"])
    def post_add_new_order():
    data = request.json
    for key in data:
        if not typesModule.key_type_and_value_type_are_equal(key, data[key]):
            return {"err": "One of the value types is incorrect"}

要将此数据插入azure data lake

如果要在python包中将数据添加到azure data lake存储Gen1,我们可以使用包
azure datalake存储
来实现它

比如说

  • 创建服务主体
  • 将服务主体分配给Azure Data Lake存储Gen1帐户文件或文件夹访问控制
  • Azure data lake gen1的ACL具有三个权限。有读取、写入和执行。请根据您的需要进行配置。有关更多详细信息,请参阅和

  • 代码

  • 如果它对你有用的话,你可以吗
    az login
    az ad sp create-for-rbac -n 'Myapp' --skip-assignment 
    
    import json
    
    import azure.datalake.store.lib as lib
    from azure.datalake.store.core import AzureDLFileSystem
    
    RESOURCE = 'https://datalake.azure.net/'
    client_id = '42e0d***c4c522d988c4'
    client_secret  = 'Gbx2eK6****ClJDfQpIjoae:'
    tenant = 'e4c9ab4e-bd27-40d5-8459-230ba2a757fb'
    
     @main.route("/order/add", methods=["POST"])
        def post_add_new_order():
            data = request.get_json()
            json_data = json.dumps(data).encode('utf-8')
            adlCreds = lib.auth(tenant_id = tenant,
                    client_secret = client_secret,
                    client_id = client_id,
                    resource=RESOURCE)
            
            adlsFileSystemClient = AzureDLFileSystem(adlCreds, store_name='testbowman')
            # check if the file exist 
            if adlsFileSystemClient.access('/test/data.json'):
                #append content
                with adlsFileSystemClient.open(path='/test/data.json', mode='ab') as f:
                    f.write(json_data)
                    f.write(b'\r\n')
            else:
                #create file and write
                with adlsFileSystemClient.open(path='/test/data.json', mode='wb') as f:
                    f.write(json_data)
                    f.write(b'\r\n')
            return {'you sent' : data}