【问题标题】:How to move all datastore Blobs to blobstore with GAE/J?如何使用 GAE/J 将所有数据存储 Blob 移动到 Blobstore?
【发布时间】:2012-04-07 23:38:57
【问题描述】:

我使用 Google Appengine Datastore Blob 来存储图像,但我想改用 Blobstore。我需要将所有现有的 blob 移动到 Blobstore。 使用 GAE for Java 的最佳方法是什么?

【问题讨论】:

    标签: java google-app-engine google-cloud-datastore blobstore


    【解决方案1】:

    AFAIK 没有自动的方法。

    您需要:

    1. 查询所有现有实体(如果有很多实体,请使用cursors),
    2. write the blob data 到 Blobstore
    3. 更新实体:删除现有的 blob 属性并添加带有 blobkey 的新属性。

    【讨论】:

    • 使用 mapreduce 会是更好的方法。
    【解决方案2】:

    我按照 Nick Johnson 的建议使用 MapReduce 解决了这个问题:

    检查项目:

    http://code.google.com/p/appengine-mapreduce/

    Ikai Lan 的精彩教程:

    http://ikaisays.com/2010/07/09/using-the-java-mapper-framework-for-app-engine/

    这就是 map 方法的样子:

    @Override
    public void map(Key key, Entity value, Context context) {
        log.info("Mapping key: " + key);
    
        try {
    
            Entity picture = value;
    
            if (!picture.hasProperty("blobKey") || picture.getProperty("blobKey") == null) {
                String fileName = (String) picture.getProperty("fileName");
                String contentType = (String) picture.getProperty("contentType");       
                Blob image = (Blob) picture.getProperty("image");       
    
                if (contentType != null && image != null) {
    
                    AppEngineFile file = fileService.createNewBlobFile(contentType,fileName);         
                    FileWriteChannel writeChannel = fileService.openWriteChannel(file, true);         
                    writeChannel.write(ByteBuffer.wrap(image.getBytes()));
                    writeChannel.closeFinally();
    
                    BlobKey blobKey = fileService.getBlobKey(file);     
    
                    picture.setProperty("blobKey",blobKey);
                    datastore.put(picture);
                }
                else {
                    log.log(Level.SEVERE, "Mapping key: " + key + "failed - null contentType or image.");
                }
    
            }
        } catch (Exception e) {
            log.log(Level.SEVERE, "Mapping key: " + key, e);
    
        }
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 2015-10-14
      • 2016-10-16
      • 1970-01-01
      • 1970-01-01
      • 2013-03-21
      • 2021-06-22
      • 2020-07-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多