Hbase伪分布式

1 conf下为hbase配置文件,
1)hbase-env.sh 配置hbase的环境变量
添加JAVA_HOME=
HBASE_CLASSPATH=${HADOOP_HOME}
export HBASE_MANAGES_ZK=true//由hbase负责启动和关闭zookeeper
2)hbase-site.xml
hbase.rootdir//hregionserver的共享目录,用来持久化hbase,和Hadoop中的配置相同
hbase.cluster.distributed 设置为true,表示分布式模式(包括伪分布式)
2vi /etc/profile
#####先启动Hadoop,在启动hbase
######################################################
#####创建table
Configuration conf=new HBaseConfiguration();
//hbase 客户端连接zookeeper的地址
conf.set(“hbase.zookeeper.quorum”, “192.168.17.128:2181”);
//创建表描述器并命名
HTableDescriptor htd=new HTableDescriptor(TableName.valueOf(“account1”));
HColumnDescriptor hcd=new HColumnDescriptor(“baseInfo”);
hcd.setMaxVersions(3);//设置列族的最大版本数3
HColumnDescriptor hcd2=new HColumnDescriptor(“contacts”);
htd.addFamily(hcd);
htd.addFamily(hcd2);
//实例化hbaseAdmin,创建表
try {
HBaseAdmin admin=new HBaseAdmin(conf);
admin.createTable(htd);
//释放资源
admin.close();
} catch (MasterNotRunningException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ZooKeeperConnectionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
-可通过hbase shell 查看
或者Hadoop dfs -ls /hbase 查看hbase 的相关
##################删除表
// try {
// HBaseAdmin admin=new HBaseAdmin(conf);
// admin.disableTable(“account1”);//先设置表不可用
// admin.deleteTable(“account1”);//删除表
// admin.close();释放资源
// } catch (MasterNotRunningException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// } catch (ZooKeeperConnectionException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }

############通过put插入一行或者多行数据
//插入一行数据
try {
//获得要操作的表的对象
HTable table=new HTable(conf, “account1”);//第一个值:配置文件,第二个值:table表名
//设置put对象,插入一行数据
Put put1=new Put(Bytes.toBytes(“11”));//设置rowkey
//设置列族,列,cell值
put1.add(Bytes.toBytes(“baseInfo”), Bytes.toBytes(“l1”), Bytes.toBytes(“baidu.com”));
put1.add(Bytes.toBytes(“baseInfo”), Bytes.toBytes(“l2”), Bytes.toBytes(“taobao.com”));
Put put2=new Put(Bytes.toBytes(“22”));//设置rowkey
put2.add(Bytes.toBytes(“contacts”), Bytes.toBytes(“l1”), Bytes.toBytes(“souhu.com”));
Listlist=new ArrayList();
list.add(put1);
list.add(put2);
table.put(list);//插入多行数据
table.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
运行完之后可以手残scan ‘account1’来查看是否插入正确
**************注意,put对象中的列族是已存在的,表明也是已存在的
HTable只能获取已存在的表对象,对其进行操作,而创建对象只能使用HtableAdmin

猜你喜欢

转载自blog.csdn.net/qq_41776004/article/details/83180522
今日推荐