【问题标题】:Error: undefined method "each" for String when running elastic-mapreduce specifying distributed cache file错误:运行 elastic-mapreduce 指定分布式缓存文件时,String 的未定义方法“each”
【发布时间】:2014-11-11 01:03:25
【问题描述】:

我遇到以下错误:

Error: undefined method `each' for "s3n://dico-count-words/Cache/dicoClazz.p#dicoClazzCache.p":String

当我运行以下命令行通过 elastic-mapreduce 在 Amazon EMR 集群上启动 mapreduce 算法时,指定一个分布式缓存文件:

    ./elastic-mapreduce --create --stream \
> --input s3n://dico-count-words/Words \
> --output s3n://dico-count-words/Output \
> --mapper s3n://dico-count-words/Code/mapper.py \
> --reducer s3n://dico-count-words/Code/reducer.py \
> --log-uri s3n://dico-count-words/Logs \
> --cache s3n://dico-count-words/Cache/dicoClazz.p#dicoClazz.p

我按照我找到的here 的说明进行操作。 我在运行类似命令来创建不需要分布式缓存文件的其他集群时没有任何问题。我还设法使用 AWS 控制台运行了这项工作。但我更愿意通过 CLI 来完成。 我认为这可能是类似于this one 的红宝石问题。但我对红宝石一无所知,所以这只是一个猜测。这也是我第一次使用 AWS 和 elastic-mapreduce。 供您参考,这是我拥有的 ruby​​ 版本:

ruby 2.0.0p451 (2014-02-24 revision 45167) [universal.x86_64-darwin13]

您对错误的来源有任何想法吗?有什么建议可以解决吗?

非常感谢。

【问题讨论】:

    标签: ruby amazon-web-services elastic-map-reduce


    【解决方案1】:

    each 不适用于 ruby​​ String 类。

    作为一个例子,让我们看看以下内容:


    x = "test"
    x.each {|character| puts character}
    >>>NoMethodError: undefined method `each' for "test":String
    

    这是您在代码中看到的,这是意料之中的。在终端中打开 irb 并尝试以下操作:

    2.0.0-p247 :001 > x = "test"
    2.0.0-p247 :002 > x.each <now hit tab twice>
    
    
    2.0.0-p247 :003 > x.each_
    x.each_byte       x.each_char       x.each_codepoint  x.each_line
    

    您应该会看到与上述类似的内容。我碰巧使用的是 ruby​​ 2.0.0-p247。您的版本可能不同。

    irb 支持自动完成。在这里,我们开始输入每个,irb 正在根据以each 开头的可用选项提出建议。如您所见,简单的each 实际上不是一个选项。相反,将字符串视为字符数组。那就是"test" 可以认为是["t", "e", "s", "t"]。鉴于这种表示,很明显x.each_char 会做什么——它将产生字符串中的每个字符。因此:

    x.each_char {|c| puts c}
    

    当然会打印字符串的每个字符。

    如果我们可以看到一些代码和数据在其中移动,那么为您推荐解决方案会更容易。但是,上述解释是您在 String 上调用 .each 时引发错误的原因。

    我希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-11
      • 2013-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-08
      相关资源
      最近更新 更多