1.任务:
3. 编程完成以下指定功能(教材P92下):
(1)createTable(String tableName, String[] fields)创建表。
(2)addRecord(String tableName, String row, String[] fields, String[] values)
(3)scanColumn(String tableName, String column)
(4)modifyData(String tableName, String row, String column)
(5)deleteRow(String tableName, String row)
代码如下:
package com.test;
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.client.*;
import java.util.ArrayList;
import java.util.List;
class HBaseTest1 {
private static final String TABLE_NAME = "stu";
private static final String FAMILY_NAME = "f1";
private static final String COLUMN_NAME = "name";
private static final String COLUMN_AGE = "age";
private static final String ROW_KEY1 = "r1";
private static final String ROW_KEY2 = "r2";
public static void main(String[] args) throws Exception {
//构造能够访问HBase的configuration对象
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.rootdir", "hdfs://h2single:9000/hbase");
conf.set("hbase.zookeeper.quorum", "h2sliver113:2181");
//HBaseAdmin是对HBase进行ddl操作的核心类
HBaseAdmin hBaseAdmin = new HBaseAdmin(conf);
if(!hBaseAdmin.tableExists(TABLE_NAME)){
HTableDescriptor htableDescriptor = new HTableDescriptor(TABLE_NAME);
htableDescriptor.addFamily(new HColumnDescriptor(FAMILY_NAME));
hBaseAdmin.createTable(htableDescriptor);
System.out.println("table create success");
}else{
System.out.println("table exists");
}
//使用HTable可以对HBase的表中的数据进行增删改查
HTable hTable = new HTable(conf, TABLE_NAME);
// 增加数据
List
Put put1 = new Put(ROW_KEY1.getBytes());
put1.add(FAMILY_NAME.getBytes(), COLUMN_NAME.getBytes(), "zhangsan".getBytes());
put1.add(FAMILY_NAME.getBytes(), COLUMN_AGE.getBytes(), "23".getBytes());
putList.add(put1);
Put put2 = new Put(ROW_KEY2.getBytes());
put2.add(FAMILY_NAME.getBytes(), COLUMN_NAME.getBytes(), "lisi".getBytes());
put2.add(FAMILY_NAME.getBytes(), COLUMN_AGE.getBytes(), "24".getBytes());
putList.add(put2);
hTable.put(putList);
// 根据rowkey得到记录后 获取此记录对应的列信息
Get get = new Get(ROW_KEY1.getBytes());
Result get1 = hTable.get(get);
String name1 = new String(get1.getValue(FAMILY_NAME.getBytes(), COLUMN_NAME.getBytes()));
String age1 = new String(get1.getValue(FAMILY_NAME.getBytes(), COLUMN_AGE.getBytes()));
//System.out.println(get1+"\t"+name1+"\t"+age1);
// 指定行范围来查询多条记录
Scan scan = new Scan();
scan.setStartRow(ROW_KEY1.getBytes());
scan.setStopRow(ROW_KEY2.getBytes());
ResultScanner scanner = hTable.getScanner(scan);
for (Result result : scanner) {
String rowKey = new String(result.getRow());
String name = new String(result.getValue(FAMILY_NAME.getBytes(), COLUMN_NAME.getBytes()));
String age = new String(result.getValue(FAMILY_NAME.getBytes(), COLUMN_AGE.getBytes()));
System.out.println(rowKey+"\t"+name+"\t"+age1);
}
// 根据rowkey删除记录
Delete delete = new Delete(ROW_KEY1.getBytes());
hTable.delete(delete);
// 删除表
hBaseAdmin.disableTable(TABLE_NAME);
hBaseAdmin.deleteTable(TABLE_NAME);
}
手机扫一扫
移动阅读更方便
你可能感兴趣的文章