用java api读取HDFS文件
import java.io.IOException;
import java.io.InputStream;
import java.security.PrivilegedExceptionAction;
import java.text.SimpleDateFormat;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.FsStatus;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.security.UserGroupInformation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import bean.TableStatistic; @Controller
@RequestMapping("/dfview")
public class DataFrameViewController extends BaseController { private ConcurrentMap<String, UserGroupInformation> cache = new ConcurrentHashMap<String, UserGroupInformation>();
private ConcurrentMap<String, FileSystem> fileSystemCache = new ConcurrentHashMap<String, FileSystem>();
private Configuration hadoopConf = new Configuration();
private static final String HDFS_JSON_NAME = "jsonObj"; @RequestMapping(value = "/getDFviewOfColumn", method = { RequestMethod.GET })
@ResponseBody
public TableStatistic getDFviewOfTable(String tableName)
throws Exception {
String user = "bi";
String dirpath = "/user/cbt/datax/temp_transfer/zzzdes";
Path homePath = new Path(dirpath);
FileSystem fs = this.createFileSystem(user);
FileStatus[] stats = fs.listStatus(homePath);
StringBuffer txtContent = new StringBuffer();
for (int i = 0; i < stats.length; ++i) {
if (stats[i].isFile()) {
FileStatus file = stats[i];
if( HDFS_JSON_NAME.equalsIgnoreCase(file.getPath().getName())){
InputStream in = fs.open(file.getPath());
byte[] b = new byte[1];
while (in.read(b) != -1)
{
// 字符串拼接
txtContent.append(new String(b));
}
in.close();
break;
}
}
}
TableStatistic ts = JSON.parseObject(txtContent.toString(), TableStatistic.class);
return ts;
} public static void main(String[] args) throws Exception {
DataFrameViewController aaa = new DataFrameViewController();
FileSystem fs = aaa.createFileSystem("bi");
Path homePath = new Path("/user/cbt/datax/temp_transfer/zzzdes");
System.out.println("***********************************");
FileStatus[] stats = fs.listStatus(homePath);
for (int i = 0; i < stats.length; ++i) {
if (stats[i].isFile()) {
FileStatus file = stats[i];
StringBuffer txtContent = new StringBuffer();
if( "jsonObj".equalsIgnoreCase(file.getPath().getName())){
InputStream in = fs.open(file.getPath());
byte[] b = new byte[1];
while (in.read(b) != -1)
{
// 字符串拼接
txtContent.append(new String(b));
}
// IOUtils.copyBytes(fs.open(file.getPath()), System.out, 4096,false);
in.close();
// fs.close();
}
System.out.print(txtContent.toString());
System.out
.println("************************************************");
JSONObject jb = JSON.parseObject(txtContent.toString());
System.out.println("********!!!!! : " + jb.get("colUnique"));
TableStatistic ts = JSON.parseObject(txtContent.toString(), TableStatistic.class);
System.out.println("********!!!!! : " + ts.getColUnique().toString()); } else if (stats[i].isDirectory()) {
System.out.println(stats[i].getPath().toString());
} else if (stats[i].isSymlink()) {
System.out.println("&&&&&&&&" + stats[i].getPath().toString());
} }
FsStatus fsStatus = fs.getStatus(homePath);
} public FileSystem createFileSystem(String user) throws Exception {
final Configuration conf = loadHadoopConf();
conf.set("hadoop.job.ugi", user);
// conf.set("HADOOP_USER_NAME", user);
if (fileSystemCache.get(user) != null) {
return fileSystemCache.get(user);
}
UserGroupInformation ugi = getProxyUser(user);
FileSystem fs = ugi.doAs(new PrivilegedExceptionAction<FileSystem>() {
public FileSystem run() throws Exception {
return FileSystem.get(conf);
}
});
fileSystemCache.put(user, fs);
return fs;
} public static final ThreadLocal<SimpleDateFormat> appDateFormat = new ThreadLocal<SimpleDateFormat>() {
@Override
public SimpleDateFormat initialValue() {
SimpleDateFormat dateformat = new java.text.SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
return dateformat;
}
}; private static final String[] HADOOP_CONF_FILES = { "core-site.xml",
"hdfs-site.xml" }; private Configuration loadHadoopConf() {
if (hadoopConf != null) {
return hadoopConf;
}
Configuration conf = new Configuration();
for (String fileName : HADOOP_CONF_FILES) {
try {
InputStream inputStream = DataFrameViewController.class
.getClassLoader().getResourceAsStream(fileName);
conf.addResource(inputStream);
} catch (Exception ex) {
}
}
return conf;
} public void destroy() {
for (UserGroupInformation ugi : cache.values()) {
try {
FileSystem.closeAllForUGI(ugi);
} catch (IOException ioe) {
// Logger.error("Exception occurred while closing filesystems for "
// + ugi.getUserName(), ioe);
}
}
cache.clear();
} private UserGroupInformation getProxyUser(String user) throws IOException {
cache.putIfAbsent(user, UserGroupInformation.createRemoteUser(user));
return cache.get(user);
}
}
用java api读取HDFS文件的更多相关文章
- java Api 读取HDFS文件内容
package dao; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.*; import java ...
- 使用JAVA API读取HDFS的文件数据出现乱码的解决方案
使用JAVA api读取HDFS文件乱码踩坑 想写一个读取HFDS上的部分文件数据做预览的接口,根据网上的博客实现后,发现有时读取信息会出现乱码,例如读取一个csv时,字符串之间被逗号分割 英文字符串 ...
- JAVA API 实现hdfs文件操作
java api 实现hdfs 文件操作会出现错误提示: Permission denied: user=hp, access=WRITE, inode="/":hdfs:supe ...
- Java API 读取HDFS的单文件
HDFS上的单文件: -bash-3.2$ hadoop fs -ls /user/pms/ouyangyewei/data/input/combineorder/repeat_rec_categor ...
- Spark:java api读取hdfs目录下多个文件
需求: 由于一个大文件,在spark中加载性能比较差.于是把一个大文件拆分为多个小文件后上传到hdfs,然而在spark2.2下如何加载某个目录下多个文件呢? public class SparkJo ...
- 使用java api操作HDFS文件
实现的代码如下: import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import ...
- 记录一次读取hdfs文件时出现的问题java.net.ConnectException: Connection refused
公司的hadoop集群是之前的同事搭建的,我(小白一个)在spark shell中读取hdfs上的文件时,执行以下指令 >>> word=sc.textFile("hdfs ...
- Spark读取HDFS文件,文件格式为GB2312,转换为UTF-8
package iie.udps.example.operator.spark; import scala.Tuple2; import org.apache.hadoop.conf.Configur ...
- 使用Java API操作HDFS文件系统
使用Junit封装HFDS import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.*; import org ...
随机推荐
- Java Web开发之详解JSP
JSP作为Java Web开发中比较重要的技术,一般当作视图(View)的技术所使用,即用来展现页面.Servlet由于其本身不适合作为表现层技术,所以一般被当作控制器(Controller)所使用, ...
- 遍历 TextBox控件
foreach (System.Windows.Forms.Control control in this.Controls) { if (control is System.Windows.Form ...
- evaluateScript--evaluatePopoverScript--区别
appcan.window.evaluateScript({}) //window.open()页面之间使用 appcan.window.evaluatePopoverScr ...
- Custome Buble Data Point
<navigation:Page xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/to ...
- 洛谷 P1541 乌龟棋
题目背景 小明过生日的时候,爸爸送给他一副乌龟棋当作礼物. 题目描述 乌龟棋的棋盘是一行N个格子,每个格子上一个分数(非负整数).棋盘第1格是唯一的起点,第N格是终点,游戏要求玩家控制一个乌龟棋子从起 ...
- 微软Hololens学院教程- Holograms 101: Introduction with Device【微软教程已经更新,本文是老版本】
这是老版本的教程,为了不耽误大家的时间,请直接看原文,本文仅供参考哦!原文链接:https://developer.microsoft.com/EN-US/WINDOWS/HOLOGRAPHIC/ho ...
- input标签文字点击变颜色
<input type="text" class="ser_input"value="从这里搜索(^_^)" onfocus=&quo ...
- IIS虚拟目录实现与文件服务器网络驱动器映射共享
这篇文章转载别人,想原创作者致敬! 我本人也遇到同样的问题,故转载记录. 本文重点描述如何使用IIS访问共享资源来架设站点或执行 ASP.Net 等脚本. 通常情况下,拥有多台服务器的朋友在使用IIS ...
- MyEclipse中文乱码,编码格式设置,文件编码格式 总结
一.设置新建常见文件的默认编码格式,也就是文件保存的格式.在不对MyEclipse进行设置的时候,默认保存文件的编码,一般跟简体中文操作系统(如windows2000,windowsXP)的编码一致, ...
- 自定义NavigationView's item 的高度
http://stackoverflow.com/questions/31204320/how-can-i-change-the-navigationviews-item-text-size 自定义s ...