把jar包倒在eclipse安装路径的plugins.zip 解压放置到对应目录
重启eclipse
eclpise调整到map/reduce视图
配置hadoop安装目录
window-》preferences-》hadoop map/reduce ->选择路径-》apply
新建hadoop location
在map reduce locations窗口右键->new hadoop location.....
再弹出的对话框里面
location name:随便
map/reduce(V2) master 下的
host改成对应的ip或主机名
port改成9001
dfs master下的
port改成9000
user name:改成自己linux系统上的用户名
选择第二个选项卡(Advanced parameters)
修改和core-site.xml里面配置相同的属性
finish
如果只能下载查看无法上传文件
改window的用户名,改成和linux一样,重启之后生效
在window下通过java API查看hdfs上的文件
新建项目map reduce项目的话
import java.io.InputStream;
import java.net.URL;
import org.apache.hadoop.fs.FsUrlStreamHandlerFactory;
public class Test {
static {
URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());
}
public static void main(String[] args) throws Exception {
InputStream in = new URL("hdfs://192.168.229.138:9000/core-site.xml").openStream();
byte[] b = new byte[1024];
int len = 0;
while ((len = in.read(b)) != -1) {
System.out.print(new String(b,0,len));
}
}
}
如果是新建的java项目
导入hadoop所需要的所有依赖包
编程上面的程序
通过FileSystem API查看hdfs上的文件
import java.io.InputStream;
import java.net.URI;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
public class FsCat {
public static void main(String[] args) throws Exception {
//获取hdfs的路径
String uri = args[0];
//加载配置文件
Configuration conf = new Configuration();
//获取文件操作系统
FileSystem fs = FileSystem.get(URI.create(uri),conf);
//获取输入流
InputStream in = fs.open(new Path(uri));
byte[] b = new byte[1024];
int len = 0;
while ((len = in.read(b)) != -1) {
}
IOUtils.closeStream(in);
}
}
随机读取数据
@Test
public void seek() throws Exception {
//获取hdfs的路径
String uri = "hdfs://192.168.229.138:9000//core-site.xml";
//加载配置文件
Configuration conf = new Configuration();
//获取文件操作系统
FileSystem fs = FileSystem.get(URI.create(uri),conf);
//获取输入流
FSDataInputStream in = fs.open(new Path(uri));
IOUtils.copyBytes(in, System.out, 38, false);
System.out.println("-------------------------");
in.seek(0);
IOUtils.copyBytes(in, System.out, 38, false);
IOUtils.closeStream(in);
}
通过FileSystem API写入数据
@Test
public void APIwrite() throws Exception {
//获取hdfs的路径
String uri = "hdfs://192.168.229.138:9000";
//加载配置文件
Configuration conf = new Configuration();
//获取文件操作系统
FileSystem fs = FileSystem.get(URI.create(uri),conf);
//创建一个新的文件目录
Path path = new Path("hdfs://192.168.229.138:9000/aa.txt");
//通过path创建新的文件,然后打开一个输出流
FSDataOutputStream output = fs.create(path);
//写
output.writeBytes("abc");
output.close();
}
创建文件夹(目录)
@Test
public void mkdirs() throws IOException {
//获取hdfs的路径
String uri = "hdfs://192.168.229.138:9000";
//加载配置文件
Configuration conf = new Configuration();
//获取文件操作系统
FileSystem fs = FileSystem.get(URI.create(uri),conf);
//创建一个新的文件目录
Path path = new Path("hdfs://192.168.229.138:9000/input/1");
//创建目录
fs.mkdirs(path);
}
获取文件状态
@Test
public void showFileStatus() throws IOException {
//获取hdfs的路径
String uri = "hdfs://192.168.229.138:9000";
//加载配置文件
Configuration conf = new Configuration();
//获取文件操作系统
FileSystem fs = FileSystem.get(URI.create(uri),conf);
//创建一个新的文件目录
Path path = new Path("hdfs://192.168.229.138:9000/aa.txt");
FileStatus file = fs.getFileStatus(path);
System.out.println(file.getAccessTime());
System.out.println(file.getBlockSize());
System.out.println(file.getLen());
System.out.println(file.getModificationTime());
}