Hbase学习--------javaAPI

3 篇文章 0 订阅
package com.xjq.phone;

import java.io.IOException;
import java.io.InterruptedIOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.RetriesExhaustedWithDetailsException;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;


public class PhoneDemo {
	 //表管理
	static HBaseAdmin admin = null;
	//数据管理
	static HTable table = null;
	//zookeeper节点
	static String zookeeperQuorum = "node02,node03,node04";
	//表名
	static String tableName="phone";
	//随机数对象
	static Random rd = new Random();
	/**
	 * 初始化
	 * @throws Exception 
	 * @throws ZooKeeperConnectionException 
	 * @throws Exception 
	 */
	
	public static void init() throws Exception {
		Configuration configuration = new Configuration();
		configuration.set("hbase.zookeeper.quorum", zookeeperQuorum);
		admin=new HBaseAdmin(configuration);
		table = new HTable(configuration, tableName.getBytes());
	}
	/**
	 * 创建表
	 * @throws Exception 
	 */
	
	public static void createTable() throws Exception {
		HTableDescriptor desc =new HTableDescriptor(TableName.valueOf(tableName));
		HColumnDescriptor family = new HColumnDescriptor("cf");
		desc.addFamily(family );
		if(admin.tableExists(tableName)) {
			admin.disableTable(tableName);
			admin.deleteTable(tableName);
		}
		admin.createTable(desc );
	}
	
	/**
	 * 插入数据
	 * @throws Exception 
	 * @throws RetriesExhaustedWithDetailsException 
	 */
	
	public static void insert() throws  Exception {
		List<Put> puts = new ArrayList<Put>();
		for(int i =0;i<10;i++) {
			String phone = getPhone("181");
			for(int j = 0;j<100;j++) {
				String oppPhone =getPhone("138");
				String longTime = String.valueOf(rd.nextInt(99));
				String type = String.valueOf(rd.nextInt(2));
				String date = getDate("2018");
				String rowKey = phone+"_"+String.valueOf((Long.MAX_VALUE-Long.valueOf(date)));
				Put put = new Put(rowKey.getBytes());
				put.add("cf".getBytes(), "opp_phone".getBytes(), oppPhone.getBytes());
				put.add("cf".getBytes(), "long_time".getBytes(), longTime.getBytes());
				put.add("cf".getBytes(), "type".getBytes(), type.getBytes());
				put.add("cf".getBytes(), "date".getBytes(), date.getBytes());
				puts.add(put);
			}
		}
		table.put(puts);
		
	}
	/**
	 * get某一行数据
	 * @throws Exception 
	 */
	
	public static void get() throws Exception {
		init();
		Get get =new Get("18195812094_9223351856752720387".getBytes());
		get.addColumn("cf".getBytes(),"opp_phone".getBytes());
		get.addColumn("cf".getBytes(),"long_time".getBytes());
		get.addColumn("cf".getBytes(),"date".getBytes());
		get.addColumn("cf".getBytes(),"type".getBytes());
		Result result = table.get(get);
		Cell cell1 = result.getColumnLatestCell("cf".getBytes(), "opp_phone".getBytes());
		Cell cell2 = result.getColumnLatestCell("cf".getBytes(), "long_time".getBytes());
		Cell cell3 = result.getColumnLatestCell("cf".getBytes(), "date".getBytes());
		Cell cell4 = result.getColumnLatestCell("cf".getBytes(), "type".getBytes());
		System.out.println(Bytes.toString(CellUtil.cloneValue(cell1)));
		System.out.println(Bytes.toString(CellUtil.cloneValue(cell2)));
		System.out.println(Bytes.toString(CellUtil.cloneValue(cell3)));
		System.out.println(Bytes.toString(CellUtil.cloneValue(cell4)));
		destory();
	}
	

	/**
	 * scan扫描表的部分数据
	 * @throws Exception 
	 */
	
	public static void scan() throws Exception {
		init();
		Scan scan =new Scan();
		String startRow= "18195812094_"+String.valueOf((Long.MAX_VALUE-Long.valueOf("20180101000000")));
		String stopRow= "18195812094_"+String.valueOf((Long.MAX_VALUE-Long.valueOf("20181231235959")));
		System.out.println(stopRow);  //字典序,倒序排序,小值在后,大值在前
		System.out.println(startRow);
		scan.setStartRow(stopRow.getBytes());
		scan.setStopRow(startRow.getBytes());
		ResultScanner rss =table.getScanner(scan);
		for (Result result : rss) {
			Cell cell1 = result.getColumnLatestCell("cf".getBytes(), "opp_phone".getBytes());
			Cell cell2 = result.getColumnLatestCell("cf".getBytes(), "long_time".getBytes());
			Cell cell3 = result.getColumnLatestCell("cf".getBytes(), "date".getBytes());
			Cell cell4 = result.getColumnLatestCell("cf".getBytes(), "type".getBytes());
			System.out.print(Bytes.toString(CellUtil.cloneValue(cell1)));
			System.out.print(Bytes.toString(CellUtil.cloneValue(cell2)));
			System.out.print(Bytes.toString(CellUtil.cloneValue(cell3)));
			System.out.println(Bytes.toString(CellUtil.cloneValue(cell4)));
		}
		System.out.println("执行完毕");
		destory();
	}
	/**
	 * 随机生成手机号函数
	 */
	public static String getPhone(String string) {
		
		//string.format()格式化输出 。%08d:%d表示整数,8表示8位,0表示不删除前面的0
		return string +String.format("%08d", rd.nextInt(99999999));
	}
	/**
	 * 随机生成通话时间
	 */
	public static String getDate(String string) {
		return string+String.format("%02d%02d%02d%02d%02d", rd.nextInt(12)+1,rd.nextInt(30)+1,
				rd.nextInt(24),rd.nextInt(60),rd.nextInt(60));
	}
	
	/**
	 * 销毁函数
	 * @throws Exception 
	 */
	public static void destory() throws Exception {
		if(admin!=null) {
			admin.close();
		}
		if(table!=null) {
			table.close();
		}
	}
	public static void main(String[] args) throws Exception {
		get();
		//scan();
	}
}

借鉴他人的写法:

package com.sxt.hbase;

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.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HConnection;
import org.apache.hadoop.hbase.client.HConnectionManager;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.HTableInterface;
import org.apache.hadoop.hbase.client.HTablePool;
import org.apache.hadoop.hbase.client.Put;
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.filter.BinaryComparator;
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.PrefixFilter;
import org.apache.hadoop.hbase.filter.RowFilter;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.filter.SubstringComparator;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Test;


public class HBaseDAOImp {

	HConnection hTablePool = null;
	static Configuration conf =null;
	public HBaseDAOImp()
	{
		 conf = new Configuration();
		String zk_list = "node1,node2,node3";
		conf.set("hbase.zookeeper.quorum", zk_list);
		try {
			hTablePool = HConnectionManager.createConnection(conf) ;
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public void save(Put put, String tableName) {
		// TODO Auto-generated method stub
		HTableInterface table = null;
		try {
			table = hTablePool.getTable(tableName) ;
			table.put(put) ;
			
		} catch (Exception e) {
			e.printStackTrace() ;
		}finally{
			try {
				table.close() ;
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 插入一个cell
	 * @param tableName
	 * @param rowKey
	 * @param family
	 * @param quailifer
	 * @param value
	 */
	public void insert(String tableName, String rowKey, String family,
			String quailifer, String value) {
		// TODO Auto-generated method stub
		HTableInterface table = null;
		try {
			table = hTablePool.getTable(tableName) ;
			Put put = new Put(rowKey.getBytes());
			put.add(family.getBytes(), quailifer.getBytes(), value.getBytes()) ;
			table.put(put);
		} catch (Exception e) {
			e.printStackTrace();
		}finally
		{
			try {
				table.close() ;
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 在一个列族下插入多个单元格
	 * @param tableName
	 * @param rowKey
	 * @param family
	 * @param quailifer
	 * @param value
	 */
	public void insert(String tableName,String rowKey,String family,String quailifer[],String value[])
	{
		HTableInterface table = null;
		try {
			table = hTablePool.getTable(tableName) ;
			Put put = new Put(rowKey.getBytes());
			// 批量添加
			for (int i = 0; i < quailifer.length; i++) {
				String col = quailifer[i];
				String val = value[i];
				put.add(family.getBytes(), col.getBytes(), val.getBytes());
			}
			table.put(put);
		} catch (Exception e) {
			e.printStackTrace();
		}finally
		{
			try {
				table.close() ;
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	public void save(List<Put> Put, String tableName) {
		// TODO Auto-generated method stub
		HTableInterface table = null;
		try {
			table = hTablePool.getTable(tableName) ;
			table.put(Put) ;
		}
		catch (Exception e) {
			// TODO: handle exception
		}finally
		{
			try {
				table.close() ;
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
	}


	public Result getOneRow(String tableName, String rowKey) {
		// TODO Auto-generated method stub
		HTableInterface table = null;
		Result rsResult = null;
		try {
			table = hTablePool.getTable(tableName) ;
			Get get = new Get(rowKey.getBytes()) ;
			rsResult = table.get(get) ;
		} catch (Exception e) {
			e.printStackTrace() ;
		}
		finally
		{
			try {
				table.close() ;
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return rsResult;
	}
	
	/**
	 * 最常用的方法,优化查询
	 * 查询一行数据,
	 * @param tableName
	 * @param rowKey
	 * @param cols
	 * @return
	 */
	public Result getOneRowAndMultiColumn(String tableName, String rowKey,String[] cols) {
		// TODO Auto-generated method stub
		HTableInterface table = null;
		Result rsResult = null;
		try {
			table = hTablePool.getTable(tableName) ;
			Get get = new Get(rowKey.getBytes()) ;
			for (int i = 0; i < cols.length; i++) {
				get.addColumn("cf".getBytes(), cols[i].getBytes()) ;
			}
			rsResult = table.get(get) ;
		} catch (Exception e) {
			e.printStackTrace() ;
		}
		finally
		{
			try {
				table.close() ;
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return rsResult;
	}

	
	public List<Result> getRows(String tableName, String rowKeyLike) {
		// TODO Auto-generated method stub
		HTableInterface table = null;
		List<Result> list = null;
		try {
			FilterList fl = new FilterList(FilterList.Operator.MUST_PASS_ALL);
			table = hTablePool.getTable(tableName) ;
			PrefixFilter filter = new PrefixFilter(rowKeyLike.getBytes());
			SingleColumnValueFilter filter1 = new SingleColumnValueFilter(
					  "order".getBytes(),
					  "order_type".getBytes(),
					  CompareOp.EQUAL,
					  Bytes.toBytes("1")
					  );
			fl.addFilter(filter);
			fl.addFilter(filter1);
			Scan scan = new Scan();
			scan.setFilter(fl);
			ResultScanner scanner = table.getScanner(scan) ;
			list = new ArrayList<Result>() ;
			for (Result rs : scanner) {
				list.add(rs) ;
			}
		} catch (Exception e) {
			e.printStackTrace() ;
		}
		finally
		{
			try {
				table.close() ;
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return list;
	}
	
	
	public List<Result> getRows(String tableName, String rowKeyLike ,String cols[]) {
		// TODO Auto-generated method stub
		HTableInterface table = null;
		List<Result> list = null;
		try {
			table = hTablePool.getTable(tableName) ;
			PrefixFilter filter = new PrefixFilter(rowKeyLike.getBytes());
			
			Scan scan = new Scan();
			for (int i = 0; i < cols.length; i++) {
				scan.addColumn("cf".getBytes(), cols[i].getBytes()) ;
			}
			scan.setFilter(filter);
			ResultScanner scanner = table.getScanner(scan) ;
			list = new ArrayList<Result>() ;
			for (Result rs : scanner) {
				list.add(rs) ;
			}
		} catch (Exception e) {
			e.printStackTrace() ;
		}
		finally
		{
			try {
				table.close() ;
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return list;
	}
	
	public List<Result> getRowsByOneKey(String tableName, String rowKeyLike ,String cols[]) {
		// TODO Auto-generated method stub
		HTableInterface table = null;
		List<Result> list = null;
		try {
			table = hTablePool.getTable(tableName) ;
			PrefixFilter filter = new PrefixFilter(rowKeyLike.getBytes());
			
			Scan scan = new Scan();
			for (int i = 0; i < cols.length; i++) {
				scan.addColumn("cf".getBytes(), cols[i].getBytes()) ;
			}
			scan.setFilter(filter);
			ResultScanner scanner = table.getScanner(scan) ;
			list = new ArrayList<Result>() ;
			for (Result rs : scanner) {
				list.add(rs) ;
			}
		} catch (Exception e) {
			e.printStackTrace() ;
		}
		finally
		{
			try {
				table.close() ;
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return list;
	}
	
	/**
	 * 范围查询
	 * @param tableName
	 * @param startRow
	 * @param stopRow
	 * @return
	 */
	public List<Result> getRows(String tableName,String startRow,String stopRow)
	{
		HTableInterface table = null;
		List<Result> list = null;
		try {
			table = hTablePool.getTable(tableName) ;
			Scan scan = new Scan() ;
			scan.setStartRow(startRow.getBytes()) ;
			scan.setStopRow(stopRow.getBytes()) ;
			ResultScanner scanner = table.getScanner(scan) ;
			list = new ArrayList<Result>() ;
			for (Result rsResult : scanner) {
				list.add(rsResult) ;
			}
			
		}catch (Exception e) {
			e.printStackTrace() ;
		}
		finally
		{
			try {
				table.close() ;
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return list;
	}
	
	
	public void deleteRecords(String tableName, String rowKeyLike){
		HTableInterface table = null;
		try {
			table = hTablePool.getTable(tableName) ;
			PrefixFilter filter = new PrefixFilter(rowKeyLike.getBytes());
			Scan scan = new Scan();
			scan.setFilter(filter);
			ResultScanner scanner = table.getScanner(scan) ;
			List<Delete> list = new ArrayList<Delete>() ;
			for (Result rs : scanner) {
				Delete del = new Delete(rs.getRow());
				list.add(del) ;
			}
			table.delete(list);
		}
		catch (Exception e) {
			e.printStackTrace() ;
		}
		finally
		{
			try {
				table.close() ;
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
	}
	
	public void deleteCell(String tableName, String rowkey,String cf,String column){
		HTableInterface table = null;
		try {
			table = hTablePool.getTable(tableName) ;
			Delete del = new Delete(rowkey.getBytes());
			del.deleteColumn(cf.getBytes(), column.getBytes());
			table.delete(del);
		}
		catch (Exception e) {
			e.printStackTrace() ;
		}
		finally
		{
			try {
				table.close() ;
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
	}
	
	public   void createTable(String tableName, String[] columnFamilys){
		try {
			// admin 对象
			HBaseAdmin admin = new HBaseAdmin(conf);
			if (admin.tableExists(tableName)) {
				System.err.println("此表,已存在!");
			} else {
				HTableDescriptor tableDesc = new HTableDescriptor(
						TableName.valueOf(tableName));

				for (String columnFamily : columnFamilys) {
					tableDesc.addFamily(new HColumnDescriptor(columnFamily));
				}

				admin.createTable(tableDesc);
				System.err.println("建表成功!");

			}
			admin.close();// 关闭释放资源
		} catch (MasterNotRunningException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ZooKeeperConnectionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/**
	 * 删除一个表
	 * 
	 * @param tableName
	 *            删除的表名
	 * */
	public   void deleteTable(String tableName)   {
		try {
			HBaseAdmin admin = new HBaseAdmin(conf);
			if (admin.tableExists(tableName)) {
				admin.disableTable(tableName);// 禁用表
				admin.deleteTable(tableName);// 删除表
				System.err.println("删除表成功!");
			} else {
				System.err.println("删除的表不存在!");
			}
			admin.close();
		} catch (MasterNotRunningException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ZooKeeperConnectionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
 
	/**
	 * 查询表中所有行
	 * @param tablename
	*/
	public void scaner(String tablename) {
	try {
	        HTable table =new HTable(conf, tablename);
	        Scan s =new Scan();
//	        s.addColumn(family, qualifier)
//	        s.addColumn(family, qualifier)
	        ResultScanner rs = table.getScanner(s);
	for (Result r : rs) {
	            
		   for(Cell cell:r.rawCells()){   
			    System.out.println("RowName:"+new String(CellUtil.cloneRow(cell))+" ");
			    System.out.println("Timetamp:"+cell.getTimestamp()+" ");
			    System.out.println("column Family:"+new String(CellUtil.cloneFamily(cell))+" ");
			    System.out.println("row Name:"+new String(CellUtil.cloneQualifier(cell))+" ");
			    System.out.println("value:"+new String(CellUtil.cloneValue(cell))+" ");
			   }
	        }
	    } catch (IOException e) {
	        e.printStackTrace();
	    }
	}
	public void scanerByColumn(String tablename) {
		
		try {
			HTable table =new HTable(conf, tablename);
			Scan s =new Scan();
	        s.addColumn("cf".getBytes(), "201504052237".getBytes());
	        s.addColumn("cf".getBytes(), "201504052237".getBytes());
			ResultScanner rs = table.getScanner(s);
			for (Result r : rs) {
				
				for(Cell cell:r.rawCells()){   
					System.out.println("RowName:"+new String(CellUtil.cloneRow(cell))+" ");
					System.out.println("Timetamp:"+cell.getTimestamp()+" ");
					System.out.println("column Family:"+new String(CellUtil.cloneFamily(cell))+" ");
					System.out.println("row Name:"+new String(CellUtil.cloneQualifier(cell))+" ");
					System.out.println("value:"+new String(CellUtil.cloneValue(cell))+" ");
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public static void main(String[] args) {
		

		
		
//		创建表
//	    String tableName="test";
//		String cfs[] = {"cf"};
//		dao.createTable(tableName,cfs);
		
//		存入一条数据
//		Put put = new Put("bjsxt".getBytes());
//		put.add("cf".getBytes(), "name".getBytes(), "cai10".getBytes()) ;
//		dao.save(put, "test") ;

//		插入多列数据
//		Put put = new Put("bjsxt".getBytes());
//		List<Put> list = new ArrayList<Put>();
//		put.add("cf".getBytes(), "addr".getBytes(), "shanghai1".getBytes()) ;
//		put.add("cf".getBytes(), "age".getBytes(), "30".getBytes()) ;
//		put.add("cf".getBytes(), "tel".getBytes(), "13889891818".getBytes()) ;
//		list.add(put) ;
//		dao.save(list, "test");
		
//		插入单行数据
//		dao.insert("test", "testrow", "cf", "age", "35") ;
//		dao.insert("test", "testrow", "cf", "cardid", "12312312335") ;
//		dao.insert("test", "testrow", "cf", "tel", "13512312345") ;
		
		
		
//		List<Result> list = dao.getRows("test", "testrow",new String[]{"age"}) ;
//		for(Result rs : list)
//		{
//		    for(Cell cell:rs.rawCells()){   
//		        System.out.println("RowName:"+new String(CellUtil.cloneRow(cell))+" ");
//		        System.out.println("Timetamp:"+cell.getTimestamp()+" ");
//		        System.out.println("column Family:"+new String(CellUtil.cloneFamily(cell))+" ");
//		        System.out.println("row Name:"+new String(CellUtil.cloneQualifier(cell))+" ");
//		        System.out.println("value:"+new String(CellUtil.cloneValue(cell))+" ");
//		       }
//		}
		
//		Result rs = dao.getOneRow("test", "testrow");
//		 System.out.println(new String(rs.getValue("cf".getBytes(), "age".getBytes())));
		
//		Result rs = dao.getOneRowAndMultiColumn("cell_monitor_table", "29448-513332015-04-05", new String[]{"201504052236","201504052237"});
//		for(Cell cell:rs.rawCells()){   
//	        System.out.println("RowName:"+new String(CellUtil.cloneRow(cell))+" ");
//	        System.out.println("Timetamp:"+cell.getTimestamp()+" ");
//	        System.out.println("column Family:"+new String(CellUtil.cloneFamily(cell))+" ");
//	        System.out.println("row Name:"+new String(CellUtil.cloneQualifier(cell))+" ");
//	        System.out.println("value:"+new String(CellUtil.cloneValue(cell))+" ");
//	       }
		
//		dao.deleteTable("cell_monitor_table");
//		创建表
	    String tableName="cell_monitor_table";
		String cfs[] = {"cf"};
//		dao.createTable(tableName,cfs);
	}
	
	
	  public static void testRowFilter(String tableName){
	     	try {
	     		 HTable table =new HTable(conf, tableName);
				Scan scan = new Scan();
				scan.addColumn(Bytes.toBytes("column1"),Bytes.toBytes("qqqq"));
				Filter filter1 = new RowFilter(CompareOp.LESS_OR_EQUAL,new BinaryComparator(Bytes.toBytes("laoxia157")));
				scan.setFilter(filter1);
				ResultScanner scanner1 = table.getScanner(scan);
				for (Result res : scanner1) {
				    System.out.println(res);
				}
				scanner1.close();

//			 
//				Filter filter2 = new RowFilter(CompareFilter.CompareOp.EQUAL,new RegexStringComparator("laoxia4\\d{2}"));
//				scan.setFilter(filter2);
//				ResultScanner scanner2 = table.getScanner(scan);
//				for (Result res : scanner2) {
//				     System.out.println(res);
//				}
//				scanner2.close();

				Filter filter3 = new RowFilter( CompareOp.EQUAL,new SubstringComparator("laoxia407"));
				scan.setFilter(filter3);
				ResultScanner scanner3 = table.getScanner(scan);
				for (Result res : scanner3) {
				      System.out.println(res);
				}
				scanner3.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    }
	    
	  @Test
	  public void testTrasaction(){
		  try{
			  HTableInterface table = null;
			  table = hTablePool.getTable("t_test".getBytes());
//			  Put put1 =new Put("002".getBytes());
//			  put1.add("cf1".getBytes(), "name".getBytes(), "王五".getBytes());
//			  table.put(put1);
			  Put newput =new Put("001".getBytes());
			  newput.add("cf1".getBytes(), "like".getBytes(), "看书".getBytes());
			  
			  boolean f= table.checkAndPut("001".getBytes(), "cf1".getBytes(), "age".getBytes(), "24".getBytes(), newput);
			  System.out.println(f);
			  
		  }catch (Exception e){
			  e.printStackTrace();
		  }
		  
	  }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: HBase-Java API 是一种用于管理 HBase 表的编程接口。使用 HBase-Java API,开发人员可以编写 Java 代码来创建、删除、修改和查询 HBase 表。HBase-Java API 提供了一系列类和方法,可以让开发人员轻松地与 HBase 表进行交互。例如,可以使用 HBaseAdmin 类来管理 HBase 表,使用 HTable 类来访问 HBase 表中的数据,使用 Put 类来插入数据,使用 Get 类来获取数据,等等。总之,HBase-Java APIHBase 的重要组成部分,它为开发人员提供了强大的工具来管理和操作 HBase 表。 ### 回答2: HBase是一个分布式的列式存储数据库,在很多大数据应用中得到广泛的使用。它采用Hadoop作为其底层基础框架,同时提供了Java API供开发人员使用。HBaseJava API为开发人员提供了一个管理表的接口,使得开发人员可以对HBase数据库中的表进行创建、读取、修改和删除等基本操作。 首先,我们需要用Java API创建一个HBase数据库中的表。使用HBaseJava API创建表的流程如下: 1. 首先需要获取HBase Configuration对象,并设置HBase连接参数以连接HBase数据库。 2. 接下来,需要获取HBase Admin对象,以便在操作HBase数据库表之前检查表是否存在,如果表不存在,需要创建该表。 3. 通过HBaseJava API创建表时,需要指定表的表名、列族的名称以及版本数等属性。 4. 创建表时需要考虑表的region的分配问题,可以对表的region进行手动或自动分片,以此来控制HBase的负载均衡。 创建了HBase数据库中的表之后,我们可以使用Java API对表进行读写操作。在进行读写操作时,需要使用HBaseJava API提供的Get的方法来获取表中的数据、Scan方法来扫描整个表、以及Put方法来向表中插入数据。此外,在进行表操作时还需要设置一些常见的数据操作参数,例如版本数、时间戳等。 在使用HBaseJava API时,还需要注意一些性能优化的问题,例如何时启用缓存、何时触发分区策略以及如何优化HBase表的大小等。这些优化措施能够在HBase的性能以及数据读写时的延迟方面提供很好的支持和帮助。 总的来说,HBaseJava API提供的表管理接口为开发人员提供了非常方便和快捷的方式来操作HBase数据库中的表。通过使用这些API,开发人员可以创建、读取、修改和删除表的数据,并且能够充分应用HBase的分布式特性来优化数据管理和性能提升。 ### 回答3: HBase是一个开源、分布式、非关系型数据库,它可以存储和处理大规模结构化、半结构化和非结构化数据。HBase Java APIHBase的官方API,它提供了对HBase表的管理和操作功能,让开发人员可以通过Java代码轻松地连接到HBase数据库。 在使用HBase Java API管理表时,首先需要创建一个HBaseConfiguration对象,它包含了连接HBase数据库所需的配置信息,如Zookeeper地址、HBase根目录等。然后,可以使用HBaseAdmin类创建、删除、修改表,以及列族等操作。例如,创建一个表可以通过以下代码实现: ``` HBaseAdmin admin = new HBaseAdmin(HBaseConfiguration.create()); HTableDescriptor tableDescriptor = new HTableDescriptor("table_name"); HColumnDescriptor columnDescriptor = new HColumnDescriptor("column_family"); tableDescriptor.addFamily(columnDescriptor); admin.createTable(tableDescriptor); ``` 创建表时,需要先通过HTableDescriptor类定义表名称,然后通过HColumnDescriptor类定义列族名称。可以通过addFamily()方法将列族添加到表描述中,最后通过HBaseAdmin的createTable()方法创建表。 除了创建表之外,HBase Java API还提供了许多其他的操作,如获取表信息、获取所有表的列表、删除表等。同时,HBase Java API还提供了对数据的CRUD操作,包括put、get、scan、delete等方法,让开发人员可以方便地进行数据操作。 总之,HBase Java API是一个非常强大的工具,它可以使开发人员轻松地管理HBase数据库,并实现数据的高效存储和处理。但是,在使用HBase Java API时,需要了解HBase的基本知识和API的用法,才能更好地发挥其功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值