hbase功能实现API

来源:互联网 发布:网络四大名著三大奇书 编辑:程序博客网 时间:2024/05/29 23:23

hbase功能实现API


package com.ucky.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.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;import com.sun.org.apache.bcel.internal.generic.FMUL;public class hbase {private static final String TABLE_NAME="table1";private static final String FAMILY_NAME="family";private static final String ROW_KEY="row1";public static void main(String[] args) throws Exception {Configuration conf = HBaseConfiguration.create();conf.set("hbase.rootdir", "hdfs://hadoop-master.dragon.org:9000/hbase");conf.set("hbase.zookeeper.quorum", "hadoop-master");HBaseAdmin hBaseAdmin = new HBaseAdmin(conf);HTable hTable = new HTable(conf, TABLE_NAME);//createTable(hBaseAdmin);//deleteTable(hBaseAdmin);//createRow(hTable);//scanTable(hTable);}private static void scanTable(HTable hTable) throws IOException {Scan scan = new Scan();ResultScanner scanner = hTable.getScanner(scan);for (Result sc : scanner) {byte[] value = sc.getValue(FAMILY_NAME.getBytes(), "age".getBytes());System.out.println(sc + "\t" + new String(value));}hTable.close();}private static void createRow(HTable hTable) throws IOException {Put put =  new Put(ROW_KEY.getBytes());put.add(FAMILY_NAME.getBytes(), "age".getBytes(), "22".getBytes());hTable.put(put);hTable.close();}private static void deleteTable(HBaseAdmin hBaseAdmin) throws IOException {hBaseAdmin.disableTable(TABLE_NAME);hBaseAdmin.deleteTable(TABLE_NAME);}private static void createTable(HBaseAdmin hBaseAdmin) throws IOException {HTableDescriptor desc = new HTableDescriptor(TABLE_NAME);HColumnDescriptor family = new HColumnDescriptor(FAMILY_NAME);desc.addFamily(family);hBaseAdmin.createTable(desc);}}



0 0