我有一个装满了上千份文件的桶。我如何搜索水桶?


当前回答

我也面临同样的问题。在S3中进行搜索应该比目前的情况容易得多。这就是为什么我在S3中实现了这个用于搜索的开源工具。

search是完全开源的S3搜索工具。它的实现始终牢记性能是关键因素,并根据基准测试在几秒钟内搜索包含~1000个文件的桶。

安装很简单。你只需要下载docker-compose文件并运行它

docker-compose up

搜索将开始,你可以在任何桶搜索任何东西。

其他回答

AWS发布了使用SQL: Amazon Athena https://aws.amazon.com/athena/查询S3桶的新服务

考虑到你在AWS…我认为你会想要使用他们的CloudSearch工具。把你想要搜索的数据放到他们的服务中…让它指向S3密钥。

http://aws.amazon.com/cloudsearch/

(至少)有两个不同的用例可以描述为“搜索桶”:

Search for something inside every object stored at the bucket; this assumes a common format for all the objects in that bucket (say, text files), etc etc. For something like this, you're forced to do what Cody Caughlan just answered. The AWS S3 docs has example code showing how to do this with the AWS SDK for Java: Listing Keys Using the AWS SDK for Java (there you'll also find PHP and C# examples). List item Search for something in the object keys contained in that bucket; S3 does have partial support for this, in the form of allowing prefix exact matches + collapsing matches after a delimiter. This is explained in more detail at the AWS S3 Developer Guide. This allows, for example, to implement "folders" through using as object keys something like folder/subfolder/file.txt If you follow this convention, most of the S3 GUIs (such as the AWS Console) will show you a folder view of your bucket.

在S3控制台中按前缀搜索

直接在AWS Console桶视图中。

使用s3-dist-cp复制需要的文件

当您有数千或数百万个文件时,另一种获取所需文件的方法是使用分布式复制将它们复制到另一个位置。您可以在Hadoop作业中的EMR上运行此操作。AWS很酷的一点是,他们提供了自定义S3版本S3 -dist-cp。它允许您在groupBy字段中使用正则表达式对需要的文件进行分组。例如,您可以在EMR的自定义步骤中使用它

[
    {
        "ActionOnFailure": "CONTINUE",
        "Args": [
            "s3-dist-cp",
            "--s3Endpoint=s3.amazonaws.com",
            "--src=s3://mybucket/",
            "--dest=s3://mytarget-bucket/",
            "--groupBy=MY_PATTERN",
            "--targetSize=1000"
        ],
        "Jar": "command-runner.jar",
        "Name": "S3DistCp Step Aggregate Results",
        "Type": "CUSTOM_JAR"
    }
]

我做了如下的事情来在我的桶中寻找模式

def getListOfPrefixesFromS3(dataPath: String, prefix: String, delimiter: String, batchSize: Integer): List[String] = {
    var s3Client = new AmazonS3Client()
    var listObjectsRequest = new ListObjectsRequest().withBucketName(dataPath).withMaxKeys(batchSize).withPrefix(prefix).withDelimiter(delimiter)
    var objectListing: ObjectListing = null
    var res: List[String] = List()

    do {
      objectListing = s3Client.listObjects(listObjectsRequest)
      res = res ++ objectListing.getCommonPrefixes
      listObjectsRequest.setMarker(objectListing.getNextMarker)
    } while (objectListing.isTruncated)
    res
  }

对于较大的存储桶,这会消耗太多时间,因为所有对象摘要都由Aws返回,而不仅仅是匹配前缀和分隔符的对象。我正在寻找提高性能的方法,到目前为止,我只发现我应该命名键,并将它们正确地组织在桶中。