- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.net.URI;
- import org.apache.hadoop.conf.Configuration;
- import org.apache.hadoop.fs.FileSystem;
- import org.apache.hadoop.fs.Path;
- public class HadoopFileSystemDemo {
- public static Configuration config = null;
- public static FileSystem fs = null;
- static{
- config = new Configuration();
- try {
- fs = FileSystem.get(URI.create("hdfs://ceoihong:9000"),config);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public static void main(String[] args) throws IOException{
- //hdfs_touch();
- //hdfs_rm();
- //hdfs_rename();
- //hdfs_cat();
- //hdfs_mkdir();
- fs.close();
- // InputStream in = fs.open(new Path("/hello"));
- // byte[] bts = new byte[2048];
- // while(in.read(bts)!=-1){
- // System.out.println(new String(bts,0,bts.length));
- // }
- // in.close();
- // fs.close();
- }
- public static void hdfs_touch() throws IOException{
- /**
- * 运行会报permission deny到错,是因为当前用户没有权限。
- * 修改master的conf/hdfs-site.xml文件,增加属性
- * <property>
- * <name>dfs.permissions</name>
- * <value>false</value>
- * <description>
- * If "true", enable permission checking in HDFS.If "false", permission checking is turned off,
- * </description>
- * </property>
- * 重启下hadoop就好了
- */
- OutputStream out = fs.create(new Path("/usr/abc/newFile"));
- out.write("测试文件!!".getBytes("UTF-8"));
- out.close();
- }
- public static void hdfs_rm() throws IOException{
- fs.delete(new Path("/usr/abc/readme"),true);
- }
- public static void hdfs_rename() throws IOException{
- fs.rename(new Path("/usr/abc/newFile"), new Path("/usr/abc/readme"));
- }
- public static void hdfs_cat() throws IOException{
- InputStream in = fs.open(new Path("/usr/abc/readme"));
- byte[] bts = new byte[1024];
- while(in.read(bts)!=-1){
- System.out.println(new String(bts,0,bts.length,"UTF-8"));
- }
- }
- public static void hdfs_mkdir() throws IOException{
- fs.mkdirs(new Path("/usr/def"));
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/1010201513782.html
来源: http://www.codesnippet.cn/detail/1010201513782.html