windows上搭建hadoop2开发环境

搭建前的准备:

1》安装ant-->配置ant环境变量和java的环境变量

2》配置jdk环境变量---》这里的jdk版本要求和build.xml 编译的版本一致否则不通过

正式搭建:

1.使用eclipse创建java project-->bigdata (这里也需要配置一下eclipse中运行的ant路径 由于该路径下有eclipse默认的ant没有的jar包---jscn-0.1.15.jar  该jar包作用就是运行window下hadoop程序用的)

2.创建lib包---》和src同级(这里必须创建lib文件夹否则后期用ant 运行build.xml 无法找到对应的hadoop的jar包)

3.创建类hdfs.App1

package hdfs;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.BlockLocation;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.Trash;
import org.apache.hadoop.fs.permission.FsPermission;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.util.Progressable;

public class App1 {
	public static void main(String[] args) throws Exception {
		final FileSystem fileSystem = FileSystem.get(new URI("hdfs://hadoop1:9000"), new Configuration());
		System.out.println("************************"+fileSystem);
		System.out.println("************************"+fileSystem.getClass());//DistributedFileSystem
		//创建文件夹 mkdir /-->表示根目录  dir1-->表示目录名称 (目录结构比较单一可用使用)
//		createDir(fileSystem);
		
		//上传文件create 参数1--》hdfs路径  参数2---》覆盖 参数3--》缓冲大小 参数4--》副本数  参数5-->块默认128m
//		upload(fileSystem);
		
		//读取hdfs文件
//		read(fileSystem);
		
		//遍历 listStatus 首字母就是ls 遍历的意思
//		FileStatus[] listStatus = fileSystem.listStatus(new Path("/dir1"));
//		for(FileStatus fileStatus : listStatus) {
//			System.out.println(fileStatus);
//			if(!fileStatus.isDirectory()) {
//				BlockLocation[] fileBlockLocations = fileSystem.getFileBlockLocations(fileStatus, 0, fileStatus.getLen());
//				for (BlockLocation blockLocation : fileBlockLocations) {
//					 String[] names = blockLocation.getNames();
//					 for (String name : names) {
//						System.out.println("name:"+name);//HOST NAME:192.168.0.2:50010
//					}
//					 String[] hosts = blockLocation.getHosts();
//					 for (String hostname : hosts) {
//						System.out.println("HOST NAME:"+hostname);
//					}
//				}
//			}
//		}
		
		//获取工作目录  hdfs://hadoop1:9000/user/root
		System.out.println(fileSystem.getWorkingDirectory().toString());
		
		//删除
//		Trash trash = new Trash(fileSystem, fileSystem.getConf());
//		trash.moveToTrash(new Path("/dir1/file1"));
//		fileSystem.delete(new Path("/dir1/file1"),true);
		
		
	}

	private static void read(final FileSystem fileSystem) throws IOException {
		FSDataInputStream in = fileSystem.open(new Path("/dir1/file1"));
		IOUtils.copyBytes(in, System.out, 1024,true); //把结果输出到控制台
	}

	private static void upload(final FileSystem fileSystem) throws IOException, FileNotFoundException {
		//		FSDataOutputStream out = fileSystem.create(new Path("/dir1/file1"),true,1024000,(short)2,1048576);// 65535 < 1048576
		//		FileInputStream in = new FileInputStream("/usr/hadoop1/hadoop.txt");
				
				//参数1--》linux本地文件io流,参数2--》hdfs输出文件流 参数3--》缓冲大小 参数4 ---》操作完是否关闭流
		//		IOUtils.copyBytes(in, out, 1024,true);
				//progressable--->表达数据上传的进程
				final AtomicInteger writeBytes = new AtomicInteger(0); //计数器
				FSDataOutputStream out = fileSystem.create(new Path("/dir1/file2"), new Progressable() {
					@Override
					public void progress() {
						System.out.println("writeBytes:"+writeBytes.get());
						
					}
				});
				FileInputStream in = new FileInputStream("/usr/hadoop1/hadoop.txt");
				byte[] buffer = new byte[1024];
				int readBytes = 0;
				while((readBytes=in.read(buffer))!=-1) {
					out.write(buffer);
					out.flush();
					out.hsync();//保证当前实际写入hdfs是成功的
					writeBytes.addAndGet(readBytes);
				}
	}

	private static void createDir(final FileSystem fileSystem) throws IOException {
		fileSystem.mkdirs(new Path("/dir1"));
//		fileSystem.mkdirs(new Path("/dir1/dir11/dir111"));//递归创建的
		//mydir位于working home--->推荐使用
		//fileSystem.mkdirs(new Path("mydir"));//没有根目录   /user/root--->hdfs存放所有用户的根目录
		//第二个参数是权限 ---drwxr-xr-x 
//		fileSystem.mkdirs(new Path("/dir2"),new FsPermission("111"));
	}

}

4.创建build.xml-->并运行

<?xml version="1.0" encoding="utf-8"?>
<project name="项目名称" basedir="." default="sshexec">
	<description>本配置文件共ANT编辑项目、自动化进行单元测试、打包并部署使用</description>
	<description>默认操作 (输入命令:ant) 为编译源程序并发布运行</description>
	
	<!-- 属性设置 -->
	<!-- property environment="env"/ -->
	<!--property file="build.properties"/ -->
	<!--property name="java.lib.dir" value="${env.JAVA_HOME}/lib"/ -->
	<property name="project" location="bigdata"/> 
	<property name="src.dir" value="${basedir}/src"/><!-- 设置变量,指向被编译的java代码的位置 --> 
	<property name="classes.dir" value="${basedir}/classes"/><!-- 设置变量,指向编译后的class文件的位置 -->
	<property name="dist.dir" value="${basedir}/dist"/> <!-- 设置变量,指向编译后生成jar包的位置 -->  
	<property name="project.lib.dir" value="${basedir}/lib"/><!-- 设置变量,指向所依赖的jar包所在的位置 --> 
	<property name="localpath.dir" value="${basedir}"/>
	<property name="remote.home" value="~"/>
	<!-- 可以修改 -->
	<property name="remote.hostname" value="hadoop1"/>
	<!-- 可以修改 -->
	<property name="remote.username" value="root"/>
	<!-- 可以修改 -->
	<property name="remote.password" value="root"/>
	<!-- 可以修改:每次需要知道的main类,写到这里 -->
	<property name="main.class" value="hdfs.App1"/>
	
	<!-- 基本编译路径设置 -->
	<path id="compile.classes">
		 <pathelement path="${dist.dir}" />       
		<fileset dir="${project.lib.dir}">
			<include name="**/*.jar"/>
		</fileset>
	</path>
	
	<!-- 清理删除临时目录-->
	<target name="clean" description="清理删除临时文件">
		<!-- delete dir="${build.dir}"-->	
		<delete dir="${dist.dir}" />
		<delete dir="${classes.dir}" />
	</target>
	<!-- 初始化,建立目录,复制文件-->
	<target name="init" depends="clean" description="初始化,建立目录,复制文件">
		<mkdir dir="${classes.dir}"/>
		<mkdir dir="${dist.dir}"/>
	</target>
	<!-- 编译源文件-->
	<target name="compile" depends="init" description="编译源文件">
		<!-- 编译的版本不能比运行的版本高 -->
		<javac srcdir="${src.dir}" destdir="${classes.dir}" fork="true" 
			executable="E:\Java\jdk1.7.0_80\bin\javac"
			includeantruntime="on" 
			classpath="${project.lib.dir}">
			<classpath refid="compile.classes"/>
		</javac>
	</target>
	<!-- 打包类文件-->
	<target name="jar" depends="compile" description="打包类文件">
		<jar jarfile="${dist.dir}/jar.jar" basedir="${classes.dir}">
			<!--fileset dir="${classes.dir}" includes="**/*.*"/-->
		</jar>
	</target>
	<!-- 上传到服务器 
	**需要把lib目录下jsch-0.1.15拷贝到$ANT_HOME/lib下,如果Eclipse下的ant环境必须在Windows->preference->ANT->RUNIIME->CLASSPATH中加入jscn-0.1.15.
	-->
	<target name="ssh" depends="jar">
		<scp file="${dist.dir}/jar.jar" todir="${remote.username}@${remote.hostname}:${remote.home}" password="${remote.password}" trust="true"/>
	</target>
	<target name="sshexec" depends="ssh">
		<sshexec command="/usr/hadoop1/hadoop-2.7.5/bin/hadoop jar ${remote.home}/jar.jar ${main.class}" host="${remote.hostname}" username="${remote.username}" password="${remote.password}" trust="true"  />
	</target>
</project>

最终运行效果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值