HBase数据库表的创建

实验三 HBase分布式数据库操作与编程

1、HBase Shell数据库表创建

【实验内容】
根据以下关系型数据库表,使用HBase Shell设计并创建适宜的HBase数据表。
在这里插入图片描述

2、创建表以及插入学生信息数据

(1)、启动Hadoop
在这里插入图片描述
(2)、启动HBase
在这里插入图片描述
(3)、进入shell界面
在这里插入图片描述
(4)、创建表
在这里插入图片描述
(5)、插入数据(我是采用的学生的学号作为RowKey即行键,因此插入数据时不必在插入学生编号,同时课程建了三个列族,用于存储同一行键下的不同课程)
①新增学号为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'

3、查看表的数据

①、新增学号2015001的学生信息过后表的数据
在这里插入图片描述
②、新增学号为2015002的学生信息过后表的数据
在这里插入图片描述
③、学生信息全都新增过后表的数据
在这里插入图片描述
总的来说,在shell命令中插入数据还挺麻烦的,每次只能添加表的一行数据的一个列,也就是一个单元格的数据,如果向这种数据比较多的话,插入数据是比较费时间的。还有一个问题就是如果创建表的时候,对于课程不创建三个列族,而是创建一个列族的话,那么对于同一行键的不同选课,就比如Zhangsan选有课程Math与English,如何将Math与English存储在同一个列族下呢?

  • 18
    点赞
  • 153
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
好的,关于在HBase数据库中使用Java创建的方法如下: 首先,需要导入HBase的Java API相关的包: ```java import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; 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; import org.apache.hadoop.hbase.client.TableDescriptor; import org.apache.hadoop.hbase.client.TableDescriptorBuilder; import org.apache.hadoop.hbase.io.compress.CompressionAlgorithm; import org.apache.hadoop.hbase.regionserver.BloomType; import org.apache.hadoop.hbase.util.Bytes; ``` 然后,通过以下代码建立与HBase数据库的连接: ```java Configuration config = HBaseConfiguration.create(); config.set("hbase.zookeeper.quorum", "localhost"); Connection connection = ConnectionFactory.createConnection(config); Admin admin = connection.getAdmin(); ``` 其中,"localhost"为HBase数据库所在的主机名。接下来,可以使用以下代码创建: ```java String tableName = "myTable"; TableName name = TableName.valueOf(tableName); TableDescriptor tableDescriptor = TableDescriptorBuilder.newBuilder(name) .setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("cf")) .setCompressionType(CompressionAlgorithm.GZ) .setBloomFilterType(BloomType.ROWCOL) .build()) .build(); admin.createTable(tableDescriptor); ``` 这里创建了一个名为"myTable"的,并设置了一个名为"cf"的列族,其中设置了压缩算法和Bloom过滤器类型。最后,使用admin.createTable()方法创建。 需要注意的是,在使用完后需要关闭与HBase数据库的连接: ```java admin.close(); connection.close(); ``` 以上就是使用Java在HBase数据库创建的方法,希望对你有所帮助。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值