hadoop的读写操作
来源:互联网 发布:上海发型设计 知乎 编辑:程序博客网 时间:2024/06/15 12:26
在txt文件中写点需要上传的数据,conf是source包,lib是jar包
package com.wangbo.day0830;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.FileReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.FSDataInputStream;import org.apache.hadoop.fs.FSDataOutputStream;import org.apache.hadoop.fs.FileSystem;import org.apache.hadoop.fs.Path;/** * hdfs操作类 * @author Administrator * */public class HdfsTest { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { //创建hdfs的配置 Configuration conf = new Configuration(); //加载配置文件 conf.addResource("conf/core-site.xml"); conf.addResource("conf/hdfs-site.xml"); //获取HDFS的句柄 FileSystem fs = FileSystem.get(conf);// //向HDFS写入数据// writeHDFS(fs); //向HDFS读数据 readHDFS(fs); } private static void readHDFS(FileSystem fs) throws IllegalArgumentException, IOException { //获取文件输入流 FSDataInputStream in = fs.open(new Path("syhz/hellohadoop.txt")); byte[] buff = new byte[1024]; BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File("d:/hdfs.txt")));// 写入文件 字节流 int length = in.read(buff); while(-1 != length) {// System.out.println(new String(buff, 0, length)); out.write(buff, 0, length); length = in.read(buff); } in.close(); out.close(); System.out.println("读取结束!"); } public static void writeHDFS(FileSystem fs) throws IOException { //在HDFS上新建一个虚拟目录 Path path = new Path("syhz/hellohadoop.txt"); //输出流 FSDataOutputStream out = fs.create(path); //读取数据写入数据 字节流// BufferedInputStream bufferIn = new BufferedInputStream(new FileInputStream(new File("text.txt")));// byte[] buff = new byte[1024];// int length = bufferIn.read(buff);// // while(-1 != length)// {// //写入// out.write(buff,0,length);// out.flush();// length = bufferIn.read(buff);// }// out.close();// bufferIn.close(); //读取数据写入数据 字符流 BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File("text.txt")),"gbk")); String line = null; while((line = reader.readLine())!=null){ out.write((line+System.getProperty("line.separator")).getBytes("utf-8")); out.flush(); } out.close(); reader.close(); }}
就是酱紫
阅读全文
0 0
- hadoop的读写操作
- Hadoop的HDFS Java pai 读写操作
- hadoop java HDFS 读写操作
- hadoop的读写机制
- hdfs——hadoop文件读写操作
- hadoop利用FileSystem API 执行hadoop文件读写操作
- hadoop文件的读写流程
- 读写模版的操作
- xml的读写操作
- 文件的读写操作
- 文件的读写操作
- nandflash的读写操作
- 文件的读写操作
- nandflash 的读写操作
- 配置文件的读写操作
- 文件的读写操作
- 简单的读写操作
- plist的读写操作
- Hibernate笔试题
- 设计模式——创建型模式
- JavaScript中使用window.open被拦截解决办法
- 导入和导出功能用例设计
- PAT_1089. Insert or Merge
- hadoop的读写操作
- Nodejs进阶:使用DiffieHellman密钥交换算法
- 关于在VM和主机之间ping不通的解决办法
- django admin自定义HTML模板界面替换logo和header
- 对称加密----AES和DES加密、解密
- 数据库原理 数据库全局概览
- 【绝对能用】阿里云ECS上Centos7安装Apache(httpd)并实现多域名转不同tomcat配置
- 【scala 语法基础】变量、函数、控制结构、数据类型
- linux ls 按照文件大小排序