Hbase Client Test Case

好吧,其实就是一个简单的Hbase客户端java操作

HbaseTestCase.java

package hbase;import java.io.IOException;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.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;/** * * <p> * Title: HbaseTestCase.java * Package hbase * </p> * <p> * Description: Hbase Client TestCase * <p> * @author Tom.Cai * @created 2015-4-28 下午9:14:01 * @version V1.0 * */public class HbaseTestCase {public static final String TABLE_NAME = "table1";public static final String FAMILY_NAME = "family1";public static final String ROW_KEY = "row1";public static void main(String[] args) throws Exception {Configuration conf = HBaseConfiguration.create();//定位到hadoopconf.set("hbase.rootdir", "hdfs://192.168.80.100:9000/hbase");//hadoop地址也可以配置成主机名的形式conf.set("hbase.zookeeper.quorum", "192.168.80.100");HBaseAdmin admin = new HBaseAdmin(conf);//新增表createTable(admin);//删除表//deleteTable(admin);HTable htable = new HTable(conf, TABLE_NAME);//增加表记录//addRecodes(htable);//获取表记录getRecodes(htable);//全表扫描表scanTable(htable);}private static void scanTable(HTable htable) throws IOException {Scan scan = new Scan();ResultScanner rScanner = htable.getScanner(scan);for (Result result : rScanner) {byte[] be = result.getValue(FAMILY_NAME.getBytes(), "qualifier".getBytes());System.out.println(result+"—->value:"+new String(be));}}private static void getRecodes(HTable htable) throws IOException {Get get = new Get(ROW_KEY.getBytes());Result result = htable.get(get);byte[] b = result.getValue(FAMILY_NAME.getBytes(), "qualifier".getBytes());System.out.println(new String(b));}private static void addRecodes(HTable htable) throws IOException {Put put = new Put(ROW_KEY.getBytes());put.add(FAMILY_NAME.getBytes(),"qualifier".getBytes(),"value".getBytes());htable.put(put);}private static void deleteTable(HBaseAdmin admin) throws IOException {admin.disableTable(TABLE_NAME);admin.deleteTable(TABLE_NAME);}private static void createTable(HBaseAdmin admin) throws IOException {if(!admin.tableExists(TABLE_NAME)){HTableDescriptor desc = new HTableDescriptor(TABLE_NAME);HColumnDescriptor family = new HColumnDescriptor(FAMILY_NAME);desc.addFamily(family);admin.createTable(desc);}}}

我的个人网站:

我的CSDN博客地址:

,没有一种不通过蔑视、忍受和奋斗就可以征服的命运。

Hbase Client Test Case

相关文章:

你感兴趣的文章:

标签云: