package day1010;
import java.io.IOException;
import java.util.ArrayList;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.junit.*;
import org.apache.hadoop.hbase.util.Bytes;
public class HBase_1 {
public void init(){
conf=HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "master");//zookeeper
}
/**
* 插入数据
* @throws IOException
* */
//输入单行数据
@Test
public void testPut() throws IOException{
HTable table =new HTable(conf, "people_5");//获取操作表
Put put = new Put(Bytes.toBytes("kr01"));
put.add(Bytes.toBytes("info"),Bytes.toBytes("name"),Bytes.toBytes("cuiyushun"));
put.add(Bytes.toBytes("info"),Bytes.toBytes("age"),Bytes.toBytes("25"));
put.add(Bytes.toBytes("info"),Bytes.toBytes("height"),Bytes.toBytes("175cm"));
table.put(put);
table.close();
}
//输入多行数据
@Test
public void testPutAll() throws IOException{
//@SuppressWarnings("deprecation")
//HTablePool pool = new HTablePool(conf,100);
HTable table =new HTable(conf, "people_5");//获取操作表
ArrayList<Put> puts = new ArrayList<Put>();
for (int i = 0; i < 1000001; i++) {
Put put = new Put(Bytes.toBytes("kr01"+i));
put.add(Bytes.toBytes("info"),Bytes.toBytes("money"),Bytes.toBytes("5870000"));
puts.add(put);
if(i%10000==0){
table.put(puts);
puts=new ArrayList<>(10000);
}
}
table.put(puts);
table.close();
}
//单行数据查询
@Test
public void testGet() throws IOException{
HTable table = new HTable(conf, "people_5");
Get get = new Get(Bytes.toBytes("rk011"));
Result result = table.get(get);
String r=Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("money")));
table.close();
public void TestDel() throws IOException{
HTable table = new HTable(conf, "people_5");
Delete delete = new Delete(Bytes.toBytes("rk011"));
table.delete(delete);
table.close();
}
//全表扫描
@Test
public void testScan() throws IOException{
HTable table = new HTable(conf, "people_5");
Scan scan = new Scan(Bytes.toBytes("kr0120"),Bytes.toBytes("kr0130"));
ResultScanner scanner =table.getScanner(scan);
for(Result result:scanner){
System.out.println(result);
}
table.close();
}
public static void main(String[] args) throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
Configuration conf =HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "master");//zookeeper
HBaseAdmin admin = new HBaseAdmin(conf);//创建表的对象
HTableDescriptor htd = new HTableDescriptor(TableName.valueOf("people_5"));//表的描述器
HColumnDescriptor hcd_info= new HColumnDescriptor("info");//表的info列族类
hcd_info.setMaxVersions(3);//设置列族的其他属性如VERSIONS
HColumnDescriptor hcd_address= new HColumnDescriptor("addr");//表的address列族类
hcd_info.setMaxVersions(5);//设置列族的其他属性如VERSIONS
HColumnDescriptor hcd_age= new HColumnDescriptor("age");//表的age列族类
//VERSIONS默认为1
htd.addFamily(hcd_info);
htd.addFamily(hcd_address);
htd.addFamily(hcd_age);
admin.createTable(htd);
admin.close();
}
}
import java.io.IOException;
import java.util.ArrayList;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.junit.*;
import org.apache.hadoop.hbase.util.Bytes;
public class HBase_1 {
private Configuration conf=null;
public void init(){
conf=HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "master");//zookeeper
}
/**
* 插入数据
* @throws IOException
* */
//输入单行数据
@Test
public void testPut() throws IOException{
HTable table =new HTable(conf, "people_5");//获取操作表
Put put = new Put(Bytes.toBytes("kr01"));
put.add(Bytes.toBytes("info"),Bytes.toBytes("name"),Bytes.toBytes("cuiyushun"));
put.add(Bytes.toBytes("info"),Bytes.toBytes("age"),Bytes.toBytes("25"));
put.add(Bytes.toBytes("info"),Bytes.toBytes("height"),Bytes.toBytes("175cm"));
table.put(put);
table.close();
}
//输入多行数据
@Test
public void testPutAll() throws IOException{
//@SuppressWarnings("deprecation")
//HTablePool pool = new HTablePool(conf,100);
HTable table =new HTable(conf, "people_5");//获取操作表
ArrayList<Put> puts = new ArrayList<Put>();
for (int i = 0; i < 1000001; i++) {
Put put = new Put(Bytes.toBytes("kr01"+i));
put.add(Bytes.toBytes("info"),Bytes.toBytes("money"),Bytes.toBytes("5870000"));
puts.add(put);
if(i%10000==0){
table.put(puts);
puts=new ArrayList<>(10000);
}
}
table.put(puts);
table.close();
}
//单行数据查询
@Test
public void testGet() throws IOException{
HTable table = new HTable(conf, "people_5");
Get get = new Get(Bytes.toBytes("rk011"));
Result result = table.get(get);
String r=Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("money")));
table.close();
}
//删除表
public void TestDel() throws IOException{
HTable table = new HTable(conf, "people_5");
Delete delete = new Delete(Bytes.toBytes("rk011"));
table.delete(delete);
table.close();
}
//全表扫描
@Test
public void testScan() throws IOException{
HTable table = new HTable(conf, "people_5");
Scan scan = new Scan(Bytes.toBytes("kr0120"),Bytes.toBytes("kr0130"));
ResultScanner scanner =table.getScanner(scan);
for(Result result:scanner){
System.out.println(result);
}
table.close();
}
public static void main(String[] args) throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
Configuration conf =HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "master");//zookeeper
HBaseAdmin admin = new HBaseAdmin(conf);//创建表的对象
HTableDescriptor htd = new HTableDescriptor(TableName.valueOf("people_5"));//表的描述器
HColumnDescriptor hcd_info= new HColumnDescriptor("info");//表的info列族类
hcd_info.setMaxVersions(3);//设置列族的其他属性如VERSIONS
HColumnDescriptor hcd_address= new HColumnDescriptor("addr");//表的address列族类
hcd_info.setMaxVersions(5);//设置列族的其他属性如VERSIONS
HColumnDescriptor hcd_age= new HColumnDescriptor("age");//表的age列族类
//VERSIONS默认为1
htd.addFamily(hcd_info);
htd.addFamily(hcd_address);
htd.addFamily(hcd_age);
admin.createTable(htd);
admin.close();
}
}