Hbase 的javaAPI基本操作用 在idea上的实现
生活随笔
收集整理的這篇文章主要介紹了
Hbase 的javaAPI基本操作用 在idea上的实现
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1.保證集群開啟:
jps有如下進程
2.pom文件中的依賴
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.zhiyou100</groupId><artifactId>hbasedemo</artifactId><version>1.0-SNAPSHOT</version><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><hbase.version>1.4.8</hbase.version></properties><dependencies><!--hbase--><dependency><groupId>org.apache.hbase</groupId><artifactId>hbase-client</artifactId><version>${hbase.version}</version></dependency><dependency><groupId>org.apache.hbase</groupId><artifactId>hbase-common</artifactId><version>${hbase.version}</version></dependency><!--要使用HBase的MapReduce API--><dependency><groupId>org.apache.hbase</groupId><artifactId>hbase-server</artifactId><version>${hbase.version}</version></dependency></dependencies></project>3.編寫配置文件:hbase-site.xml
將集群上$HBASE_HOME/conf/hbase-site.xml拷貝過來就可以,也可以直接復(fù)制下面內(nèi)容
<?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="configuration.xsl"?> <!-- /**** Licensed to the Apache Software Foundation (ASF) under one* or more contributor license agreements. See the NOTICE file* distributed with this work for additional information* regarding copyright ownership. The ASF licenses this file* to you under the Apache License, Version 2.0 (the* "License"); you may not use this file except in compliance* with the License. You may obtain a copy of the License at** http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/ --> <configuration><property><name>hbase.rootdir</name><value>hdfs://master2:9000/hbase</value></property><property><name>hbase.cluster.distributed</name><value>true</value></property> <!--conf.set("hbase.zookeeper.quorum", "master2");--><!--// 設(shè)置Zookeeper,直接設(shè)置IP地址--><property><name>hbase.zookeeper.quorum</name><value>master2</value></property></configuration>---注意:如果沒有再本地的C:\Windows\System32\drivers\etc路徑下的hosts文件中配置IP 與之對應(yīng)的主機名
請將上面的主機名(master2)填寫成自己的IP
4.編寫java代碼--創(chuàng)建類:HBaseDDL
package com.hbase.DDL;import com.google.common.io.Resources; 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.*; import org.apache.hadoop.hbase.util.Bytes;import java.io.IOException;public class HBaseDDL {private static Configuration configuration;private static Connection connection;private static Admin admin;static {//1.獲得Configuration實例并進行相關(guān)設(shè)置configuration = HBaseConfiguration.create();configuration.addResource(Resources.getResource("hbase-site.xml"));//2.獲得Connection實例try {connection = ConnectionFactory.createConnection(configuration);//3.1獲得Admin接口admin = connection.getAdmin();} catch (IOException e) {e.printStackTrace();}}public static void main(String[] args) throws IOException {//創(chuàng)建表String familyNames[]={"nature","social"};createTable("test_hbase",familyNames);//向表中插入數(shù)據(jù)insert("test_hbase","libai","nature","height","25");//刪除表dropTable("test_hbase");}/*** 創(chuàng)建表* @param tableName 表名* @param familyNames 列族名* */public static void createTable(String tableName, String familyNames[]) throws IOException {//如果表存在退出if (admin.tableExists(TableName.valueOf(tableName))) {System.out.println("Table exists!");return;}//通過HTableDescriptor類來描述一個表,HColumnDescriptor描述一個列族HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));for (String familyName : familyNames) {tableDescriptor.addFamily(new HColumnDescriptor(familyName));}//tableDescriptor.addFamily(new HColumnDescriptor(familyName));admin.createTable(tableDescriptor);System.out.println("createtable success!");}/*** 刪除表* @param tableName 表名* */public static void dropTable(String tableName) throws IOException {//如果表不存在報異常if (!admin.tableExists(TableName.valueOf(tableName))) {System.out.println(tableName+"不存在");return;}//刪除之前要將表disableif (!admin.isTableDisabled(TableName.valueOf(tableName))) {admin.disableTable(TableName.valueOf(tableName));}admin.deleteTable(TableName.valueOf(tableName));System.out.println("deletetable " + tableName + "ok.");}/*** 指定行/列中插入數(shù)據(jù)* @param tableName 表名* @param rowKey 主鍵rowkey* @param family 列族* @param column 列* @param value 值* TODO: 批量PUT*/public static void insert(String tableName, String rowKey, String family, String column, String value) throws IOException {//3.2獲得Table接口,需要傳入表名Table table =connection.getTable(TableName.valueOf(tableName));Put put = new Put(Bytes.toBytes(rowKey));put.addColumn(Bytes.toBytes(family), Bytes.toBytes(column), Bytes.toBytes(value));table.put(put);System.out.println("insertrecored " + rowKey + " totable " + tableName + "ok.");}/*** 刪除表中的指定行* @param tableName 表名* @param rowKey rowkey* TODO: 批量刪除*/public static void delete(String tableName, String rowKey) throws IOException {//3.2獲得Table接口,需要傳入表名Table table = connection.getTable(TableName.valueOf(tableName));Delete delete = new Delete(Bytes.toBytes(rowKey));table.delete(delete);} }?
測試:1.創(chuàng)建表-----createTable("test_hbase",familyNames);
在hbase shell中l(wèi)ist查看表
2.向創(chuàng)建的表中插入數(shù)據(jù)-----insert("test_hbase","luban","nature","height","250");
在hbase shell中scan 'test_hbase'查看表內(nèi)容
3.刪除表------dropTable("test_hbase");
在hbase shell中l(wèi)ist查看表,已經(jīng)沒有了test_hbase
?
轉(zhuǎn)載于:https://www.cnblogs.com/pigdata/p/10305588.html
總結(jié)
以上是生活随笔為你收集整理的Hbase 的javaAPI基本操作用 在idea上的实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: NPOI之Excel——设置单元格背景色
- 下一篇: Unity3D_(游戏)2D坦克大战