gpt4 book ai didi

java - 如何将多个输入格式文件传递给 map-reduce 作业?

转载 作者:行者123 更新时间:2023-12-02 21:49:57 25 4
gpt4 key购买 nike

我正在编写 map-reduce 程序来查询 cassandra 列族。我只需要从一个列族中读取行的子集(使用行键)。我有一组我必须阅读的行键。如何将“行键集”传递给 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?
}

任何人都可以建议从java应用程序调用mapreduce的最佳方法以及如何将一组键传递给mapreduce?

最佳答案

从 Java 调用 map reduce

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

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();

在作业中,您将能够通过上下文对象访问参数:
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

//...
}

但是,如果您的集合可能是无限的,则将其作为参数传递将是一个坏主意。相反,您应该在文件中传递 key ,并利用分布式缓存。
然后,您可以在提交作业之前将此行添加到上面的部分:
job.addCacheFile(new Path(pathToCassandraKeySetFile).toUri());

//...

job.submit();

在作业中,您将能够通过上下文对象访问此文件:
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该方法非常相似,但在不同的对象上调用了一些相关方法。

关于java - 如何将多个输入格式文件传递给 map-reduce 作业?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21897568/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com