写点什么

大数据训练营 -0725 课后作业

用户头像
cc
关注
发布于: 3 小时前
作业:编程实践,使用 Java API 操作 HBase

主要实践建表、插入数据、删除数据、查询等功能。要求建立一个如下所示的表:

  • 表名:$your_name:student

  • 空白处自行填写, 姓名学号一律填写真实姓名和学号



  • 服务器版本为 2.1.0(hbase 版本和服务器上的版本可以不一致,但尽量保证一致)

复制代码

<dependency><groupId>org.apache.hbase</groupId><artifactId>hbase-client</artifactId><version>2.1.0</version></dependency>
复制代码


解答如下:
1.创建表
hbase shell#创建表create 'gaoyong:student', {NAME => 'name', VERSIONS => 2}, {NAME => 'info', VERSIONS => 2}, {NAME => 'score', VERSIONS => 2}#插入第一行数据(按列插入)put 'gaoyong:student','20210000000001','name','Tom'put 'gaoyong:student','20210000000001','info:student_id','20210000000001'put 'gaoyong:student','20210000000001','info:class','1'put 'gaoyong:student','20210000000001','score:understanding','75'put 'gaoyong:student','20210000000001','score:programming','82'
复制代码

服务器执行如下:

2.写 TestHbase 进行表数据增,删,查操作

2.1 定义列模型 ColumnData

package com.gaoyong.bd.hbase;
import org.apache.hadoop.hbase.util.Bytes;
/** * Hbase列定义 */public class ColumnData { //行键 private byte[] rowKey; //列族 private byte[] family; //属性 private byte[] qualifier; //列值 private byte[] value;
public ColumnData(String rowKey, String family, String qualifier, String value) { this.rowKey = Bytes.toBytes(rowKey); this.family = Bytes.toBytes(family); if(qualifier!=null){ this.qualifier = Bytes.toBytes(qualifier); } else{ qualifier=null; } this.value = Bytes.toBytes(value); }
public byte[] getRowKey() { return rowKey; }
public void setRowKey(byte[] rowKey) { this.rowKey = rowKey; }
public byte[] getFamily() { return family; }
public void setFamily(byte[] family) { this.family = family; }
public byte[] getQualifier() { return qualifier; }
public void setQualifier(byte[] qualifier) { this.qualifier = qualifier; }
public byte[] getValue() { return value; }
public void setValue(byte[] value) { this.value = value; }}
复制代码

2.2 进行增删查操作

package com.gaoyong.bd.hbase;

import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.CompareOperator;import org.apache.hadoop.hbase.HBaseConfiguration;import org.apache.hadoop.hbase.TableName;import org.apache.hadoop.hbase.client.*;import org.apache.hadoop.hbase.filter.CompareFilter;import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;import org.apache.hadoop.hbase.util.Bytes;
import javax.ws.rs.GET;import java.io.IOException;import java.util.ArrayList;import java.util.List;
public class TestHbase { private Configuration conf=null; private Connection conn =null; private Table table = null;
/** * 与HBASE建立连接 * @throws IOException */ private void createConf() throws IOException { conf = HBaseConfiguration.create(); //设置ZK集群地址,默认2181端口,可以不设置 conf.set("hbase.zookeeper.quorum","47.101.206.249,47.101.216.12,47.101.204.23"); //建立连接 conn = ConnectionFactory.createConnection(conf);
}
/** * * @param tableName 表名 * @param columnData 列字段 * @throws IOException */ private void insertData(String tableName,ColumnData columnData) throws IOException { table = conn.getTable(TableName.valueOf(tableName)); //创建put对象 Put put = new Put(columnData.getRowKey()); //添加一行列数据 put.addColumn(columnData.getFamily(),columnData.getQualifier(),columnData.getValue()); //put.addColumn("name".getBytes(),null,"jack".getBytes()); table.put(put); System.out.println("插入数据完成"); }
/** * * @param tableName 表名 * @param columnDatas 批量数据 * @throws IOException */ private void batchInsrtDatas(String tableName, List<ColumnData> columnDatas) throws IOException { table = conn.getTable(TableName.valueOf(tableName)); for(ColumnData columnData:columnDatas){ //创建put对象 Put put = new Put(columnData.getRowKey()); //添加一行列数据 put.addColumn(columnData.getFamily(),columnData.getQualifier(),columnData.getValue()); //put.addColumn("name".getBytes(),null,"jack".getBytes()); table.put(put); } System.out.println("批量数据插入完成");

}
/** * 根据键值查询单条数据 * @param tableName 表名 * @param rowKey 行键值 */ private void getDataBykey(String tableName,String rowKey) throws IOException { table = conn.getTable(TableName.valueOf(tableName)); Get get = new Get(Bytes.toBytes(rowKey)); Result result=table.get(get); byte[] name = result.getValue(Bytes.toBytes("name"),null); byte[] student_id = result.getValue(Bytes.toBytes("info"),Bytes.toBytes("student_id")); byte[] clazz = result.getValue(Bytes.toBytes("info"),Bytes.toBytes("class")); byte[] understanding = result.getValue(Bytes.toBytes("score"),Bytes.toBytes("understanding")); byte[] programming = result.getValue(Bytes.toBytes("score"),Bytes.toBytes("programming")); String nameStr=null; String student_id_Str=null; String clazzStr=null; String understandingStr=null; String programmingStr=null; if (name!=null) { nameStr = Bytes.toString(name); } if (student_id!=null) { student_id_Str = Bytes.toString(student_id); } if (clazz!=null) { clazzStr = Bytes.toString(clazz); } if (understanding!=null) { understandingStr = Bytes.toString(understanding); } if (programming!=null) { programmingStr = Bytes.toString(programming); }
System.out.println("查询到结果,行键值:"+rowKey+",name:"+nameStr+",student_id:"+student_id_Str+",class:"+clazzStr+",understanding:"+understandingStr+",programming:"+programmingStr); }
/** * 根据列值查询数据 * @param tableName 表名 * @param columnData 列数据 */ private void getDataByColumn(String tableName,ColumnData columnData) throws IOException { table = conn.getTable(TableName.valueOf(tableName)); SingleColumnValueFilter singleColumnValueFilter = new SingleColumnValueFilter(columnData.getFamily(),columnData.getQualifier(), CompareFilter.CompareOp.EQUAL,columnData.getValue()); Scan scan = new Scan(); scan.setFilter(singleColumnValueFilter); ResultScanner scanner = table.getScanner(scan); printResult(scanner); }
/** * 根据键值删除数据 * @param tableName 表名 * @param rowKey 行键值 */
private void delDataByKey(String tableName,String rowKey) throws IOException { table = conn.getTable(TableName.valueOf(tableName)); Delete delete = new Delete(Bytes.toBytes(rowKey)); table.delete(delete); System.out.println("删除数据成功,键值为:"+rowKey);
}
/** * 根据列值删除数据 * @param tableName 表名 * @param rowKey 表名 行键值 * @param columnData 列数据 */ private void delDataByColumn(String tableName,String rowKey,ColumnData columnData) throws IOException { table = conn.getTable(TableName.valueOf(tableName)); Delete delete = new Delete(Bytes.toBytes(rowKey)); delete.addColumn(columnData.getFamily(),columnData.getQualifier()); table.delete(delete); System.out.println("删除数据成功,键值为:"+rowKey+",Family:"+Bytes.toString(columnData.getFamily())+",Qualifier:"+Bytes.toString(columnData.getQualifier())); }
private void printResult(ResultScanner scanner){ for (Result result:scanner){ byte[] rowKey = result.getRow(); byte[] name = result.getValue(Bytes.toBytes("name"),null); byte[] student_id = result.getValue(Bytes.toBytes("info"),Bytes.toBytes("student_id")); byte[] clazz = result.getValue(Bytes.toBytes("info"),Bytes.toBytes("class")); byte[] understanding = result.getValue(Bytes.toBytes("score"),Bytes.toBytes("understanding")); byte[] programming = result.getValue(Bytes.toBytes("score"),Bytes.toBytes("programming")); String rowKeyStr=null; String nameStr=null; String student_id_Str=null; String clazzStr=null; String understandingStr=null; String programmingStr=null; rowKeyStr = Bytes.toString(rowKey); if (name!=null) { nameStr = Bytes.toString(name); } if (student_id!=null) { student_id_Str = Bytes.toString(student_id); } if (clazz!=null) { clazzStr = Bytes.toString(clazz); } if (understanding!=null) { understandingStr = Bytes.toString(understanding); } if (programming!=null) { programmingStr = Bytes.toString(programming); } System.out.println("查询到结果,行键值:"+rowKeyStr+",name:"+nameStr+",student_id:"+student_id_Str+",class:"+clazzStr+",understanding:"+understandingStr+",programming:"+programmingStr); }
}



public static void main(String[] args) throws IOException { TestHbase testHbase =new TestHbase(); testHbase.createConf(); //ColumnData columnData =new ColumnData("20210000000003","info","class","2"); //testHbase.insertData("gaoyong:student",columnData); ColumnData row1_columnData1 =new ColumnData("20210000000002","name",null,"Jerry"); ColumnData row1_columnData2 =new ColumnData("20210000000002","info","student_id","20210000000001"); ColumnData row1_columnData3 =new ColumnData("20210000000002","info","class","1"); ColumnData row1_columnData4 =new ColumnData("20210000000002","score","understanding","85"); ColumnData row1_columnData5 =new ColumnData("20210000000002","score","programming","67");
ColumnData row2_columnData1 =new ColumnData("20210000000003","name",null,"Jack"); ColumnData row2_columnData2 =new ColumnData("20210000000003","info","student_id","20210000000003"); ColumnData row2_columnData3 =new ColumnData("20210000000003","info","class","2"); ColumnData row2_columnData4 =new ColumnData("20210000000003","score","understanding","80"); ColumnData row2_columnData5 =new ColumnData("20210000000003","score","programming","80");
ColumnData row3_columnData1 =new ColumnData("20210000000004","name",null,"Rose"); ColumnData row3_columnData2 =new ColumnData("20210000000004","info","student_id","20210000000004"); ColumnData row3_columnData3 =new ColumnData("20210000000004","info","class","2"); ColumnData row3_columnData4 =new ColumnData("20210000000004","score","understanding","60"); ColumnData row3_columnData5 =new ColumnData("20210000000004","score","programming","61");
ColumnData row4_columnData1 =new ColumnData("G20200388040069","name",null,"高勇"); ColumnData row4_columnData2 =new ColumnData("G20200388040069","info","student_id","G20200388040069"); ColumnData row4_columnData3 =new ColumnData("G20200388040069","info","class","1"); ColumnData row4_columnData4 =new ColumnData("G20200388040069","score","understanding","78"); ColumnData row4_columnData5 =new ColumnData("G20200388040069","score","programming","87");
List<ColumnData> columnDatas = new ArrayList<ColumnData>(); columnDatas.add(row1_columnData1); columnDatas.add(row1_columnData2); columnDatas.add(row1_columnData3); columnDatas.add(row1_columnData4); columnDatas.add(row1_columnData5);
columnDatas.add(row2_columnData1); columnDatas.add(row2_columnData2); columnDatas.add(row2_columnData3); columnDatas.add(row2_columnData4); columnDatas.add(row2_columnData5);
columnDatas.add(row3_columnData1); columnDatas.add(row3_columnData2); columnDatas.add(row3_columnData3); columnDatas.add(row3_columnData4); columnDatas.add(row3_columnData5);
columnDatas.add(row4_columnData1); columnDatas.add(row4_columnData2); columnDatas.add(row4_columnData3); columnDatas.add(row4_columnData4); columnDatas.add(row4_columnData5);
testHbase.batchInsrtDatas("gaoyong:student",columnDatas); //testHbase.getDataBykey("gaoyong:student","20210000000001"); /*ColumnData columnData =new ColumnData("20210000000003","name",null,"Tom"); testHbase.getDataByColumn("gaoyong:student",columnData);*/ //testHbase.delDataByKey("gaoyong:student","20210000000001"); /*ColumnData columnData =new ColumnData("20210000000003","score","understanding","80"); testHbase.delDataByColumn("gaoyong:student","20210000000003",columnData);*/ }}
复制代码


查询数据结果截图如下:


用户头像

cc

关注

还未添加个人签名 2018.03.19 加入

还未添加个人简介

评论

发布
暂无评论
大数据训练营 -0725 课后作业