首先,main函数如下: public static void main(String[] arg) throws Exception { Configuration conf = new Configuration(); //给conf设置地址与操作用户 conf.set("hbase地址","用户名user"); HBaseAdmin admin = new HBaseAdmin(co
首先,main函数如下:
public static void main(String[] arg) throws Exception {
Configuration conf = new Configuration();
//给conf设置地址与操作用户
conf.set("hbase地址","用户名user");
HBaseAdmin admin = new HBaseAdmin(conf);
// member777
// Hbase_All.createTable(admin,"member777");
// Hbase_All.addData(conf,"member777");
// Hbase_All.scan(conf,"member7777");
// Hbase_All.delete(conf,"member777");
}
1.创建表:
private static void createTable(HBaseAdmin admin, String tablename) throws IOException {
//实例化一个表对象
HTableDescriptor tdesc=new HTableDescriptor(tablename);
//给表添加列族
tdesc.addFamily(new HColumnDescriptor("id"));
tdesc.addFamily(new HColumnDescriptor("info"));
tdesc.addFamily(new HColumnDescriptor("address"));
admin.createTable(tdesc); //创建表
System.out.println("create OK");
admin.close();
}
2.给表添加数据:
protected static void addData(Configuration conf,String tableName) throws IOException {
HTable table = new HTable(conf,tableName);
//实例化List对象,存放添加的多个数据的put对象
List<Put> putList = new ArrayList<Put>();
//----给Rain行键添加数据
Put put = new Put(Bytes.toBytes("Rain"));
put.addColumn(Bytes.toBytes("id"), Bytes.toBytes(""), Bytes.toBytes("31"));
put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("age"), Bytes.toBytes("28"));
put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("birthday"), Bytes.toBytes("1990-05-01"));
put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("industry"), Bytes.toBytes("architect"));
put.addColumn(Bytes.toBytes("address"), Bytes.toBytes("city"), Bytes.toBytes("Shenzhen"));
put.addColumn(Bytes.toBytes("address"), Bytes.toBytes("country"), Bytes.toBytes("China"));
putList.add(put);
//----给Sariel行键添加数据
put = new Put(Bytes.toBytes("Sariel"));
put.addColumn(Bytes.toBytes("id"), Bytes.toBytes(""), Bytes.toBytes("21"));
put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("age"), Bytes.toBytes("26"));
put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("birthday"), Bytes.toBytes("1992-05-09"));
put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("industry"), Bytes.toBytes("it"));
put.addColumn(Bytes.toBytes("address"), Bytes.toBytes("city"), Bytes.toBytes("Shanghai"));
put.addColumn(Bytes.toBytes("address"), Bytes.toBytes("country"), Bytes.toBytes("China"));
putList.add(put);
//----给Elvis行键添加数据
put = new Put(Bytes.toBytes("Elvis"));
put.addColumn(Bytes.toBytes("id"), Bytes.toBytes(""), Bytes.toBytes("22"));
put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("age"), Bytes.toBytes("26"));
put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("birthday"), Bytes.toBytes("1991-09-14"));
put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("industry"), Bytes.toBytes("it"));
put.addColumn(Bytes.toBytes("address"), Bytes.toBytes("city"), Bytes.toBytes("Beijing"));
put.addColumn(Bytes.toBytes("address"), Bytes.toBytes("country"), Bytes.toBytes("China"));
putList.add(put);
//由于把需要添加的数据的put对象都添加到List里面了,这时候直接把List对象添加到表即可完成添加数据.
table.put(putList);
System.out.println("add data OK");
table.close();
}
3.插入数据:
private static void insertOne(Configuration conf,String tablename,String rownum,String infoname,String colunmnName,String value) throws IOException {
HTable table = new HTable(conf,tablename);
//通过传参数达到插入一条数据的目的,也可以for循环迭代传入多条数据;也可以类似添加数据那样插入数据。
Put put = new Put(Bytes.toBytes(rownum));//行键
put.addColumn(
Bytes.toBytes(infoname),//列族
Bytes.toBytes(colunmnName),//列名
Bytes.toBytes(value)//值
);
table.put(put);
System.out.println("insert OK");
table.close();
}
4.获取数据:
private static void get(Configuration conf,String tablename,String rownum,String infoname,String columnname) throws IOException {
HTable table = new HTable(conf,tablename);
Get get = new Get(Bytes.toBytes(rownum));//获取一行记录
Result record = table.get(get);
String name = Bytes.toString(record.getValue(Bytes.toBytes(infoname),Bytes.toBytes(columnname)));
System.out.println(name);//查看获取的数据
System.out.println("get ok");
table.close();
5.扫描查看表:
private static void scan(Configuration conf,String tablename,String rownum,String infoname,String columnnames) throws IOException {
HTable table = new HTable(conf,tablename);
Scan scanner = new Scan(); // 类似 select * from tablename
// scanner.setFilter(filter); // 这里可以定义一些过滤器使用过滤器对数据进行限制扫描
ResultScanner rs = table.getScanner(scanner); //执行查找
String[] columns = columnnames.split("-");
for (Result r:rs) {
String name = Bytes.toString(r.getValue(Bytes.toBytes(infoname),Bytes.toBytes(columns[0])));
String age = Bytes.toString(r.getValue(Bytes.toBytes(infoname),Bytes.toBytes(columns[1])));
System.out.println(name+" "+age);
}
System.out.println("scan OK");
table.close();
}
6.删除表:
private static void delete(Configuration conf,String tablename) throws IOException {
HBaseAdmin client = new HBaseAdmin(conf);
client.disableTable(tablename);
client.deleteTable(tablename);
System.out.println("delete OK");
client.close();
}