Java操作HBase简单操作学生成绩信息表

云计算实验,记录备忘


创建以下内容的表格并实现要求的操作

(1)将表中的内容添加到表中,并显示出来。

(2)显示姓名为 zhang 的同学的所有相关记录。

(3)删除姓名为 zhang 的同学在 english 课程中的成绩,并显示删除之后 zhang同学的相关记录。



{CSDN:CODE:2600342}



注意,其中

put1.add("course".getBytes(), Bytes.toBytes("computer"),COMPUTER1.getBytes());
第一个getByte()是列族名,Bytes.toBytes()是列名,第三个COMPUTER1.getBytes()是列的value。如果列族中只有一个列,列族名=列名,可以直接将Bytes.toBytes()写为NULL

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这是一个比较复杂的问题,需要一步步来实现。首先,我们需要安装和配置HadoopHBase集群,并将学生成绩数据导入到HBase中。这里假设这些步骤已经完成。 接下来,我们需要编写Java程序来读取HBase中的数据,并进行平均分计算。以下是实现的步骤: 1. 定义HBase的配置信息名 ```java Configuration conf = HBaseConfiguration.create(); TableName tableName = TableName.valueOf("student_scores"); ``` 2. 创建HBase连接和对象 ```java Connection connection = ConnectionFactory.createConnection(conf); Table table = connection.getTable(tableName); ``` 3. 定义MapReduce作业 ```java Job job = Job.getInstance(conf, "CalculateScores"); job.setJarByClass(CalculateScores.class); job.setMapperClass(ScoreMapper.class); job.setReducerClass(ScoreReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(FloatWritable.class); ``` 4. 实现Mapper类,读取HBase中的数据,并将每个学生的成绩和课程作为键值对传递给Reducer类 ```java public static class ScoreMapper extends TableMapper<Text, FloatWritable> { private Text outKey = new Text(); private FloatWritable outValue = new FloatWritable(); public void map(ImmutableBytesWritable row, Result value, Context context) throws IOException, InterruptedException { String studentId = new String(value.getValue(Bytes.toBytes("info"), Bytes.toBytes("student_id"))); String course = new String(value.getValue(Bytes.toBytes("info"), Bytes.toBytes("course"))); float score = Float.parseFloat(new String(value.getValue(Bytes.toBytes("info"), Bytes.toBytes("score")))); outKey.set(studentId + "," + course); outValue.set(score); context.write(outKey, outValue); } } ``` 5. 实现Reducer类,计算每个学生的平均分和每门课程的平均分 ```java public static class ScoreReducer extends Reducer<Text, FloatWritable, Text, FloatWritable> { private FloatWritable outValue = new FloatWritable(); public void reduce(Text key, Iterable<FloatWritable> values, Context context) throws IOException, InterruptedException { float sum = 0; int count = 0; for (FloatWritable value : values) { sum += value.get(); count++; } float avg = sum / count; outValue.set(avg); context.write(key, outValue); } } ``` 6. 运行MapReduce作业并输出结果 ```java if (job.waitForCompletion(true)) { Scan scan = new Scan(); ResultScanner scanner = table.getScanner(scan); float sum = 0; int count = 0; float mathSum = 0; int mathCount = 0; float englishSum = 0; int englishCount = 0; for (Result result : scanner) { String studentId = new String(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("student_id"))); String course = new String(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("course"))); float score = Float.parseFloat(new String(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("score")))); sum += score; count++; if (course.equals("math")) { mathSum += score; mathCount++; } else if (course.equals("english")) { englishSum += score; englishCount++; } } System.out.println("Average score: " + sum / count); System.out.println("Math average score: " + mathSum / mathCount); System.out.println("English average score: " + englishSum / englishCount); } ``` 以上就是利用Java API编程HBase中读取学生成绩实现计算每个学生成绩平均分和每门课程成绩平均分的步骤。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值