创建API表
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.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import java.io.IOException;
public class TestAPI {
public static void main(String[] args) throws IOException {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","master:2181,node1:2181,node2:2181");
Connection conn = ConnectionFactory.createConnection(conf);
Admin admin = conn.getAdmin();
HTableDescriptor testAPI1 = new HTableDescriptor(TableName.valueOf("testAPI1"));
HColumnDescriptor cf1 = new HColumnDescriptor("cf1");
cf1.setMaxVersions(3);
testAPI1.addFamily(cf1);
admin.createTable(testAPI1);
admin.close();
conn.close();
}
}
API基本操作
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class Test2API {
Connection conn;
Admin admin;
@Before
public void createConn() throws IOException {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "master:2181,node1:2181,node2:2181");
conn = ConnectionFactory.createConnection(conf);
admin = conn.getAdmin();
}
@Test
public void createtable() throws IOException {
HTableDescriptor students = new HTableDescriptor(TableName.valueOf("students"));
HColumnDescriptor info =new HColumnDescriptor("info");
students.addFamily(info);
admin.createTable(students);
}
@Test
public void droptable() throws IOException {
TableName test= TableName.valueOf("test");
if (admin.tableExists(test)){
admin.disableTable(test);
admin.deleteTable(test);
}
}
@Test
public void infotable() throws IOException {
TableName test = TableName.valueOf("testAPI");
HTableDescriptor tableDescriptor = admin.getTableDescriptor(test);
HColumnDescriptor[] columnFamilies = tableDescriptor.getColumnFamilies();
for (HColumnDescriptor cf : columnFamilies) {
if ("info".equals(cf.getNameAsString())) {
cf.setTimeToLive(10000);
}
}
HColumnDescriptor cf1 = new HColumnDescriptor("0001");
tableDescriptor.addFamily(cf1);
admin.modifyTable(test,tableDescriptor);
}
@Test
public void listtable() throws IOException {
TableName[] tableNames= admin.listTableNames();
for (TableName name:tableNames){
System.out.println(name.getNameAsString());
}
}
@Test
public void puttable() throws IOException {
Table testAPI = conn.getTable(TableName.valueOf("testAPI"));
Put put=new Put("0002".getBytes());
put.addColumn("cf1".getBytes(),"name".getBytes(),"LL".getBytes());
put.addColumn("cf1".getBytes(),"age".getBytes(),"23".getBytes());
put.addColumn("cf1".getBytes(),"phone".getBytes(),"17777777".getBytes());
testAPI.put(put);
}
@Test
public void get() throws IOException {
Table testAPI = conn.getTable(TableName.valueOf("testAPI"));
Get get =new Get("0002".getBytes());
Result rs=testAPI.get(get);
byte[] rk = rs.getRow();
System.out.println(rk);
System.out.println(Bytes.toString(rk));
byte[] name = rs.getValue("cf1".getBytes(),"name".getBytes());
System.out.println(name);
System.out.println(Bytes.toString(name));
}
@Test
public void putAll() throws IOException {
BufferedReader br = new BufferedReader(new FileReader("E:\\code\\BigData13\\src\\main\\java\\com\\shujia\\students.txt"));
Table student = conn.getTable(TableName.valueOf("student"));
String line =null;
ArrayList<Put> puts = new ArrayList<>();
int batchSize = 11;
while ((line=br.readLine())!=null){
String[] split = line.split(",");
String id =split[0];
String name=split[1];
String age =split[2];
String gender=split[3];
String clazz =split[4];
Put put = new Put(id.getBytes());
byte[] info = "info".getBytes();
put.addColumn(info,"name".getBytes(),name.getBytes());
put.addColumn(info,"age".getBytes(),age.getBytes());
put.addColumn(info,"gender".getBytes(),gender.getBytes());
put.addColumn(info,"clazz".getBytes(),clazz.getBytes());
puts.add(put);
if (put.size()==batchSize){
student.put(puts);
puts.clear();
}
}
System.out.println(puts.isEmpty());
System.out.println(puts.size());
if (!puts.isEmpty()) {
student.put(puts);
}
br.close();
}
@Test
public void scantable() throws IOException {
Table student = conn.getTable(TableName.valueOf("student"));
Scan scan = new Scan();
scan.withStartRow("1500100100".getBytes());
scan.withStopRow("1500100111".getBytes());
scan.setLimit(10);
for (Result rs:student.getScanner(scan)){
String id =Bytes.toString(rs.getRow());
String name = Bytes.toString(rs.getValue("info".getBytes(), "name".getBytes()));
String age = Bytes.toString(rs.getValue("info".getBytes(), "age".getBytes()));
String gender = Bytes.toString(rs.getValue("info".getBytes(), "gender".getBytes()));
String clazz = Bytes.toString(rs.getValue("info".getBytes(), "clazz".getBytes()));
System.out.println(id + "," + name + "," + age + "," + gender + "," + clazz);
}
}
@Test
public void scanWithCellUtil() throws IOException {
Table student = conn.getTable(TableName.valueOf("student"));
Scan scan = new Scan();
scan.withStartRow("1500100990".getBytes());
for (Result rs :student.getScanner(scan)){
String id =Bytes.toString(rs.getRow());
System.out.print(id + " ");
List<Cell> cells = rs.listCells();
for (Cell cell:cells){
String string = Bytes.toString(CellUtil.cloneValue(cell));
System.out.println(string+" ");
}
System.out.println();
}
}
@After
public void close() throws IOException {
admin.close();
conn.close();
}
|