您的位置:首页 > 编程语言 > Java开发

Hbase Java编程基本操作

2016-12-18 23:01 363 查看
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;
import org.apache.hadoop.io.BytesWritable;

import org.apache.hadoop.hbase.util.Bytes;

public class HbaseTestCase {

// 声明静态配置
static Configuration configuration = HBaseConfiguration.create();

// 创建表
public static void create(String tablename, String colunmnFamily)
throws IOException {
HBaseAdmin admin = new HBaseAdmin(configuration);
if (admin.tableExists(tablename)) {
System.out.println("table exists!");
admin.disableTable(tablename);
admin.deleteTable(tablename);
}
HTableDescriptor descriptor = new HTableDescriptor(tablename);
descriptor.addFamily(new HColumnDescriptor(colunmnFamily));
admin.createTable(descriptor);
System.out.println("create table success!");
}

// 添加一条数据
public static void put(String tablename, String row, String columnFamily,
String column, String data) throws IOException {
HTable table = new HTable(configuration, tablename);
Put p1 = new Put(Bytes.toBytes(row));
p1.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column),
Bytes.toBytes(data));
table.put(p1);
System.out.println("put " + row + "," + columnFamily + "," + column
+ "," + data);
System.out.println("===========================================");
}

// 得到一条记录
public static void get(String tablename, String row) throws IOException {
HTable table = new HTable(configuration, tablename);
Get get = new Get(Bytes.toBytes(row));
Result result = table.get(get);
System.out.println("Get:" + result);
System.out.println("===========================================");
}

// 扫描表
public static void scan(String tablename) throws Exception {
HTable table = new HTable(configuration, tablename);
Scan scan = new Scan();
ResultScanner rscanner = table.getScanner(scan);
for (Result result : rscanner) {
System.out.println("Scan:" + result);
}
System.out.println("===========================================");
}

// 删除表
public static boolean delete(String tablename) throws Exception {
HBaseAdmin admin = new HBaseAdmin(configuration);
try {
if (admin.tableExists(tablename)) {
admin.disableTable(tablename);
admin.deleteTable(tablename);
}
} catch (Exception e) {
e.printStackTrace();
}
return true;
}

public static void main(String[] args) throws Exception {
String tablename = "hbase_tb";
String columnFamily = "cf";
create(tablename, columnFamily);
put(tablename, "row1", columnFamily, "cf1", "data");
get(tablename, "row1");
scan(tablename);
delete(tablename);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: