【问题标题】:How to read date, time and Timestamp from hbase column如何从 hbase 列中读取日期、时间和时间戳
【发布时间】:2017-05-30 20:55:57
【问题描述】:

我使用 Phoenix 在 Hbase 中创建下表。

CREATE TABLE test_Table
( test_date date not null,
CONSTRAINT PK_test PRIMARY KEY (test_date)
);

然后使用以下命令将一条记录插入到同一条记录中。

upsert into test_Table(test_date) values('2013-11-30');

我可以使用 Hbase 读取字符串、int、float 和 double 数据类型值。 Hbase 客户端 API,但不是日期类型。

我正在使用以下代码读取所有内容,但不确定如何从字节中读取日期。

    import org.apache.spark._
    import org.apache.spark.rdd._
    import org.apache.spark.sql.SQLContext
    import org.apache.spark.sql.DataFrame
    import org.apache.hadoop.conf.Configuration
    import org.apache.hadoop.fs.Path
    import org.apache.hadoop.hbase.HBaseConfiguration
    import org.apache.hadoop.hbase.spark.HBaseContext
    import org.apache.hadoop.hbase.client.Scan
    import org.apache.hadoop.hbase.util.Bytes
    import org.apache.spark.sql.types._
    import org.apache.hadoop.hbase.filter.PrefixFilter
    import org.apache.hadoop.hbase.{ TableName, HBaseConfiguration }
    import java.io.File
    import java.text.SimpleDateFormat

def scanHBaseTable(tableName: String, sqlContext: SQLContext): Unit = {

@transient val conf = getHbaseConfiguration();
@transient var scan = new Scan()
//scan.setAllowPartialResults(Constants.ALLOW_HBASE_PARTIAL_SCAN)
//scan.setCaching(Constants.HBASE_SCAN_CACHE)  

val hbaseContext = new HBaseContext(sqlContext.sparkContext, conf);
val hbaseRawRDD = hbaseContext.hbaseRDD(TableName.valueOf(tableName), scan)
hbaseRawRDD.foreach(v =>
  {
    println(Bytes.toString(v._1.get()))
    println((new SimpleDateFormat("yyyy-MM-dd").parse(Bytes.toString(v._1.get()))))
  })

println("Length: " + hbaseRawRDD.map(r => r._1.copyBytes()).collect().length);

}

有人可以为我提供相同的解决方案吗?

【问题讨论】:

    标签: java scala hbase


    【解决方案1】:

    v 的类型是(ImmutableBytesWritable, Result) 所以你可以从Result 对象中获取日期。 您可以使用方法result.getColumnLatestCell(family, qualifier).getTimestamp

    我不知道 Phoenix 使用的是什么族或限定符,您可以列出表中的所有值并知道它们使用的结构。您可以使用返回Map[Family, Map[Qualifier, Map[CreateTime, Value]]]的方法Result.getMap

    【讨论】:

      猜你喜欢
      • 2023-03-22
      • 2013-07-17
      • 2017-02-01
      • 2021-09-26
      • 1970-01-01
      • 2020-06-27
      • 1970-01-01
      • 2023-03-30
      相关资源
      最近更新 更多