要读取的文件为:/user/hdfs/stdin.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <request>
  3. <jobinstanceid>SK9cohJD4yklcD8dJuZXDA</jobinstanceid>
  4. <context>
  5. <property name="userName" value="xdf"/>
  6. <property name="queueName" value="queue1"/>
  7. <property name="processId" value="dns"/>
  8. <property name="jobId" value="jobID"/>
  9. <property name="hiveServerAddress" value="IP:port "/>
  10. <property name="databaseName" value="wx"/>
  11. <property name="basePath" value="HDFS_BasePath1/20141216/jobinstanceid/${operator.name}"/>
  12. </context>
  13.  
  14. <operator name="convert" alias="lowerUpperCaseConvert" class="lowerUpperCaseConvert">
  15. <parameterlist name="fields">
  16. <parametermap fieldname="name" fieldvalue="m_uuid()" fieldtype="String"/>
  17. </parameterlist>
  18. </operator>
  19. <datasets>
  20. <dataset name="inport1">
  21. <row>default.test1</row>
  22. </dataset>
  23. </datasets>
  24. </request>

要存的文件为:/user/hdfs/stdin.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2.  
  3. <response>
  4. <jobinstanceid>SK9cohJD4yklcD8dJuZXDA</jobinstanceid>
  5. <datasets>
  6. <dataset name="outport1">
  7. <row>default.tmp_e93eba2c_f22d_4dc1_9e86_a342a0ea0625</row>
  8. </dataset>
  9. </datasets>
  10. <operatortracker>
  11. <portcounter name="inport1" dataCount="4"/>
  12. <portcounter name="outport1" dataCount="4"/>
  13. </operatortracker>
  14. </response>

读stdin.xml文件的实现如下:

  1. public List<Map> parseStdinXml(String xmlParams) throws Exception {
  2.  
  3. String userName = null;
  4. String operatorName = null;
  5. String dbName = null;
  6. String inputTabName = null;
  7. String strs = null;
  8. String fieldName = null;
  9. String fieldType = null;
  10. String jobinstanceid = null;
  11. int fieldCount = 0;
  12.  
  13. List<Map> list = new ArrayList<Map>();
  14. Map<String, String> map = new HashMap<String, String>();
  15. Document document = DocumentHelper.parseText(xmlParams); // 将字符串转化为xml
  16. Element node1 = document.getRootElement(); // 获得根节点
  17. Iterator iter1 = node1.elementIterator(); // 获取根节点下的子节点
  18. while (iter1.hasNext()) {
  19. Element node2 = (Element) iter1.next();
  20.  
  21. // 获取jobinstanceid
  22. if ("jobinstanceid".equals(node2.getName())) {
  23. jobinstanceid = node2.getText();
  24. map.put("jobinstanceid", jobinstanceid);
  25. }
  26. // 获取通用参数
  27. if ("context".equals(node2.getName())) {
  28. Iterator iter2 = node2.elementIterator();
  29. while (iter2.hasNext()) {
  30. Element node3 = (Element) iter2.next();
  31. if ("property".equals(node3.getName())) {
  32. if ("userName".equals(node3.attributeValue("name"))) {
  33. userName = node3.attributeValue("value");
  34. }
  35. }
  36. map.put("userName", userName);
  37. }
  38. }
  39.  
  40. // 获取算子参数
  41. if ("operator".equals(node2.getName())) {
  42. operatorName = node2.attributeValue("name");
  43. map.put("operatorName", operatorName);
  44. Iterator iter2 = node2.elementIterator();
  45. while (iter2.hasNext()) {
  46. Element node3 = (Element) iter2.next();
  47. if ("parameterlist".equals(node3.getName())) {
  48. if ("fields".equals(node3.attributeValue("name"))) {
  49. Iterator iter3 = node3.elementIterator();
  50. while (iter3.hasNext()) {
  51. Element node4 = (Element) iter3.next();
  52. if ("parametermap".equals(node4.getName())) {
  53. fieldName = node4
  54. .attributeValue("fieldname");
  55. fieldType = node4
  56. .attributeValue("fieldtype");
  57. fieldCount++;
  58. map.put("fieldName" + fieldCount, fieldName);
  59. map.put("fieldType" + fieldCount, fieldType);
  60. }
  61. }
  62. }
  63. }
  64. }
  65. map.put("fieldCount", Integer.toString(fieldCount));
  66. }
  67. // 获取输入数据库
  68. if ("datasets".equals(node2.getName())) {
  69. Iterator iter2 = node2.elementIterator();
  70. while (iter2.hasNext()) {
  71. Element node3 = (Element) iter2.next();
  72. if ("inport1".equals(node3.attributeValue("name"))) {
  73. Iterator iter3 = node3.elementIterator();
  74. while (iter3.hasNext()) {
  75. Element node4 = (Element) iter3.next();
  76. strs = node4.getText();
  77. }
  78. }
  79. if (!"".equals(strs.trim())) {
  80. String[] arr = strs.split("\\.");
  81. dbName = arr[0];
  82. inputTabName = arr[1];
  83. }
  84. map.put("dbName", dbName);
  85. map.put("inputTabName", inputTabName);
  86. }
  87. }
  88. }
  89. list.add(map);
  90. return list;
  91. }

存stdout.xml文件的实现如下:

  1. public void genStdoutXml(String fileName, List<Map> listOut) {
  2.  
  3. String jobinstance = null;
  4. String dbName = null;
  5. String outputTable = null;
  6. String outputDataCount = null;
  7. String inputDataCount = null;
  8.  
  9. dbName = listOut.get(0).get("dbName").toString();
  10. jobinstance = listOut.get(0).get("jobinstanceid").toString();
  11. outputTable = listOut.get(0).get("outputTable").toString();
  12. inputDataCount = listOut.get(0).get("inputDataCount").toString();
  13. outputDataCount = listOut.get(0).get("outputDataCount").toString();
  14.  
  15. Document document = DocumentHelper.createDocument();
  16. Element response = document.addElement("response");
  17. Element jobinstanceid = response.addElement("jobinstanceid");
  18. jobinstanceid.setText(jobinstance);
  19. Element datasets = response.addElement("datasets");
  20. Element dataset = datasets.addElement("dataset");
  21. dataset.addAttribute("name", "outport1");
  22. Element row = dataset.addElement("row");
  23. row.setText(dbName + "." + outputTable);
  24. Element operatortracker = response.addElement("operatortracker");
  25. Element portcounter1 = operatortracker.addElement("portcounter");
  26. portcounter1.addAttribute("name", "inport1");
  27. portcounter1.addAttribute("dataCount", inputDataCount);
  28. Element portcounter2 = operatortracker.addElement("portcounter");
  29. portcounter2.addAttribute("name", "outport1");
  30. portcounter2.addAttribute("dataCount", outputDataCount);
  31.  
  32. try {
  33. Configuration conf = new Configuration();
  34. FileSystem fs = FileSystem.get(URI.create(fileName), conf);
  35. OutputStream out = fs.create(new Path(fileName),
  36. new Progressable() {
  37. public void progress() {
  38. }
  39. });
  40. OutputFormat format = OutputFormat.createPrettyPrint();
  41. format.setEncoding("UTF-8");
  42. XMLWriter xmlWriter = new XMLWriter(out, format);
  43. xmlWriter.write(document);
  44. xmlWriter.close();
  45. } catch (IOException e) {
  46. System.out.println(e.getMessage());
  47. }
  48.  
  49. }

在hdfs上存取xml文件的实现代码的更多相关文章

  1. Java读写hdfs上的avro文件

    1.通过Java往hdfs写avro文件 import java.io.File; import java.io.IOException; import java.io.OutputStream; i ...

  2. Delphi调用JAVA的WebService上传XML文件(XE10.2+WIN764)

    相关资料:1.http://blog.csdn.net/luojianfeng/article/details/512198902.http://blog.csdn.net/avsuper/artic ...

  3. hadoop(十)hdfs上传删除文件(完全分布式七)|12

    集群测试 上传小文件到集群,随便选择一个小文件上传到hdfs的根目录 [shaozhiqi@hadoop102 hadoop-3.1.2]$ bin/hdfs dfs -put wcinput/wc. ...

  4. python读取hdfs上的parquet文件方式

    在使用python做大数据和机器学习处理过程中,首先需要读取hdfs数据,对于常用格式数据一般比较容易读取,parquet略微特殊.从hdfs上使用python获取parquet格式数据的方法(当然也 ...

  5. 使用XML文件和Java代码控制UI界面

    Android推荐使用XML文件设置UI界面,然后用Java代码控制逻辑部分,这体现了MVC思想. MVC全名是Model View Controller,是模型(model)-视图(view)-控制 ...

  6. Android color(颜色) 在XML文件和java代码中

    Android color(颜色) 在XML文件和java代码中,有需要的朋友可以参考下. 1.使用Color类的常量,如: int color = Color.BLUE;//创建一个蓝色 是使用An ...

  7. XML文件生成C++代码(基于rapidxml)

    简述 与XML文件生成C++代码(基于pugixml)中的功能一致,只是这里改用的rapidxml来实现.就不多说了,直接放代码. 代码 #include "rapidxml-1.13/ra ...

  8. 使用SAXReader读取ftp服务器上的xml文件(原创)

    根据项目需求,需要监测ftp服务器上的文件变化情况,并将新添加的文件读入项目系统(不需要下载). spring配置定时任务就不多说了,需要注意的一点就是,现在的项目很多都是通过maven构建的,分好多 ...

  9. 上传XML文件字符编码问题

    1.上传的XML文件的空格的字符编码和倒入到数据库的空格的字符编码不是一种编码格式,导致导入到数据库的数据和XML文件的数据不一致的情况,进而使展示到界面上的数据在进行搜索时不能搜索出来.解决办法: ...

随机推荐

  1. BZOJ3928 [Cerc2014] Outer space invaders

    第一眼,我勒个去...然后看到n ≤ 300的时候就2333了 首先把时间离散化,则对于一个时间的区间,可以知道中间最大的那个一定要被选出来,然后把区间分成左右两份 于是区间DP就好了,注意用左开右开 ...

  2. SQL语句大全(mysql,sqlserver,oracle)

    SQL语句大全 --语句功能--数据操作SELECT --从数据库表中检索数据行和列-selectINSERT --向数据库表添加新数据行-insertDELETE --从数据库表中删除数据行-del ...

  3. 项目中Enum枚举的使用

    在.NET中,枚举一般有两种常见用法,一是表示唯一的元素序列,比如表示订单状态(未提交,待处理,处理中...).另外一种是表示多种组合的状态,比如表示权限,因为可同时有多个不同权限. 基本用法 这里拿 ...

  4. HtmlHelper—DropDownList:SelectList、SelectListItem

    前言 在项目中经常使用到DropDownList来显示数据库中的数据,典型的例子为为某书籍选择所属类型. 使用SelectList来实现: 实现一: Controller 代码 SelectList ...

  5. API 进程、线程函数

    CancelWaitableTimer 这个函数用于取消一个可以等待下去的计时器操作 CallNamedPipe 这个函数由一个希望通过管道通信的一个客户进程调用 ConnectNamedPipe 指 ...

  6. 菜鸟开始学习SSDT HOOK((附带源码)

    看了梦无极的ssdt_hook教程,虽然大牛讲得很细,但是很多细节还是要自己去体会,才会更加深入.在这里我总结一下我的分析过程,若有不对的地方,希望大家指出来.首先我们应该认识 ssdt是什么?从梦无 ...

  7. IPTables系列:如何配置Ubuntu 14.04中的IPTables防火墙

    IPTables基本命令 在向大家介绍复杂防火墙规则之前,还是先上一些简单的料,让大家对IPTables最为基本的命令有一些简单了解. 首先要说明的是IPTables命令必需以root权限运行,这意味 ...

  8. 理解ROS rqt_console和 roslaunch

    1.使用rqt_console和roslaunch 这篇教程将介绍使用rqt_console和rqt_logger_level来调试以及使用roslaunch一次启动许多nodes.如果你使用ROS  ...

  9. MicroPython开发板TPYBoard关于USB-HID的应用

    USB-HID是Human Interface Device的缩写,属于人机交互操作的设备,如USB鼠标,USB键盘,USB游戏操纵杆,USB触摸板,USB轨迹球.电话拨号设备.VCR遥控等等设备. ...

  10. php中的include()的使用技巧

    php中的include()的使用技巧 include() 语句包括并运行指定文件. 以下文档也适用于 require().这两种结构除了在如何处理失败之外完全一样.include() 产生一个警告而 ...