package com.sse.hbase.Test;
import java.io.IOException;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.Delete;
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.hbase.util.Bytes;
import com.sse.hbase.db.HBaseConfig;
public class FirstHBase {
static Configuration conf = null;
public FirstHBase(){
conf = HBaseConfig.conf;//设置好Configuration对象
}
/**
* @throws IOException
* @date 2015-11-18 创建hbase的表
*/
public void createHBaseTable() throws IOException{
String tableName = "Student";
HBaseAdmin admin = new HBaseAdmin(conf);//HBaseAdmin负责管理HBase集群,添加和丢弃表。
if (admin.tableExists(tableName)){
//表不存在
System.out.println("表已存在,不需要新建表~~");
return ;
}
HTableDescriptor desc = new HTableDescriptor(tableName);//HBase中表的名字
desc.addFamily(new HColumnDescriptor("Name"));//创建列族,名字是Name
desc.addFamily(new HColumnDescriptor("Address"));//创建列族,名字是Address
admin.createTable(desc);//新建表
}
/**@author
* @throws IOException
* @date 2015-11-18 往HBase指定表中插入记录
*/
public void insertHBase() throws IOException{
HTable table = new HTable(conf, Bytes.toBytes("Student"));//HTabel负责跟记录相关的操作如增删改查等
Put put = new Put(Bytes.toBytes("firstRow"));//设置行键,插入相应的一行记录
put.add(Bytes.toBytes("Name"), Bytes.toBytes("firstname"), Bytes.toBytes("zhang"));
put.add(Bytes.toBytes("Name"), Bytes.toBytes("secondname"), Bytes.toBytes("liang"));
put.add(Bytes.toBytes("Address"), Bytes.toBytes("province"), Bytes.toBytes("henan"));
put.add(Bytes.toBytes("Address"), Bytes.toBytes("city"), Bytes.toBytes("puyang"));
put.add(Bytes.toBytes("Address"), Bytes.toBytes("county"), Bytes.toBytes("puyang"));
table.put(put);
}
/**
* 根据行键查该行存储的记录信息.
*/
public void selectHBaseByRowkey() throws IOException{
HTable table = new HTable(conf, Bytes.toBytes("Student"));//HTabel负责跟记录相关的操作如增删改查等
Get get = new Get(Bytes.toBytes("firstRow"));
Result result = table.get(get);
for(KeyValue kv :result.list()){
System.out.println("family:" +Bytes.toString(kv.getFamily()));//所属列族名
System.out.println("qualifier:" +Bytes.toString(kv.getQualifier()));//列名字
System.out.println("value:" +Bytes.toString(kv.getValue()));//存储的值
System.out.println("Timestamp:" +kv.getTimestamp());//时间戳
}
}
/**
* 遍历HBase表.
*/
public void selectHBaseScann() throws IOException{
HTable table = new HTable(conf, Bytes.toBytes("Student"));//HTabel负责跟记录相关的操作如增删改查等
/**=========遍历查询=========*/
Scan scan = new Scan();
ResultScanner rs =null;
try {
rs = table.getScanner(scan);
for (Result r : rs) {
for(KeyValue kv :r.list()){
System.out.println("family:" +Bytes.toString(kv.getFamily()));
System.out.println("qualifier:" +Bytes.toString(kv.getQualifier()));
System.out.println("value:" +Bytes.toString(kv.getValue()));
}
}
} finally {
rs.close();
}
}
/**
* 更新HBase中某张表的一个记录.
* @throws IOException
*/
public void updateHBase() throws IOException{
HTable table = new HTable(conf, Bytes.toBytes("Student"));//HTabel负责跟记录相关的操作如增删改查等
Put put = new Put(Bytes.toBytes("firstRow")); //设置行键
put.add(Bytes.toBytes("Address"), Bytes.toBytes("city"), Bytes.toBytes("shanghai"));//更新的时候找对族名和列名,再给定新的value值就可以了
table.put(put);
}
/**
* 查询nickname的多个(本示例为2个)版本值.
*/
public void selectSomeVersion() throws IOException{
HTable table = new HTable(conf, Bytes.toBytes("Student"));//HTabel负责跟记录相关的操作如增删改查等
Get get = new Get(Bytes.toBytes("firstRow"));
get.addColumn(Bytes.toBytes("Address"), Bytes.toBytes("city"));
get.setMaxVersions(2);
List<KeyValue> results = table.get(get).list();
int total = results.size();
System.out.println("Address 列族中 city 列的各个版本值");
for(int i=0;i<total;i++){
System.out.println(Bytes.toString(results.get(i).getValue()));
}
}
/**
* 删除指定的某一行中的指定column.
* @throws IOException
*/
public void deleteColumn() throws IOException{
HTable table = new HTable(conf, Bytes.toBytes("Student"));//HTabel负责跟记录相关的操作如增删改查等
Delete deleteColumn = new Delete(Bytes.toBytes("firstRow"));
deleteColumn.deleteColumns(Bytes.toBytes("Address"),Bytes.toBytes("city"));
table.delete(deleteColumn);
}
/**
* 删除指定的某一行.
* @throws IOException
*/
public void deleteAll() throws IOException{
HTable table = new HTable(conf, Bytes.toBytes("Student"));//HTabel负责跟记录相关的操作如增删改查等
Delete deleteAll = new Delete(Bytes.toBytes("firstRow"));
table.delete(deleteAll);
}
/**
* HBase中的整张表.
*/
public void deleteTable() throws IOException{
HBaseAdmin admin = new HBaseAdmin(conf);//HBaseAdmin负责管理HBase集群,添加和丢弃表。
admin.disableTable("Student");//首先禁用表,然后删除它
admin.deleteTable("Student");
}
public static void main(String[] args) throws IOException {
FirstHBase f = new FirstHBase();
f.createHBaseTable();
// System.out.println("往表中插入数据");
// f.insertHBase();
// System.out.println("往表中插入数据结束");
//
// System.out.println("查询指定行的值");
// f.selectHBaseByRowkey();
// System.out.println("遍历查询某张表");
// f.selectHBaseScann();
// System.out.println("更新指定表的指定行的指定列族的指定列的某个值");
// f.updateHBase();
// System.out.println("查看更新的那一个列对应的各个版本的值");
// f.selectSomeVersion();
// System.out.println("删除指定表的指定行的指定列族的指定列");
// f.deleteColumn();
// System.out.println("删除指定行");
// f.deleteAll();
// System.out.println("删除整张表");
// f.deleteTable();
}
}
package com.sse.hbase.db;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
public class HBaseConfig {
public static Configuration conf = null;
public static String CONFIG_FILE=System.getProperty("user.dir")+File.separator+"config"+File.separator+"config.properties";
static {
Properties p = new Properties();
try {
InputStream in = new BufferedInputStream(new FileInputStream(CONFIG_FILE));//读进来配置文件中的信息,设置conf对象
p.load(in);
} catch (IOException e) {
e.printStackTrace();
}
conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", p.getProperty("hbasenodes"));// 使用eclipse时须添加这个,否则无法定位master需要配置hosts
conf.set("hbase.zookeeper.property.clientPort", "2181");
conf.set("hbase.master", p.getProperty("hbasemaster"));
}
}
配置文件内容如下:
hbasemaster=192.168.0.120
hbasenodes=192.168.0.120,192.168.0.121,192.168.0.122,192.168.0.123,192.168.0.124,192.168.0.125,192.168.0.126,192.168.0.127,192.168.0.128,,192.168.0.129,192.168.0.130