刚开始学习hadoop

刚刚写了或的表描述、扫描表数据、添加表数据、创建表。代码如下:

package com.hbase.test;



import org.apache.hadoop.conf.Configuration;
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.HBaseAdmin;

/**
 * 
 * 类功能说明:创建表
 *
 * @author 
 * @version 
 */
public class HbaseTest {

	public static void main(String[] args) throws Exception {
		//创建一个配置类
		Configuration conf = HBaseConfiguration.create();		
		//配置hbase的zookeeper
		conf.set("hbase.zookeeper.quorum", "hmaster,hslave01,hslave02");
		
		//创建数据库管理员
		HBaseAdmin admin = new HBaseAdmin(conf) ;
		
		//创建表描述对象
		HTableDescriptor hdc = new HTableDescriptor(TableName.valueOf("sc")) ;
		
		//列描述对象
		HColumnDescriptor hcd = new HColumnDescriptor("cinfo") ;
		hcd.setMaxVersions(3) ;	//设置最大版本号
		
		//进行设置
		hdc.addFamily(hcd);
		if(!admin.tableExists("sc")){
			admin.createTable(hdc);
		}
		//释放 资源
		admin.close();
		System.out.println("===表创建成功===");
	}

}

package com.hbase.test;

import java.io.IOException;

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.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.Scan;
import org.apache.hadoop.hbase.util.Bytes;

public class HbaseDome2 {
	
	private static Configuration conf = null;
	static{
		conf = HBaseConfiguration.create();
		conf.set("hbase.zookeeper.quorum", "hmaster,hslave01,hslave02");
	}
	//添加数据
	public static void addOneDate() throws IOException{
		//创建表对象
		HTable ht  = new HTable(conf,"sc");
		
		//创建put对象 
		Put put = new Put(Bytes.toBytes("p0001"));
		
		put.add(Bytes.toBytes("cinfo"), Bytes.toBytes("name"),Bytes.toBytes("zhangsan"));
		
		//添加put到表对象
		ht.put(put);
		ht.close();
		System.out.println("数据插入成功");
	}
	
	//获得表描述
	public static void getTableDesc(String tableName) throws IOException{
		HTable ht  = new HTable(conf,"sc");
		//获得表描述对象
		HTableDescriptor td = ht.getTableDescriptor();
		//获得列描述对象数组
		HColumnDescriptor[] hds = td.getColumnFamilies();
		for(HColumnDescriptor hd : hds){
			//列族名
			String name = hd.getNameAsString();
			int bs = hd.getBlocksize();
			int minVers = hd.getMinVersions();
			int maxVers = hd.getMaxVersions();
			int defVers = HColumnDescriptor.DEFAULT_VERSIONS;
			System.out.println("name"+name+
					"blocksize"+bs+"minVer:"+minVers+"maxVer:"+maxVers+"defVer:"+defVers);	
		}
		ht.close();
	}
	//获取表的所有数据
	public static void scanTable(String tableName) throws IOException{
		HTable ht= new HTable(conf,"sc");
		
		Scan scan = new Scan();
		
		//ResultScanner	是客户端获取值的接口
		ResultScanner scanner = ht.getScanner(scan);
		//每行的数据就是result,存储get获得scan操作后获得单行的值、
		for (Result res : scanner) {
			
			for (Cell cell : res.listCells()) {
				System.out.println("***********************");
				System.out.println("行键 rowkey:"+Bytes.toString(res.getRow()));
				System.out.println("列族 clumnFam:"+Bytes.toString(CellUtil.cloneFamily(cell)));
				System.out.println("列 clumn:"+Bytes.toString(CellUtil.cloneQualifier(cell)));
				System.out.println("时间戳 timestamp:"+cell.getTimestamp());
				System.out.println("值 value:"+Bytes.toString(CellUtil.cloneValue(cell)));
				System.out.println("mvcc版本"+cell.getMvccVersion());
			}
		}
		ht.close();
		
	}
	public static void main(String[] args) throws IOException {
		//getTableDesc("sc");
		//addOneDate();
		scanTable("sc");
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值