Hadoop之Hbase建表(详细步骤)

1、启动HDFS和HBase

cd /home/ZQ/hadoop-2.7.6/sbin
start-dfs.sh
cd /home/ZQ/hbase-1.4.13/bin/
start-hbase.sh

2、进入hbase shell

cd
hbase shell

 3、创建表

create 'StudentAndCourse','student','course1','course2','course3'

4、插入数据

①新增学号为2015001的学生的所有信息

put 'StudentAndCourse','2015001','student:S_Name','Zhangsan'
put 'StudentAndCourse','2015001','student:S_Sex','male'
put 'StudentAndCourse','2015001','student:S_Age','23'
put 'StudentAndCourse','2015001','course1:C_No','123001'
put 'StudentAndCourse','2015001','course1:C_Name','Math'
put 'StudentAndCourse','2015001','course1:C_Credit','2.0'
put 'StudentAndCourse','2015001','course1:Score','86'
put 'StudentAndCourse' ,'2015001','course3:C_No','123003'
put 'StudentAndCourse' ,'2015001','course3:C_Name','English'
put 'StudentAndCourse' ,'2015001','course3:C_Credit','3.0'
put 'StudentAndCourse' ,'2015001','course3:Score','69'

②、新增学号为2015002的学生的所有信息

put 'StudentAndCourse' ,'2015002','student:S_Name','Mary'
put 'StudentAndCourse' ,'2015002','student:S_Sex','female'
put 'StudentAndCourse' ,'2015002','student:S_Age','22'
put 'StudentAndCourse' ,'2015002','course2:C_No','123002'
put 'StudentAndCourse' ,'2015002','course2:C_Name','Conputer Science'
put 'StudentAndCourse' ,'2015002','course2:Credit','5.0'
put "StudentAndCourse" ,'2015002','course2:Score','77'
put 'StudentAndCourse' ,'2015002','course3:C_No','123003'
put 'StudentAndCourse' ,'2015002','course3:C_Name','English'
put 'StudentAndCourse' ,'2015002','course3:Credit','3.0'
put 'StudentAndCourse' ,'2015002','course3:Score','99'

③、新增学号为2015003的学生的所有信息

put 'StudentAndCourse' ,'2015003','student:S_Name','Lisi'
put 'StudentAndCourse' ,'2015003','student:S_Sex','male'
put 'StudentAndCourse' ,'2015003','student:S_Age','24'
put 'StudentAndCourse' ,'2015003','course1:C_No','123001'
put 'StudentAndCourse' ,'2015003','course1:C_Name','Math'
put 'StudentAndCourse' ,'2015003','course1:C_Credit','2.0'
put 'StudentAndCourse' ,'2015003','course1:Score','98'
put 'StudentAndCourse' ,'2015003','course2:C_No','123002'
put 'StudentAndCourse' ,'2015003','course2:C_Name','Computer Science'
put 'StudentAndCourse' ,'2015003','course2:C_Credit','5.0'
put 'StudentAndCourse' ,'2015003','course2:Score','95'

5、查看表的数据

scan 'StudentAndCourse'

6、查看web界面

在浏览器中输入10.103.105.62:60010(主节点)进行查看

 

退出hbase shell 

exit

7、停止hbase和HDFS

cd /home/ZQ/hbase-1.4.13/bin/
stop-hbase.sh
cd /home/ZQ/hadoop-2.7.6/sbin
stop-dfs.sh

  • 26
    点赞
  • 170
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
HBase是一个分布式的、面向列的NoSQL数据库,其表的结构与关系型数据库不同。HBase表的建表语句可以通过HBase Shell或HBase API来执行。 下面是一个示例的HBase建表语句: ```shell create 'mytable', 'cf1', 'cf2' ``` 上述语句创建了名为"mytable"的表,并且指定了两个列族"cf1"和"cf2"。列族是表中列的集合,可以理解为逻辑上的分组。 在HBase中,列是没有预定义的模式的,因此可以根据需要动态添加列。对于每个列,可以存储多个版本的数据。 可以通过以下语句修改表的配置: ```shell alter 'mytable', NAME => 'cf1', VERSIONS => 3 ``` 上述语句将列族"cf1"的版本数设置为3。 另外,还可以通过HBase API来创建表。以下是一个Java API的示例: ```java 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.TableName; import org.apache.hadoop.hbase.client.Admin; import org.apache.hadoop.hbase.client.Connection; import org.apache.hadoop.hbase.client.ConnectionFactory; public class CreateTableExample { public static void main(String[] args) throws Exception { Configuration conf = HBaseConfiguration.create(); try (Connection connection = ConnectionFactory.createConnection(conf); Admin admin = connection.getAdmin()) { TableName tableName = TableName.valueOf("mytable"); HTableDescriptor tableDescriptor = new HTableDescriptor(tableName); HColumnDescriptor columnDescriptor1 = new HColumnDescriptor("cf1"); HColumnDescriptor columnDescriptor2 = new HColumnDescriptor("cf2"); tableDescriptor.addFamily(columnDescriptor1); tableDescriptor.addFamily(columnDescriptor2); admin.createTable(tableDescriptor); } } } ``` 上述代码使用HBase的Java API创建了名为"mytable"的表,并添加了两个列族"cf1"和"cf2"。 需要注意的是,上述示例只是简单的表结构创建示例,实际使用时,还需根据具体需求进行适当调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值