HBase java API mysplitpage 我的HBasejavaAPI翻页实现

运行环境:

VMware Workstation Pro 12

CentSO6.5(VM虚拟机)

hadoop-2.6.0

hbase-1.2.6

apache-tomcat-7.0.59

eclipse-jee-neon-3-win32-x86_64


1

vo包:

Tableinfo类:

package vo;

public class Tableinfo {
	private String rowkey;// 键
	private String family;// 列族
	private String qualifier;// 列
	private long Timestamp;// 时间戳
	private String value;// 值

	public String getRowkey() {
		return rowkey;
	}

	public void setRowkey(String rowkey) {
		this.rowkey = rowkey;
	}

	public String getFamily() {
		return family;
	}

	public void setFamily(String family) {
		this.family = family;
	}

	public String getQualifier() {
		return qualifier;
	}

	public void setQualifier(String qualifier) {
		this.qualifier = qualifier;
	}

	public long getTimestamp() {
		return Timestamp;
	}

	public void setTimestamp(long timestamp) {
		Timestamp = timestamp;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public Tableinfo(String rowkey, String family, String qualifier, long timestamp, String value) {
		super();
		this.rowkey = rowkey;
		this.family = family;
		this.qualifier = qualifier;
		Timestamp = timestamp;
		this.value = value;
	}

	public Tableinfo() {
		super();
	}

	@Override
	public String toString() {
		return "Tableinfo [键=" + rowkey + ", 列族=" + family + ", 列=" + qualifier + ", 时间戳=" + Timestamp + ", 值=" + value
				+ "]";
	}

}


2

mysplitpage包:

Page类:

package mysplitpage;

import java.util.List;

import vo.Tableinfo;

public class Page {
	private int currentPage;// 当前页数
	private int pageSize;// 页面大小
	private int totalCount;// 总条数
	private int totalPage;// 总页数
	List list;// 页面需要显示的信息
	Tableinfo last;// 最后一条信息不显示

	@Override
	public String toString() {
		String str = "";
		for (Tableinfo tableinfo : list) {
			str += ("\n" + tableinfo.toString());
		}
		return "页面 [当前页数=" + currentPage + ", 页面大小=" + pageSize + ", 总条数=" + totalCount + ", 总页数=" + totalPage
				+ ", list=" + str + ", \n last=" + last + "]";
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public List getList() {
		return list;
	}

	public void setList(List list) {
		this.list = list;
	}

	public Tableinfo getLast() {
		return last;
	}

	public void setLast(Tableinfo last) {
		this.last = last;
	}

}

3

mysplitpage包:

SplitPageUtil类:

package mysplitpage;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HConstants;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;

import vo.Tableinfo;

public class SplitPageUtil {

	private static String IP = "192.168.4.129";
	private static int PORT = 2181;

	Configuration conf = null;
	Connection conn = null;
	Table table = null;
	Admin admin = null;
	Tableinfo ti = null;
	List list = null;
	int count;

	// 关闭HBase连接
	public void connClose() {
		try {
			conn.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	// 关闭表连接
	public void tableClose() {
		try {
			table.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	// 关闭表管理连接
	public void adminClose() {
		try {
			admin.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public SplitPageUtil() {
		conf = HBaseConfiguration.create();
		conf.set(HConstants.ZOOKEEPER_QUORUM, IP);
		conf.setInt(HConstants.ZOOKEEPER_CLIENT_PORT, PORT);
	}

	/**
	 * 获取第一个表信息
	 * 
	 * @param tablename
	 * @return
	 */
	public Tableinfo getOneTableinfo(String tablename) {
		try {
			conn = ConnectionFactory.createConnection(conf);
			table = conn.getTable(TableName.valueOf(tablename));
			Scan scan = new Scan();
			scan.setMaxVersions(1);
			ResultScanner rs = table.getScanner(scan);
			Cell[] cells = rs.next().rawCells();
			rs.close();
			Cell cell = cells[0];
			ti = new Tableinfo();
			ti.setRowkey(Bytes.toString(CellUtil.cloneRow(cell)));// 获取行键
			ti.setFamily(Bytes.toString(CellUtil.cloneFamily(cell)));// 获取列族
			ti.setQualifier(Bytes.toString(CellUtil.cloneQualifier(cell)));// 获取列
			ti.setTimestamp(cell.getTimestamp());// 获取时间戳
			ti.setValue(Bytes.toString(CellUtil.cloneValue(cell)));// 获取值
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			connClose();
			tableClose();
		}
		return ti;
	}

	/**
	 * 根据表信息获取不包括该信息的顺序的i条表信息
	 * 
	 * @param tablename
	 * @param tableinfo
	 * @param i
	 * @return
	 */
	public List getNumberTableinfo(String tablename, Tableinfo tableinfo, int i) {
		try {
			conn = ConnectionFactory.createConnection(conf);
			table = conn.getTable(TableName.valueOf(tablename));
			list = new ArrayList();
			Scan scan = new Scan(Bytes.toBytes(tableinfo.getRowkey()));
			scan.setMaxVersions(1);
			ResultScanner rs = table.getScanner(scan);
			int is = -1;
			boolean no = false;
			for (Result re : rs) {
				for (Cell cell : re.rawCells()) {
					ti = new Tableinfo();
					ti.setRowkey(Bytes.toString(CellUtil.cloneRow(cell)));// 获取键
					ti.setFamily(Bytes.toString(CellUtil.cloneFamily(cell)));// 获取列族
					ti.setQualifier(Bytes.toString(CellUtil.cloneQualifier(cell)));// 获取列
					ti.setTimestamp(cell.getTimestamp());// 获取时间戳
					ti.setValue(Bytes.toString(CellUtil.cloneValue(cell)));// 获取值
					if (tableinfo.getRowkey().equals(ti.getRowkey()) && tableinfo.getFamily().equals(ti.getFamily())
							&& tableinfo.getQualifier().equals(ti.getQualifier())
							&& tableinfo.getTimestamp() == ti.getTimestamp()
							&& tableinfo.getValue().equals(ti.getValue())) {
						no = true;
					}
					if (no) {
						list.add(ti);
						is++;
					}
					if (i == is) {
						break;
					}
				}
				if (i == is) {
					break;
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			connClose();
			tableClose();
		}
		return list;
	}

	/**
	 * 获取该表的总记录数
	 * 
	 * @param tablename
	 * @return
	 */
	public int getcountTableinfo(String tablename) {
		try {
			conn = ConnectionFactory.createConnection(conf);
			table = conn.getTable(TableName.valueOf(tablename));
			Scan scan = new Scan();
			ResultScanner rs = table.getScanner(scan);
			count = 0;
			for (Result re : rs) {
				count += re.size();
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			connClose();
			tableClose();
		}
		return count;
	}

	/**
	 * 
	 * @param currentPage
	 *            当前页数
	 * @param pageSize
	 *            页面大小
	 * @param totalCount
	 *            总条数
	 * @param totalPage
	 *            总页数
	 * @return
	 */
	public Page getPage(String tablename, int currentPage, int pageSize) {
		Page page = new Page();
		if (currentPage == 1) {
			page.setLast(getOneTableinfo(tablename));
		} else {
			page.setLast(getAppoint(tablename, currentPage, pageSize));
		}
		int totalCount = getcountTableinfo(tablename);
		int totalPage = (int) Math.ceil((double) totalCount / (double) pageSize);// 向上取整
		if (currentPage > totalPage) {
			System.out.println("输入页码超过总页数");
			return null;
		}
		page.setCurrentPage(currentPage);
		page.setPageSize(pageSize);
		page.setTotalCount(totalCount);
		page.setTotalPage(totalPage);
		list = getNumberTableinfo(tablename, page.getLast(), pageSize);
		Tableinfo tl = list.get(list.size() - 1);
		page.setLast(tl);
		// 判断是否是最后一页
		if (currentPage != totalPage) {
			list.remove(list.size() - 1);
		}
		page.setList(list);
		return page;
	}

	/**
	 * 获取指定页的表信息
	 * 
	 * @param currentPage
	 * @param pageSize
	 * @return
	 */
	public Tableinfo getAppoint(String tablename, int currentPage, int pageSize) {
		try {
			conn = ConnectionFactory.createConnection(conf);
			table = conn.getTable(TableName.valueOf(tablename));
			Scan scan = new Scan();
			scan.setMaxVersions(1);
			ResultScanner rs = table.getScanner(scan);
			int count = 0;
			for (Result re : rs) {
				for (Cell cell : re.rawCells()) {
					count++;
					if (count == ((currentPage - 1) * pageSize) + 1) {
						ti = new Tableinfo();
						ti.setRowkey(Bytes.toString(CellUtil.cloneRow(cell)));// 获取行键
						ti.setFamily(Bytes.toString(CellUtil.cloneFamily(cell)));// 获取列族
						ti.setQualifier(Bytes.toString(CellUtil.cloneQualifier(cell)));// 获取列
						ti.setTimestamp(cell.getTimestamp());// 获取时间戳
						ti.setValue(Bytes.toString(CellUtil.cloneValue(cell)));// 获取值
					}
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			connClose();
			tableClose();
		}
		return ti;
	}
}

4

mysplitpage包:

SplitPageUtilTest类:

package mysplitpage;

import java.util.List;

import vo.Tableinfo;

public class SplitPageUtilTest {

	public static void main(String[] args) {
		SplitPageUtil s = new SplitPageUtil();
		String tablename = "testtable";

		// Tableinfo t = s.getOneTableinfo(tablename);
		// System.out.println(t);
		//
		// t.setRowkey("row-1");
		// t.setFamily("colfam2");
		// t.setQualifier("col-3");
		// t.setTimestamp(1498780148222l);
		// t.setValue("val-1.3");
		//
		// List l = s.getNumberTableinfo(tablename, t, 5);
		// for (Tableinfo st : l) {
		// System.out.println(st);
		// }

		// int i = s.getcountTableinfo(tablename);
		// System.out.println(i);

		Page p = s.getPage(tablename, 3, 11);
		System.out.println(p);

		// Tableinfo a = s.getAppoint(tablename, 2, 11);
		// System.out.println(a);
	}

}


HBase java API mysplitpage 我的HBasejavaAPI翻页实现_第1张图片


HBase java API mysplitpage 我的HBasejavaAPI翻页实现_第2张图片


HBase java API mysplitpage 我的HBasejavaAPI翻页实现_第3张图片




你可能感兴趣的:(HBase,java,API,HBase,java,API,分页)