mysql集群 hbase_HBase连接数据库(集群)

packagecn.itcast.hbase;importjava.io.IOException;importjava.util.ArrayList;importjava.util.List;importorg.apache.hadoop.conf.Configuration;//import org.apache.hadoop.fs.shell.CopyCommands.Get;

import org.apache.hadoop.hbase.client.*;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.HBaseAdmin;importorg.apache.hadoop.hbase.client.HTable;importorg.apache.hadoop.hdfs.DFSClient.Conf;importorg.junit.Before;importorg.junit.Test;importorg.apache.hadoop.hbase.util.Bytes;public classHBaseDemo {private Configuration conf=null;//在所有方法之间初始化

@Beforepublic voidinit(){

conf=HBaseConfiguration.create();

conf.set("hbase.zookeeper.quorum", "itcast04:2181,itcast05:2181,itcast06:2181");

}//-------------一次插入一条数据------------------//插入数据

@Testpublic void testPut() throwsException{//得到一个表对象

HTable table =new HTable(conf, "peoples");//得到一个Put对象//将字符串转换为字符数组

Put put=new Put(Bytes.toBytes("kr0001"));

put.add(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("zhangsanfeng"));

put.add(Bytes.toBytes("info"), Bytes.toBytes("age"), Bytes.toBytes("300"));

put.add(Bytes.toBytes("info"), Bytes.toBytes("money"), Bytes.toBytes(3000));//在表中放入put对象

table.put(put);

table.close();

}//插入100万条,速度会很快(服务器几秒,自己的电脑1分半)对比Oracle,mysql//-------------一次插入海量数据------------------

@Testpublic void testPutAll() throwsIOException{//HTablePool pool=new HTablePool(config,10);

HTable table =new HTable(conf, "peoples");//得到list对象

List puts=new ArrayList(10000);// //第一种方式// //将put放入list中//for(int i=1;i<=1000000;i++){//Put put=new Put(Bytes.toBytes("kr"+i));//put.add(Bytes.toBytes("info"), Bytes.toBytes("money"), Bytes.toBytes(""+i));//puts.add(put);//}// //在表中放入List//table.put(puts);//table.close();//第二种方式

for(int i=1;i<=1000000;i++){

Put put=new Put(Bytes.toBytes("kr"+i));

put.add(Bytes.toBytes("info"), Bytes.toBytes("money"), Bytes.toBytes(""+i));

puts.add(put);//每隔1w条放一次

if(i%10000==0){

table.put(puts);

puts=new ArrayList(10000);//相当于清空

}

}

table.put(puts);

table.close();

}//--------查询一个(不到1s)-----------------

@Testpublic void testGet() throwsIOException{

HTable table=new HTable(conf, "peoples");

Get get=new Get(Bytes.toBytes("kr999999"));//传get对象//返回result对象

Result result=table.get(get);

String r=Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("money")));

System.out.println(r);

table.close();

}//--------查询多个-----------------

@Testpublic void testScan() throwsIOException{

HTable table=new HTable(conf, "peoples");//创建scan对象(按照字典顺序排[))

Scan scan=new Scan(Bytes.toBytes("kr299990"), Bytes.toBytes("kr300000"));//返回结果集

ResultScanner scanner=table.getScanner(scan);for(Result result:scanner){

String r=Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("money")));

System.out.println(r);

}

table.close();

}//--------更新(put将老版本覆盖,查询最新的)-----------------//--------删除成功--------------------

@Testpublic void testDel() throwsIOException{

HTable table=new HTable(conf, "peoples");//创建delete对象

Delete delete = new Delete(Bytes.toBytes("kr999999"));

table.delete(delete);

table.close();

}//---------创建表---------------------------

public static void main(String[] args) throwsException {//使用java接口创建表

HBaseAdmin admin=newHBaseAdmin(conf);//指定表名

HTableDescriptor htd=new HTableDescriptor(TableName.valueOf("peoples"));//添加列族(info,data)

HColumnDescriptor hcd_info=new HColumnDescriptor("info");

hcd_info.setMaxVersions(3);

HColumnDescriptor hcd_data=new HColumnDescriptor("data");

htd.addFamily(hcd_info);

htd.addFamily(hcd_data);//创建表

admin.createTable(htd);//关闭

admin.close();

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值