要读取的文件为:/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文件的实现代码的更多相关文章

  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. linux percona-toolkit的安装

    percona-toolkit是一套linux下的工具集,我们需要使用到 pt-query-digest 对mysql慢日志查询来做分析. 1.下载 http://www.percona.com/do ...

  2. Window["aaa"]这个在JS里是什么意思?

    答案:定义一个全局的变量 aaa,这个的方式是数组,实际上是等于 window.aaa

  3. HDU 5763 Another Meaning

    HDU 5763 Another Meaning 题意:一个字串有可能在模式串出现多次,问有多少种可能出现的情况.关键是有重合的字串是不能同时计入的. 思路:先用kmp求出所有字串的位置.然后,dp. ...

  4. mybatis使用

    mybatis网站:http://mybatis.github.io/spring/zh/ mybatis spring下载网址:https://github.com/mybatis/spring/r ...

  5. 基于TCP的通信 客户端

    #include <WINSOCK2.H> #include <stdio.h> // socket 套接字 #pragma comment (lib,"Ws2_32 ...

  6. Android窃取用户信息新思路

    0×01 我们能得到哪些android手机上的app敏感信息手机上的app敏感信息◦通讯录,通讯记录,短信◦各种app的帐号密码,输入信息资料等◦各种影音资料,照片资料◦等等0×02  我们有哪些方法 ...

  7. python错误集锦

    1.lt与list等同,不能作为变量名 2.中文路径名:os.path.normcase(filepath) 如果遇到 ascii码在路径某处不能转换, 那么 filepath.encode('gbk ...

  8. HDU 3567 Eight II 打表,康托展开,bfs,g++提交可过c++不可过 难度:3

    http://acm.hdu.edu.cn/showproblem.php?pid=3567 相比Eight,似乎只是把目标状态由确定的改成不确定的,但是康托展开+曼哈顿为h值的A*和IDA*都不过, ...

  9. ubuntu 14.04 安装截图工具 Shutter及使用

    一.安装截图工具 Shutter 1. 添加安装包软件源 sudo add-apt-repository ppa:shutter/ppa 1 2. 更新源并安装 shutter sudo apt-ge ...

  10. JavaScript数字精度上代码。

    /**不能超过 9007199254740992 * floatObj 包含加减乘除四个方法,能确保浮点数运算不丢失精度 * * 我们知道计算机编程语言里浮点数计算会存在精度丢失问题(或称舍入误差), ...