微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

HBase javaAP二:实现对表简单的增删查改

前面讲个JavaAPI几个主要类的使用方法

这次使用JavaAPI实现在hbase上 新建、删除表,以及在表里面插入、删除查询数据,里面有注释

package Demo;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
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.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
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.Delete;
import org.apache.hadoop.hbase.client.Get;
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.Row;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;

//import org.apache.hadoop.hbase.ipc.RpcServer.Connection;

public class HBaseAPI {
	static final String tablename="test";//设置表名
	static final String FAMILY1="info";//列簇名1
	static final String FAMILY2="scors";//列簇名2
	static final String ROWKEY1="rk-100";//行号1
	static final String ROWKEY2="rk-200";//行号2
	static final String COL1="name";//列名1
	static final String COL2="age";//列名2
	
	public static void main(String[] args) throws IOException, InterruptedException{
		
		//hbase连接配置
		Configuration conf = HBaseConfiguration.create();
		conf.set("hbase.master", "master:16000");
		conf.set("hbase.rootdir", "hdfs://master:8020/hbase");
		conf.set("hbase.zookeeper.quorum", "slave1,slave2,slave3");
		conf.set("hbase.zookeeper.property.clientPort", "2181");
		
		Connection conn = ConnectionFactory.createConnection(conf);
		
		//调用函数进行增删查改
		createTable(conn);
		putData(conn);
		getData(conn);
		scanData(conn);
		alterFamily(conn);
		batchData(conn);
	}
	
	//创建或删除表
	public static void createTable(Connection conn) throws IOException{
		Admin admin=conn.getAdmin();
		TableName tableName=TableName.valueOf(tablename);
		//设计表结构
		HTableDescriptor ht=new HTableDescriptor(tableName);//创建表
		ht.addFamily(new HColumnDescriptor(FAMILY1));//创建列簇1
		ht.addFamily(new HColumnDescriptor("FAMILY2").setMaxVersions(10));//创建列簇2并设置版本为10
		byte[][] regions=new byte[][]{   //设置region分割区间
			Bytes.toBytes("10"),
			Bytes.toBytes("50"),
			Bytes.toBytes("90"),			
		};
		
		if(!admin.tableExists(tableName)){  //判断表是否存在
			admin.createTable(ht,regions);
		}
		else{
			if(!admin.isTabledisabled(tableName)){   //判断表是否有为disable状态
				admin.disableTable(tableName);
			}
			admin.deleteTable(tableName);
			System.out.println(tablename+"表已删除");
			admin.createTable(ht,regions);
			
		}
		System.out.println(tablename+"表已创建");
	}
	
	//插入数据到表中
	public static void putData(Connection conn) throws IOException{
		//建立表连接
		Table table=conn.getTable(TableName.valueOf(tablename));
		
		Put put =new Put(ROWKEY1.getBytes());
		put.addColumn(FAMILY1.getBytes(), COL1.getBytes(), "zhansan".getBytes());
		put.addColumn(FAMILY1.getBytes(), COL2.getBytes(), "20".getBytes());
		table.put(put);
		
		put =new Put(ROWKEY2.getBytes());
		put.addColumn(FAMILY1.getBytes(), COL1.getBytes(), "lisi".getBytes());
		put.addColumn(FAMILY1.getBytes(), COL2.getBytes(), "25".getBytes());
		table.put(put);
		
		System.out.println("插入数据成功");
	}
	
	//读取数据
	public static void getData(Connection conn) throws IOException{
		//建立表连接
		Table table=conn.getTable(TableName.valueOf(tablename));
		
		Get get=new Get(ROWKEY1.getBytes());//读取ROWKEY1行的数据
		get.addFamily(FAMILY1.getBytes());//读取FAMILY1簇的数据
		Result result= table.get(get);
		
//		System.out.println(new String(result.getValue(FAMILY1.getBytes(), COL1.getBytes())));
		for(Cell cell:result.rawCells()){
			System.out.print(new String(CellUtil.cloneRow(cell))+'\t');
			System.out.print(new String(CellUtil.cloneFamily(cell))+':');
			System.out.print(new String(CellUtil.cloneQualifier(cell))+' ');
			System.out.print(new String(CellUtil.cloneValue(cell)));
			System.out.println();
		}
//		System.out.println(new String(result.value()));
		
	}
	
	//scan获取数据
	public static void scanData(Connection conn) throws IOException{
		//建立表连接
		Table table=conn.getTable(TableName.valueOf(tablename));
		
//		Scan scan=new Scan(ROWKEY2.getBytes());//查看rowkey为ROWKEY2的数据,如果查询全部则去掉ROWKEY2.getBytes()
		Scan scan=new Scan();
		//设置rowkey区间,区间为左闭右开
		scan.setStartRow("rk-100".getBytes());
		scan.setStopRow("rk-300".getBytes());
		
		ResultScanner scanner = table.getScanner(scan);
		Iterator<Result> results = scanner.iterator();
		while(results.hasNext()){
			Result result=results.next();
			for(Cell cell:result.rawCells()){
				System.out.print(new String(CellUtil.cloneRow(cell))+'\t');
				System.out.print(new String(CellUtil.cloneFamily(cell))+':');
				System.out.print(new String(CellUtil.cloneQualifier(cell))+' ');
				System.out.print(new String(CellUtil.cloneValue(cell)));
				System.out.println();
			}
		}
	}
	
	//修改列簇
	public static void alterFamily(Connection conn) throws IOException{
		Admin admin = conn.getAdmin();
		TableName tableName = TableName.valueOf(tablename);
		if(admin.tableExists(tableName)){
//			admin.disableTable(tableName);
			HTableDescriptor hTableDescriptor = admin.getTableDescriptor(TableName.valueOf(tablename));
			hTableDescriptor.removeFamily("info".getBytes());//删除info列簇
			System.out.println("列簇以删除");
			hTableDescriptor.addFamily(new HColumnDescriptor("info1"));//新增info1列簇
			System.out.println("列簇已添加");
			admin.modifyTable(tableName, hTableDescriptor);
			admin.enableTable(tableName);
//			admin.close();
			
		}
		
	}
	
	//将创建表,插入数据,删除数据、查看数据整合一起远行
	public static void batchData(Connection conn) throws IOException, InterruptedException{
	    Table table = conn.getTable(TableName.valueOf(tablename));
	  
	    List<Row> batch = new ArrayList<Row>();
	  
	    Put put = new Put(ROWKEY1.getBytes());//插入ROWKEY1簇数据
	    put.addColumn("scors".getBytes(), "courseid".getBytes(),"c001".getBytes());//参数:列簇,列名,值
	    put.addColumn("info".getBytes(), "courseid".getBytes(),"c001".getBytes());
	    batch.add(put);
	    
	    put = new Put(ROWKEY2.getBytes());
	    put.addColumn("scors".getBytes(), "courseid".getBytes(),"c001".getBytes());
	    put.addColumn("info".getBytes(), "courseid".getBytes(),"c001".getBytes());
	    batch.add(put);
	  
	    Delete delete = new Delete(ROWKEY2.getBytes());
	  	delete.addFamily("info".getBytes());//删除rk-200里面的“info”簇数据
	  	batch.add(delete);
	  
	  	Get get = new Get(ROWKEY1.getBytes());
	  	get.getMaxVersions();
	  	batch.add(get);
	  	Object[] results = new Object[batch.size()];
	  	table.batch(batch,results);
	  	Result result = (Result) results[3];
	  	for(Cell cell:result.rawCells()){		
	  		System.out.println(new String(CellUtil.cloneRow(cell)));
		   	System.out.println(new String(CellUtil.cloneFamily(cell)));
		   	System.out.println(new String(CellUtil.cloneQualifier(cell)));
		   	System.out.println(new String(CellUtil.cloneValue(cell)));
		   	System.out.println();
		  }
	 }
	
}

 

创建表结果

插入数据结构

读取数据结果

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐