我正在尝试将HDFS数据移入MongoDB。我知道如何使用sqoop将数据导出到mysql中。我不认为我可以使用MongoDb的sqoop。我需要帮助了解如何做到这一点。将HDFS数据移入MongoDB
0
A
回答
0
基本问题是mongo以BSON格式(二进制JSON)存储其数据,而您的hdfs数据可能有不同的格式(txt,sequence,avro)。最容易做的事情是使用猪用这个驱动程序加载结果:
https://github.com/mongodb/mongo-hadoop/tree/master/pig
到蒙戈分贝。你必须将你的值映射到你的集合 - 在git hub页面上有一个很好的例子。
1
此配方将使用MongoOutputFormat类将HDFS实例 中的数据加载到MongoDB集合中。
Getting ready
最简单的方式开始使用Hadoop的蒙哥适配器是克隆从GitHub蒙戈 - Hadoop的 项目,并建立配置Hadoop的特定版本的项目。必须安装Git 客户端才能克隆此项目。 这个配方假设你使用的是Hadoop的CDH3发行版。 官方Git客户端可在http://git-scm.com/downloads找到。
Mongo Hadoop适配器可以在GitHub上找到https://github.com/mongodb/ mongo-hadoop。该项目需要为特定版本的Hadoop构建。生成的 JAR文件必须安装在$ HADOOP_HOME/lib文件夹中的每个节点上。 需要在$ HADOOP_HOME/ lib文件夹中的每个节点上安装Mongo Java驱动程序。它可以在https://github.com/mongodb/mongo-java-driver/ 下载找到。
怎么办呢?
Complete the following steps to copy data form HDFS into MongoDB:
1. Clone the mongo-hadoop repository with the following command line:
git clone https://github.com/mongodb/mongo-hadoop.git
2. Switch to the stable release 1.0 branch:
git checkout release-1.0
3. Set the Hadoop version which mongo-hadoop should target. In the folder
that mongo-hadoop was cloned to, open the build.sbt file with a text editor.
Change the following line:
hadoopRelease in ThisBuild := "default"
to
hadoopRelease in ThisBuild := "cdh3"
4. Build mongo-hadoop :
./sbt package
This will create a file named mongo-hadoop-core_cdh3u3-1.0.0.jar in the
core/target folder.
5. Download the MongoDB Java Driver Version 2.8.0 from https://github.com/
mongodb/mongo-java-driver/downloads .
6. Copy mongo-hadoop and the MongoDB Java Driver to $HADOOP_HOME/lib on
each node:
cp mongo-hadoop-core_cdh3u3-1.0.0.jar mongo-2.8.0.jar $HADOOP_
HOME/lib
7. Create a Java MapReduce program that will read the weblog_entries.txt file
from HDFS and write them to MongoDB using the MongoOutputFormat class:
import java.io.*;
import org.apache.commons.logging.*;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.*;
import org.bson.*;
import org.bson.types.ObjectId;
import com.mongodb.hadoop.*;
import com.mongodb.hadoop.util.*;
public class ExportToMongoDBFromHDFS {
private static final Log log = LogFactory.getLog(ExportToMongoDBFromHDFS.class);
public static class ReadWeblogs extends Mapper<LongWritable, Text, ObjectId, BSONObject>{
public void map(Text key, Text value, Context context)
throws IOException, InterruptedException{
System.out.println("Key: " + key);
System.out.println("Value: " + value);
String[] fields = value.toString().split("\t");
String md5 = fields[0];
String url = fields[1];
String date = fields[2];
String time = fields[3];
String ip = fields[4];
BSONObject b = new BasicBSONObject();
b.put("md5", md5);
b.put("url", url);
b.put("date", date);
b.put("time", time);
b.put("ip", ip);
context.write(new ObjectId(), b);
}
}
public static void main(String[] args) throws Exception{
final Configuration conf = new Configuration();
MongoConfigUtil.setOutputURI(conf,"mongodb://<HOST>:<PORT>/test. weblogs");
System.out.println("Configuration: " + conf);
final Job job = new Job(conf, "Export to Mongo");
Path in = new Path("/data/weblogs/weblog_entries.txt");
FileInputFormat.setInputPaths(job, in);
job.setJarByClass(ExportToMongoDBFromHDFS.class);
job.setMapperClass(ReadWeblogs.class);
job.setOutputKeyClass(ObjectId.class);
job.setOutputValueClass(BSONObject.class);
job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(MongoOutputFormat.class);
job.setNumReduceTasks(0);
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
8. Export as a runnable JAR file and run the job:
hadoop jar ExportToMongoDBFromHDFS.jar
9. Verify that the weblogs MongoDB collection was populated from the Mongo shell:
db.weblogs.find();
相关问题
- 1. 将数据从hdfs导出到mongodb
- 2. 将Sqoop数据从HDFS移动到Hive
- 3. 何时将数据移动到HDFS/Hive?
- 4. 使用Storm将数据移动到HDFS
- 5. Apache Hadoop:将压缩数据插入HDFS
- 6. 将hdfs数据导入hbase时出错
- 7. 如何将加密数据写入HDFS
- 8. 将数据从HDFS导入到Hive表
- 9. 将数据导入openshift mongoDb
- 10. 将数据写入MongoDB的
- 11. 使用Sqoop从MongoDB中提取数据以写入HDFS?
- 12. 导入HDFS数据到Opentsdb
- 13. 将数据从mongoDB复制到hdfs时出现hadoop jar错误
- 14. 将数据从PostgreSQL迁移到MongoDB
- 15. 将3mill的数据迁移到MongoDB
- 16. hadoop mongodb连接器 - 输出数据不是mongodb,但hdfs
- 17. 将移动事件数据提取到hdfs中
- 18. 如何将XML类型的oracle数据迁移到Hadoop HDFS
- 19. 使用copyFromLocal开关将数据移动到hdfs
- 20. 将群集数据从HDFS移动到Hive
- 21. 如何加载将PostgresSQL中的数据逐步迁移到HDFS?
- 22. 使用Sqoop将数据从MS-SQL服务器转移到HDFS
- 23. 风暴hdfs连接器...试图使用风暴将数据写入hdfs
- 24. 未将数据插入到MongoDB中
- 25. 将mongodb数据库导入到mongolab
- 26. 将发布数据插入到MongoDB中
- 27. 使用Crunch将数据写入MongoDB
- 28. 将数据从MySQL数据库迁移到MongoDB
- 29. 从HDFS导入数据到HBase(cdh3u2)
- 30. 在HDFS上写入会弄乱数据
能否请您发布的map-reduce代码从MongoDB中收集数据并将其存储在HDFS。 – MChirukuri 2015-07-14 11:23:16