1package Hbase; 2import java.io.IOException; 3import org.apache.hadoop.conf.Configuration; 4import org.apache.hadoop.hbase.HBaseConfiguration; 5import org.apache.hadoop.hbase.HColumnDescriptor; 6import org.apache.hadoop.hbase.HTableDescriptor; 7import org.apache.hadoop.hbase.MasterNotRunningException; 8import org.apache.hadoop.hbase.ZooKeeperConnectionException; 9import org.apache.hadoop.hbase.client.Get; 10import org.apache.hadoop.hbase.client.HBaseAdmin; 11import org.apache.hadoop.hbase.client.HTable; 12import org.apache.hadoop.hbase.client.Put; 13import org.apache.hadoop.hbase.client.Result; 14import org.apache.hadoop.hbase.client.ResultScanner; 15import org.apache.hadoop.hbase.client.Scan; 16public class HbaseApp { 17public static final String TABLE_NAME = "table1"; 18public static final String FAMILY_NAME = "family1"; 19public static final String ROW_KEY = "rowkey1"; 20public static void main(String[] args) throws Exception { 21// 创建表、删除表使用HbaseAdmin 22Configuration conf = HBaseConfiguration.create(); 23conf.set("hbase.rootdir", "hdfs://hadoop:9000/hbase"); 24conf.set("hbase.zookeeper.quorum", "hadoop"); 25HBaseAdmin hBaseAdmin = new HBaseAdmin(conf); 26//createTable(hBaseAdmin); 27//deleteTabele(hBaseAdmin); 28// 插入记录、查询一条记录、查询所有记录HTable 29HTable hTable = new HTable(conf, TABLE_NAME); 30//putRecord(hTable); 31//getRecord(hTable); 32Scan scan = new Scan(); 33ResultScanner scanner = hTable.getScanner(scan); 34for (Result result : scanner) { 35byte[] value = result.getValue(FAMILY_NAME.getBytes(), "age".getBytes()); 36System.out.println(result+"\t"+ new String(value)); 37} 38hTable.close(); 39} 40private static void getRecord(HTable hTable) throws IOException { 41Get get = new Get(ROW_KEY.getBytes()); 42Result result = hTable.get(get); 43System.out.println(new String(result.getValue(FAMILY_NAME.getBytes(), "age".getBytes()))); 44} 45private static void putRecord(HTable hTable) throws IOException { 46Put put = new Put(ROW_KEY.getBytes()); 47put.add(FAMILY_NAME.getBytes(),"age".getBytes(),"25".getBytes()); 48hTable.put(put); 49} 50private static void deleteTabele(HBaseAdmin hBaseAdmin) throws IOException { 51hBaseAdmin.disableTable(TABLE_NAME); 52hBaseAdmin.deleteTable(TABLE_NAME); 53} 54private static void createTable(HBaseAdmin hBaseAdmin) throws IOException { 55if (!hBaseAdmin.tableExists(TABLE_NAME)) { 56HTableDescriptor tableDescripetor = new HTableDescriptor(TABLE_NAME); 57HColumnDescriptor family = new HColumnDescriptor(FAMILY_NAME); 58tableDescripetor.addFamily(family); 59hBaseAdmin.createTable(tableDescripetor); 60} 61} 62}
Hbase的java_API操作
Stella981
2021-10-11
963 0 0
点赞
收藏
评论区
加载中...