HDFS下文件读取实验

实现在hadoop hdfs下读出指定文件,并将指定文件中的内容排序,输出到hdfs中。具体代码如下

头文件:

#package org.hadoop.sort;

import java.io.BufferedReader;

import java.lang.Object;

import java.util.List;

//import java.util.Arrays;

import java.io.InputStreamReader;

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;

其他代码部分:


public class HDFSfileifexist {

public static void myWrite(String content, String name){

try

{

Configuration conf=new Configuration();

conf.set("fs.defaultFS", "hdfs://localhost:9000");

conf.set("fs.hdfs.omp", "org.apache.hadoop.hdfs.DistributedFileSystem");

FileSystem fs=FileSystem.get(conf);

byte[] buff=content.getBytes();

String filename=name;

FSDataOutputStream os=fs.create(new Path(filename));

os.write(buff,0,buff.length);

System.out.println("Creat:"+filename);

os.close();

fs.close();

}

catch(Exception e)

{

e.printStackTrace();

}

}

public static String myRead(String fileName){

String content = null;

try

{

Configuration conf=new Configuration();

conf.set("fs.defaultFS", "hdfs://localhost:9000");

conf.set("fs.hdfs.omp", "org.apache.hadoop.hdfs.DistributedFileSystem");

FileSystem fs=FileSystem.get(conf);

Path file=new Path(fileName);

FSDataInputStream getIt=fs.open(file);

BufferedReader d=new BufferedReader(new InputStreamReader(getIt));

content=d.readLine();

System.out.println(""+content);

d.close();

fs.close();


}




catch(Exception e)

{

e.printStackTrace();

}

return content;

}


public static String merge(String s1){

String a = s1;

String [] s = a.split(" ");

String b = "";

s[0].compareTo(s[1]);

int temp;


int i,j;

for(i=0;i<s.length;i++)

        {

            for(j=0;j<s.length-1-i;j++)

            {

                if(Integer.parseInt(s[j])>Integer.parseInt(s[j+1]))

                {

                    String temp1=s[j];

                    s[j]=s[j+1];

                  s[j+1]=temp1;

                }

            }

        }


//Arrays.sort(s);

//Connections.sort();

System.out.println("a:"+a);

for(int i1 = 0; i1 < s.length; i1++){

b += s[i1]+" ";

}

System.out.println(b);

return b;

}


public static void main(String[] args) {

String s1 = myRead("hdfs://localhost:9000/input/1_3.txt");

//String s2 = myRead("hdfs://localhost:9000/input/1_2.txt");

String s3 = merge(s1);

myWrite(s3,"hdfs://localhost:9000/input/result.txt");

}

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • HDFS入门 hadoop架构 Hadoop 1.0中的资源管理方案 Hadoop 1.0指的是版本为Apache...
    依天立业阅读 1,159评论 0 1
  • 一.简述如何安装配置apache 的一个开源的hadoop 1.使用root账户登陆 2.修改ip 3.修改hos...
    栀子花_ef39阅读 4,989评论 0 52
  • 当数据量增大到超出了单个物理计算机存储容量时,有必要把它分开存储在多个不同的计算机中。那些管理存储在多个网络互连的...
    单行线的旋律阅读 1,970评论 0 7
  • 【什么是大数据、大数据技术】 大数据,又称巨量资料,指的是所涉及的数据资料量规模巨大到无法在合理时间内通过传统的应...
    kimibob阅读 2,789评论 0 51
  • 人生在世, 幸福与否不在于你的 金钱、地位、而在于你做人的德行。 生而为人, 我们的一生跌宕起伏,喜忧参半, 通达...
    三水畔阅读 207评论 1 2