上傳檔案在hdfs上,通路namenode節點:ip:50070
讀取hdfs的java代碼如下:
public void ReadFile(String hdfs) throws IOException {
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(URI.create(hdfs),conf);
System.out.println(URI.create(hdfs));
//fs.listFiles(f, recursive)
FileStatus[] status = fs.listStatus(new Path(hdfs));
for (FileStatus file : status) {
System.out.println(file.getPath().getName());
if (!file.getPath().getName().endsWith(".txt")) {
continue;
}
System.out.println(file.getPath().getName());
FSDataInputStream hdfsInStream = fs.open(file.getPath());
byte[] ioBuffer = new byte[];
int readLen = hdfsInStream.read(ioBuffer);
while(readLen!=-)
{
System.out.write(ioBuffer, , readLen);
readLen = hdfsInStream.read(ioBuffer);
}
hdfsInStream.close();
// fs.close();
}
}
public static void main(String[] args) throws IOException {
String hdfs = "hdfs://ip:8020/檔案夾";
text t = new text();
//t.WriteFile(hdfs);
t.ReadFile(hdfs);
}
建立目錄:
public static void mkdir(String dir) throws IOException
{
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(conf);
fs.mkdirs(new Path(dir));
fs.close();
}
寫入hdfs中:
public void WriteFile(String hdfs) throws IOException {
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(URI.create(hdfs),conf);
FSDataOutputStream hdfsOutStream = fs.create(new Path(hdfs));
hdfsOutStream.writeChars("hello");
hdfsOutStream.close();
fs.close();
}