import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellScanner;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Before;
import org.junit.Test;
public class DMLfour {
Connection conn = null;
@Before
public void init() throws Exception {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "hadoop1:2181,hadoop2:2181,hadoop3:2181");
conn = ConnectionFactory.createConnection(conf);
}
/*
* 增
*/
@Test
public void testPutBig() throws Exception {
Table table = conn.getTable(TableName.valueOf("tt"));
Put put = new Put(Bytes.toBytes("001"));
put.addColumn(Bytes.toBytes("basic_info"), Bytes.toBytes("name"), Bytes.toBytes("Nul"));
put.addColumn(Bytes.toBytes("high_info"), Bytes.toBytes("sex"), Bytes.toBytes("male"));
Put put2 = new Put(Bytes.toBytes("002"));
put2.addColumn(Bytes.toBytes("basic_info"), Bytes.toBytes("name"), Bytes.toBytes("sle"));
put2.addColumn(Bytes.toBytes("basic_info"), Bytes.toBytes("age"), Bytes.toBytes("15"));
ArrayList<Put> puts = new ArrayList<>();
puts.add(put);
puts.add(put2);
table.put(puts);
table.close();
conn.close();
}
/*
* 增加1000行数据
*/
@Test
public void testPut() throws Exception {
Table table = conn.getTable(TableName.valueOf("tt"));
ArrayList<Put> puts = new ArrayList<>();
for(int i = 0; i < 1000; i++) {
Put put = new Put(Bytes.toBytes("" + i));
put.addColumn(Bytes.toBytes("basic_info"), Bytes.toBytes("name"), Bytes.toBytes("Nul"+i));
put.addColumn(Bytes.toBytes("basic_info"), Bytes.toBytes("age"), Bytes.toBytes((18+i)+""));
put.addColumn(Bytes.toBytes("high_info"), Bytes.toBytes("sex"), Bytes.toBytes("male"));
puts.add(put);
}
table.put(puts);
table.close();
conn.close();
}
/*
* 删
*/
@Test
public void testDele() throws Exception {
Table table = conn.getTable(TableName.valueOf("tt"));
Delete delete1 = new Delete(Bytes.toBytes("001"));
delete1.addColumn(Bytes.toBytes("basic_info"), Bytes.toBytes("name"));
Delete delete2 = new Delete(Bytes.toBytes("002"));
ArrayList<Delete> dels = new ArrayList<>();
dels.add(delete1);
dels.add(delete2);
table.delete(dels);
table.close();
conn.close();
}
/*
* 查
*/
@Test
public void testGet() throws Exception {
Table table = conn.getTable(TableName.valueOf("tt"));
Get get = new Get("002".getBytes());
Result result = table.get(get);
//从结果中取用户指定的某个key的value值
byte[] value = result.getValue("basic_info".getBytes(), "name".getBytes());
System.out.println(new String(value));
System.out.println("-------------------------");
//遍历整行结果中的所有kv单元格
CellScanner cellScanner = result.cellScanner();
while(cellScanner.advance()) {
Cell cell = cellScanner.current();
byte[] rowArray = cell.getRowArray(); //本kv所属的行键的字节数组
byte[] familyArray = cell.getFamilyArray(); //列族名的字节数组
byte[] qualifierArray = cell.getQualifierArray(); //列名的字节数组
byte[] valueArray = cell.getValueArray(); //value的字节数组
System.out.println("行键" + new String(rowArray));
System.out.println("列族名" + new String(familyArray));
System.out.println("列名" + new String(qualifierArray));
System.out.println("value" + new String(valueArray));
}
table.close();
conn.close();
}
/*
* 按行键范围查询数据
*/
@Test
public void testscan() throws Exception {
Table table = conn.getTable(TableName.valueOf("tt"));
//包含起始行键,不包含结束行键,但是如果真的想查询出末尾的那个行键,那么可以在末尾行键拼接一个不可见字节(\000)
Scan scan = new Scan("001".getBytes(), "100\001".getBytes());
ResultScanner scanner = table.getScanner(scan);
Iterator<Result> iterator = scanner.iterator();
while(iterator.hasNext()) {
Result result = iterator.next();
//遍历整行结果中的所有kv单元格
CellScanner cellScanner = result.cellScanner();
while(cellScanner.advance()) {
Cell cell = cellScanner.current();
byte[] rowArray = cell.getRowArray(); //本kv所属的行键的字节数组
byte[] familyArray = cell.getFamilyArray(); //列族名的字节数组
byte[] qualifierArray = cell.getQualifierArray(); //列名的字节数组
byte[] valueArray = cell.getValueArray(); //value的字节数组
System.out.println("行键" + new String(rowArray));
System.out.println("列族名" + new String(familyArray));
System.out.println("列名" + new String(qualifierArray));
System.out.println("value" + new String(valueArray));
}
System.out.println("-------------------");
}
table.close();
conn.close();
}
/*
* 测试末尾行键
*/
@Test
public void test(){
String a = "000";
String b = "000\0";
System.out.println(a);
System.out.println(b);
byte[] bytes = a.getBytes();
byte[] bytes2 = b.getBytes();
System.out.println("");
}
}