java操作hdfs到数据库或者缓存
使用hadoop工具将数据分析出来以后,须要做入库处理或者存到缓存中。不然就没了意义
一下是使用javaAPI操作hdfs存入缓存的代码:
<span style="font-family:Microsoft YaHei;font-size:14px;">public class InterestToRedisJob { FileSystem hdfs = null; public InterestToRedisJob(){
init();
} private void init(){
Configuration conf = new Configuration();
conf.set("fs.default.name", "hdfs地址");
try {
hdfs = FileSystem.get(conf);
} catch (IOException e) {
e.printStackTrace();
}
} public void ReadFileToReids(String path) throws IOException{
Path paths = new Path(path);
FileStatus[] files = hdfs.listStatus(paths);</span>
<span style="font-family:Microsoft YaHei;font-size:14px;"><span style="white-space:pre"> </span>//这儿是自己实现的一个路径顾虑器,也可不适用,在【1】处直接推断part-r-等标示</span>
<span style="font-family:Microsoft YaHei;font-size:14px;"> PathFilter filter = new ResultNameFilter("part-r-");
Text line = new Text();
RedisClient redis = new RedisClient();
for(FileStatus file:files){
if(file.isDir() || !filter.accept(file.getPath())){//【1】
continue;
}else{
FSDataInputStream input = null;
try{
input = hdfs.open(file.getPath());
LineReader reader = new LineReader(input);
while(reader.readLine(line) > 0){
System.out.println(line);
String[] arr = line.toString().split("\t");</span>
<span style="font-family:Microsoft YaHei;font-size:14px;"><span style="white-space:pre"> </span>//做存入redis处理
redis.saveHsetValue(arr[0], "interest", arr[1]);
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(input != null){
input.close();
}
}
}
}
} public static void main(String[] args) {
InterestToRedisJob job = new InterestToRedisJob();
try {
job.ReadFileToReids("你的path");
} catch (IOException e) {
e.printStackTrace();
}
}
}</span>
上面代码须要改动后使用,请勿直接粘贴。
java操作hdfs到数据库或者缓存的更多相关文章
- hadoop集群配置和在windows系统上运用java操作hdfs
安装 配置 概念 hadoop常用shell命令 使用java操作hadoop 本文介绍hadoop集群配置和在windows系统上运用java操作hdfs 安装 http://mirror.bit. ...
- hadoop学习(三)HDFS常用命令以及java操作HDFS
一.HDFS的常用命令 1.查看根目录下的信息:./hadoop dfs -ls 2.查看根目录下的in目录中的内容:./hadoop dfs -ls in或者./hadoop dfs -ls ./i ...
- java操作hdfs实例
环境:window7+eclipse+vmware虚拟机+搭建好的hadoop环境(master.slave01.slave02) 内容:主要是在windows环境下,利用eclipse如何来操作hd ...
- 使用java操作HDFS
新建Java Project; 1,右击项目,属性,Java Build Path,Libraries,Add External JARs(haddopp根目录下的所以jar): 2,做一下项目关联, ...
- java 操作sqllite的数据库
介绍 sqllite是一个小型数据库,不依赖于数据库服务器,操作它可以像操作本地的文本文件一样.在Android中是用来存储数据到本地的,java中可能也会有用到sqllite需要. 详细 sqlli ...
- JAVA 操作 DBF 文件数据库
1.依赖夹包 javadbf-[].4.1.jar jconn3.jar 2.添加属性文件 jdbc.properties jdbc.driverClassName=com.sybase.jdbc3. ...
- hadoop3自学入门笔记(3)-java 操作hdfs
1.core-site.xml <configuration> <property> <name>fs.defaultFS</name> <val ...
- Java操作HDFS代码样例
代码在GitHub上. 包括如下几种样例代码: 新建文件夹 删除文件/文件夹 重命名文件/文件夹 查看指定路径下的所有文件 新建文件 读文件 写文件 下载文件至本地 上传本地文件 https://gi ...
- JAVA 操作远程mysql数据库实现单表增删改查操作
package MysqlTest; import java.sql.DriverManager; import java.sql.ResultSet; import com.mysql.jdbc.C ...
随机推荐
- ExtJS创建选项卡
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Java 复制大文件方式(nio2 FileChannel 拷贝文件能力测试)
目前为止,我们已经学习了很多 Java 拷贝文件的方式,除了 FileChannel 提供的方法外,还包括使用 Files.copy() 或使用字节数组的缓冲/非缓冲流.那个才是最好的选择呢?这个问题 ...
- Vmware esx/esxi Vlan三种配置模式(VST、EST和VGT)
在Vmware esx/esxi中使用虚拟Lan(Vlan)时,一般是指Vlan Trunk的使用,使用的Vlan配置还有其它三种类型: Virtual Switch Tagging(虚拟交换机标记, ...
- 深度学习材料:从感知机到深度网络A Deep Learning Tutorial: From Perceptrons to Deep Networks
In recent years, there’s been a resurgence in the field of Artificial Intelligence. It’s spread beyo ...
- Informatica 常用组件Source Qualifier之九 创建SQ转换
可以配置 Designer 在您将源拖到映射中时默认创建源限定符转换,您也可以手动创建源限定符转换. 默认创建源限定符转换 可以配置 Designer 在您将源拖到映射中时自动创建一个源限定符转换. ...
- 点云数据(point cloud) 【转】
转自caimagic的专栏 一:什么是点云数据 点云数据是指在一个三维坐标系统中的一组向量的集合.这些向量通常以X,Y,Z三维坐标的形式表示,而且一般主要用来代表一个物体的外表面形状.不经如此,除(X ...
- VMware与Cisco DRAC中的virtual disk的对应关系
笔者面临的问题如下: 笔者有一台Cisco C240的服务器, 其中有十块容量一样大的SAS的local disk, 一块SSD. 其中的两块SAS盘组成了一个RAID 1的virtual drive ...
- iOS开发-简单的图片查看器
现在你只要拿着手机,不管你Android还是iOS,新闻类的App不可避免都有一个功能就是图片查看,做个专题,查看一下内容,App Store中也有专门针对图片浏览的App,鉴于目前所知有限,无法做到 ...
- bind原理图释
(原文:http://blog.think-async.com/2010/04/bind-illustrated.html) 本文解释了bind 是如何工作的.为了清晰,我对图中的语法作了一些简化(例 ...
- PasswordlessAPI
passwordlessapiYOURLS允许API调用的老式的方法,使用用户名和密码参数(如果你的设置是私人的,很明显).如果担心将证书发送到野外,还可以使用秘密签名令牌进行API调用.签名的令牌你 ...