简体   繁体   English

如何使用 Databricks Notebook 从 Key:Value Pair 中提取值

[英]How to pull out value from Key:Value Pair with Databricks Notebook

The following code recieves information from Service Bus and prints the information.以下代码从服务总线接收信息并打印信息。

def myfunc():
    with ServiceBusClient.from_connection_string(CONNECTION_STR) as client:
        # max_wait_time specifies how long the receiver should wait with no incoming messages before stopping receipt.
        # Default is None; to receive forever.

          with client.get_queue_receiver(QUEUE_NAME, session_id=session_id, max_wait_time=3600) as receiver:
            for msg in receiver:
                # print("Received: " + str(msg))
                themsg = json.loads(str(msg))
                # complete the message so that the message is removed from the queue
                receiver.complete_message(msg)
                return themsg

I then assign a variable to the function as follows:然后我将一个变量分配给 function,如下所示:

result = myfunc()

When I enter the following code当我输入以下代码时

result['mappings']

I get the following output:我得到以下 output:

Out[45]: [{'ELLIPSE': {'method': 'ellipseItem',
   'tables': [{'database': 'foundation',
     'schema': 'AZ_FH_ELLIPSE',
     'table': 'ADS_FND_MSF620',
     'primaryKey': [{'column': 'DSTRCT_CODE', 'seqno': 1},
      {'column': 'WORK_ORDER', 'seqno': 2}]}],
   'columns': [{'column': 'D_WORK_ORDER_KEY',

I am now trying to extract 'AZ_FH_ELLIPSE' and 'ADS_FND_MSF620'我现在正在尝试提取“AZ_FH_ELLIPSE”和“ADS_FND_MSF620”

My attempt is as follows:我的尝试如下:

result['mappings']['table']

But I get the Typeerror:但是我得到了类型错误:

TypeError: list indices must be integers or slices, not str

I'm aware this is simply piece of python code I should know, but any thoughts welcomed我知道这只是我应该知道的 python 代码的一部分,但欢迎任何想法

The reason you're getting this error is because list always expects index with integers.您收到此错误的原因是因为 list 总是需要整数索引。 Coming to the error - This error occurs because you have tried to access the dictionary using the key, when the dictionary is inside of a list.出现错误 - 发生此错误是因为当字典位于列表内部时,您尝试使用键访问字典。 If you follow closely then key 'table' is within the dictionary 'ELLIPSE' and it exists at the first index position of the parent list.如果您仔细观察,那么键“table”在字典“ELLIPSE”中,并且它存在于父列表的第一个索引 position 中。

Thereby, try accessing the dictionary keys as below -因此,尝试访问字典键如下 -

result['mappings'][0]['ELLIPSE']['tables'][0]['schema']

Similarly,相似地,

result['mappings'][0]['ELLIPSE']['tables'][0]['table']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 Databricks 中的 JSON 或字典或键值对格式创建 Apache Spark DataFrame - How to Create an Apache Spark DataFrame from JSON or Dictonary or Key Value pairs format in Databricks 如何在redshift中解析带有键值对的字符串 - How to parse string with key value pair inside in redshift 删除 firestore 字段中的键值对? - Delete key value pair in firestore field? 如何删除 firestore 上的键值对? (不只是价值或关键两者) - how to delete key-value pair on firestore? (not just value or key both of them) 从节点js中的箭头函数输出的数组/json输出的键值对的返回值 - Returning value of key value pair from array/json output from an arrow function in node js 将新的键值对添加到现有的 Firebase - Add new key value pair to existing Firebase 我如何从 Azure Devops 自动化 Databricks 笔记本 - How do i automate Databricks notebook from Azure Devops 我们如何比较terraform中对应的变量和select的右键、值对 - How do we compare variable and select right key, value pair accordingly in terraform 如何从firebase中的值中获取key - How to get the key from the value in firebase 如何为 json 查询中的每个键值对创建单独的列 - How to create a separate column for each key-value pair in json query
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM