在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文件的数据不一致的情况,进而使展示到界面上的数据在进行搜索时不能搜索出来.解决办法: ...
随机推荐
- shell之两个文档找出相同的之后在选
for i in `cat t1` ; do echo "$i" | awk '{sub(/^ */,"");sub(/ *$/,"")}1 ...
- PHP+mysql常用类库
<?php /** * @title: Ekcms mysql类库 * @version: 1.0 * @author: perry <perry@1kyou.com> * @pub ...
- sql取年月日
Sql Server 中一个非常强大的日期格式化函数 Select CONVERT(varchar(100), GETDATE(), 0): 05 16 2006 10:57AM Select CON ...
- [css3]搜索框focus时变长
结构: <form class="demo-a"> <input placeholder="Search" type="sea ...
- 获取css style值
var obj=document.getElementById("btn");var currentStyle=null;if(obj.currentStyle){ current ...
- RM报表里的变量
// RMVariables['JEDT']:= InvoiceJeDx('123.55', 1); 这个是整个程序的全局变量 // RMReport2.Dictionary.Variables['J ...
- struts中获取域
在struts的Action中,有三种方法可以得到request.session.servletContext域. 1.通过ServletActionContext类获取对象 HttpServletR ...
- 使用eclipse创建在myeclipse中运行的web工程
今天在跟随慕课网学习java时,遇到课程中老师使用Myeclipse,我用的是eclipse,那么就使用eclipse创建在Myeclipse项目 参考: 如何在Eclipse配置Tomcat服务器 ...
- powershell加win的dns服务器,解决网站负载均衡问题
用我发明的powershell填坑法,加windows的dns服务器.从调整dns服务器解析ip时间段的角度,解决网站负载均衡问题. ------------------------win2012r2 ...
- Linux内核-链表
linux内核链表的定义(定义了双向链表,不含数据域) 定义在 /linux-source-3.13.0/include/linux/types.h 头文件中. struct list_head { ...