前提: hadoop集群已经搭建成功,并启动hadoop
启动hadoop命令:start-all.sh
在所有节点上添加环境变量CLASSPATH
export CLASSPATH=$($HADOOP_HOME/bin/hadoop classpath):$CLASSPATH
在所有节点上刷新环境变量
source /etc/profile
通过 javac 命令编译 Chapter3.java
javac Chapter3.java
把Chapter3.class文件打包成Chapter3.jar
jar -cvf Chapter3.jar ./Chapter3.class
执行jar包
hadoop jar Chapter3.jar Chapter3
Chapter3.java代码如下:
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class Chapter3 {
public static void main(String[] args) {
try {
String filename = "test";#测试文件
Configuration conf = new Configuration();
conf.set("fs.defaultFS","hdfs://hadoop8:9000");#hadoop8为主节点
conf.set("fs.hdfs.impl","org.apache.hadoop.hdfs.DistributedFileSystem");
FileSystem fs = FileSystem.get(conf);
if(fs.exists(new Path(filename))){
System.out.println("file exist");
}else{
System.out.println("file not exist");
}
fs.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}



