package day1
;
import org
.apache
.hadoop
.conf
.Configuration
;
import org
.apache
.hadoop
.fs
.*
;
import org
.apache
.hadoop
.fs
.FileSystem
;
import org
.junit
.Before
;
import org
.junit
.Test
;
import java
.io
.*
;
import java
.util
.HashMap
;
import java
.util
.Map
;
public class HDFSdemo {
FileSystem fs
= null
;
@Before
public void init() throws IOException
{
System
.setProperty("HADOOP_USER_NAME","root");
Configuration conf
= new Configuration();
conf
.set("fs.defaultFS","hdfs://pc-01:9000");
fs
= FileSystem
.get(conf
);
}
@Test
public void testMkdirs() throws IOException
{
fs
.mkdirs(new Path("/bbb/zzz/"));
fs
.close();
}
@Test
public void testDelete() throws IOException
{
fs
.delete(new Path("/bbb/zzz/"),true);
fs
.close();
}
@Test
public void testMv() throws IOException
{
fs
.rename(new Path("/bbb/zxz/"),new Path("/zxz/"));
fs
.close();
}
@Test
public void testPut() throws IOException
{
fs
.copyFromLocalFile(new Path("d:/a.jpg"),new Path("/zxz/"));
fs
.close();
}
@Test
public void testDownload() throws IOException
{
fs
.copyToLocalFile(new Path("/zxz/"),new Path("d:/java"));
fs
.close();
}
@Test
public void testLs1() throws IOException
{
RemoteIterator
<LocatedFileStatus> files
= fs
.listFiles(new Path("/"), true);
while (files
.hasNext()) {
LocatedFileStatus next
= files
.next();
System
.out
.println(next
);
}
fs
.close();
}
@Test
public void testLs2() throws IOException
{
FileStatus
[] status
= fs
.listStatus(new Path("/"));
for (FileStatus fileStatus
: status
) {
System
.out
.println(fileStatus
.toString());
}
fs
.close();
}
@Test
public void testReaddata() throws IOException
{
HashMap
<String, Integer> cotMap
= new HashMap<>();
FSDataInputStream in
= fs
.open(new Path("/aaa1.txt"));
BufferedReader br
= new BufferedReader(new InputStreamReader(in
));
String next
;
while ((next
= br
.readLine()) != null
) {
System
.out
.println(next
);
String
[] split
= next
.split(" ");
for (String s
: split
) {
cotMap
.put(s
,cotMap
.getOrDefault(s
,0)+1);
}
}
for (Map
.Entry
<String, Integer> entry
: cotMap
.entrySet()) {
System
.out
.println(entry
.getKey()+"--->"+entry
.getValue());
}
in
.close();
FSDataOutputStream append
= fs
.create(new Path("/result.txt"));
BufferedWriter bw
= new BufferedWriter(new OutputStreamWriter(append
));
for (Map
.Entry
<String, Integer> entry
: cotMap
.entrySet()) {
bw
.write(entry
.getKey()+"--->"+entry
.getValue());
bw
.newLine();
}
bw
.flush();
bw
.close();
fs
.close();
}
@Test
public void testAppendData() throws IOException
{
FSDataOutputStream os
= fs
.append(new Path("/aaa1.txt"));
os
.write("这个男的已经21K了".getBytes());
os
.close();
fs
.close();
}
}
转载请注明原文地址:https://tech.qufami.com/read-19772.html