hadoop学习之JavaAPI

1.首先需要引入hadoop的maven依赖

        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-common</artifactId>
            <version>2.7.4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-hdfs</artifactId>
            <version>2.7.4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>2.7.4</version>
        </dependency>

2.连接到hdfs文件系统

//创建一个配置类
Configuration conf = new Configuration();

//创建一个访问hdfs的客户端对象
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop-node1:9000"), conf, "root"); //指定用户

3.API命令
文件创建

fs.creat(new path("/user/yang/a.txt"))

查看所有的文件


Configuration conf = new Configuration();

//创建一个访问hdfs的访问对象
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop-node1:9000"), conf, "root");

//得到所有的文件
RemoteIterator<LocatedFileStatus> list = fs.listFiles(new Path("/"), true);
while(list.hasNext())
{
     	 //显示其中一个文件
		LocatedFileStatus next = list.next();
		//文件的名字
		System.out.println(next.getPath().getName());
		//得到块的大小
		System.out.println(next.getBlockSize());
		//得到文件的读写权限
		System.out.println(next.getPermission());
		//获取文件的所有者
		System.out.println(next.getOwner());
		//得到文件的大小
		System.out.println(next.getLen());

		BlockLocation[] locations = next.getBlockLocations();

		for (BlockLocation location : locations) {
				//block-length表示块的大小,block-offset表示偏移量
				System.out.println("block-length"+location.getLength()+",block-offset"+location.getOffset());
				String[] hosts = location.getHosts();
				for (String host : hosts) {
			//查看备份的所在主机
				System.out.println(host);
		}
}

System.out.println("-----------------------------------");
}

查看当前的文件和文件夹

Configuration conf = new Configuration();

FileSystem fs = FileSystem.get(new URI("hdfs://hadoop-node1:9000"), conf, "root");

FileStatus[] listStatus = fs.listStatus(new Path("/"));


String flag="f--";
for (FileStatus status : listStatus) {
if(status.isFile())
{
		flag="f--";
}
else {
		flag="d--";
}
System.out.println(flag+status.getPath().getName());
System.out.println(status.getPermission())

目录操作

创建目录
     fs.mkdirs(new Path("/user/yang/java-test"));
删除目录
     fs.delete(new Path("/user/yang/test"),true);//true表示递归删除
重命名目录
     fs.rename(new Path("/user/yang/test"),new Path("/user/yang/test01"));
   //前面的是原来的名字,后面是要改的名字
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Hadoop是一个分布式处理框架,它提供了Java API来编写分布式程序。下面是使用Java API编程的步骤: 1. 引入Hadoop依赖 在Java项目中,需要引入Hadoop的依赖。可以使用Maven或手动导入jar包。 2. 配置Hadoop环境 在Java代码中,需要配置Hadoop的环境变量,包括Hadoop的安装路径、Hadoop的配置文件路径等。 3. 编写MapReduce程序 MapReduce是Hadoop的核心编程模型,它包括Map和Reduce两个阶段。在Java代码中,需要编写Map和Reduce函数,并将它们组合在一起,形成MapReduce程序。 4. 配置MapReduce作业 在Java代码中,需要设置MapReduce作业的输入路径、输出路径、Mapper类、Reducer类等参数。 5. 提交MapReduce作业 在Java代码中,需要调用Hadoop API提交MapReduce作业,并等待作业执行完成。 以下是一个简单的WordCount程序的示例代码: ``` import java.io.IOException; import java.util.StringTokenizer; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Job; import org.apache.hadoop.mapreduce.Mapper; import org.apache.hadoop.mapreduce.Reducer; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; public class WordCount { public static class TokenizerMapper extends Mapper<LongWritable, Text, Text, IntWritable>{ private final static IntWritable one = new IntWritable(1); private Text word = new Text(); public void map(LongWritable key, Text value, Context context ) throws IOException, InterruptedException { StringTokenizer itr = new StringTokenizer(value.toString()); while (itr.hasMoreTokens()) { word.set(itr.nextToken()); context.write(word, one); } } } public static class IntSumReducer extends Reducer<Text,IntWritable,Text,IntWritable> { private IntWritable result = new IntWritable(); public void reduce(Text key, Iterable<IntWritable> values, Context context ) throws IOException, InterruptedException { int sum = 0; for (IntWritable val : values) { sum += val.get(); } result.set(sum); context.write(key, result); } } public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); Job job = Job.getInstance(conf, "word count"); job.setJarByClass(WordCount.class); job.setMapperClass(TokenizerMapper.class); job.setCombinerClass(IntSumReducer.class); job.setReducerClass(IntSumReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); FileInputFormat.addInputPath(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); System.exit(job.waitForCompletion(true) ? 0 : 1); } } ``` 以上代码实现了一个简单的WordCount程序,它统计输入文件中每个单词出现的次数,并输出到输出文件中。在main函数中,首先创建一个Configuration对象,并通过它创建一个Job对象。然后,设置MapReduce作业的各种参数,包括Mapper类、Reducer类、输入路径、输出路径等。最后,调用job.waitForCompletion方法提交作业并等待执行结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值