利用hbase来两张表的联立,求出一个班学生的总分并把结果写到hbase(java 代码实现)

  • Post author:
  • Post category:java


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
//查询文科一班学生总分排名前10的学生(输出:学号,姓名,班级,总分)结果写到hbase
public class HomeWork {
    private HConnection conn;
    private HBaseAdmin hAdmin;
//获取连接
    @Before
    public void connect() {
        try {
            //1获取Hadoop的相关配置环镜
            Configuration conf = new Configuration();
            //2获取zookeeper的配置
            conf.set("hbase.zookeeper.quorum", "master:2181,node1:2181,node2:2181");
            //获取与hbase的连接
            conn = HConnectionManager.createConnection(conf);
            //获取HMaster对象
            hAdmin = new HBaseAdmin(conf);
            System.out.println("建立连接成功:" + conn + ",HMaster获取成功:" + hAdmin);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


//建立score表
    @Test
    public void CreateTable(){
        try {
            //使用HTableDescriptor类创建一个表对象
            HTableDescriptor students = new HTableDescriptor("score");
            //创建表的时候,至少指定一个列簇
            HColumnDescriptor info = new HColumnDescriptor("info");
            //将列簇添加到表中
            students.addFamily(info);
            //真正



版权声明:本文为CC072615原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。