

# 列出向量
列出向量

可以使用 [ListVectors](https://docs.amazonaws.cn/AmazonS3/latest/API/API_S3VectorBuckets_ListVectors.html) API 操作列出向量索引中的向量。有关每页可以返回的最大向量数的更多信息，请参阅[限制和局限性](s3-vectors-limitations.md)。当结果被截断时，响应中包含一个分页标记。有关 `ListVectors` 的响应元素的更多信息，请参阅《Amazon S3 API Reference》**中的 [ListVectors](https://docs.amazonaws.cn/AmazonS3/latest/API/API_S3VectorBuckets_ListVectors)。也可以使用 `ListVectors` 从指定的向量索引导出向量数据。`ListVectors` 为强一致性。执行 WRITE 操作后，可以立即列出反映了所有变化的向量。

## 使用 Amazon CLI


要列出向量，请使用以下示例命令。将*用户输入占位符*替换为您自己的信息。

`segment-count` 和 `segment-index` 参数支持您跨多个并行请求对列出操作进行分区。指定 `segment-count` 值（例如 `2`）时，可以将索引划分为许多分段。`segment-index` 参数（从 0 开始）决定要列出哪个分段。这种方法通过启用并行处理来协助提高列出大型向量索引时的性能。有关 `segment-count` 和 `segment-index` 的更多信息，请参阅《Amazon S3 API Reference》**中的 [ListVectors](https://docs.amazonaws.cn/AmazonS3/latest/API/API_S3VectorBuckets_ListVectors)。

**列出索引中的所有向量**

请求示例：

```
aws s3vectors list-vectors \
  --vector-bucket-name "amzn-s3-demo-vector-bucket" \
  --index-name "idx" \
  --segment-count 2 \
  --segment-index 0 \
  --return-data \
  --return-metadata
```

响应示例：

```
{
    "vectors": [
        {
            "key": "vec3",
            "data": {
                "float32": [0.4000000059604645]
            },
            "metadata": {
                "nonFilterableKey": "val4",
                "filterableKey": "val2"
            }
        }
    ]
}
```

**列出带分页的向量**

请求示例：

```
aws s3vectors list-vectors \
  --vector-bucket-name "amzn-s3-demo-vector-bucket" \
  --index-name "idx" \
  --segment-count 2 \
  --segment-index 0 \
  --return-data \
  --return-metadata \
  --next-token "zWfh7e57H2jBfBtRRmC7OfMwl209G9dg3j2qM6kM4t0rps6ClYzJykgMOil9eGqU5nhf_gTq53IfoUdTnsg"
```

响应示例：

```
{
    "vectors": [
        {
            "key": "vec1",
            "data": {
                "float32": [0.5]
            },
            "metadata": {
                "nonFilterableKey": "val2",
                "filterableKey": "val1"
            }
        }
    ]
}
```

## 使用 Amazon SDK


------
#### [ SDK for Python ]

示例：列出向量索引中的向量

```
import boto3

# Create a S3 Vectors client in the AWS Region of your choice. 
s3vectors = boto3.client("s3vectors", region_name="us-west-2")

#List vectors in your vector index 

response = s3vectors.list_vectors( 
    vectorBucketName="media-embeddings",
    indexName="movies",
    maxResults = 600,
    returnData = True,
    returnMetadata = True
)

vectors = response["vectors"]

print(vectors)
```

示例：并行列出向量索引中的所有向量

```
import boto3

# Create a S3 Vectors client in the AWS Region of your choice. 
s3vectors = boto3.client("s3vectors", region_name="us-west-2")

#List vectors in the 1st half of vectors in the index.
response = s3vectors.list_vectors( 
    vectorBucketName="media-embeddings",
    indexName="movies",
    segmentCount=2,
    segmentIndex=1,
    maxResults = 600,
    returnData = True,
    returnMetadata = True
)

vectors = response["vectors"]

#List vectors starting from the 2nd half of vectors in the index.
# This can be ran in parallel with the first `list_vectors` call.
response = s3vectors.list_vectors( 
    vectorBucketName="media-embeddings",
    indexName="movies",
    segmentCount=2,
    segmentIndex=1,
    maxResults = 600,
    returnData = True,
    returnMetadata = True
)

vectors = response["vectors"]

print(vectors)
```

------