import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
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 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;
public class HbaseAPI {
public static final String TABLE = "users20202005149";
public static final String FAMILY = "info";
public static final String COL1 = "name";
public static final String COL2 = "telephone";
public static final String COL3 = "email";
public static final String ROWKEY1 = "003";
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
System.setProperty("hadoop.home.dir", "D://Download/bigData/hadoop-2.7.3win");
Configuration conf = HBaseConfiguration.create(); //创建一个环境,没有对环境设置的话,就会自动从hbase-site.xml读取
Connection connection = ConnectionFactory.createConnection(conf);// 获取连接
put(connection);
get(connection);
scan(connection);
connection.close();
}
public static void get(Connection connection) throws IOException {
// TODO Auto-generated method stub
System.out.println("get.......");
Table table = connection.getTable(TableName.valueOf(TABLE));
Get get = new Get(ROWKEY1.getBytes()); //提供行键
Result result = table.get(get); //使用get方法取出来放到result中
System.out.println(new String(result.getValue(Bytes.toBytes(FAMILY),Bytes.toBytes(COL1)) ) );
System.out.println(new String(result.getValue(Bytes.toBytes(FAMILY),Bytes.toBytes(COL2)) ) );
table.close();
}
public static void createTable(Connection connection) throws IOException {
HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf(TABLE)); //对表的描述
tableDescriptor.addFamily(new HColumnDescriptor("ID"));//列族名
tableDescriptor.addFamily(new HColumnDescriptor(FAMILY));
Admin admin = connection.getAdmin();
admin.createTable(tableDescriptor);
}
public static void put(Connection connection) throws IOException {
Table table = connection.getTable(TableName.valueOf(TABLE)); //和表进行连接
List<Put> list = new ArrayList<Put>();
Put putt = new Put(Bytes.toBytes("003"));
putt.addColumn(Bytes.toBytes(FAMILY), Bytes.toBytes(COL1), Bytes.toBytes("zhangsan"));
putt.addColumn(Bytes.toBytes(FAMILY), Bytes.toBytes(COL2), Bytes.toBytes("139001"));
putt.addColumn(Bytes.toBytes(FAMILY), Bytes.toBytes(COL3), Bytes.toBytes("123@qq.com"));
list.add(putt);
putt = new Put(Bytes.toBytes("004"));
putt.addColumn(Bytes.toBytes(FAMILY), Bytes.toBytes(COL1), Bytes.toBytes("lisi"));
putt.addColumn(Bytes.toBytes(FAMILY), Bytes.toBytes(COL2), Bytes.toBytes("136002"));
putt.addColumn(Bytes.toBytes(FAMILY), Bytes.toBytes(COL3), Bytes.toBytes("lisi@qq.com"+"lisi@scnu.edu.cn"));
putt.addColumn(Bytes.toBytes(FAMILY), Bytes.toBytes(COL3), Bytes.toBytes("lisi@scnu.edu.cn"));
list.add(putt);
table.put(list);
System.out.println("data putted!");
table.close();
}
public static void scan(Connection connection) throws IOException {
System.out.println("scan...........");
Table table = connection.getTable(TableName.valueOf(TABLE));
Scan scan = new Scan();
ResultScanner scanner = table.getScanner(scan);
Iterator<Result> list = scanner.iterator();
Result result = null;
while(list.hasNext()) {
result = list.next();
System.out.println(new String(result.getRow())+":"+
new String(result.getValue(Bytes.toBytes(FAMILY), Bytes.toBytes(COL1))));
System.out.println(new String(result.getRow())+":"+
new String(result.getValue(Bytes.toBytes(FAMILY), Bytes.toBytes(COL2))));
}
table.close();
}
}
文件目录