Hbase–练习题一(步骤+踩过的坑)


文章目录

  • Hbase--练习题一(步骤+踩过的坑)
  • 一:读取本地文件到Hdfs
  • 1.代码实现
  • 2.遇到的错误及注意要点
  • 二:无论namespaces下存不存在表,都要删除给定名字的namespaces
  • 1.代码实现
  • 2.遇到的错误以及注意要点
  • 三:根据指定的页码和每页的大小,返回数据,方法定义为public static ResultScanner getIndexContext(int pageIndex,int pageSize)
  • 1.代码实现
  • 2.遇到的错误以及注意要点


一:读取本地文件到Hdfs

1.代码实现
public class FileOutPutBbase {
	public static void main(String[] args) throws IOException {
		//获取配置
		Configuration conf=HBaseConfiguration.create();
		//指定zookeeper连接
		conf.set("hbase.zookeeper.quorum", "hadoop01:2181,hadoop02:2181,hadoop03:2181");
		//创建连接
		Connection connection=ConnectionFactory.createConnection(conf);
		//建立表连接
		Table table=connection.getTable(TableName.valueOf("HbaseAPI:HbaseAPI01"));
		//获取输入流
		FileReader reader=new FileReader("D:\\datas.txt");
		//得到缓冲流
		BufferedReader br=new BufferedReader(reader);
		List<Put> list=new ArrayList<Put>();
		
		String line;
		while((line=br.readLine())!=null) {
			String[] data=new String[4];
			data=line.split(",");
			if (data.length==4) {
				Put put=new Put(Bytes.toBytes(data[0]));
				put.addColumn("info01".getBytes(), "week".getBytes(), data[0].getBytes());
				put.addColumn("info01".getBytes(), "week".getBytes(), data[1].getBytes());
				put.addColumn("info01".getBytes(), "week".getBytes(), data[2].getBytes());
				put.addColumn("info01".getBytes(), "week".getBytes(), data[3].getBytes());
				list.add(put);	
			}
		}
		table.put(list);
		table.close();
	}
}
2.遇到的错误及注意要点

1)最好选用文件流,因为readLine()方便读取整行数据;

2)获取表连接的时候,表名的格式是:“namespaces:table”

二:无论namespaces下存不存在表,都要删除给定名字的namespaces

1.代码实现
public class dropNamespace {
	public static void main(String[] args) throws IOException {
		dropNamespace("name");
	}

	public static void dropNamespace(String ns) throws IOException {
		System.setProperty("HADOOP_USER_NAME", "hadoop");
		// 获取配置
		Configuration conf = HBaseConfiguration.create();
		// 指定zookeeper连接
		conf.set("hbase.zookeeper.quorum", "hadoop01:2181,hadoop02:2181,hadoop03:2181");
		// 创建连接
		Connection connection = ConnectionFactory.createConnection(conf);
		// 获取管理员
		HBaseAdmin admin =(HBaseAdmin) connection.getAdmin();
		//创建命名空间描述器
		TableName[] tablename=admin.listTableNamesByNamespace(ns);
		if (tablename.length==0) {
			admin.deleteNamespace(ns);
		}else {
			for (TableName ta : tablename) {
				admin.disableTable(ta);
				admin.deleteTable(ta);
			}
			admin.deleteNamespace(ns);
		}
	}
}
2.遇到的错误以及注意要点

1)权限问题导致报错,加上这句话,System.setProperty(“HADOOP_USER_NAME”, “hadoop”);“hadoop”是linux用户名,管理权限;

2)针对于表以及namespace的操作需要管理员admin,删除namespace需要HbaseAdmin对象;

3)listTableNamesByNamespace(ns)方法用于返回给定namespace下的所有表的集合;

4)删除表要先禁用表,删除namespace要保证namespace下没有表。

三:根据指定的页码和每页的大小,返回数据,方法定义为public static ResultScanner getIndexContext(int pageIndex,int pageSize)

1.代码实现
public class PageIndex {
	public static void main(String[] args) throws IOException {
		///需要注意的是这里取出来的值是按rowkey排序的
		Iterator<Result> it=getIndexContext(1,5).iterator();
		while(it.hasNext()) {
			for (Cell cell : it.next().rawCells()) {
				System.out.println(Bytes.toString(CellUtil.cloneRow(cell))+":"+
			Bytes.toString(CellUtil.cloneQualifier(cell))+":"+Bytes.toString(CellUtil.cloneValue(cell)));
			}
		}
	}
	
	public static ResultScanner getIndexContext(int pageIndex,int pageSize) throws IOException {
		//获取配置
		Configuration conf=HBaseConfiguration.create();
		//指定zk的访问
		conf.set("hbase.zookeeper.quorum", "hadoop01:2181,hadoop02:2181,hadoop03:2181");
		//连接的工厂通过配置获取连接
		Connection connection=ConnectionFactory.createConnection(conf);
		//获取表连接
		Table table=connection.getTable(TableName.valueOf("user_hdfs"));
		//获取扫描器对象
		Scan scan=new Scan();
		//通过pageIndex和pageSize求出startrowkey,求出当前页之前的条数;
		int count=(pageIndex-1)*pageSize;
		PageFilter page1=new PageFilter(count);
		scan.setFilter(page1);
		ResultScanner scanner=table.getScanner(scan);
		Iterator<Result> it=scanner.iterator();
		String startrowkey="";
		while(it.hasNext()) {
			startrowkey=new String(it.next().getRow());
		}
		//获取分页过滤器对象
		PageFilter page2=new PageFilter(pageSize);
		//设置rowkey的其实位置
		scan.setStartRow(startrowkey.getBytes());
		scan.setFilter(page2);
		ResultScanner resultscanner=table.getScanner(scan);
		
		return resultscanner;
	}
}
2.遇到的错误以及注意要点

1)要理解pageIndex和pageSize如何求出startrowkey;

2)分页过滤器PageFileter()返回的数据是以行键为准的,例如多条数据都是同一个行键,那么这多条数据只会算一条;

3)分页得到的数据是按照行键的字典顺序排序的