我使用hadoop处理一个xml文件,所以我用python编写了mapper文件,reducer文件。在
假设需要处理的输入是测试.xml
映射器.py文件
^{pr2}$
减速器.py文件import sys
if __name__ == ‘__main__’:
for line in sys.stdin:
print line.strip()
我用以下命令运行hadoopbin/hadoop jar contrib/streaming/hadoop-streaming-1.0.4.jar
- file /path/to/mapper.py file -mapper /path/to/mapper.py file
-file /path/to/reducer.py file -reducer /path/to/reducer.py file
-input /path/to/input_file/test.xml
-output /path/to/output_folder/to/store/file
当我运行上面的命令时,hadoop正在输出路径处创建一个输出文件,其格式是我们在reducer.py文件中提到的格式,其中包含必需的数据
现在,我要做的是,我不想在运行上述命令时haddop默认创建的文本文件中存储输出数据,而是希望将数据保存到MYSQL数据库中
因此,我在reducer.py文件中编写了一些python代码,直接将数据写入MYSQL数据库,并尝试通过删除输出路径来运行上面的命令,如下所示bin/hadoop jar contrib/streaming/hadoop-streaming-1.0.4.jar
- file /path/to/mapper.py file -mapper /path/to/mapper.py file
-file /path/to/reducer.py file -reducer /path/to/reducer.py file
-input /path/to/input_file/test.xml
我得到的错误如下所示12/11/08 15:20:49 ERROR streaming.StreamJob: Missing required option: output
Usage: $HADOOP_HOME/bin/hadoop jar
$HADOOP_HOME/hadoop-streaming.jar [options]
Options:
-input DFS input file(s) for the Map step
-output DFS output directory for the Reduce step
-mapper The streaming command to run
-combiner The streaming command to run
-reducer The streaming command to run
-file File/dir to be shipped in the Job jar file
-inputformat TextInputFormat(default)|SequenceFileAsTextInputFormat|JavaClassName Optional.
-outputformat TextOutputFormat(default)|JavaClassName Optional.
…
…毕竟,我的疑问是,在处理完文件之后,如何将数据保存在Database中?在
在哪个文件中(mapper.py/减速器.py?)我们能把数据写入数据库的代码吗
哪个命令用于运行hadoop以将数据保存到数据库中,因为当我删除hadoop命令中的输出文件夹路径时,它显示了一个错误。在
谁能帮我解决上面的问题。。。。。。。。。。。。。在
已编辑
后续处理如上所述创建了mapper和{}文件,它们读取xml文件并通过hadoop command在某个文件夹中创建一个文本文件
例如:文本文件(使用hadoop命令处理xml文件的结果)所在的文件夹
/home/local/user/Hadoop/xml_processing/xml_output/part-00000
这里的xml文件大小是1.3 GB,使用hadoop处理后,text file创建的大小是345 MB
现在我要做的就是尽快reading the text file in the above path and saving data to the mysql database。在
我已经用basicpython尝试过这个方法,但is正在使用一些350 sec来处理文本文件并保存到mysql数据库中。在现在如nichole所示,下载了sqoop并在下面的某个路径上解压缩
/home/local/user/sqoop-1.4.2.bin_yuhadoop-0.20
然后进入bin文件夹,输入./sqoop,我收到了下面的错误sh-4.2$ ./sqoop
Warning: /usr/lib/hbase does not exist! HBase imports will fail.
Please set $HBASE_HOME to the root of your HBase installation.
Warning: $HADOOP_HOME is deprecated.
Try ‘sqoop help’ for usage.
下面我也试过了./sqoop export --connect jdbc:mysql://localhost/Xml_Data --username root --table PerformaceReport --export-dir /home/local/user/Hadoop/xml_processing/xml_output/part-00000 --input-fields-terminated-by ’ ’
结果Warning: /usr/lib/hbase does not exist! HBase imports will fail.
Please set $HBASE_HOME to the root of your HBase installation.
Warning: $HADOOP_HOME is deprecated.
12/11/27 11:54:57 INFO manager.MySQLManager: Preparing to use a MySQL streaming resultset.
12/11/27 11:54:57 INFO tool.CodeGenTool: Beginning code generation
12/11/27 11:54:57 ERROR sqoop.Sqoop: Got exception running Sqoop: java.lang.RuntimeException: Could not load db driver class: com.mysql.jdbc.Driver
java.lang.RuntimeException: Could not load db driver class: com.mysql.jdbc.Driver
at org.apache.sqoop.manager.SqlManager.makeConnection(SqlManager.java:636)
at org.apache.sqoop.manager.GenericJdbcManager.getConnection(GenericJdbcManager.java:52)
at org.apache.sqoop.manager.SqlManager.execute(SqlManager.java:525)
at org.apache.sqoop.manager.SqlManager.execute(SqlManager.java:548)
at org.apache.sqoop.manager.SqlManager.getColumnTypesForRawQuery(SqlManager.java:191)
at org.apache.sqoop.manager.SqlManager.getColumnTypes(SqlManager.java:175)
at org.apache.sqoop.manager.ConnManager.getColumnTypes(ConnManager.java:262)
at org.apache.sqoop.orm.ClassWriter.getColumnTypes(ClassWriter.java:1235)
at org.apache.sqoop.orm.ClassWriter.generate(ClassWriter.java:1060)
at org.apache.sqoop.tool.CodeGenTool.generateORM(CodeGenTool.java:82)
at org.apache.sqoop.tool.ExportTool.exportTable(ExportTool.java:64)
at org.apache.sqoop.tool.ExportTool.run(ExportTool.java:97)
at org.apache.sqoop.Sqoop.run(Sqoop.java:145)
at org.apache.hadoop.util.ToolRunner.run(ToolRunner.java:65)
at org.apache.sqoop.Sqoop.runSqoop(Sqoop.java:181)
at org.apache.sqoop.Sqoop.runTool(Sqoop.java:220)
at org.apache.sqoop.Sqoop.runTool(Sqoop.java:229)
at org.apache.sqoop.Sqoop.main(Sqoop.java:238)
at com.cloudera.sqoop.Sqoop.main(Sqoop.java:57)
上面的sqoop命令对于读取文本文件并保存到数据库中的功能是否有用?,因为我们必须从文本文件进行处理并插入到数据库中!!!!在