源碼先給出。
package lekko.hbase;/** * Created by root on 2016/12/13. */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.CellUtil;import org.apache.hadoop.hbase.HBaseConfiguration;import org.apache.hadoop.hbase.HColumnDescriptor;import org.apache.hadoop.hbase.HTableDescriptor;import org.apache.hadoop.hbase.MasterNotRunningException;import org.apache.hadoop.hbase.TableName;import org.apache.hadoop.hbase.ZooKeeperConnectionException;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;public class HbaseDemo { public static Configuration conf; static { conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "master"); conf.addResource("hbase-site.xml"); } /** * 建立表 * * @param tablename 表名 * @param columnFamily 列族 * @throws MasterNotRunningException * @throws ZooKeeperConnectionException * @throws IOException */ public static void createTable(String tablename, String columnFamily) throws MasterNotRunningException, IOException, ZooKeeperConnectionException { Connection conn = ConnectionFactory.createConnection(conf); Admin admin = conn.getAdmin(); try { if (admin.tableExists(TableName.valueOf(tablename))) { System.out.println("Create table data: " + tablename + " already exists, and drop it successfully."); } else { TableName tableName = TableName.valueOf(tablename); HTableDescriptor tableDesc = new HTableDescriptor(tableName); tableDesc.addFamily(new HColumnDescriptor(columnFamily)); admin.createTable(tableDesc); System.out.println("Create table data: " + tablename + " created succeed."); } } finally { admin.close(); conn.close(); } } /** * 向表中插入一條新資料 * * @param tableName 表名 * @param row 行鍵key * @param columnFamily 列族 * @param column 列名 * @param data 要插入的資料 * @throws IOException */ public static void putData(String tableName, String row, String columnFamily, String column, String data) throws IOException { Connection conn = ConnectionFactory.createConnection(conf); Table table = conn.getTable(TableName.valueOf(tableName)); try { Put put = new Put(Bytes.toBytes(row)); put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(data)); table.put(put); System.out.println("put '" + row + "','" + columnFamily + ":" + column + "','" + data + "'" + " successfully."); } finally { table.close(); conn.close(); } } /** * add a column family to an existing table * * @param tableName table name * @param columnFamily column family * @throws IOException */ public static void putFamily(String tableName, String columnFamily) throws IOException { Connection conn = ConnectionFactory.createConnection(conf); Admin admin = conn.getAdmin(); try { if (!admin.tableExists(TableName.valueOf(tableName))) { System.out.println(tableName + " not exists"); } else { admin.disableTable(TableName.valueOf(tableName)); HColumnDescriptor cf1 = new HColumnDescriptor(columnFamily); admin.addColumn(TableName.valueOf(tableName), cf1); admin.enableTable(TableName.valueOf(tableName)); System.out.println("Put family data: " + TableName.valueOf(tableName) + ", " + columnFamily + " put succeed."); } } finally { admin.close(); conn.close(); } } /** * 根據key讀取一條資料 * * @param tableName 表名 * @param row 行鍵key * @param columnFamily 列族 * @param column 列名 * @throws IOException */ public static void getData(String tableName, String row, String columnFamily, String column) throws IOException{ Connection conn = ConnectionFactory.createConnection(conf); Table table = conn.getTable(TableName.valueOf(tableName)); try{ Get get = new Get(Bytes.toBytes(row)); Result result = table.get(get); byte[] rb = result.getValue(Bytes.toBytes(columnFamily), Bytes.toBytes(column)); String value = new String(rb, "UTF-8"); System.out.println("Get data " +value+" successfully."); } finally { table.close(); conn.close(); } } /** * get all data of a table * * @param tableName table name * @throws IOException */ public static void scanAll(String tableName) throws IOException { Connection conn = ConnectionFactory.createConnection(conf); Table table = conn.getTable(TableName.valueOf(tableName)); try { Scan scan = new Scan(); ResultScanner resultScanner = table.getScanner(scan); for(Result result : resultScanner){ List<Cell> cells = result.listCells(); for(Cell cell : cells){ String row = new String(result.getRow(), "UTF-8"); String family = new String(CellUtil.cloneFamily(cell), "UTF-8"); String qualifier = new String(CellUtil.cloneQualifier(cell), "UTF-8"); String value = new String(CellUtil.cloneValue(cell), "UTF-8"); System.out.println("Scan all data: [行健row:"+row+"],[列簇family:"+family+"],[列qualifier:"+qualifier+"],[value值:"+value+"],[時間戳記:"+cell.getTimestamp()+"] successfully."); } } } finally { table.close(); conn.close(); } } /** * delete a data by row key * * @param tableName table name * @param rowKey row key * @throws IOException */ public static void delData(String tableName, String rowKey) throws IOException { Connection conn = ConnectionFactory.createConnection(conf); Table table = conn.getTable(TableName.valueOf(tableName)); try { List<Delete> list = new ArrayList<Delete>(); Delete del = new Delete(rowKey.getBytes()); list.add(del); table.delete(list); System.out.println("Delete record " + rowKey + " successfully."); } finally { table.close(); conn.close(); } } /** * delete a column's value of a row * * @param tableName table name * @param rowKey row key * @param familyName family name * @param columnName column name * @throws IOException */ public static void deleteColumn(String tableName, String rowKey, String familyName, String columnName) throws IOException { Connection conn = ConnectionFactory.createConnection(conf); Table table = conn.getTable(TableName.valueOf(tableName)); try{ Delete del = new Delete(Bytes.toBytes(rowKey)); del.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(columnName)); List<Delete> list = new ArrayList<Delete>(1); list.add(del); table.delete(list); System.out.println("Delete column data: [table:"+tableName+"],row:"+rowKey+"],[family:"+familyName+"],[qualifier:"+columnName+"] successfully."); } finally { table.close(); conn.close(); } } /** * delete a columnFamily's all columns value of a row * * @param tableName table name * @param rowKey row key * @param familyName family name * @throws IOException */ public static void deleteFamily(String tableName, String rowKey, String familyName) throws IOException { Connection conn = ConnectionFactory.createConnection(conf); Table table = conn.getTable(TableName.valueOf(tableName)); try{ Delete del = new Delete(Bytes.toBytes(rowKey)); del.addFamily(Bytes.toBytes(familyName)); List<Delete> list = new ArrayList<Delete>(1); list.add(del); table.delete(list); System.out.println("Delete family data: [table:"+tableName+"],row:"+rowKey+"],[family:"+familyName+"] successfully."); } finally { table.close(); conn.close(); } } /** * delete a table * * @param tableName table name * @throws IOException * @throws MasterNotRunningException * @throws ZooKeeperConnectionException */ public static void deleteTable(String tableName) throws IOException, MasterNotRunningException, ZooKeeperConnectionException { Connection conn = ConnectionFactory.createConnection(conf); Admin admin = conn.getAdmin(); try { admin.disableTable(TableName.valueOf(tableName)); admin.deleteTable(TableName.valueOf(tableName)); System.out.println("Delete table " + tableName + " ok."); } finally { admin.close(); conn.close(); } } public static void main(String[] args) { System.err.println("HBase test start...");// String tableName = "student"; String tableName = "hpwy.studentinfo"; try{ deleteTable(tableName); createTable(tableName, "stu");// createTable(tableName + "2", "stu");/* createTable(tableName, "load"); putData(tableName, "row_1", "load", "no", "0001"); putData(tableName, "row_1", "load", "rec_date", "2016-06-03"); putData(tableName, "row_1", "load", "rec_time", "09:49:00"); putData(tableName, "row_1", "load", "power", "154.24");*/ putData(tableName, "row_1", "stu", "stu_id", "001"); putData(tableName, "row_2", "stu", "stu_id", "002"); putData(tableName, "row_3", "stu", "stu_id", "003"); getData(tableName, "row_1", "stu", "stu_id"); delData(tableName, "row_3"); scanAll(tableName);// deleteTable(tableName + "2");// putFamily(tableName, "score");// putData(tableName, "row_4", "score", "chinese", "90");// putData(tableName, "row_5", "score", "math", "91");// scanAll(tableName);// deleteColumn(tableName, "row_4", "score", "chinese");// deleteFamily(tableName, "row_5", "score");// scanAll(tableName); } catch(Exception ex){ ex.printStackTrace(); } System.err.println("HBase test end..."); }}
同時這裡給出xml的設定檔的配置
<?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://master:9000/hbase</value></property><property> <name>hbase.cluser.distributed</name> <value>true</value></property><property> <name>hbase.zookeeper.property.clientPort</name> <value>2182</value> </property> <property> <name>hbase.master</name> <value>master:60000</value></property><property> <name>base.zookeeper.quorum</name> <value>slave1,slave2,slave3</value></property><property> <name>hbase.security.authorization</name> <value>false</value></property><property> <name>zookeeper.session.timeout</name> <value>1200000</value></property><property> <name>hbase.zookeeper.property.dataDir</name> <value>/home/zookeeper/dataDir</value> </property><property> <name>zookeeper.znode.parent</name> <value>/hbase</value> <description>Root ZNode for HBase in ZooKeeper. All of HBase's ZooKeeper files that are configured with a relative path will go under this node. By default, all of HBase's ZooKeeper file path are configured with a relative path, so they will all go under this directory unless changed. </description> </property> <property> <name>zookeeper.znode.rootserver</name> <value>root-region-server</value> <description>Path to ZNode holding root region location. This is written by the master and read by clients and region servers. If a relative path is given, the parent folder will be ${zookeeper.znode.parent}. By default, this means the root location is stored at /hbase/root-region-server. </description> </property> </configuration>
最後分享出結果
"C:\Program Files\Java\jdk1.8.0_74\bin\java" -Didea.launcher.port=7534 "-Didea.launcher.bin.path=D:\IDEA\IntelliJ IDEA 15.0.2\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_74\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_74\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_74\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_74\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_74\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_74\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_74\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_74\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_74\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_74\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_74\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_74\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_74\jre\lib\ext\sunpkcs1