java對hbase的儲存
阿新 • • 發佈:2019-01-26
1 host檔案修改
找到hosts檔案位置
修改hosts檔案
2 java引入hbase的包
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> <hbase.version>1.1.2</hbase.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>com.bwol</groupId> <artifactId>bwframework-starter</artifactId> <version>4.1.2-SNAPSHOT</version> </dependency> <!-- --> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>1.1.3</version> <exclusions> <exclusion> <artifactId>guava</artifactId> <groupId>com.google.guava</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>15.0</version> </dependency> <!-- --> <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.38</version> </dependency> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>${hbase.version}</version> <exclusions> <exclusion> <groupId>jdk.tools</groupId> <artifactId>jdk.tools</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-common</artifactId> <version>${hbase.version}</version> </dependency> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-server</artifactId> <version>${hbase.version}</version> </dependency> </dependencies>
3 配置連線物件
配置檔案
zkhost=hmaster
獲取連線
@Configuration
public class MyConfiguration {
@Bean
public HBaseService getHBaseService(){
return new HBaseServiceImpl();
}
}
private static volatile HBase instance = null; private static HBaseService hBaseService; public static HBase getInstance(String zkHost) { if (instance == null) { synchronized (HBase.class) { HBaseUtil.init(zkHost); if (instance == null) { instance = new HBase(); } } } return instance; }
/** * 初始化配置 * @param zkHost */ public static void init(String zkHost){ try { if (conf == null) { conf = HBaseConfiguration.create(); // conf.set("hbase.zookeeper.property.clientPort", 2181); conf.set("hbase.zookeeper.quorum", zkHost); //conf.set("zookeeper.znode.parent", "/hbase"); } getConnection(); } catch (Exception e) { logger.error("HBase Configuration Initialization failure !"); throw new RuntimeException(e) ; } }
在業務中獲取Hbase物件
HBase hbase = HBase.getInstance(BwolConfig.get("zkhost"));
存資料
Put put = new Put(Bytes.toBytes(rowKey));
put.addColumn(Bytes.toBytes("data"), Bytes.toBytes(columnTime),Bytes.toBytes(dataSource.getDataValue()));//縱族名稱 列名 值
hbase.put(HbaseConstans.ONE_MINUTES, put, false);//名稱空間資料庫名 put物件