hbase提供了java客户端去操作数据,我们通过创建一个maven项目来实现简单的数据操作。
-
pom.xml添加hbase插件
<dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-server</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-common</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency>
-
java代码
- 创建一个学生类
public class Student{ private Integer id; private String name; private String age; create getter and setter ... create toString ... }
- 创建一个HBaseDemo类
import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.Cell; 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.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.slf4j.Logger; import org.slf4j.LoggerFactory; import com.guoyu.hbase.entry.Student; public class HBaseDemo { private static Logger logger = LoggerFactory.getLogger(HBaseDemo.class); private static Admin admin; public static void main(String[] args) throws IOException { //System.out.println("hhhh"); // boolean flag = createTable("dbtest:user_table",new String[] {"information","contact"}); // if(flag) { // System.out.println("创建成功"); // } //插入和更新数据 // Student stu = new Student(); // stu.setId(1); // stu.setName("xiaoxin"); // stu.setAge("18"); // insertData("dbtest:student",stu); // System.out.println("插入成功"); Student stu = getDataByRowKey("dbtest:student","1"); System.out.println(stu.toString()); String name = getCellData("dbtest:student","1","info","name"); System.out.println(name); logger.info("student查询成功"); List<Student> students = getAllData("dbtest:student"); students.forEach(System.out::println); //deleteByRowKey("dbtest:student","student-1"); //logger.info("删除成功"); deleteTable("dbtest:user_table"); logger.info("删除表成功"); } // 创建HBase的连接 public static Connection initHBase() throws IOException { Configuration conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.property.clientPort", "2181"); conf.set("hbase.zookeeper.quorum", "hadoop2,hadoop3,hadoop4"); conf.set("hbase.master", "hadoop2:60000"); Connection connection = ConnectionFactory.createConnection(); return connection; } // 创建表 public static boolean createTable(String tableName, String[] cols) throws IOException { TableName table = TableName.valueOf(tableName); admin = initHBase().getAdmin(); if (admin.tableExists(table)) { System.out.println("表已存在!"); return false; } else { HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName); for (String col : cols) { HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(col); hTableDescriptor.addFamily(hColumnDescriptor); } admin.createTable(hTableDescriptor); return true; } } //更新或插入数据 public static void insertData(String tableName, Student stu) throws IOException { TableName table = TableName.valueOf(tableName); Put put = new Put((stu.getId()+"").getBytes()); put.addColumn("info".getBytes(), "name".getBytes(), stu.getName().getBytes()); put.addColumn("info".getBytes(), "age".getBytes(), stu.getAge().getBytes()); Table tab = initHBase().getTable(table); tab.put(put); System.out.println("插入成功"); } //根据rowKey进行查询 public static Student getDataByRowKey(String tableName, String rowKey) throws IOException { Table table = initHBase().getTable(TableName.valueOf(tableName)); Get get = new Get(rowKey.getBytes()); Student stu = new Student(); stu.setId(Integer.valueOf(rowKey)); if(!get.isCheckExistenceOnly()) { Result result = table.get(get); for(Cell cell : result.rawCells()) { String colName = Bytes.toString(cell.getQualifierArray(),cell.getQualifierOffset(),cell.getQualifierLength()); String value = Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength()); if(colName.equals("name")){ stu.setName(value); } if(colName.equals("age")){ stu.setAge(value); } } } return stu; } //查询制定单cell内容 public static String getCellData(String tableName,String rowKey,String family,String col) { try { Table table = initHBase().getTable(TableName.valueOf(tableName)); String result = null; Get get = new Get(rowKey.getBytes()); if(!get.isCheckExistenceOnly()){ get.addColumn(Bytes.toBytes(family),Bytes.toBytes(col)); Result res = table.get(get); byte[] resByte = res.getValue(Bytes.toBytes(family), Bytes.toBytes(col)); return result = Bytes.toString(resByte); }else{ return result = "查询结果不存在"; } }catch(Exception e) { e.printStackTrace(); } return "出现异常"; } public static List<Student> getAllData(String tableName){ Table table = null; List<Student> list = new ArrayList<Student>(); try { table = initHBase().getTable(TableName.valueOf(tableName)); ResultScanner results = table.getScanner(new Scan()); Student stu = null; for(Result result : results) { String id = new String(result.getRow()); System.out.println("用户名:"+new String(result.getRow())); stu = new Student(); for(Cell cell : result.rawCells()) { String row = Bytes.toString(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength()); String colName = Bytes.toString(cell.getQualifierArray(),cell.getQualifierOffset(),cell.getQualifierLength()); String value = Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength()); stu.setId(Integer.valueOf(row)); if(colName.equals("name")){ stu.setName(value); } if(colName.equals("age")){ stu.setAge(value); } } list.add(stu); } }catch(Exception e) { e.printStackTrace(); } return list; } //删除指定cell数据 public static void deleteByRowKey(String tableName, String rowKey) throws IOException { Table table = initHBase().getTable(TableName.valueOf(tableName)); Delete delete = new Delete(Bytes.toBytes(rowKey)); table.delete(delete); } //删除表 public static void deleteTable(String tableName) { TableName table = TableName.valueOf(tableName); try { admin = initHBase().getAdmin(); admin.disableTable(table); admin.deleteTable(table); } catch (IOException e) { e.printStackTrace(); } } }
-
启动hbase集群,参考hbase2.0.0集群
-
启动java程序进行测试