1.将hbase-site.xml文件放到resourcs目录下

<configuration>
<!-- 指定hbase在hdfs上存储的路径 -->
        <property>
            <name>hbase.rootdir</name>
            <value>hdfs://hadoop1:9000/hbase</value>
        </property>
	 <property>
        <name>hbase.master</name>
        <value>hadoop1</value>
    </property>
        <!-- 指定hbase是分布式的 -->
        <property>
            <name>hbase.cluster.distributed</name>
            <value>true</value>
        </property>
        <!-- 指定zk的地址,多个用“,”分割 -->
        <property>
            <name>hbase.zookeeper.quorum</name>
            <value>hadoop1:2181,hadoop2:2181,hadoop3:2181</value>
        </property>
	 <property>
        <name>zookeeper.session.timeout</name>
        <value>60000000</value>
    </property>
    <property>
        <name>dfs.support.append</name>
        <value>true</value>
    </property>
</configuration>

2.修改本机的hosts文件

192.168.30.141 hadoop1
192.168.30.139 hadoop2
192.168.30.140 hadoop3

 3.样例

public class HbaseDemo {
    private static Configuration conf = HBaseConfiguration.create();
    private static Admin admin;

    static {
        conf.set("hbase.rootdir", "hdfs://hadoop1:9000/hbase");
        conf.set("hbase.zookeeper.quorum", "hadoop1,hadoop2,hadoop3");

        try {
            Connection connection = ConnectionFactory.createConnection(conf);
            admin = connection.getAdmin();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public 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 tb = new HTableDescriptor(tableNameObj);
                for (int i = 0; i < columnFamily.length; i++) {
                    HColumnDescriptor family = new HColumnDescriptor(columnFamily[i]);
                    tb.addFamily(family);
                }
                admin.createTable(tb);
                System.out.println(tableName + "创建成功");
            }
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println(tableName + "创建失败");
        }
    }

    public static void main(String[] args) {
        HbaseDemo hbaseDemo = new HbaseDemo();
        hbaseDemo.createTable("student2", "cf1","cf2");
    }
}

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐