hbase建表

import java.util.ArrayList;

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.KeyValue;
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.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;

public class HBaseUtil {
	private static HBaseConfiguration conf = null;

	static {
		conf = new HBaseConfiguration();
		//conf.set("hbase.master", "10.3.61.141:60000");
		//conf.set("hbase.zookeeper.quorum", "hadoop141,hadoop142,hadoop143,hadoop144");
		//conf.set("hbase.master.port", "60000");
		conf.addResource("hbase-site.xml");
	}

	// add
	public void createTable(String tableName, String[] cfs) throws IOException {
		HBaseAdmin admin = new HBaseAdmin(conf);
		if (admin.tableExists(tableName)) {
			System.out.println("表已经存在了");
		} else {
			HTableDescriptor tableDesc = new HTableDescriptor(tableName);//表描述
			for (int i = 0; i < cfs.length; i++) {
				tableDesc.addFamily(new HColumnDescriptor(cfs[i]));//列族
			}
			admin.createTable(tableDesc);
			System.out.println("表创建成功!");
		}
	}

	// delete table
	public void deleteTable(String tableName) throws IOException {
		try {
			HBaseAdmin admin = new HBaseAdmin(conf);
			admin.disableTable(tableName);
			admin.deleteTable(tableName);
			System.out.println("删除表成功");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void writeRow(String tableName, String[] cfs) {
		try {
			HTable table = new HTable(conf, tableName);
			Put put = new Put(org.apache.hadoop.hbase.util.Bytes.toBytes(cfs[0]));
			put.add(org.apache.hadoop.hbase.util.Bytes.toBytes(cfs[1]),
						org.apache.hadoop.hbase.util.Bytes.toBytes(cfs[2]),
						org.apache.hadoop.hbase.util.Bytes.toBytes(cfs[3]));
			table.put(put);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	// delete value
	public void deleteRow(String tableName, String rowKey) throws IOException {
		HTable table = new HTable(conf, tableName);
		java.util.List<Delete> list = new ArrayList<Delete>();
		Delete dl = new Delete(rowKey.getBytes());
		list.add(dl);
		table.delete(list);
		System.out.println("删除成功!");
	}

	public void selectRow(String tableName, String rowKey) throws IOException {
		HTable table = new HTable(conf, tableName);
		Get g = new Get(rowKey.getBytes());
		Result rs = table.get(g);
		for (KeyValue kv : rs.raw()) {
			System.out.println(new String(kv.getRow()) + " ");
			System.out.println(new String(kv.getFamily()) + ":");
			System.out.println(new String(kv.getQualifier()) + " ");
			System.out.println(kv.getTimestamp() + " ");
			System.out.println(new String(kv.getValue()) + " ");
		}
	}

	public void scaner(String tableName) {
		try {
			HTable table = new HTable(conf, tableName);
			Scan scan = new Scan();
			ResultScanner rs = table.getScanner(scan);

			for (Result r : rs) {
				KeyValue[] kv = r.raw();
				for (int i = 0; i < kv.length; i++) {
					System.out.print(new String(kv[i].getRow()) + " ");
					System.out.print(new String(kv[i].getFamily()) + ": ");
					System.out.print(new String(kv[i].getQualifier()) + " ");
					System.out.print(kv[i].getTimestamp() + " ");
					System.out.println(new String(kv[i].getValue()));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

 

 

2.HBaseTest.java

 

package cn.netcenter.test;

import java.io.IOException;

import cn.netcenter.hbase.HBaseUtil;

public class HBaseTest {

	public static void main(String[] args) throws IOException {
		//createTable();
		getRow();
		//put();
		
	}
	
	public static void createTable() throws IOException{
		HBaseUtil util=new HBaseUtil();
		String[] cfs={"grade","course"};
		util.createTable("score", cfs);
	}
	
	public static void put() throws IOException{
		HBaseUtil util=new HBaseUtil();
		//String[] cfs={"zkb","grade","","5"};
		String[] cfs={"baoniu","course","art","80"};
		
		util.writeRow("scores", cfs);
		
	}
	
	public static void scaner()throws IOException{
		HBaseUtil util=new HBaseUtil();
		util.scaner("bio");
	}
	

	public static void getRow()throws IOException{
		HBaseUtil util=new HBaseUtil();
		util.selectRow("scores", "baoniu");
	}
	public static void deleteTable()throws IOException{
		HBaseUtil util=new HBaseUtil();
		util.deleteTable("score");
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值