HDFSApp、WordCountApp和hive、mysql
在linux中:
#查看hdfs根目录下面的文件
hdfs dfs -ls /
#mkdir在hdfs根目录创建一个test目录
hdfs dfs -mkdir /test
hdfs dfs -put 1.txt /test
#创建要执行统计的目录
hdfs dfs -mkdir /wc
#上传文本文件
vi 1.txt
#上传本地1.txt文件到hdfs上面。
hdfs dfs -put 1.txt /wc
目录的删除:hdfs dfs -rmdir /目录名
文件的删除:hdfs dfs -rm /目录名/文件名
防火墙的关闭:start-all.sh
查看jps(要有5到6个)
---
--------------先删目录中的文件再删目录---------------------
写入idea软件中:
*****操作前----注意事项
拒绝当前用户访问的话,需要设置环境变量将用户替换为root用户。
设置环境变量HADOOP_USER_NAME=root。(全部设置环境变量为root)
MapReduce跑不通参考这个链接解决:https://blog.youkuaiyun.com/whs0329/article/details/121878162
记得设置用户root环境变量,exit 0表示词频统计运行成功。
查看词频统计结果。
package hdfs.app;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.net.URI;
/**
* Java API操作HDFS
*/
public class HDFSApp {
public static final String HDFS_PATH = "hdfs://192.168.48.130:9000";
Configuration configuration = null;
FileSystem fileSystem = null;
@Before
public void setUp() throws Exception {
System.out.println("HDFSApp.setUp()");
configuration = new Configuration();
fileSystem = FileSystem.get(new URI(HDFS_PATH), configuration);
}
@After
public void tearDown() throws Exception {
fileSystem = null;
configuration = null;
System.out.println("HDFSApp.tearDown()");
}
/**
* 创建目录
*/
@Test
public void mkdir() throws Exception {
fileSystem.mkdirs(new Path("/hdfsapi/test"));
}
/**
* 创建文件
*/
@Test
public void create() throws Exception {
FSDataOutputStream output = fileSystem.create(new Path("/hdfsapi/test/a.txt"));
output.write("hello world".getBytes());
output.flush();
output.close();
}
/**
* 重命名
*/
@Test
public void rename() throws Exception {
Path oldPath = new Path("/hdfsapi/test/a.txt");
Path newPath = new Path("/hdfsapi/test/b.txt");
System.out.println(fileSystem.rename(oldPath, newPath));