You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Groovy解析DSDB返回键值对并提取指定值的技术需求

Solution

Let's break this down into two straightforward parts: parsing the DSDB output into a Groovy Map, then building dedicated functions to pull out the Owner Name and Environment values you need.

1. Parse DSDB Output into a Map

First, we'll run the shell command to fetch the DSDB response, then process each line to build our key-value map. This handles empty values (like the Comments entry in your sample) gracefully.

// Replace with your actual DSDB shell command
def dsdbOutput = "your-dsdb-command-here".execute().text

// Convert the newline-separated key-value pairs into a Map
def dsdbMap = [:]
dsdbOutput.splitEachLine(': ') { key, value ->
    // Clean up whitespace and handle empty values
    def cleanedKey = key.trim()
    dsdbMap[cleanedKey] = value?.trim() ?: ''
}

Quick breakdown:

  • splitEachLine(': ') splits each line exactly at the first : (space after colon) to avoid breaking values that might contain colons.
  • value?.trim() ?: '' ensures empty entries (like Comments:) are stored as empty strings instead of null, keeping our map consistent.

2. Helper Functions to Extract Target Values

Now we'll create two standalone functions that take our parsed map and return the specific values you're after.

/**
 * Pulls the Owner Name from the DSDB map
 * @param dsdbMap The parsed DSDB key-value map
 * @return Owner Name as a String (empty if the key isn't found)
 */
String getOwnerName(Map dsdbMap) {
    return dsdbMap['Owner Name'] ?: ''
}

/**
 * Pulls the Environment from the DSDB map
 * @param dsdbMap The parsed DSDB key-value map
 * @return Environment as a String (empty if the key isn't found)
 */
String getEnvironment(Map dsdbMap) {
    return dsdbMap['Environment'] ?: ''
}

How to use it:

// Grab the values using our functions
def owner = getOwnerName(dsdbMap)
def env = getEnvironment(dsdbMap)

// Example output
println "Owner Name: $owner" // Prints: Owner Name: name1
println "Environment: $env"  // Prints: Environment: dev

This setup is flexible too—if your DSDB output ever adds more fields, the map will automatically include them, and you can add similar helper functions for other values in no time.

内容的提问来源于stack exchange,提问作者wintechdev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 06:30:21