单节点集群测试
初始化连接
/**
*
* 操作hbase数据库简单查询例子
* Created by wan on 17-11-7.
*/
public class HbaseTest {
public static Configuration conf;
static {
conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "172.26.40.122");//必须有
conf.set("hbase.zookeeper.property.clientPort", "2181");//可有可无
conf.set("hbase.rootdir", "172.26.40.122:9001");//可有可无
//conf更多参数配置需要看详细看百度
System.out.println("连接创建完成!" + conf);
}
public static void main(String[] args)throws Exception {
String tableName = "luffy_1107table01";
HTable table = new HTable(conf,tableName);
/**
* 调用创建表方法 第一个参数:表明;第二个参数:列族的名字(可以设置更多,此处只设置一个参数)
*/
//HbaseTest.createTable(tableName,"name");
//HbaseTest.deleteTable(tableName);
/*HbaseTest.putCell(table,"001","name","luffy","chao");
HbaseTest.putCell(table,"002","name","luffy","chao");
HbaseTest.putCell(table,"003","name","luffy","chao");*/
//HbaseTest.getRow(table,"001");
//HbaseTest.deleteRow(table,"002");
//ResultScanner rs = HbaseTest.scanAll(table);
ResultScanner rs = HbaseTest.scanRange(table,"001","003");
for(Result r:rs) {
System.out.println("Scan: "+r);
}
table.close();
}
/**
* 返回指定row行数的数据,包头不包尾.
* @param table
* @param startrow
* @param endrow
* @return
* @throws Exception
*/
public static ResultScanner scanRange(HTable table,String startrow,String endrow) throws Exception {
Scan s =new Scan(Bytes.toBytes(startrow),Bytes.toBytes(endrow));
ResultScanner rs = table.getScanner(s);
return rs;
}
/**
* 返回指定table名字的所有数据
* @param table
* @return
* @throws Exception
*/
public static ResultScanner scanAll(HTable table) throws Exception {
Scan s =new Scan();
ResultScanner rs = table.getScanner(s);
return rs;
}
/**
* 根据标识删除hbase中的数据
* @param table
* @param rowKey
* @throws Exception
*/
public static void deleteRow(HTable table, String rowKey) throws Exception {
Delete delete = new Delete(Bytes.toBytes(rowKey));
table.delete(delete);
System.out.println("Delete row: "+rowKey);
}
/**
* 根据行标识获取hbase中的数据
* @param table
* @param rowKey
* @return
* @throws Exception
*/
public static Result getRow(HTable table, String rowKey) throws Exception {
Get get = new Get(Bytes.toBytes(rowKey));
Result result = table.get(get);
System.out.println("Get: "+result);
return result;
}
/**
* 增加数据
* @param table
* @param rowKey 行数唯一标识
* @param columnFamily 列族
* @param identifier 列名
* @param data 值
* @throws Exception
*/
public static void putCell(HTable table, String rowKey, String columnFamily, String identifier, String data) throws Exception{
Put p1 = new Put(Bytes.toBytes(rowKey));
p1.add(Bytes.toBytes(columnFamily), Bytes.toBytes(identifier), Bytes.toBytes(data));
table.put(p1);
System.out.println("put '"+rowKey+"', '"+columnFamily+":"+identifier+"', '"+data+"'");
}
/**
* 删除表
* @param tablename 表名
* @return
* @throws Exception
*/
public static boolean deleteTable(String tablename) throws Exception {
HBaseAdmin admin = new HBaseAdmin(conf);
if(admin.tableExists(tablename)) {
try {
admin.disableTable(tablename);
admin.deleteTable(tablename);
System.out.print("delete table success.");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
admin.close();
return false;
}
}
admin.close();
return true;
}
/**
* 创建table:table name :表名
* @param tablename
* @param columnFamily
* @throws Exception
*/
public static void createTable(String tablename, String columnFamily) throws Exception {
//获取hbase管理
HBaseAdmin admin = new HBaseAdmin(conf);
//判断表是否存在
if(admin.tableExists(tablename)) {
System.out.println("Table exists!");
System.exit(0);
}else {
//创建表
HTableDescriptor tableDesc = new HTableDescriptor(TableName.valueOf(tablename));
tableDesc.addFamily(new HColumnDescriptor(columnFamily));
admin.createTable(tableDesc);
System.out.println("create table success!");
}
//关闭
admin.close();
}
}
//如果连接成功之后,会报异常连接失败
需要做主机映射
修改windows下面的hosts文件
增加
172.26.40.122 luffy
修改linux环境下面的/etc/hosts
注释127.0.0.1 luffy
增加
172.26.40.122 luffy