如何使用shell脚本解析Rackspace的大数据API响应 [英] How to parse rackspace big data api response using shell scripting

查看:189
本文介绍了如何使用shell脚本解析Rackspace的大数据API响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出一个集群已设置后,如果是积极和使用shell脚本命名节点的IP地址。下面是一个使用这里列出的shell命令时,该API输出的响应:的http://docs.rackspace.com/cbd/api/v1.0/cbd-getting-started/content/viewing_Details.html.

I am trying to find out after a cluster has been provisioned, if it is active and the IP address of the named node using shell scripting. Below is the response of the API output when using the shell commands listed here: http://docs.rackspace.com/cbd/api/v1.0/cbd-getting-started/content/viewing_Details.html.

+----------+--------------------------------------+
| Property | Value |
+----------+--------------------------------------+
| Id | 4820deb2-6212-44f9-b92f-979fe723ffb8 |
| Name | foo |
| Status | ACTIVE |
| Nodes | 3 |
| Type | HADOOP_HDP2_1 |
| Flavor | hadoop1-7 |
+----------+--------------------------------------+

--------------+
| Id | Name | Role | Status | Public IP | Private IP |
+--------------------------------------+--------------+----------+--------+----------------+----------------+
| f530a9f1-79a8-4378-bf2a-b7f7e0c2bdd3 | NAMENODE-1 | NAMENODE | ACTIVE | 166.78.132.85  | 10.190.240.88  |

我相信我能使用某种正则表达式或的sed / awk中的做到这​​一点。所以,只是为了澄清,我想提取当前状态和公共IP

I believe I can do this using some sort of regex or sed/awk. So just to clarify I would like to extract the current status and the public IP

感谢您在高级

推荐答案

为了处理您的数据,可能更容易将其转换的格式,它更容易阅读,对于这一点,使用此过滤器:

In order to process your data, it might be easier to convert it in a format which is easier to read, for this, use this filter:

sed -e '/^+[-+]*$/d;s/^| //;s/ |$//;s/ | /|/g'

这将您的第一个表格转换成

which will convert your first table into

Property|Value
Id|4820deb2-6212-44f9-b92f-979fe723ffb8
Name|foo
Status|ACTIVE
Nodes|3
Type|HADOOP_HDP2_1
Flavor|hadoop1-7

如果你愿意,你可以通过调节滤波器这个样子滴头:

If you wish, you can drop the header by adjusting the filter like this:

sed -e '1,3d;/^+[-+]*$/d;s/^| //;s/ |$//;s/ | /|/g'

您可以再与第二个过滤器一起使用能在任何你想要的方式进一步处理这样的数据:

You can then combine this with a second filter to process this data further in any way you want:

awk -F'|' '$1 == "Status" {print($2)}"

您可以通过包装这两个步骤进入功能提高脚本的可读性和可维护性,并在你的脚本结合这些功能:

You can improve the readability and maintainability of your script by wrapping these two steps into functions and combine these functions in your script:

# rackspace_canonize
#  Canonize output of racksapace report tools
rackspace_canonize()
{
  sed -e '1,3d;/^+[-+]*$/d;s/^| //;s/ |$//;s/ | /|/g'
}

# rackspace_extract_status
#  Extract the status field
rackspace_extract_status()
{
  awk -F'|' '$1 == "Status" {print($2)}'
}

# rackspace_extract_public_ip
#  Extract the public IP
rackspace_extract_public_ip()
{
  awk -F'|' '{print($5)}'
}

那么下面的组成为您提供状态:

Then the following composition gives you the status:

rackspace_canonize | rackspace_extract_status

和下面组合物中的公共IP

and the following composition the public IP:

rackspace_canonize | rackspace_extract_public_ip

请注意,您可以轻松地添加中介条件以缩小两 * *提取函数的输入。

Note that you can easily add intermediary filters to narrow down the input of the two *extract* functions.

这篇关于如何使用shell脚本解析Rackspace的大数据API响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆