HbaseAPi入門操作

1:添加pom文件

  <repositories>
        <repository>
            <id>cloudera</id>
            <url>https://repository.cloudera.com/artifactory/cloudera-repos/</url>
        </repository>
    </repositories>

    <dependencies>

        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>2.6.0-mr1-cdh5.14.0</version>
        </dependency>


        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-client</artifactId>
            <version>1.2.0-cdh5.14.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-server</artifactId>
            <version>1.2.0-cdh5.14.0</version>
        </dependency>


        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.14.3</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>RELEASE</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                    <!--    <verbal>true</verbal>-->
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.2</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <filters>
                                <filter>
                                    <artifact>*:*</artifact>
                                    <excludes>
                                        <exclude>META-INF/*.SF</exclude>
                                        <exclude>META-INF/*.DSA</exclude>
                                        <exclude>META-INF/*/RSA</exclude>
                                    </excludes>
                                </filter>
                            </filters>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

2:創建一個表(使用測試類或者直接從主方法調用靜態方法都行)

    @Test
    public   void   createTable() throws IOException {
    	//實例化配置對象 hbaseConfiguration 也行,但是也繼承了Configuration
        Configuration conf = new Configuration();
        //設置指定節點
        conf.set("hbase.zookeeper.quorum","node01:2181,node02:2181,node03:2181");
        //連接工廠
        Connection connection = ConnectionFactory.createConnection(conf);
		//得到admin
        Admin admin = connection.getAdmin();
        //實例化一個新的表,並指定表名
        HTableDescriptor newTable191217 = new HTableDescriptor(TableName.valueOf("newuser"));
        //hbase創建表必須配備列族
        newTable191217.addFamily(new HColumnDescriptor("f1"));
        newTable191217.addFamily(new HColumnDescriptor("f2"));
        //把新表對象傳入到createTable函數
        admin.createTable(newTable191217);
        //關閉連接
        admin.close();
        connection.close();




    }

3:給新表添加數據

    @Test
    public  void  putData() throws IOException {
        Configuration conf = new Configuration();
        conf.set("hbase.zookeeper.quorum","node01:2181,node02:2181,node03:2181");
        Connection connection = ConnectionFactory.createConnection(conf);
        Table newTable191217 = connection.getTable(TableName.valueOf("newTable191217"));
		//直接實例化put對象,調用添加列把參數填好即可
        Put put = new Put("f0001".getBytes());
        put.addColumn("family1".getBytes(),"name".getBytes(),"zhangsan".getBytes());
        put.addColumn("family1".getBytes(),"sex".getBytes(),"1".getBytes());
        put.addColumn("family1".getBytes(),"age".getBytes(),"16".getBytes());

        newTable191217.put(put);
        newTable191217.close();
        connection.close();

    }

4 api給表添加put數組

    @Test
    public   void  putList() throws IOException {
        Configuration conf = new Configuration();
        conf.set("hbase.zookeeper.quorum","node001:2181,node002:2181,node003:2181");
        Connection connection = ConnectionFactory.createConnection(conf);
        Table newTable191217 = connection.getTable(TableName.valueOf("newuser"));
        //實例化一個put類型的數組
        List<Put> puts = new ArrayList<>();
        //創建多個put對象
        Put put = new Put("0001".getBytes());
        put.addColumn("f1".getBytes(),"id".getBytes(),Bytes.toBytes(1));
        put.addColumn("f1".getBytes(),"name".getBytes(),Bytes.toBytes("曹操"));
        put.addColumn("f1".getBytes(),"age".getBytes(),Bytes.toBytes(30));
        put.addColumn("f2".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));
        put.addColumn("f2".getBytes(),"address".getBytes(),Bytes.toBytes("沛國譙縣"));
        put.addColumn("f2".getBytes(),"phone".getBytes(),Bytes.toBytes("16888888888"));
        put.addColumn("f2".getBytes(),"say".getBytes(),Bytes.toBytes("helloworld"));

        Put put2 = new Put("0002".getBytes());
        put2.addColumn("f1".getBytes(),"id".getBytes(),Bytes.toBytes(2));
        put2.addColumn("f1".getBytes(),"name".getBytes(),Bytes.toBytes("劉備"));
        put2.addColumn("f1".getBytes(),"age".getBytes(),Bytes.toBytes(32));
        put2.addColumn("f2".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));
        put2.addColumn("f2".getBytes(),"address".getBytes(),Bytes.toBytes("幽州涿郡涿縣"));
        put2.addColumn("f2".getBytes(),"phone".getBytes(),Bytes.toBytes("17888888888"));
        put2.addColumn("f2".getBytes(),"say".getBytes(),Bytes.toBytes("talk is cheap , show me the code"));


        Put put3 = new Put("0003".getBytes());
        put3.addColumn("f1".getBytes(),"id".getBytes(),Bytes.toBytes(3));
        put3.addColumn("f1".getBytes(),"name".getBytes(),Bytes.toBytes("孫權"));
        put3.addColumn("f1".getBytes(),"age".getBytes(),Bytes.toBytes(35));
        put3.addColumn("f2".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));
        put3.addColumn("f2".getBytes(),"address".getBytes(),Bytes.toBytes("下邳"));
        put3.addColumn("f2".getBytes(),"phone".getBytes(),Bytes.toBytes("12888888888"));
        put3.addColumn("f2".getBytes(),"say".getBytes(),Bytes.toBytes("what are you 弄啥嘞!"));

        Put put4 = new Put("0004".getBytes());
        put4.addColumn("f1".getBytes(),"id".getBytes(),Bytes.toBytes(4));
        put4.addColumn("f1".getBytes(),"name".getBytes(),Bytes.toBytes("諸葛亮"));
        put4.addColumn("f1".getBytes(),"age".getBytes(),Bytes.toBytes(28));
        put4.addColumn("f2".getBytes(),"sex".getBytes(), Bytes.toBytes("1"));
        put4.addColumn("f2".getBytes(),"address".getBytes(),Bytes.toBytes("四川隆中"));
        put4.addColumn("f2".getBytes(),"phone".getBytes(),Bytes.toBytes("14888888888"));
        put4.addColumn("f2".getBytes(),"say".getBytes(),Bytes.toBytes("出師表你背了嘛"));

        Put put5 = new Put("0005".getBytes());
        put5.addColumn("f1".getBytes(),"id".getBytes(),Bytes.toBytes(5));
        put5.addColumn("f1".getBytes(),"name".getBytes(),Bytes.toBytes("司馬懿"));
        put5.addColumn("f1".getBytes(),"age".getBytes(),Bytes.toBytes(27));
        put5.addColumn("f2".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));
        put5.addColumn("f2".getBytes(),"address".getBytes(),Bytes.toBytes("哪裏人有待考究"));
        put5.addColumn("f2".getBytes(),"phone".getBytes(),Bytes.toBytes("15888888888"));
        put5.addColumn("f2".getBytes(),"say".getBytes(),Bytes.toBytes("跟諸葛亮死掐"));


        Put put6 = new Put("0006".getBytes());
        put6.addColumn("f1".getBytes(),"id".getBytes(),Bytes.toBytes(5));
        put6.addColumn("f1".getBytes(),"name".getBytes(),Bytes.toBytes("xiaobubu—呂布"));
        put6.addColumn("f1".getBytes(),"age".getBytes(),Bytes.toBytes(28));
        put6.addColumn("f2".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));
        put6.addColumn("f2".getBytes(),"address".getBytes(),Bytes.toBytes("內蒙人"));
        put6.addColumn("f2".getBytes(),"phone".getBytes(),Bytes.toBytes("15788888888"));
        put6.addColumn("f2".getBytes(),"say".getBytes(),Bytes.toBytes("貂蟬去哪了"));
		//把多個put對象添加到數組中
        puts.add(put);
        puts.add(put2);
        puts.add(put3);
        puts.add(put4);
        puts.add(put5);
        puts.add(put6);
        newTable191217.put(puts);
        newTable191217.close();
        connection.close();

    }

5獲取表中數據

    @Test
    public  void getRowkeyData() throws IOException {
        Configuration conf = new Configuration();
        conf.set("hbase.zookeeper.quorum","node01:1181,node02:2181,node03:2181");
        Connection connection = ConnectionFactory.createConnection(conf);
        Table newuser = connection.getTable(TableName.valueOf("newuser"));
        //實例化get對象
        Get get = new Get("0001".getBytes());
        //可以查詢指定列族
        get.addFamily("f1".getBytes());
        //可以查詢指定列
        get.addColumn("f1".getBytes(),"name".getBytes());
        //通過對象調用get方法把get對象傳進去
        Result result = newuser.get(get);
        //調用rawcells返回一個數組
        Cell[] cells = result.rawCells();
        //便利數組
        for (Cell cell : cells) {
        //調用cellutil可以直接讀取想要的數據
            if 
	            (Bytes.toString(CellUtil.cloneQualifier(cell)).equals("id")||Bytes.toString(CellUtil.cloneQualifier(cell)).equals("age")){
                System.out.println(Bytes.toString(CellUtil.cloneFamily(cell)));
                System.out.println(Bytes.toString(CellUtil.cloneQualifier(cell)));
                System.out.println(Bytes.toInt(CellUtil.cloneValue(cell)));
            }else{
                System.out.println(Bytes.toString(CellUtil.cloneFamily(cell)));
                System.out.println(Bytes.toString(CellUtil.cloneQualifier(cell)));
                System.out.println(Bytes.toString(CellUtil.cloneValue(cell)));
            }


        }

        connection.close();
    }

還有很多api建議自己實踐一下

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章