创建 文件目录如下
/yourdir/HDFStest/conf
/yourdir/HDFStest/lib
/yourdir/HDFStest/bin
eclipse里面
package test.hdfs;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
public class HDFStest {
public static void main(String[] args) throws IOException {
Configuration conf;
FileSystem fs;
conf = new Configuration();
conf.addResource("hdfstest-site.xml");
conf.addResource("core-site.xml");
conf.addResource("hdfs-site.xml");
fs = FileSystem.get(conf);
HDFStest hdfs = new HDFStest();
byte[] bytes = new byte[10000];
Path path = new Path("//HdfsTest/");
if (fs.exists(path)) {
fs.delete(path, true);
}
fs.mkdirs(path);
Path path2 = new Path("hdfs://192.168.30.119:54310///HdfsTest/file");
fs.createNewFile(path2);
FSDataOutputStream out = fs.create(path2, true);
out.write(bytes);
out.close();
}
}
不行试试这个
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class HDFStest {
public static void main(String[] args) throws IOException {
Configuration conf;
FileSystem fs = null;
conf = new Configuration();
conf.addResource(new Path("/usr/local/hadoop/etc/hadopp/core-site.xml"));
conf.addResource(new Path("/usr/local/hadoop/etc/hadopp/hdfs-site.xml"));
try {
fs = FileSystem.get(new URI("hdfs://192.168.1.111:9000"), conf);
} catch (URISyntaxException e) {
e.printStackTrace();
}
char[] cbuf = new char[1000];
File local = new File("/usr/local/wordcount.txt");
FileReader fr = new FileReader(local);
fr.read(cbuf);
String str = new String(cbuf);
Path path = new Path("hdfs://192.168.1.111:9000///HdfsTest/");
if (fs.exists(path)) {
fs.delete(path, true);
}
fs.mkdirs(path);
Path path2 = new Path(
"hdfs://192.168.1.111:9000///HdfsTest/wordcount.txt");
fs.createNewFile(path2);
FSDataOutputStream out = fs.create(path2, true);
out.writeChars(str);
out.close();
}
}