• Hbase API


    hbase版本:2.3.5

    1、创建maven工程,引入pom依赖

    1. <dependencies>
    2. <dependency>
    3. <groupId>junitgroupId>
    4. <artifactId>junitartifactId>
    5. <version>4.11version>
    6. dependency>
    7. <dependency>
    8. <groupId>org.apache.hbasegroupId>
    9. <artifactId>hbase-clientartifactId>
    10. <version>2.3.5version>
    11. dependency>
    12. <dependency>
    13. <groupId>org.apache.hbasegroupId>
    14. <artifactId>hbase-commonartifactId>
    15. <version>2.3.5version>
    16. dependency>
    17. dependencies>

    2、编写测试类,与hbase创建连接

    2.1 初始化配置

    1. private Connection connection = null;
    2. private Table table = null;
    3. @Before
    4. public void init() throws IOException {
    5. Configuration conf = HBaseConfiguration.create();
    6. conf.set("hbase.zookeeper.quorum","192.168.153.135");
    7. conf.set("hbase.zookeeper.property.clientPort","2181");
    8. connection = ConnectionFactory.createConnection(conf);
    9. }

    2.2 测试

    1. @Test
    2. public void testConnection(){
    3. System.out.println(connection);
    4. }

    效果图:

    2.3 关闭连接

    1. @After
    2. public void close() throws IOException {
    3. if (connection!=null)
    4. connection.close();
    5. }

    3、创建表空间

    1. @Test
    2. public void createNamespace() throws IOException {
    3. Admin admin = connection.getAdmin();
    4. NamespaceDescriptor.Builder builder = NamespaceDescriptor.create("bigdata888");
    5. NamespaceDescriptor namespaceDescriptor = builder.build();
    6. admin.createNamespace(namespaceDescriptor);
    7. }

    4.创建表

    1. @Test
    2. public void createTable() throws IOException {
    3. Admin admin = connection.getAdmin();
    4. TableName tableName = TableName.valueOf("bigdata888:test001");
    5. TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(tableName);
    6. ColumnFamilyDescriptor columnFamilyDescriptor1 = ColumnFamilyDescriptorBuilder.of("baseinfo");
    7. ColumnFamilyDescriptor columnFamilyDescriptor2 = ColumnFamilyDescriptorBuilder.of("schoolinfo");
    8. tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptor1);
    9. tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptor2);
    10. TableDescriptor descriptor = tableDescriptorBuilder.build();
    11. admin.createTable(descriptor);
    12. }

    5、插入数据

    5.1 插入单行数据

    1. @Test
    2. public void insertValue() throws IOException {
    3. table = connection.getTable(TableName.valueOf("bigdata888:test001"));
    4. Put rowkey1 = new Put(Bytes.toBytes("rowkey1"));
    5. rowkey1.addColumn(Bytes.toBytes("baseinfo"),Bytes.toBytes("name"),Bytes.toBytes("zhangsan"));
    6. rowkey1.addColumn(Bytes.toBytes("baseinfo"),Bytes.toBytes("gender"),Bytes.toBytes("male"));
    7. rowkey1.addColumn(Bytes.toBytes("baseinfo"),Bytes.toBytes("pwd"),Bytes.toBytes("111"));
    8. rowkey1.addColumn(Bytes.toBytes("schoolinfo"),Bytes.toBytes("name"),Bytes.toBytes("njdx"));
    9. rowkey1.addColumn(Bytes.toBytes("schoolinfo"),Bytes.toBytes("location"),Bytes.toBytes("nj"));
    10. table.put(rowkey1);
    11. }

    5.2 插入多行数据

    1. @Test
    2. public void insertValue2() throws IOException {
    3. table = connection.getTable(TableName.valueOf("bigdata888:test001"));
    4. Put rowkey2 = new Put(Bytes.toBytes("rowkey2"));
    5. rowkey2.addColumn(Bytes.toBytes("baseinfo"),Bytes.toBytes("name"),Bytes.toBytes("lisi"));
    6. rowkey2.addColumn(Bytes.toBytes("baseinfo"),Bytes.toBytes("gender"),Bytes.toBytes("female"));
    7. rowkey2.addColumn(Bytes.toBytes("baseinfo"),Bytes.toBytes("pwd"),Bytes.toBytes("222"));
    8. rowkey2.addColumn(Bytes.toBytes("schoolinfo"),Bytes.toBytes("name"),Bytes.toBytes("bjdx"));
    9. rowkey2.addColumn(Bytes.toBytes("schoolinfo"),Bytes.toBytes("location"),Bytes.toBytes("bj"));
    10. Put rowkey3 = new Put(Bytes.toBytes("rowkey3"));
    11. rowkey3.addColumn(Bytes.toBytes("baseinfo"),Bytes.toBytes("name"),Bytes.toBytes("wangwu"));
    12. rowkey3.addColumn(Bytes.toBytes("baseinfo"),Bytes.toBytes("gender"),Bytes.toBytes("male"));
    13. rowkey3.addColumn(Bytes.toBytes("baseinfo"),Bytes.toBytes("pwd"),Bytes.toBytes("333"));
    14. rowkey3.addColumn(Bytes.toBytes("schoolinfo"),Bytes.toBytes("name"),Bytes.toBytes("qhdx"));
    15. rowkey3.addColumn(Bytes.toBytes("schoolinfo"),Bytes.toBytes("location"),Bytes.toBytes("bj"));
    16. ArrayList list = new ArrayList();
    17. list.add(rowkey2);
    18. list.add(rowkey3);
    19. table.put(list);
    20. }

    6、扫描表

    1. @Test
    2. public void scanValue() throws IOException {
    3. table = connection.getTable(TableName.valueOf("bigdata888:test001"));
    4. Scan scan = new Scan();
    5. ResultScanner scanner = table.getScanner(scan);
    6. for (Result result :
    7. scanner) {
    8. byte[] stuname = result.getValue(Bytes.toBytes("baseinfo"), Bytes.toBytes("name"));
    9. byte[] gender = result.getValue(Bytes.toBytes("baseinfo"), Bytes.toBytes("gender"));
    10. byte[] pwd = result.getValue(Bytes.toBytes("baseinfo"), Bytes.toBytes("pwd"));
    11. byte[] schoolname = result.getValue(Bytes.toBytes("schoolinfo"), Bytes.toBytes("name"));
    12. byte[] location = result.getValue(Bytes.toBytes("schoolinfo"), Bytes.toBytes("location"));
    13. System.out.println("-----------------------------------------");
    14. System.out.print(Bytes.toString(stuname)+"\t");
    15. System.out.print(Bytes.toString(gender)+"\t");
    16. System.out.print(Bytes.toString(pwd)+"\t");
    17. System.out.print(Bytes.toString(schoolname)+"\t");
    18. System.out.println(Bytes.toString(location)+"\t");
    19. }
    20. }

    效果图:

    7、查询单行数据

    1. @Test
    2. public void getValue() throws IOException {
    3. table = connection.getTable(TableName.valueOf("bigdata888:test001"));
    4. Get zhangsan = new Get(Bytes.toBytes("rowkey2"));
    5. Result result = table.get(zhangsan);
    6. byte[] name = result.getValue(Bytes.toBytes("baseinfo"), Bytes.toBytes("name"));
    7. System.out.println(Bytes.toString(name));
    8. }

    效果图:

    8、删除数据

    1. @Test
    2. public void del() throws IOException {
    3. table = connection.getTable(TableName.valueOf("bigdata888:test001"));
    4. // Delete wangwu = new Delete(Bytes.toBytes("rowkey3"));
    5. // lisi.addColumn(Bytes.toBytes("baseinfo"),Bytes.toBytes("name")); // 删除具体列
    6. // table.delete(wangwu);
    7. Delete delwangwu = new Delete(Bytes.toBytes("rowkey3")); // 删除整行
    8. table.delete(delwangwu);
    9. }

  • 相关阅读:
    精品基于django的高校竞赛比赛管理系统Python
    国际版阿里云/腾讯云免开户:云存储服务:云存储服务能够让你随时随地拜访和同享文件
    实现动态页面的技术Servlet
    建造者模式
    define宏定义和const的区别
    攻防世界-misc-流量分析1
    阿里云ACP认证哪个值得考?考试时间怎么安排?
    List,Set,Map集合总结
    CSS - 响应式布局(二)响应式栅格系统
    [附源码]java毕业设计红河旅游信息服务系统论文
  • 原文地址:https://blog.csdn.net/jmz98/article/details/132882982