HBase 基本API操作

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

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

public class HBaseDemo {

	public static Configuration configuration;
	
	static {
		configuration = HBaseConfiguration.create();
//		configuration.set("hbase.zookeeper.quorum", "192.168.1.21");
//		configuration.set("hbase.zookeeper.property.clientPort", "2181");

	}
	
	// 判断表是否存在 true-》存在 false-》不存在
	public static boolean isExist(String tableName) throws IOException {
		Connection connection = ConnectionFactory.createConnection(configuration);
		Admin admin = connection.getAdmin();
		return admin.tableExists(TableName.valueOf(tableName));
	}
	
	// 创建表 
	public static void createTable(String tableName, String ...columnFamilys) throws IOException {
		if (!isExist(tableName)) {
			Connection connection = ConnectionFactory.createConnection(configuration);
			Admin admin = connection.getAdmin();
			HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
			for (String columnFamily : columnFamilys) {
				hTableDescriptor.addFamily(new HColumnDescriptor(columnFamily));
			}
			System.out.println("【"+tableName+"】表成功创建。。。");
			admin.createTable(hTableDescriptor);
		} else {
			System.out.println("【"+tableName+"】表已经存在。。。");
		}
		return;
	}
	
	// 删除表
	public static void deleteTable(String tableName) throws IOException {
		if (isExist(tableName)) {
			Connection connection = ConnectionFactory.createConnection(configuration);
			Admin admin = connection.getAdmin();
			if (admin.isTableEnabled(TableName.valueOf(tableName))) {
				admin.disableTable(TableName.valueOf(tableName));
			}
			admin.deleteTable(TableName.valueOf(tableName));
			System.out.println("【"+tableName+"】表成功删除。。。");
		} else {
			System.out.println("【"+tableName+"】表尚未创建。。。");
		}
		return;
	}
	
	// 向表中插入一行数据
	public static void addRowData(String tableName, String rowKey, String columnFamily, String column, String value) throws IOException {
		if (isExist(tableName)) {
			Connection connection = ConnectionFactory.createConnection(configuration);
			Table table = connection.getTable(TableName.valueOf(tableName));
			Put put = new Put(Bytes.toBytes(rowKey));
			put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytesBinary(value));
			table.put(put);
			table.close();
			System.out.println("【"+tableName+"】表插入数据成功。。。");
		} else {
			System.out.println("【"+tableName+"】表尚未创建。。。");
		}
		return;
	}
	
	// 向表中插入多行数据
	public static void addRowDatas(String tableName, String rowKey, String columnFamily, Map<String,String> kv) throws IOException {
		for (Entry<String, String> entry : kv.entrySet()) {
			addRowData(tableName, rowKey, columnFamily, entry.getKey(), entry.getValue());
		}
		return;
	}
	
	// 删除多行数据
	public static void deleteRowsData(String tableName, String ...rows) throws IOException {
		if (isExist(tableName)) {
			Connection connection = ConnectionFactory.createConnection(configuration);
			Table table = connection.getTable(TableName.valueOf(tableName));
			List<Delete> delList = new ArrayList<>();
			for (String row : rows) {
				Delete del = new Delete(Bytes.toBytes(row));
				delList.add(del);
			}
			table.delete(delList);
			table.close();
			System.out.println("【"+tableName+"】表删除数据成功。。。");
		} else {
			System.out.println("【"+tableName+"】表尚未创建。。。");
		}
		return;
	}
	
	// 得到所有数据
	public static void getDataWithTable(String tableName) throws IOException {
		Connection connection = ConnectionFactory.createConnection(configuration);
		Table table = connection.getTable(TableName.valueOf(tableName));
		Scan scan = new Scan();
		ResultScanner resultScanner = table.getScanner(scan);
		for (Result result : resultScanner) {
			Cell[] cells = result.rawCells();
			for (Cell cell : cells) {
				System.out.println("---------------------------------------");
				// 获取rowKey
				System.out.println(Bytes.toString(CellUtil.cloneRow(cell)));
				// 获取columnFamily
				System.out.println(Bytes.toString(CellUtil.cloneFamily(cell)));
				// 获取column
				System.out.println(Bytes.toString(CellUtil.cloneQualifier(cell)));
				// 获取value
				System.out.println(Bytes.toString(CellUtil.cloneValue(cell)));
			}
		}
		table.close();
		return;
	}
	
	// 得到某一行所有数据
	public static void getDataWithRowkey(String tableName, String rowKey) throws IOException {
		Connection connection = ConnectionFactory.createConnection(configuration);
		Table table = connection.getTable(TableName.valueOf(tableName));
		Get get = new Get(Bytes.toBytes(rowKey));
		Result result = table.get(get);
		Cell[] cells = result.rawCells();
		for (Cell cell : cells) {
			System.out.println("---------------------------------------");
			// 获取rowKey
			System.out.println(Bytes.toString(CellUtil.cloneRow(cell)));
			// 获取columnFamily
			System.out.println(Bytes.toString(CellUtil.cloneFamily(cell)));
			// 获取column
			System.out.println(Bytes.toString(CellUtil.cloneQualifier(cell)));
			// 获取value
			System.out.println(Bytes.toString(CellUtil.cloneValue(cell)));
		}
		table.close();
		return;
	}
	
	// 获取某一行指定“列族:列”的数据
	public static void getDataWithColumn(String tableName, String rowKey, String columnFamily, String column) throws IOException {
		Connection connection = ConnectionFactory.createConnection(configuration);
		Table table = connection.getTable(TableName.valueOf(tableName));
		Get get = new Get(Bytes.toBytes(rowKey));
		get.addColumn(Bytes.toBytes(columnFamily) ,Bytes.toBytes(column));
		Result result = table.get(get);
		Cell[] cells = result.rawCells();
		for (Cell cell : cells) {
			System.out.println("---------------------------------------");
			// 获取rowKey
			System.out.println(Bytes.toString(CellUtil.cloneRow(cell)));
			// 获取columnFamily
			System.out.println(Bytes.toString(CellUtil.cloneFamily(cell)));
			// 获取column
			System.out.println(Bytes.toString(CellUtil.cloneQualifier(cell)));
			// 获取value
			System.out.println(Bytes.toString(CellUtil.cloneValue(cell)));
		}
		table.close();
		return;
	}
	
	public static void main(String[] args) throws IOException {
		System.out.println("start......");
//		createTable("student", "describe", "ability");
		Map<String,String> bodyinfos = new HashMap<>();
		bodyinfos.put("sex", "male");
		bodyinfos.put("nick", "leo");
		bodyinfos.put("favor", "caoliu");
		addRowDatas("student", "9527", "describe", bodyinfos);
		addRowDatas("student", "9528", "describe", bodyinfos);
		addRowDatas("student", "9529", "describe", bodyinfos);
//		deleteRowsData("student", "9527", "9529");
//		deleteTable("student");
//		getDataWithTable("student");
//		getDataWithRowkey("student", "9527");
		getDataWithColumn("student", "9527", "describe", "nick");
		System.out.println("end......");
	}
}

maven 项目的依赖如下:

<dependency>
	    <groupId>org.apache.hbase</groupId>
	    <artifactId>hbase-server</artifactId>
	    <version>1.3.1</version>
	</dependency>
        <dependency>
	    <groupId>org.apache.hbase</groupId>
	    <artifactId>hbase-client</artifactId>
	    <version>1.3.1</version>
	</dependency>
    <!-- 下面这个依赖不是必须添加 -->
	<dependency>
		<groupId>jdk.tools</groupId>
		<artifactId>jdk.tools</artifactId>
		<version>1.6</version>
		<scope>system</scope>
		<systemPath>${JAVA_HOME}/lib/tools.jar</systemPath>
	</dependency>
还有为了让Configuration的值不用手动设置需要在/src/main/resources中加入下列文件和log输出文件

core-site.xml
hbase-site.xml
hdfs-site.xml
log4j.properties


猜你喜欢

转载自blog.csdn.net/liangzelei/article/details/80298043