2016-09-07 7 views
0

SnapshotCreateTimeが最大のスナップショットのSnapshotIdentifierを取得し、それをClusterIdentifierでフィルタリングしたいとします。ここでjson docのチェーン選択とmax_byとbashのjq

aws redshift describe-cluster-snapshots --region us-west-2 | 
    jq -r '.Snapshots[] 
     | select(.ClusterIdentifier == "dev-cluster") 
     | max_by(.SnapshotCreateTime) 
     | .SnapshotIdentifier ' 

JSON

{ 
    "Snapshots": [   
     { 
       "EstimatedSecondsToCompletion": 0, 
       "OwnerAccount": "45645641155", 
       "CurrentBackupRateInMegaBytesPerSecond": 6.2857, 
       "ActualIncrementalBackupSizeInMegaBytes": 22.0, 
       "NumberOfNodes": 3, 
       "Status": "available", 
       "VpcId": "myvpc", 
       "ClusterVersion": "1.0", 
       "Tags": [], 
       "MasterUsername": "ayxbizops", 
       "TotalBackupSizeInMegaBytes": 192959.0, 
       "DBName": "dev", 
       "BackupProgressInMegaBytes": 22.0, 
       "ClusterCreateTime": "2016-09-06T15:56:08.170Z", 
       "RestorableNodeTypes": [ 
        "dc1.large" 
       ], 
       "EncryptedWithHSM": false, 
       "ClusterIdentifier": "dev-cluster", 
       "SnapshotCreateTime": "2016-09-06T16:00:25.595Z", 
       "AvailabilityZone": "us-west-2c", 
       "NodeType": "dc1.large", 
       "Encrypted": false, 
       "ElapsedTimeInSeconds": 3, 
       "SnapshotType": "manual", 
       "Port": 5439, 
       "SnapshotIdentifier": "thismorning" 
     } 
    ] 
} 

答えて

2
  1. max_byされた入力配列として期待:ここでは私が使用しているコマンドがあります。したがって、あなたのフィルタの以下のバリアントは動作します:あなたの口頭の説明に

    [.Snapshots[] | select(.ClusterIdentifier == "dev-cluster")] 
    | max_by(.SnapshotCreateTime) 
    | .SnapshotIdentifier 
    
  2. をベースに、あなたが実行したいと思われるmax_byselect前:

    .Snapshots 
    | max_by(.SnapshotCreateTime) 
    | select(.ClusterIdentifier == "dev-cluster") 
    | .SnapshotIdentifier 
    
  3. 可能性が複数ある場合最大のオブジェクト、あなたはmax_byではなくmaximal_byを使用することがあります:

    def maximal_by(f): 
        (map(f) | max) as $mx 
        | .[] | select(f == $mx); 
    
+0

おかげさまで、最初の解決策が私に役立ちました。 @JeffMercado、もう一度私を助けてくれてありがとう! – RagePwn

関連する問題