java生成数据插入hbase_用Java操纵HBase数据库(新建表,插入,删除,查找)

该博客介绍了如何使用Java连接并操作HBase数据库,包括初始化配置、创建表、插入数据、删除数据、查询数据等步骤,并提供了详细的代码示例。
摘要由CSDN通过智能技术生成

importjava.io.BufferedWriter;importjava.io.File;importjava.io.FileWriter;importjava.io.IOException;importjava.io.Serializable;importjava.util.ArrayList;importjava.util.List;importorg.apache.hadoop.conf.Configuration;importorg.apache.hadoop.hbase.Cell;importorg.apache.hadoop.hbase.CellUtil;importorg.apache.hadoop.hbase.HBaseConfiguration;importorg.apache.hadoop.hbase.HColumnDescriptor;importorg.apache.hadoop.hbase.HTableDescriptor;importorg.apache.hadoop.hbase.MasterNotRunningException;importorg.apache.hadoop.hbase.TableName;importorg.apache.hadoop.hbase.ZooKeeperConnectionException;importorg.apache.hadoop.hbase.client.Connection;importorg.apache.hadoop.hbase.client.ConnectionFactory;importorg.apache.hadoop.hbase.client.Delete;importorg.apache.hadoop.hbase.client.Get;importorg.apache.hadoop.hbase.client.HBaseAdmin;importorg.apache.hadoop.hbase.client.HTable;importorg.apache.hadoop.hbase.client.Put;importorg.apache.hadoop.hbase.client.Result;importorg.apache.hadoop.hbase.client.ResultScanner;importorg.apache.hadoop.hbase.client.Scan;importorg.apache.hadoop.hbase.filter.CompareFilter;importorg.apache.hadoop.hbase.filter.Filter;importorg.apache.hadoop.hbase.filter.PrefixFilter;importorg.apache.hadoop.hbase.filter.RegexStringComparator;importorg.apache.hadoop.hbase.filter.RowFilter;importorg.apache.hadoop.hbase.io.compress.Compression.Algorithm;importorg.apache.hadoop.hbase.util.Bytes;public class HBaseDBDao implementsSerializable {private static final long serialVersionUID = -3338957140027388957L;//声明静态配置

private static Configuration conf = null;private staticHBaseAdmin hAdmin;//private static HTable table;

private staticConnection conn ;//private static HTable table;

publicHBaseDBDao() {

conf=HBaseConfiguration.create();

conf.set("hbase.zookeeper.quorum", "192.168.1.154");

conf.set("hbase.zookeeper.property.clientPort", "2181");

conf.set("hbase.master", "192.168.1.154:60000");try{//初始化数据库管理员//hAdmin = new HBaseAdmin(conf);

conn =ConnectionFactory.createConnection(conf);

hAdmin=(HBaseAdmin) conn.getAdmin();

}catch(MasterNotRunningException e) {//TODO Auto-generated catch block

e.printStackTrace();

}catch(ZooKeeperConnectionException e) {//TODO Auto-generated catch block

e.printStackTrace();

}catch(IOException e) {//TODO Auto-generated catch block

e.printStackTrace();

}

}public HTable initHTable(String tableName) throwsIOException {

HTable table=(HTable) conn.getTable(TableName.valueOf(tableName));//table = new HTable(conf, Bytes.toBytes(tableName));

returntable;

}//关闭自动提交

public voidcloseAutoFlush(HTable table) {

table.setAutoFlush(false, false);

}//关闭表格

public void closeTable(HTable table) throwsIOException{

table.close();

System.out.println("成功关闭表格!");

}//关闭connection

public void closeConnection() throwsIOException{

conn.close();

System.out.println("成功关闭链接!");

}//判断表是否存在

public boolean isExist(String tableName) throwsIOException {returnhAdmin.tableExists(tableName);

}//创建数据库表

public voidcreateTable(String tableName, String[] columnFamilys)throwsException {if(hAdmin.tableExists(tableName)) {

System.out.println("表 " + tableName + " 已存在!");//System.exit(0);

} else{//新建一个表的描述

HTableDescriptor tableDesc = newHTableDescriptor(TableName.valueOf(tableName));//在描述里添加列族

for(String columnFamily : columnFamilys) {//新建一个列的描述

HColumnDescriptor hcd = newHColumnDescriptor(columnFamily);//在列描述中设置Compression压缩格式//hcd.setCompressionType(Algorithm.GZ);

tableDesc.addFamily(hcd);

}//根据配置好的描述建表

hAdmin.createTable(tableDesc);

System.out.println("创建表 " + tableName + " 成功!");

}

}//删除数据库表

public void deleteTable(String tableName) throwsException {if(hAdmin.tableExists(tableName)) {//关闭一个表

hAdmin.disableTable(tableName);

hAdmin.deleteTable(tableName);

System.out.println("删除表 " + tableName + " 成功!");

}else{

System.out.println("删除的表 " + tableName + " 不存在!");

System.exit(0);

}

}//批量添加数据

public voidaddRowBatch(HTable table, String row, String columnFamily,

String column, String value)throwsException {//将自动提交关闭,如果不关闭,每写一条数据都会进行提交,是导入数据较慢的主要因素//table.setAutoFlush(false, false);//设置缓存大小,当缓存大于设置值时,hbase会自动提交。此处可自己尝试大小,一般对大数据量,设置为5M即可,本文设置为3M。

table.setWriteBufferSize(5 * 1024 * 1024);

Put put= new Put(Bytes.toBytes(row));//指定行//参数分别:列族、列、值

put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column),

Bytes.toBytes(value));

table.put(put);

}//执行flushCommits()操作,防止最后一次数据丢失(尤其是在spark中一个分片结束时)

public void flushCommits(HTable table) throwsException {

table.flushCommits();

}//添加一条数据

public voidaddRow(HTable table, String row, String columnFamily,

String column, String value)throwsException {

Put put= new Put(Bytes.toBytes(row));//指定行//参数分别:列族、列、值

put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column),

Bytes.toBytes(value));

table.put(put);

}//删除一条(行)数据

public static void delRow(HTable table, String row) throwsException {

Delete del= newDelete(Bytes.toBytes(row));

table.delete(del);

}//删除多条数据

public void delMultiRows(HTable table, String[] rows) throwsException {

List delList = new ArrayList();for(String row : rows) {

Delete del= newDelete(Bytes.toBytes(row));

delList.add(del);

}

table.delete(delList);

}//获取一条数据的详细信息

public void getRow(HTable table, String row) throwsException {

Get get= newGet(Bytes.toBytes(row));

Result result=table.get(get);//输出结果,raw方法返回所有keyvalue数组

for(Cell cell : result.rawCells()) {

System.out.print("行名:" + new String(CellUtil.cloneRow(cell)) + " ");

System.out.print("时间戳:" + cell.getTimestamp() + " ");

System.out.print("列族名:" + new String(CellUtil.cloneFamily(cell)) + " ");

System.out.print("列名:" + new String(CellUtil.cloneQualifier(cell))+ " ");

System.out.println("值:" + newString(CellUtil.cloneValue(cell)));

}

}//获取一条数据的值

public void getRowValue(HTable table,String row) throwsException{

Get geter= newGet(Bytes.toBytes(row));

Result result=table.get(geter);for(Cell cell : result.rawCells()){

System.out.print(new String(CellUtil.cloneQualifier(cell))+ " ");

System.out.println(newString(CellUtil.cloneValue(cell)));

}

}//获取所有数据的详细信息

public void getAllRows(HTable table) throwsException {

Scan scan= newScan();

ResultScanner results=table.getScanner(scan);//输出结果

for(Result result : results) {for(Cell cell : result.rawCells()) {

System.out.print("行名:" + new String(CellUtil.cloneRow(cell))+ " ");

System.out.print("时间戳:" + cell.getTimestamp() + " ");

System.out.print("列族名:"+ new String(CellUtil.cloneFamily(cell)) + " ");

System.out.print("列名:" + new String(CellUtil.cloneQualifier(cell)) + " ");

System.out.println("值:" + newString(CellUtil.cloneValue(cell)));

}

}

}//获取所有数据的值

public void getAllRowsValue(HTable table) throwsException {int rownum = 0;

Scan scan= newScan();

ResultScanner results=table.getScanner(scan);

File file= new File("/usr/local/myjar/txt-for-project/hbase.txt");

FileWriter filewriter= newFileWriter(file);

BufferedWriter bfw= newBufferedWriter(filewriter);//输出结果

for(Result result : results) {

String str= new String(result.getRow())+" ";

rownum++;

System.out.print(new String(result.getRow())+" ");for(Cell cell : result.rawCells()) {

str= str + cell.getTimestamp()+" ";

System.out.print(new String(CellUtil.cloneQualifier(cell)) + " ");

System.out.print(new String(CellUtil.cloneValue(cell))+" ");

}

bfw.write(str+"\n");

System.out.println();

}

System.out.println("条目数量"+rownum);

bfw.close();

}//获取所有数据的值 加上filter

public void getAllRowsValueWithFilter(HTable table) throwsException {int rownum = 0;//String prefix = "144860945905310140";//Scan scan = new Scan(prefix.getBytes());//scan.setFilter(new PrefixFilter(prefix.getBytes()));

Filter myFilter= new RowFilter(CompareFilter.CompareOp.EQUAL,new RegexStringComparator(".*1449453890982"));

Scan scan= newScan();

scan.setFilter(myFilter);

ResultScanner results=table.getScanner(scan);

File file= new File("/usr/local/myjar/txt-for-project/hbase.txt");

FileWriter filewriter= newFileWriter(file);

BufferedWriter bfw= newBufferedWriter(filewriter);//输出结果

for(Result result : results) {

String str= new String(result.getRow()) + " ";

rownum++;

System.out.print(new String(result.getRow()) + " ");for(Cell cell : result.rawCells()) {

str= str + cell.getTimestamp() + " ";

System.out.print(newString(CellUtil.cloneQualifier(cell))+ " ");

System.out.print(new String(CellUtil.cloneValue(cell)) + " ");

}

bfw.write(str+ "\n");

System.out.println();

}

System.out.println("条目数量" +rownum);

bfw.close();

}//主函数

public static voidmain(String[] args) {

HBaseDBDao hb= newHBaseDBDao();try{

String tableName= "student";//hb.deleteTable(tableName);//第一步:创建数据库表:“student”

String[] columnFamilys = { "info", "course"};if(!hb.isExist(tableName))

hb.createTable(tableName, columnFamilys);

HTable table=hb.initHTable(tableName);//第二步:向数据表的添加数据//添加第一行数据

if(hb.isExist(tableName)) {

hb.addRow(table,"zpc", "info", "age", "20");

hb.addRow(table,"zpc", "info", "sex", "boy");

hb.addRow(table,"zpc", "course", "china", "97");

hb.addRow(table,"zpc", "course", "math", "128");

hb.addRow(table,"zpc", "course", "english", "85");//添加第二行数据

hb.addRow(table, "henjun", "info", "age", "19");

hb.addRow(table,"henjun", "info", "sex", "boy");

hb.addRow(table,"henjun", "course", "china","90");

hb.addRow(table,"henjun", "course", "math","120");

hb.addRow(table,"henjun", "course", "english","90");//添加第三行数据

hb.addRow(table, "niaopeng", "info", "age", "18");

hb.addRow(table,"niaopeng", "info", "sex","girl");

hb.addRow(table,"niaopeng", "course", "china","100");

hb.addRow(table,"niaopeng", "course", "math","100");

hb.addRow(table,"niaopeng", "course", "english","99");//第三步:获取一条数据

System.out.println("**************获取一条(zpc)数据*************");

hb.getRow(table,"zpc");//第四步:获取所有数据

System.out.println("**************获取所有数据***************");

hb.getAllRows(table);//第五步:删除一条数据

System.out.println("************删除一条(zpc)数据************");

HBaseDBDao.delRow(table,"zpc");

hb.getAllRows(table);//第六步:删除多条数据

System.out.println("**************删除多条数据***************");

String rows[]= new String[] { "henjun","niaopeng"};

hb.delMultiRows(table, rows);

hb.getAllRows(table);//第七步:删除数据库

System.out.println("***************删除数据库表**************");

hb.deleteTable(tableName);

System.out.println("表"+tableName+"存在吗?"+hb.isExist(tableName));

}else{

System.out.println(tableName+ "此数据库表不存在!");

}

}catch(Exception e) {

e.printStackTrace();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值