配置ssh免登陆
生成ssh免登陆密钥
cd ~,进入到我的home目录
cd .ssh/
ssh-keygen -t rsa (四个回车)
执行完这个命令后,会生成两个文件id_rsa(私钥)、id_rsa.pub(公钥)
将公钥拷贝到要免登陆的机器上
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys cat是查看
或
ssh-copy-id -i localhost
关闭所有
stop-all.sh
开启所有
start-all.shhadoop写java代码
1.创建一个工程
2.lib包放在工程目录下,并将lib包添加到库
3.写Java文件
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.URI; import java.net.URISyntaxException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IOUtils; import org.junit.Before; import org.junit.Test;
windows系统的hadoop可以不配置系统变量,直接配置Path public class JavaToHDFS{ //定义类变量:文件系统,用来连接hdfs FileSystem fs=null; //预处理 @Before public void init() throws Exception{ fs=FileSystem.get(new URI("hdfs://192.168.153.129:9000"),new Configuration(),"root"); }
@Test public void mkdir(){ System.out.println(fs) } @Test public void testUpload() throws Exception{ //上传文件 InputStream in = new FileInputStream("/root/install.log"); OutputStream out = fs.create(new Path("/log123.log")); IOUtils.copyBytes(in, out, 1024, true); } @Test public void testMkdir() throws IllegalArgumentException, IOException{ //创建文件 boolean flag = fs.mkdirs(new Path("/a/aa"));System.out.println(flag); } @Test public void testDel() throws IllegalArgumentException, IOException{ //删除文件 boolean flag = fs.delete(new Path("/a"), true); System.out.println(flag); } public static void main(String[] args) throws Exception { FileSystem fs = FileSystem.get(new URI("hdfs://itcast01:9000"), new Configuration()); InputStream in = fs.open(new Path("/jdk")); OutputStream out = new FileOutputStream("/home/jdk1.7.tar.gz"); IOUtils.copyBytes(in, out, 4096, true);} }