Java 如何将多个输入格式文件传递到map reduce作业?

Java 如何将多个输入格式文件传递到map reduce作业?,java,hadoop,mapreduce,cassandra,Java,Hadoop,Mapreduce,Cassandra,我正在编写MapReduce程序来查询cassandra列族。我只需要从一个列族中读取行的子集(使用行键)。我有一组我必须阅读的行的行键。如何将“row key set”传递给map reduce作业,以便它只能输出来自cassandra columnfamily的那些行子集 摘要: enter code here class GetRows() { public set<String> getRowKeys() { logic..... r

我正在编写MapReduce程序来查询cassandra列族。我只需要从一个列族中读取行的子集(使用行键)。我有一组我必须阅读的行的行键。如何将“row key set”传递给map reduce作业,以便它只能输出来自cassandra columnfamily的那些行子集

摘要:

enter code here

  class GetRows()
  {
   public set<String> getRowKeys()
   {
     logic.....
     return set<string>;
   }
  }


  class MapReduceCassandra()
  {
    inputformat---columnFamilyInputFormat
     .
     ;
    also need input key-set .. How to get it?
  } 
在此处输入代码
类GetRows()
{
公共集getRowKeys()
{
思维方式
返回集;
}
}
类MapReduceCassandra()
{
inputformat---columnFamilyInputFormat
.
;
还需要输入密钥集..如何获取?
} 

有谁能建议从java应用程序调用mapreduce的最佳方法以及如何将一组键传递给mapreduce吗?

从java调用mapreduce

为此,您可以在java应用程序中使用
org.apache.hadoop.mapreduce
命名空间中的类(您可以使用非常类似的方法使用较旧的
mapred
,只需查看API文档即可):

Job job = Job.getInstance(new Configuration());
// configure job: set input and output types and directories, etc.

job.setJarByClass(MapReduceCassandra.class);
job.submit();
将数据传递到mapreduce作业

如果行键集非常小,则可以将其序列化为字符串,并将其作为配置参数传递:

job.getConfiguration().set("CassandraRows", getRowsKeysSerialized()); // TODO: implement serializer

//...

job.submit();
nside作业您将能够通过上下文对象访问参数:

public void map(
    IntWritable key,  // your key type
    Text value,       // your value type
    Context context
)
{
    // ...

    String rowsSerialized = context.getConfiguration().get("CassandraRows");
    String[] rows = deserializeRows(rowsSerialized);  // TODO: implement deserializer

    //...
}
public void map(
    IntWritable key,  // your key type
    Text value,       // your value type
    Context context
)
{
    // ...

    URI[] cacheFiles = context.getCacheFiles();

    // find, open and read your file here

    // ...
}
但是,如果您的集合可能是无界的,那么将其作为参数传递将是一个坏主意。相反,您应该在文件中传递密钥,并利用分布式缓存。 然后,您可以在提交作业之前将此行添加到上面的部分:

job.addCacheFile(new Path(pathToCassandraKeySetFile).toUri());

//...

job.submit();
在作业内部,您可以通过上下文对象访问此文件:

public void map(
    IntWritable key,  // your key type
    Text value,       // your value type
    Context context
)
{
    // ...

    String rowsSerialized = context.getConfiguration().get("CassandraRows");
    String[] rows = deserializeRows(rowsSerialized);  // TODO: implement deserializer

    //...
}
public void map(
    IntWritable key,  // your key type
    Text value,       // your value type
    Context context
)
{
    // ...

    URI[] cacheFiles = context.getCacheFiles();

    // find, open and read your file here

    // ...
}
注意:所有这些都是针对新的API(
org.apache.hadoop.mapreduce
)。如果您使用的是
org.apache.hadoop.mapred
,方法非常相似,但是在不同的对象上调用了一些相关的方法