在hdfs上存取xml文件的实现代码
要读取的文件为:/user/hdfs/stdin.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <request>
- <jobinstanceid>SK9cohJD4yklcD8dJuZXDA</jobinstanceid>
- <context>
- <property name="userName" value="xdf"/>
- <property name="queueName" value="queue1"/>
- <property name="processId" value="dns"/>
- <property name="jobId" value="jobID"/>
- <property name="hiveServerAddress" value="IP:port "/>
- <property name="databaseName" value="wx"/>
- <property name="basePath" value="HDFS_BasePath1/20141216/jobinstanceid/${operator.name}"/>
- </context>
- <operator name="convert" alias="lowerUpperCaseConvert" class="lowerUpperCaseConvert">
- <parameterlist name="fields">
- <parametermap fieldname="name" fieldvalue="m_uuid()" fieldtype="String"/>
- </parameterlist>
- </operator>
- <datasets>
- <dataset name="inport1">
- <row>default.test1</row>
- </dataset>
- </datasets>
- </request>
要存的文件为:/user/hdfs/stdin.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <response>
- <jobinstanceid>SK9cohJD4yklcD8dJuZXDA</jobinstanceid>
- <datasets>
- <dataset name="outport1">
- <row>default.tmp_e93eba2c_f22d_4dc1_9e86_a342a0ea0625</row>
- </dataset>
- </datasets>
- <operatortracker>
- <portcounter name="inport1" dataCount="4"/>
- <portcounter name="outport1" dataCount="4"/>
- </operatortracker>
- </response>
读stdin.xml文件的实现如下:
- public List<Map> parseStdinXml(String xmlParams) throws Exception {
- String userName = null;
- String operatorName = null;
- String dbName = null;
- String inputTabName = null;
- String strs = null;
- String fieldName = null;
- String fieldType = null;
- String jobinstanceid = null;
- int fieldCount = 0;
- List<Map> list = new ArrayList<Map>();
- Map<String, String> map = new HashMap<String, String>();
- Document document = DocumentHelper.parseText(xmlParams); // 将字符串转化为xml
- Element node1 = document.getRootElement(); // 获得根节点
- Iterator iter1 = node1.elementIterator(); // 获取根节点下的子节点
- while (iter1.hasNext()) {
- Element node2 = (Element) iter1.next();
- // 获取jobinstanceid
- if ("jobinstanceid".equals(node2.getName())) {
- jobinstanceid = node2.getText();
- map.put("jobinstanceid", jobinstanceid);
- }
- // 获取通用参数
- if ("context".equals(node2.getName())) {
- Iterator iter2 = node2.elementIterator();
- while (iter2.hasNext()) {
- Element node3 = (Element) iter2.next();
- if ("property".equals(node3.getName())) {
- if ("userName".equals(node3.attributeValue("name"))) {
- userName = node3.attributeValue("value");
- }
- }
- map.put("userName", userName);
- }
- }
- // 获取算子参数
- if ("operator".equals(node2.getName())) {
- operatorName = node2.attributeValue("name");
- map.put("operatorName", operatorName);
- Iterator iter2 = node2.elementIterator();
- while (iter2.hasNext()) {
- Element node3 = (Element) iter2.next();
- if ("parameterlist".equals(node3.getName())) {
- if ("fields".equals(node3.attributeValue("name"))) {
- Iterator iter3 = node3.elementIterator();
- while (iter3.hasNext()) {
- Element node4 = (Element) iter3.next();
- if ("parametermap".equals(node4.getName())) {
- fieldName = node4
- .attributeValue("fieldname");
- fieldType = node4
- .attributeValue("fieldtype");
- fieldCount++;
- map.put("fieldName" + fieldCount, fieldName);
- map.put("fieldType" + fieldCount, fieldType);
- }
- }
- }
- }
- }
- map.put("fieldCount", Integer.toString(fieldCount));
- }
- // 获取输入数据库
- if ("datasets".equals(node2.getName())) {
- Iterator iter2 = node2.elementIterator();
- while (iter2.hasNext()) {
- Element node3 = (Element) iter2.next();
- if ("inport1".equals(node3.attributeValue("name"))) {
- Iterator iter3 = node3.elementIterator();
- while (iter3.hasNext()) {
- Element node4 = (Element) iter3.next();
- strs = node4.getText();
- }
- }
- if (!"".equals(strs.trim())) {
- String[] arr = strs.split("\\.");
- dbName = arr[0];
- inputTabName = arr[1];
- }
- map.put("dbName", dbName);
- map.put("inputTabName", inputTabName);
- }
- }
- }
- list.add(map);
- return list;
- }
存stdout.xml文件的实现如下:
- public void genStdoutXml(String fileName, List<Map> listOut) {
- String jobinstance = null;
- String dbName = null;
- String outputTable = null;
- String outputDataCount = null;
- String inputDataCount = null;
- dbName = listOut.get(0).get("dbName").toString();
- jobinstance = listOut.get(0).get("jobinstanceid").toString();
- outputTable = listOut.get(0).get("outputTable").toString();
- inputDataCount = listOut.get(0).get("inputDataCount").toString();
- outputDataCount = listOut.get(0).get("outputDataCount").toString();
- Document document = DocumentHelper.createDocument();
- Element response = document.addElement("response");
- Element jobinstanceid = response.addElement("jobinstanceid");
- jobinstanceid.setText(jobinstance);
- Element datasets = response.addElement("datasets");
- Element dataset = datasets.addElement("dataset");
- dataset.addAttribute("name", "outport1");
- Element row = dataset.addElement("row");
- row.setText(dbName + "." + outputTable);
- Element operatortracker = response.addElement("operatortracker");
- Element portcounter1 = operatortracker.addElement("portcounter");
- portcounter1.addAttribute("name", "inport1");
- portcounter1.addAttribute("dataCount", inputDataCount);
- Element portcounter2 = operatortracker.addElement("portcounter");
- portcounter2.addAttribute("name", "outport1");
- portcounter2.addAttribute("dataCount", outputDataCount);
- try {
- Configuration conf = new Configuration();
- FileSystem fs = FileSystem.get(URI.create(fileName), conf);
- OutputStream out = fs.create(new Path(fileName),
- new Progressable() {
- public void progress() {
- }
- });
- OutputFormat format = OutputFormat.createPrettyPrint();
- format.setEncoding("UTF-8");
- XMLWriter xmlWriter = new XMLWriter(out, format);
- xmlWriter.write(document);
- xmlWriter.close();
- } catch (IOException e) {
- System.out.println(e.getMessage());
- }
- }
在hdfs上存取xml文件的实现代码的更多相关文章
- Java读写hdfs上的avro文件
1.通过Java往hdfs写avro文件 import java.io.File; import java.io.IOException; import java.io.OutputStream; i ...
- Delphi调用JAVA的WebService上传XML文件(XE10.2+WIN764)
相关资料:1.http://blog.csdn.net/luojianfeng/article/details/512198902.http://blog.csdn.net/avsuper/artic ...
- hadoop(十)hdfs上传删除文件(完全分布式七)|12
集群测试 上传小文件到集群,随便选择一个小文件上传到hdfs的根目录 [shaozhiqi@hadoop102 hadoop-3.1.2]$ bin/hdfs dfs -put wcinput/wc. ...
- python读取hdfs上的parquet文件方式
在使用python做大数据和机器学习处理过程中,首先需要读取hdfs数据,对于常用格式数据一般比较容易读取,parquet略微特殊.从hdfs上使用python获取parquet格式数据的方法(当然也 ...
- 使用XML文件和Java代码控制UI界面
Android推荐使用XML文件设置UI界面,然后用Java代码控制逻辑部分,这体现了MVC思想. MVC全名是Model View Controller,是模型(model)-视图(view)-控制 ...
- Android color(颜色) 在XML文件和java代码中
Android color(颜色) 在XML文件和java代码中,有需要的朋友可以参考下. 1.使用Color类的常量,如: int color = Color.BLUE;//创建一个蓝色 是使用An ...
- XML文件生成C++代码(基于rapidxml)
简述 与XML文件生成C++代码(基于pugixml)中的功能一致,只是这里改用的rapidxml来实现.就不多说了,直接放代码. 代码 #include "rapidxml-1.13/ra ...
- 使用SAXReader读取ftp服务器上的xml文件(原创)
根据项目需求,需要监测ftp服务器上的文件变化情况,并将新添加的文件读入项目系统(不需要下载). spring配置定时任务就不多说了,需要注意的一点就是,现在的项目很多都是通过maven构建的,分好多 ...
- 上传XML文件字符编码问题
1.上传的XML文件的空格的字符编码和倒入到数据库的空格的字符编码不是一种编码格式,导致导入到数据库的数据和XML文件的数据不一致的情况,进而使展示到界面上的数据在进行搜索时不能搜索出来.解决办法: ...
随机推荐
- BZOJ3928 [Cerc2014] Outer space invaders
第一眼,我勒个去...然后看到n ≤ 300的时候就2333了 首先把时间离散化,则对于一个时间的区间,可以知道中间最大的那个一定要被选出来,然后把区间分成左右两份 于是区间DP就好了,注意用左开右开 ...
- SQL语句大全(mysql,sqlserver,oracle)
SQL语句大全 --语句功能--数据操作SELECT --从数据库表中检索数据行和列-selectINSERT --向数据库表添加新数据行-insertDELETE --从数据库表中删除数据行-del ...
- 项目中Enum枚举的使用
在.NET中,枚举一般有两种常见用法,一是表示唯一的元素序列,比如表示订单状态(未提交,待处理,处理中...).另外一种是表示多种组合的状态,比如表示权限,因为可同时有多个不同权限. 基本用法 这里拿 ...
- HtmlHelper—DropDownList:SelectList、SelectListItem
前言 在项目中经常使用到DropDownList来显示数据库中的数据,典型的例子为为某书籍选择所属类型. 使用SelectList来实现: 实现一: Controller 代码 SelectList ...
- API 进程、线程函数
CancelWaitableTimer 这个函数用于取消一个可以等待下去的计时器操作 CallNamedPipe 这个函数由一个希望通过管道通信的一个客户进程调用 ConnectNamedPipe 指 ...
- 菜鸟开始学习SSDT HOOK((附带源码)
看了梦无极的ssdt_hook教程,虽然大牛讲得很细,但是很多细节还是要自己去体会,才会更加深入.在这里我总结一下我的分析过程,若有不对的地方,希望大家指出来.首先我们应该认识 ssdt是什么?从梦无 ...
- IPTables系列:如何配置Ubuntu 14.04中的IPTables防火墙
IPTables基本命令 在向大家介绍复杂防火墙规则之前,还是先上一些简单的料,让大家对IPTables最为基本的命令有一些简单了解. 首先要说明的是IPTables命令必需以root权限运行,这意味 ...
- 理解ROS rqt_console和 roslaunch
1.使用rqt_console和roslaunch 这篇教程将介绍使用rqt_console和rqt_logger_level来调试以及使用roslaunch一次启动许多nodes.如果你使用ROS ...
- MicroPython开发板TPYBoard关于USB-HID的应用
USB-HID是Human Interface Device的缩写,属于人机交互操作的设备,如USB鼠标,USB键盘,USB游戏操纵杆,USB触摸板,USB轨迹球.电话拨号设备.VCR遥控等等设备. ...
- php中的include()的使用技巧
php中的include()的使用技巧 include() 语句包括并运行指定文件. 以下文档也适用于 require().这两种结构除了在如何处理失败之外完全一样.include() 产生一个警告而 ...