1. 程式人生 > >Springboot 連線Hbase例項

Springboot 連線Hbase例項

在這裡插入圖片描述
需要配置hbase-site.xml:

<?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="configuration.xsl"?> hbase.rootdir hdfs://主機1(zhz100):9000/hbase
<property>

    <name>hbase.cluster.distributed</name>

    <value>true</value>

</property>

<property>

    <name>hbase.master</name>

    <value>主機名(zhz100):60000</value>

</property>

<property>

    <name>hbase.zookeeper.property.dataDir</name>

    <value>/usr/java/zookeeper3.4.10/temp</value>

</property>

<property>

    <name>hbase.zookeeper.quorum</name>

    <value>主機1(zhz100),主機2,主機3</value>

</property>

<property>

    <name>hbase.zookeeper.property.clientPort</name>

    <value>2181</value>

</property>

配置application.yml:
server:
port: 8080

hbase:
conf:
confMaps:
‘hbase.zookeeper.quorum’ : ‘IP地址1:2181,IP地址2:2181’

pom.xml 引入的依賴:

org.apache.hbase
hbase-client
1.2.0


org.slf4j
slf4j-log4j12




org.springframework.data
spring-data-hadoop
2.5.0.RELEASE


org.apache.hadoop
hadoop-hdfs
2.5.1


org.springframework.data
spring-data-hadoop-core
2.4.0.RELEASE


org.apache.hbase
hbase
1.2.1
pom

HbaseDemo 對hbase資料庫進行操作:
public class HbaseDemo{
private static Configuration conf = HBaseConfiguration.create();
private static Admin admin;
static {
conf.set(“hbase.rootdir”, “hdfs://node1:9000/hbase”);
// 設定Zookeeper,直接設定IP地址
conf.set(“hbase.zookeeper.quorum”, “主機1(zhz100),主機2,主機3”);
try {
Connection connection = ConnectionFactory.createConnection(conf);
admin = connection.getAdmin();
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* 建立表,可以同時建立多個列簇
*
* @param tableName
* @param columnFamily
*/

public static void createTable(String tableName, String... columnFamily) {

    TableName tableNameObj = TableName.valueOf(tableName);
    try {
        if (admin.tableExists(tableNameObj)) {
            System.out.println("Table : " + tableName + " already exists !");
        } else {
            HTableDescriptor td = new HTableDescriptor(tableNameObj);
            int len = columnFamily.length;
            for (int i = 0; i < len; i++) {
                HColumnDescriptor family = new HColumnDescriptor(columnFamily[i]);
                td.addFamily(family);
            }
            admin.createTable(td);
            System.out.println(tableName + " 表建立成功!");
        }
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println(tableName + " 表建立失敗!");
    }

}

/*
* 建立表:cross_history 在測試類中測試:Demo2ApplicationTests
* */
@Test
public void testCreateTable() {
HbaseDemo.createTable(“dr_ps_20181112”, “trademark” , “dr_type” , “service_id” }

/**
* 刪除表
*
*/

public void delTable(String tableName) {
    TableName tableNameObj = TableName.valueOf(tableName);
    try {
        if (this.admin.tableExists(tableNameObj)) {
            admin.disableTable(tableNameObj);
            admin.deleteTable(tableNameObj);
            System.out.println(tableName + " 表刪除成功!");
        } else {
            System.out.println(tableName + " 表不存在!");
        }
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println(tableName + " 表刪除失敗!");
    }
}

/*
*插入資料
* */
public static void insertRecord(String tableName, String rowKey, String columnFamily, String qualifier, String value) {
try {
Connection connection = ConnectionFactory.createConnection(conf);
Table table = connection.getTable(TableName.valueOf(tableName));
Put put = new Put(rowKey.getBytes());
put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(qualifier), Bytes.toBytes(value));
table.put(put);
table.close();
connection.close();
System.out.println(tableName + " 表插入資料成功!");
} catch (Exception e) {
e.printStackTrace();
System.out.println(tableName + " 表插入資料失敗!");
}
}

/*
* 根據KEYROW刪除表中的資料
* */
public static void deleteRecord(String tableName, String rowKey) {
try {
Connection connection = ConnectionFactory.createConnection(conf);
Table table = connection.getTable(TableName.valueOf(tableName));
Delete del = new Delete(rowKey.getBytes());
table.delete(del);
System.out.println(tableName + " 表刪除資料成功!");
} catch (Exception e) {
e.printStackTrace();
System.out.println(tableName + " 表刪除資料失敗!");
}
}

/*
* 根據表名字獲取所有資料
* */
public static List getAll(String tableName) {
try {
Connection connection = ConnectionFactory.createConnection(conf);
Table table = connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
ResultScanner scanner = table.getScanner(scan);
List list = new ArrayList();
for (Result r : scanner) {
list.add®;
}
scanner.close();
System.out.println(tableName + " 表獲取所有記錄成功!");
return list;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}

// 刪除表
public static void deleteTable(String tableName) {
    try {
        Connection connection = ConnectionFactory.createConnection(conf);
        Admin admin = connection.getAdmin();
        TableName table = TableName.valueOf(tableName);
        admin.disableTable(table);//使表失效
        admin.deleteTable(table);//刪除表
        System.out.println("delete table " + tableName + " ok.");
    } catch (IOException e) {
        e.printStackTrace();
    }

}

注*此為專案中部分程式碼,出現問題可以聯絡我[email protected],大家共同探討。