首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

hbase的一些容易操作

2013-08-04 
hbase的一些简单操作使用URL的方式读取一个文件内容,需要设置一个handler工厂,这个工厂只能设置一次static

hbase的一些简单操作
使用URL的方式读取一个文件内容,需要设置一个handler工厂,这个工厂只能设置一次

static {URL.setURLStreamHandlerFactory( new FsUrlStreamHandlerFactory() );}public void test1() throws IOException {URL u = new URL("hdfs://IP:54310/test");InputStream is = u.openStream();BufferedReader br = new BufferedReader(new InputStreamReader(is));String line = null;while( (line=br.readLine()) != null ) {System.out.println(line);}br.close();}



使用hadoop的FileSystem读取文件
public void test2() throws IOException {String url = "hdfs://IP:54310/test-data/hello.txt";Configuration config = new Configuration();FileSystem fs = FileSystem.get(URI.create(url),config);InputStream is = null;is = fs.open(new Path(url));IOUtils.copyBytes(is, System.out, 4096, false);}


将一个本地文件拷贝到hadoop文件系统中
public void test3() throws IOException {String src = "C:\\test.txt";String dest = "hdfs://IP:54310/test-data/hello.txt";InputStream is = new BufferedInputStream(new FileInputStream(src));Configuration config = new Configuration();FileSystem fs = FileSystem.get(URI.create(dest), config);OutputStream os = fs.create(new Path(dest), new Progressable() {@Overridepublic void progress() {System.out.print(".");}});IOUtils.copyBytes(is, os, 4096, true);System.out.println("ok~");}


列出文件属性
public void test4() throws IOException {String url = "hdfs:/IP:54310/test-data/hello.txt";Configuration config = new Configuration();FileSystem fs = FileSystem.get(URI.create(url),config);FileStatus status = fs.getFileStatus(new Path(url));System.out.println("AccessTime : "+status.getAccessTime());System.out.println("BlockSize : "+status.getBlockSize());System.out.println("group : "+status.getGroup());System.out.println("len : "+status.getLen());System.out.println("ModificationTime : "+status.getModificationTime());System.out.println("owner : "+status.getOwner());System.out.println("is dir ? : "+status.isDir());System.out.println("path : "+status.getPath());System.out.println("permission : "+status.getPermission());System.out.println("replication : "+status.getReplication());}



通过路径过滤器查找文件
public void test5() throws IOException {String url = "hdfs://IP:54310/test-data/*";Configuration config = new Configuration();FileSystem fs = FileSystem.get(URI.create(url),config);FileStatus[] status = fs.globStatus( new Path(url), new RegexPathFilter("^.*hello.*$") );for(FileStatus s:status) {System.out.println(s.getPath().toString());}System.out.println("filter execute ok");}//路径正则过滤器类public class RegexPathFilter implements PathFilter {private final String regex;public RegexPathFilter(String regex) {this.regex = regex;}@Overridepublic boolean accept(Path path) {return path.toString().matches(regex);}}


删除,支持递归删除
public void delete() throws IOException {String url = "hdfs://IP:54310/test-data/xxx.txt";Configuration config = new Configuration();FileSystem fs = FileSystem.get(URI.create(url),config);fs.delete(new Path(url), false);System.out.println("delete ok");}


重命名
public void test5_rename() throws IOException {String url = "hdfs://IP:54310/test-data/xx.txt";String url2 = "hdfs://IP:54310/test-data/modify-xx.txt";Configuration config = new Configuration();FileSystem fs = FileSystem.get(URI.create(url),config);boolean isok = fs.rename(new Path(url), new Path(url2));System.out.println("complete : "+isok);}



检查文件是否存在
public void exist() throws IOException {String url = "hdfs:/IP:54310/test-data/modify-xx.txt";Configuration config = new Configuration();FileSystem fs = FileSystem.get(URI.create(url),config);boolean isExist = fs.exists( new Path(url));System.out.println("exist ? "+isExist);}



查找某个文件在HDFS中的位置
public void test5_location() throws IOException {String url = "hdfs://IP:54310/test-data/hello.txt";Configuration config = new Configuration();FileSystem fs = FileSystem.get(URI.create(url),config);FileStatus status = fs.getFileStatus(new Path(url));BlockLocation[] bls = fs.getFileBlockLocations(status, 0, status.getLen());for(int i=0;i<bls.length;i++) {String[] hosts = bls[i].getHosts();System.out.println("block :"+i+"\tlocation : "+hosts[i]);}}


获取HDFS集群上所有节点的名称
public void test5_allnode() throws IOException {String url2 = "hdfs://IP:54310/test-data/modify-xx.txt";Configuration config = new Configuration();FileSystem fs = FileSystem.get(URI.create(url2),config);DistributedFileSystem hdfs = (DistributedFileSystem)fs;DatanodeInfo[] status = hdfs.getDataNodeStats();for(DatanodeInfo d:status) {System.out.println(d.getHostName());}}




热点排行